--- /dev/null
+Return-Path: <bremner@tesseract.cs.unb.ca>\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 1946C431FC7\r
+ for <notmuch@notmuchmail.org>; Fri, 3 Oct 2014 14:19:30 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ 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 J2W23DgTWYEF for <notmuch@notmuchmail.org>;\r
+ Fri, 3 Oct 2014 14:19:26 -0700 (PDT)\r
+Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])\r
+ (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id EE1EB431FBC\r
+ for <notmuch@notmuchmail.org>; Fri, 3 Oct 2014 14:19:25 -0700 (PDT)\r
+Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
+ (envelope-from <bremner@tesseract.cs.unb.ca>)\r
+ id 1XaAG3-0000Kx-Ou; Fri, 03 Oct 2014 18:19:23 -0300\r
+Received: (nullmailer pid 21179 invoked by uid 1000); Fri, 03 Oct 2014\r
+ 21:19:08 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v2.5 3/4] cli/insert: require succesful message indexing for\r
+ success status\r
+Date: Fri, 3 Oct 2014 23:18:59 +0200\r
+Message-Id: <1412371140-21051-4-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <1412371140-21051-1-git-send-email-david@tethera.net>\r
+References: <1412371140-21051-1-git-send-email-david@tethera.net>\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: Fri, 03 Oct 2014 21:19:30 -0000\r
+\r
+From: Jani Nikula <jani@nikula.org>\r
+\r
+Add --keep option to keep any remaining stuff in index or file. We\r
+could distinguish between failures to index and failures to apply tags\r
+or maildir sync, but for simplicity just have one.\r
+---\r
+ doc/man1/notmuch-insert.rst | 19 ++++++++++++-------\r
+ notmuch-insert.c | 36 ++++++++++++++++++++++++++++++------\r
+ test/T070-insert.sh | 3 +--\r
+ 3 files changed, 43 insertions(+), 15 deletions(-)\r
+\r
+diff --git a/doc/man1/notmuch-insert.rst b/doc/man1/notmuch-insert.rst\r
+index 2be1a7b..e396f6c 100644\r
+--- a/doc/man1/notmuch-insert.rst\r
++++ b/doc/man1/notmuch-insert.rst\r
+@@ -38,16 +38,21 @@ Supported options for **insert** include\r
+ does not exist. Otherwise the folder must already exist for mail\r
+ delivery to succeed.\r
+ \r
++ ``--keep``\r
++ Keep the message file if indexing fails, and keep the message\r
++ indexed if applying tags or maildir flag synchronization\r
++ fails. Ignore these errors and return exit status 0 to\r
++ indicate succesful mail delivery.\r
++\r
+ EXIT STATUS\r
+ ===========\r
+ \r
+-This command returns exit status 0 if the message was successfully added\r
+-to the mail directory, even if the message could not be indexed and\r
+-added to the notmuch database. In the latter case, a warning will be\r
+-printed to standard error but the message file will be left on disk.\r
+-\r
+-If the message could not be written to disk then a non-zero exit status\r
+-is returned.\r
++This command returns exit status 0 on succesful mail delivery,\r
++non-zero otherwise. The default is to indicate failed mail delivery on\r
++any errors, including message file delivery to the filesystem, message\r
++indexing to Notmuch database, changing tags, and synchronizing tags to\r
++maildir flags. The ``--keep`` option may be used to settle for\r
++successful message file delivery.\r
+ \r
+ SEE ALSO\r
+ ========\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 0ea4380..7074077 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -443,6 +443,7 @@ add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops,\r
+ int\r
+ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ {\r
++ notmuch_status_t status, close_status;\r
+ notmuch_database_t *notmuch;\r
+ struct sigaction action;\r
+ const char *db_path;\r
+@@ -452,6 +453,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 keep = FALSE;\r
+ notmuch_bool_t synchronize_flags;\r
+ const char *maildir;\r
+ char *newpath;\r
+@@ -461,6 +463,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, &keep, "keep", 0, 0 },\r
+ { NOTMUCH_OPT_END, 0, 0, 0, 0 }\r
+ };\r
+ \r
+@@ -535,11 +538,32 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return EXIT_FAILURE;\r
+ }\r
+ \r
+- /* Add the message to the index.\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 (notmuch, newpath, tag_ops, synchronize_flags, TRUE);\r
++ /* Index the message. */\r
++ status = add_file (notmuch, newpath, tag_ops, synchronize_flags, keep);\r
++\r
++ /* Commit changes. */\r
++ close_status = notmuch_database_destroy (notmuch);\r
++ if (close_status) {\r
++ /* Hold on to the first error, if any. */\r
++ if (! status)\r
++ status = close_status;\r
++ fprintf (stderr, "%s: failed to commit database changes: %s\n",\r
++ keep ? "Warning" : "Error",\r
++ notmuch_status_to_string (close_status));\r
++ }\r
++\r
++ if (status) {\r
++ if (keep) {\r
++ status = NOTMUCH_STATUS_SUCCESS;\r
++ } else {\r
++ /* If maildir flag sync failed, this might fail. */\r
++ if (unlink (newpath)) {\r
++ fprintf (stderr, "Warning: failed to remove '%s' from maildir "\r
++ "after errors: %s. Please run 'notmuch new' to fix.\n",\r
++ newpath, strerror (errno));\r
++ }\r
++ }\r
++ }\r
+ \r
+- notmuch_database_destroy (notmuch);\r
+- return EXIT_SUCCESS;\r
++ return status ? EXIT_FAILURE : EXIT_SUCCESS;\r
+ }\r
+diff --git a/test/T070-insert.sh b/test/T070-insert.sh\r
+index 40a7636..be8060e 100755\r
+--- a/test/T070-insert.sh\r
++++ b/test/T070-insert.sh\r
+@@ -23,7 +23,7 @@ test_expect_code 1 "Insert zero-length file" \\r
+ \r
+ # This test is a proxy for other errors that may occur while trying to\r
+ # add a message to the notmuch database, e.g. database locked.\r
+-test_expect_code 0 "Insert non-message" \\r
++test_expect_code 1 "Insert non-message" \\r
+ "echo bad_message | notmuch insert"\r
+ \r
+ test_begin_subtest "Database empty so far"\r
+@@ -199,7 +199,6 @@ end\r
+ run\r
+ EOF\r
+ test_begin_subtest "error exit when add_message returns $code"\r
+-test_subtest_known_broken\r
+ gdb --batch-silent --return-child-result -x index-file-$code.gdb \\r
+ --args notmuch insert < $gen_msg_filename\r
+ test_expect_equal $? 1\r
+-- \r
+2.1.0\r
+\r