--- /dev/null
+Return-Path: <jani@nikula.org>\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 362BD431FC2\r
+ for <notmuch@notmuchmail.org>; Sun, 28 Sep 2014 07:41:26 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[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 5DJXtGY08lpG for <notmuch@notmuchmail.org>;\r
+ Sun, 28 Sep 2014 07:41:22 -0700 (PDT)\r
+Received: from mail-wg0-f51.google.com (mail-wg0-f51.google.com\r
+ [74.125.82.51]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client\r
+ certificate requested) by olra.theworths.org (Postfix) with ESMTPS id\r
+ 09285431FC9 for <notmuch@notmuchmail.org>; Sun, 28 Sep 2014 07:41:14 -0700\r
+ (PDT)\r
+Received: by mail-wg0-f51.google.com with SMTP id b13so3106608wgh.34\r
+ for <notmuch@notmuchmail.org>; Sun, 28 Sep 2014 07:41:13 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
+ :references:in-reply-to:references;\r
+ bh=3EV3jE6RmW2Q945sUGQHm0+z6FhInuvENA4SdquFU0c=;\r
+ b=k3cGsDY9Fkv2AFOo1WIBqJ8TnApsBOwzZwSQm8D1IFn4DVbwrGm5imSatENJSLvkPG\r
+ CuHoJp9Xwa5ohDGxBs8UgR4m0WBdaawJVeZfhcaLW3qREx5LJIXp+yK9pvft0MGkFsq5\r
+ MB3fOfJ8Ag6iqYSw0Kd0rIkS+efac6hS3NFjActwIhG/vtc8/Ass5tJfOrwyXNEk4hie\r
+ LhLNTB7+uB3UdHlUmu6TqviFfJcvqQUqJfXffrr8dCE9/vkh0vfb4NbXfOschqsfpjJG\r
+ YrvCupNZCL8s5p4SDajewL048mSzRfQw3GNRFsfvYmTJhXGG1PuP4QOZe3PO9QY1rA/Q\r
+ mvhg==\r
+X-Gm-Message-State:\r
+ ALoCoQlS2mAazokYLwcHIEGqBY4aNcHnTav0Erfr9wPzNqhd1p9idzQaHle2DKhieY3bYBg0mLka\r
+X-Received: by 10.180.21.199 with SMTP id x7mr23392746wie.73.1411915273736;\r
+ Sun, 28 Sep 2014 07:41:13 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw2-58c36c-245.dhcp.inet.fi.\r
+ [88.195.108.245]) by mx.google.com with ESMTPSA id\r
+ lg6sm12673477wjb.40.2014.09.28.07.41.12 for <multiple recipients>\r
+ (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+ Sun, 28 Sep 2014 07:41:13 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 2/3] cli/insert: require succesful message indexing for\r
+ success status\r
+Date: Sun, 28 Sep 2014 17:40:58 +0300\r
+Message-Id:\r
+ <627e789a0574d93b66b3e935f36328661df4e99d.1411914914.git.jani@nikula.org>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <cover.1411914914.git.jani@nikula.org>\r
+References: <cover.1411914914.git.jani@nikula.org>\r
+In-Reply-To: <cover.1411914914.git.jani@nikula.org>\r
+References: <cover.1411914914.git.jani@nikula.org>\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, 28 Sep 2014 14:41:26 -0000\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 | 2 +-\r
+ 3 files changed, 43 insertions(+), 14 deletions(-)\r
+\r
+diff --git a/doc/man1/notmuch-insert.rst b/doc/man1/notmuch-insert.rst\r
+index 2be1a7b8841c..e396f6cf2279 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 0ea438015dbe..7074077ca699 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 ea9db07e2fa2..aacc643b7288 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
+-- \r
+2.1.0\r
+\r