--- /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 8D8F9431FC0\r
+ for <notmuch@notmuchmail.org>; Fri, 3 Oct 2014 14:19:54 -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 fzeQLSamo+md for <notmuch@notmuchmail.org>;\r
+ Fri, 3 Oct 2014 14:19:48 -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 DC7A1431FC3\r
+ for <notmuch@notmuchmail.org>; Fri, 3 Oct 2014 14:19:35 -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 1XaAGF-0000LL-2p; Fri, 03 Oct 2014 18:19:35 -0300\r
+Received: (nullmailer pid 21177 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 2/4] cli/insert: add fail path to add_file_to_database\r
+Date: Fri, 3 Oct 2014 23:18:58 +0200\r
+Message-Id: <1412371140-21051-3-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:54 -0000\r
+\r
+From: Jani Nikula <jani@nikula.org>\r
+\r
+Handle failures gracefully in add_file_to_database, renamed simply\r
+add_file while at it. Add keep option to not remove the message from\r
+database if tagging or tag syncing to maildir flags fails. Expand the\r
+function documentation to cover the changes.\r
+---\r
+ notmuch-insert.c | 99 ++++++++++++++++++++++++++++++++++++--------------------\r
+ 1 file changed, 64 insertions(+), 35 deletions(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 5ef6e66..0ea4380 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -364,50 +364,80 @@ FAIL:\r
+ return NULL;\r
+ }\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
+-add_file_to_database (notmuch_database_t *notmuch, const char *path,\r
+- tag_op_list_t *tag_ops, notmuch_bool_t synchronize_flags)\r
++/*\r
++ * Add the specified message file to the notmuch database, applying\r
++ * tags in tag_ops. If synchronize_flags is TRUE, the tags are\r
++ * synchronized to maildir flags (which may result in message file\r
++ * rename).\r
++ *\r
++ * Return NOTMUCH_STATUS_SUCCESS on success, errors otherwise. If keep\r
++ * is TRUE, errors in tag changes and flag syncing are ignored and\r
++ * success status is returned; otherwise such errors cause the message\r
++ * to be removed from the database. Failure to add the message to the\r
++ * database results in error status regardless of keep.\r
++ */\r
++static notmuch_status_t\r
++add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops,\r
++ notmuch_bool_t synchronize_flags, notmuch_bool_t keep)\r
+ {\r
+ notmuch_message_t *message;\r
+ notmuch_status_t status;\r
+ \r
+ status = notmuch_database_add_message (notmuch, path, &message);\r
+- switch (status) {\r
+- case NOTMUCH_STATUS_SUCCESS:\r
+- case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
+- break;\r
+- default:\r
+- case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
+- case NOTMUCH_STATUS_READ_ONLY_DATABASE:\r
+- case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
+- case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
+- case NOTMUCH_STATUS_FILE_ERROR:\r
+- case NOTMUCH_STATUS_NULL_POINTER:\r
+- case NOTMUCH_STATUS_TAG_TOO_LONG:\r
+- case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:\r
+- case NOTMUCH_STATUS_UNBALANCED_ATOMIC:\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
+- }\r
+-\r
+- if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
+- /* Don't change tags of an existing message. */\r
+- if (synchronize_flags) {\r
+- status = notmuch_message_tags_to_maildir_flags (message);\r
+- if (status != NOTMUCH_STATUS_SUCCESS)\r
+- fprintf (stderr, "Error: failed to sync tags to maildir flags\n");\r
++ if (status == NOTMUCH_STATUS_SUCCESS) {\r
++ status = tag_op_list_apply (message, tag_ops, 0);\r
++ if (status) {\r
++ fprintf (stderr, "%s: failed to apply tags to file '%s': %s\n",\r
++ keep ? "Warning" : "Error",\r
++ path, notmuch_status_to_string (status));\r
++ goto DONE;\r
+ }\r
++ } else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
++ status = NOTMUCH_STATUS_SUCCESS;\r
++ } else if (status == NOTMUCH_STATUS_FILE_NOT_EMAIL) {\r
++ fprintf (stderr, "Error: delivery of non-mail file: '%s'\n", path);\r
++ goto FAIL;\r
+ } else {\r
+- tag_op_flag_t flags = synchronize_flags ? TAG_FLAG_MAILDIR_SYNC : 0;\r
++ fprintf (stderr, "Error: failed to add '%s' to notmuch database: %s\n",\r
++ path, notmuch_status_to_string (status));\r
++ goto FAIL;\r
++ }\r
+ \r
+- tag_op_list_apply (message, tag_ops, flags);\r
++ if (synchronize_flags) {\r
++ status = notmuch_message_tags_to_maildir_flags (message);\r
++ if (status != NOTMUCH_STATUS_SUCCESS)\r
++ fprintf (stderr, "%s: failed to sync tags to maildir flags for '%s': %s\n",\r
++ keep ? "Warning" : "Error",\r
++ path, notmuch_status_to_string (status));\r
++\r
++ /*\r
++ * Note: Unfortunately a failed maildir flag sync might\r
++ * already have renamed the file, in which case the cleanup\r
++ * path may fail.\r
++ */\r
+ }\r
+ \r
++ DONE:\r
+ notmuch_message_destroy (message);\r
++\r
++ if (status) {\r
++ if (keep) {\r
++ status = NOTMUCH_STATUS_SUCCESS;\r
++ } else {\r
++ notmuch_status_t cleanup_status;\r
++\r
++ cleanup_status = notmuch_database_remove_message (notmuch, path);\r
++ if (cleanup_status != NOTMUCH_STATUS_SUCCESS &&\r
++ cleanup_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
++ fprintf (stderr, "Warning: failed to remove '%s' from database "\r
++ "after errors: %s. Please run 'notmuch new' to fix.\n",\r
++ path, notmuch_status_to_string (cleanup_status));\r
++ }\r
++ }\r
++ }\r
++\r
++ FAIL:\r
++ return status;\r
+ }\r
+ \r
+ int\r
+@@ -508,8 +538,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\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_to_database (notmuch, newpath, tag_ops,\r
+- synchronize_flags);\r
++ add_file (notmuch, newpath, tag_ops, synchronize_flags, TRUE);\r
+ \r
+ notmuch_database_destroy (notmuch);\r
+ return EXIT_SUCCESS;\r
+-- \r
+2.1.0\r
+\r