--- /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 99376431FDF\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:55:31 -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 gjpDth77LbDT for <notmuch@notmuchmail.org>;\r
+ Mon, 22 Sep 2014 02:55:26 -0700 (PDT)\r
+Received: from mail-we0-f176.google.com (mail-we0-f176.google.com\r
+ [74.125.82.176]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 29FE8429E26\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:55:01 -0700 (PDT)\r
+Received: by mail-we0-f176.google.com with SMTP id w61so1742241wes.35\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:55:00 -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=acttXzxAi/1AvUHUOanoOBLDkWKGmWhG+eJ4FyJWBLA=;\r
+ b=VTYDxlShsDt13c98hrs2KepWQ53iakb8ZM1UFrIp98j5TsjxfPXmaJYxRwj3IXxyHZ\r
+ AfyOW6+xWBDOSGaF2WUcHltI6rILfYp7w+AB6UjOTnVHEL+uCOZZVZ0ByXsyRc7+KLJn\r
+ fuVC+0ps7ODO/w+1aT/BMjbhSJTrw5I+v2IuU9fE4WI3EmeB83XbiYG8Ngii7o60rYWW\r
+ JuXkx6KbvvC1ct9W//5OKwoylCzTACo0qm8RCKcIosUzmuOisX+6WqVrWtZahCwpbqOu\r
+ OmODV75eEwRNjaI7INSxjoJyImaMDkfTKNBKJ0ChFUnWK0IXHVuscGmESSNoD5CSibzR\r
+ sGXA==\r
+X-Gm-Message-State:\r
+ ALoCoQkV5RumLQhCMr3ssUqgLypC5S24+XAclXSaUdm7nTJkeqzkBcBww0E4U/9XTU8jrRsD4GAo\r
+X-Received: by 10.194.157.135 with SMTP id wm7mr1485658wjb.117.1411379699962; \r
+ Mon, 22 Sep 2014 02:54:59 -0700 (PDT)\r
+Received: from localhost ([2001:4b98:dc0:43:216:3eff:fe1b:25f3])\r
+ by mx.google.com with ESMTPSA id ek8sm9670807wib.23.2014.09.22.02.54.59\r
+ for <multiple recipients>\r
+ (version=TLSv1.1 cipher=RC4-SHA bits=128/128);\r
+ Mon, 22 Sep 2014 02:54:59 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 09/11] cli/insert: add fail path to add_file_to_database\r
+Date: Mon, 22 Sep 2014 11:55:00 +0200\r
+Message-Id:\r
+ <5089ce31afa0ba764520270af1e680a4ab50b4e6.1411379395.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.2.5\r
+In-Reply-To: <cover.1411379395.git.jani@nikula.org>\r
+References: <cover.1411379395.git.jani@nikula.org>\r
+In-Reply-To: <cover.1411379395.git.jani@nikula.org>\r
+References: <cover.1411379395.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: Mon, 22 Sep 2014 09:55:32 -0000\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 | 89 ++++++++++++++++++++++++++++++++---------------------\r
+ 1 files changed, 54 insertions(+), 35 deletions(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 5ef6e66..80f52d4 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -364,50 +364,70 @@ 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
++ return status;\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
++ return status;\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 will 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_database_remove_message (notmuch, path);\r
++ }\r
++\r
++ return status;\r
+ }\r
+ \r
+ int\r
+@@ -508,8 +528,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
+1.7.2.5\r
+\r