From: Peter Wang Date: Wed, 16 Apr 2014 12:59:23 +0000 (+1000) Subject: [PATCH v2 08/10] cli: add insert --must-index option X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=622f79ba3b82f03eeb1ce7e0623a3d74d896d657;p=notmuch-archives.git [PATCH v2 08/10] cli: add insert --must-index option --- diff --git a/27/719d2199da82f0dfade8482de06dc4684941f0 b/27/719d2199da82f0dfade8482de06dc4684941f0 new file mode 100644 index 000000000..b176bd814 --- /dev/null +++ b/27/719d2199da82f0dfade8482de06dc4684941f0 @@ -0,0 +1,192 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 1E52D431FCB + for ; Wed, 16 Apr 2014 06:00:51 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.799 +X-Spam-Level: +X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5 + tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, + FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id dltdtOkg6xEz for ; + Wed, 16 Apr 2014 06:00:45 -0700 (PDT) +Received: from mail-pb0-f42.google.com (mail-pb0-f42.google.com + [209.85.160.42]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 5E3A6431FBF + for ; Wed, 16 Apr 2014 06:00:18 -0700 (PDT) +Received: by mail-pb0-f42.google.com with SMTP id rr13so10952745pbb.1 + for ; Wed, 16 Apr 2014 06:00:17 -0700 (PDT) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; + h=from:to:cc:subject:date:message-id:in-reply-to:references; + bh=2nfJSnS2AN9Bu8cnvUWtOuFvcuU+svUvdOi7ZrvXAU4=; + b=GaNY3R7ByqpLBS/mVLV94PxkbUWev4Txz/L/ZnEWpiRHNuTYZ6ktvUuUUVAz0lKRPq + 25j356mEaMpu7j+Gbe6+iAzZqDb0tincEXAemXlvKr0FwErYFEKfUIJGuMgRaVegI/wP + ZE0TvTB6WbgvHlUyNgWMf+nS0QdoYnjqr5Dw32WOmR1vhP091EThyPOgQ5Wb1lKRuuZ2 + Ek1h7y1mji4wIdhEznqJzJ52iuOTK1IGFWHawMKW4Lrhwd84EjUWRgf6T9DQiVRyy0ul + YXmplnKZp2j2c0ktOPsxuu4r5CF/Xx2Y2+OVrbGSLU86LDF9Bktq+AGxDEfOmoKIql1l + Et0Q== +X-Received: by 10.68.240.5 with SMTP id vw5mr8230493pbc.113.1397653217736; + Wed, 16 Apr 2014 06:00:17 -0700 (PDT) +Received: from localhost (215.42.233.220.static.exetel.com.au. + [220.233.42.215]) by mx.google.com with ESMTPSA id + dy7sm111054220pad.9.2014.04.16.06.00.15 for + (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); + Wed, 16 Apr 2014 06:00:16 -0700 (PDT) +From: Peter Wang +To: notmuch@notmuchmail.org +Subject: [PATCH v2 08/10] cli: add insert --must-index option +Date: Wed, 16 Apr 2014 22:59:23 +1000 +Message-Id: <1397653165-15620-9-git-send-email-novalazy@gmail.com> +X-Mailer: git-send-email 1.8.4 +In-Reply-To: <1397653165-15620-1-git-send-email-novalazy@gmail.com> +References: <1397653165-15620-1-git-send-email-novalazy@gmail.com> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Wed, 16 Apr 2014 13:00:51 -0000 + +This option causes notmuch insert to fail (with exit code 3) on failure +to index the message, or failure to set the tags on the message, or if +closing (flushing) the database fails. Failure to sync tags to flags +has no effect. +--- + notmuch-insert.c | 57 +++++++++++++++++++++++++++++++++++++++----------------- + 1 file changed, 40 insertions(+), 17 deletions(-) + +diff --git a/notmuch-insert.c b/notmuch-insert.c +index 29d82c9..83257f4 100644 +--- a/notmuch-insert.c ++++ b/notmuch-insert.c +@@ -31,7 +31,8 @@ + enum { + INSERT_EXIT_SUCCESS = 0, + INSERT_EXIT_FAILURE = 1, +- INSERT_EXIT_FAILED_WRITE = 2 ++ INSERT_EXIT_FAILED_WRITE = 2, ++ INSERT_EXIT_FAILED_INDEX = 3 + }; + + static volatile sig_atomic_t interrupted; +@@ -298,13 +299,15 @@ copy_stdin (int fdin, int fdout) + } + + /* Add the specified message file to the notmuch database, applying tags. +- * The file is renamed to encode notmuch tags as maildir flags. */ +-static void ++ * If synchronize_flags is set then file is renamed to encode notmuch tags as ++ * maildir flags. */ ++static notmuch_bool_t + add_file_to_database (notmuch_database_t *notmuch, const char *path, + tag_op_list_t *tag_ops, notmuch_bool_t synchronize_flags) + { + notmuch_message_t *message; + notmuch_status_t status; ++ notmuch_status_t sync; + + status = notmuch_database_add_message (notmuch, path, &message); + switch (status) { +@@ -324,23 +327,28 @@ add_file_to_database (notmuch_database_t *notmuch, const char *path, + case NOTMUCH_STATUS_LAST_STATUS: + fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n", + path, notmuch_status_to_string (status)); +- return; ++ return FALSE; + } + + if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) { + /* Don't change tags of an existing message. */ +- if (synchronize_flags) { +- status = notmuch_message_tags_to_maildir_flags (message); +- if (status != NOTMUCH_STATUS_SUCCESS) +- fprintf (stderr, "Error: failed to sync tags to maildir flags\n"); +- } ++ status = NOTMUCH_STATUS_SUCCESS; + } else { +- tag_op_flag_t flags = synchronize_flags ? TAG_FLAG_MAILDIR_SYNC : 0; ++ status = tag_op_list_apply (message, tag_ops, 0); ++ } + +- tag_op_list_apply (message, tag_ops, flags); ++ /* Call notmuch_message_tags_to_maildir_flags directly instead of doing it ++ * as part of tag_op_list_apply. For --must-index we want to succeed if ++ * tagging succeeds, but disregard whether synchronizing flags fails. */ ++ if (status == NOTMUCH_STATUS_SUCCESS && synchronize_flags) { ++ sync = notmuch_message_tags_to_maildir_flags (message); ++ if (sync != NOTMUCH_STATUS_SUCCESS) ++ fprintf (stderr, "Error: failed to sync tags to maildir flags\n"); + } + + notmuch_message_destroy (message); ++ ++ return (status == NOTMUCH_STATUS_SUCCESS); + } + + static notmuch_bool_t +@@ -400,15 +408,19 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + char *query_string = NULL; + const char *folder = NULL; + notmuch_bool_t create_folder = FALSE; ++ notmuch_bool_t must_index = FALSE; + notmuch_bool_t synchronize_flags; + const char *maildir; + char *newpath; + int opt_index; + unsigned int i; ++ notmuch_bool_t indexed; ++ notmuch_status_t status; + + notmuch_opt_desc_t options[] = { + { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 }, + { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 }, ++ { NOTMUCH_OPT_BOOLEAN, &must_index, "must-index", 0, 0 }, + { NOTMUCH_OPT_END, 0, 0, 0, 0 } + }; + +@@ -485,12 +497,23 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + return INSERT_EXIT_FAILED_WRITE; + } + +- /* Add the message to the index. +- * Even if adding the message to the notmuch database fails, +- * the message is on disk and we consider the delivery completed. */ +- add_file_to_database (notmuch, newpath, tag_ops, ++ /* Add the message to the index. */ ++ indexed = add_file_to_database (notmuch, newpath, tag_ops, + synchronize_flags); + +- notmuch_database_destroy (notmuch); +- return INSERT_EXIT_SUCCESS; ++ /* If must_index is FALSE then succeed as the message is on disk. ++ * Otherwise message indexing and tagging must succeed, and the database ++ * must be flushed. Don't flush the database if there was an earlier ++ * error, so as to abandon the transaction (is there a better way?) */ ++ if (! must_index) { ++ notmuch_database_destroy (notmuch); ++ return INSERT_EXIT_SUCCESS; ++ } ++ if (indexed) { ++ status = notmuch_database_destroy (notmuch); ++ if (status == NOTMUCH_STATUS_SUCCESS) ++ return INSERT_EXIT_SUCCESS; ++ } ++ unlink (newpath); ++ return INSERT_EXIT_FAILED_INDEX; + } +-- +1.8.4 +