--- /dev/null
+Return-Path: <novalazy@gmail.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id DAF12431FC2\r
+ for <notmuch@notmuchmail.org>; Sat, 20 Jul 2013 17:08:03 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id nwV+ObB15cWw for <notmuch@notmuchmail.org>;\r
+ Sat, 20 Jul 2013 17:07:56 -0700 (PDT)\r
+Received: from mail-pb0-f51.google.com (mail-pb0-f51.google.com\r
+ [209.85.160.51]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 27C48431FB6\r
+ for <notmuch@notmuchmail.org>; Sat, 20 Jul 2013 17:07:56 -0700 (PDT)\r
+Received: by mail-pb0-f51.google.com with SMTP id um15so5763157pbc.10\r
+ for <notmuch@notmuchmail.org>; Sat, 20 Jul 2013 17:07:55 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:x-mailer;\r
+ bh=xy4St4yA/Cfj6QR8U0e1DLl54f8kW6J3/5v0YQNjyT4=;\r
+ b=KJ1WZ5UwwlQatlIrSjIuwlLBkFLanp5S6VWJBSnKidaZgNEXVsxD4LR+O3399QzCgw\r
+ qUInfNNc4ZO2bQxdjH0PP0H2ridGypYkoR7U/rt84BAW+ZB0UT5bFtRQeckjJB7eRyri\r
+ 91mh4VZZ02XWhXeJesPDQqhFmgRufrcfh1cdEPkNBtwOrdkr5rex3bOX43CSgoC8kR04\r
+ kFxL28gcN9Z13dRBFaF7Ztvqeen7uII1pB9FRkHz5NV1pl7p9AYw/YFGpE9FwA8FtmyQ\r
+ NVxak5CnI6Mzm3zx1/wBqPt7vwPe7fHmh0MW18kRo8f6xzzTJHizXLNOA9hUdQfFOSNL\r
+ itPA==\r
+X-Received: by 10.66.240.2 with SMTP id vw2mr25577980pac.137.1374365274354;\r
+ Sat, 20 Jul 2013 17:07:54 -0700 (PDT)\r
+Received: from localhost (215.42.233.220.static.exetel.com.au.\r
+ [220.233.42.215]) by mx.google.com with ESMTPSA id\r
+ ht5sm27318443pbb.29.2013.07.20.17.07.50 for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Sat, 20 Jul 2013 17:07:53 -0700 (PDT)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 1/3] cli: add insert --must-index option\r
+Date: Sun, 21 Jul 2013 10:07:32 +1000\r
+Message-Id: <1374365254-13227-1-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 21 Jul 2013 00:08:04 -0000\r
+\r
+This option causes notmuch insert to fail as a whole if the message\r
+failed to be added to the notmuch database. The new message file\r
+will be deleted from disk, and a distinct status code (2) returned.\r
+---\r
+ notmuch-insert.c | 76 ++++++++++++++++++++++++++++++++++----------------------\r
+ 1 file changed, 46 insertions(+), 30 deletions(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 2207b1e..505b647 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -28,6 +28,10 @@\r
+ #include <sys/stat.h>\r
+ #include <fcntl.h>\r
+ \r
++#define INSERT_EXIT_SUCCESS 0\r
++#define INSERT_EXIT_FAIL_WRITE 1\r
++#define INSERT_EXIT_FAIL_INDEX 2\r
++\r
+ static volatile sig_atomic_t interrupted;\r
+ \r
+ static void\r
+@@ -293,12 +297,13 @@ copy_stdin (int fdin, int fdout)\r
+ \r
+ /* Add the specified message file to the notmuch database, applying tags.\r
+ * The file is renamed to encode notmuch tags as maildir flags. */\r
+-static void\r
++static notmuch_status_t\r
+ add_file_to_database (notmuch_database_t *notmuch, const char *path,\r
+ tag_op_list_t *tag_ops)\r
+ {\r
+ notmuch_message_t *message;\r
+ notmuch_status_t status;\r
++ notmuch_status_t sync;\r
+ \r
+ status = notmuch_database_add_message (notmuch, path, &message);\r
+ switch (status) {\r
+@@ -318,47 +323,52 @@ add_file_to_database (notmuch_database_t *notmuch, const char *path,\r
+ case NOTMUCH_STATUS_LAST_STATUS:\r
+ fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n",\r
+ path, notmuch_status_to_string (status));\r
+- return;\r
++ return status;\r
+ }\r
+ \r
+ if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
+ /* Don't change tags of an existing message. */\r
+- status = notmuch_message_tags_to_maildir_flags (message);\r
+- if (status != NOTMUCH_STATUS_SUCCESS)\r
++ sync = notmuch_message_tags_to_maildir_flags (message);\r
++ if (sync != NOTMUCH_STATUS_SUCCESS)\r
+ fprintf (stderr, "Error: failed to sync tags to maildir flags\n");\r
+ } else {\r
+ tag_op_list_apply (message, tag_ops, TAG_FLAG_MAILDIR_SYNC);\r
+ }\r
+ \r
+ notmuch_message_destroy (message);\r
++\r
++ return status;\r
+ }\r
+ \r
+-static notmuch_bool_t\r
++static int\r
+ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,\r
+- const char *dir, tag_op_list_t *tag_ops)\r
++ const char *dir, tag_op_list_t *tag_ops,\r
++ notmuch_bool_t must_index)\r
+ {\r
+ char *tmppath;\r
+ char *newpath;\r
+ char *newdir;\r
+ int fdout;\r
+- char *cleanup_path;\r
++ notmuch_status_t status;\r
+ \r
+ fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir);\r
+ if (fdout < 0)\r
+- return FALSE;\r
++ return INSERT_EXIT_FAIL_WRITE;\r
+ \r
+- cleanup_path = tmppath;\r
+-\r
+- if (! copy_stdin (fdin, fdout))\r
+- goto FAIL;\r
++ if (! copy_stdin (fdin, fdout)) {\r
++ close (fdout);\r
++ unlink (tmppath);\r
++ return INSERT_EXIT_FAIL_WRITE;\r
++ }\r
+ \r
+ if (fsync (fdout) != 0) {\r
+ fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno));\r
+- goto FAIL;\r
++ close (fdout);\r
++ unlink (tmppath);\r
++ return INSERT_EXIT_FAIL_WRITE;\r
+ }\r
+ \r
+ close (fdout);\r
+- fdout = -1;\r
+ \r
+ /* Atomically move the new message file from the Maildir 'tmp' directory\r
+ * to the 'new' directory. We follow the Dovecot recommendation to\r
+@@ -367,25 +377,28 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,\r
+ */\r
+ if (rename (tmppath, newpath) != 0) {\r
+ fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno));\r
+- goto FAIL;\r
++ unlink (tmppath);\r
++ return INSERT_EXIT_FAIL_WRITE;\r
+ }\r
+ \r
+- cleanup_path = newpath;\r
+-\r
+- if (! sync_dir (newdir))\r
+- goto FAIL;\r
++ if (! sync_dir (newdir)) {\r
++ unlink (newpath);\r
++ return INSERT_EXIT_FAIL_WRITE;\r
++ }\r
+ \r
+- /* Even if adding the message to the notmuch database fails,\r
+- * the message is on disk and we consider the delivery completed. */\r
+- add_file_to_database (notmuch, newpath, tag_ops);\r
++ status = add_file_to_database (notmuch, newpath, tag_ops);\r
+ \r
+- return TRUE;\r
++ /* If must_index is TRUE, then indexing must succeed. Otherwise, we\r
++ * consider the delivery completed as long as the message is on disk. */\r
++ if (must_index &&\r
++ status != NOTMUCH_STATUS_SUCCESS &&\r
++ status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)\r
++ {\r
++ unlink (newpath);\r
++ return INSERT_EXIT_FAIL_INDEX;\r
++ }\r
+ \r
+- FAIL:\r
+- if (fdout >= 0)\r
+- close (fdout);\r
+- unlink (cleanup_path);\r
+- return FALSE;\r
++ return INSERT_EXIT_SUCCESS;\r
+ }\r
+ \r
+ int\r
+@@ -400,6 +413,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ char *query_string = NULL;\r
+ const char *folder = NULL;\r
+ notmuch_bool_t create_folder = FALSE;\r
++ notmuch_bool_t must_index = FALSE;\r
+ const char *maildir;\r
+ int opt_index;\r
+ unsigned int i;\r
+@@ -408,6 +422,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ notmuch_opt_desc_t options[] = {\r
+ { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },\r
+ { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },\r
++ { NOTMUCH_OPT_BOOLEAN, &must_index, "must-index", 0, 0 },\r
+ { NOTMUCH_OPT_END, 0, 0, 0, 0 }\r
+ };\r
+ \r
+@@ -471,9 +486,10 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))\r
+ return 1;\r
+ \r
+- ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops);\r
++ ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops,\r
++ must_index);\r
+ \r
+ notmuch_database_destroy (notmuch);\r
+ \r
+- return (ret) ? 0 : 1;\r
++ return ret;\r
+ }\r
+-- \r
+1.7.12.1\r
+\r