--- /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 2CA17431FC2\r
+ for <notmuch@notmuchmail.org>; Sun, 28 Sep 2014 07:41:11 -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 bY31HXN5-c52 for <notmuch@notmuchmail.org>;\r
+ Sun, 28 Sep 2014 07:41:06 -0700 (PDT)\r
+Received: from mail-wi0-f180.google.com (mail-wi0-f180.google.com\r
+ [209.85.212.180]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 8A0D4431FBF\r
+ for <notmuch@notmuchmail.org>; Sun, 28 Sep 2014 07:41:06 -0700 (PDT)\r
+Received: by mail-wi0-f180.google.com with SMTP id q5so1818710wiv.13\r
+ for <notmuch@notmuchmail.org>; Sun, 28 Sep 2014 07:41:05 -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=YW+Nxie2xerdBnEr05PSaMJt5Yq5ucTNH6q8mM0zOMg=;\r
+ b=hA6rRoWQaf+68Ig6iTZegZcfZBpWCLBQCiDtHXf4rwdCQ6IBE8DIi/f8NcSFh7/vG2\r
+ x4bDDsY8zyDHQ+Hyz4clft4kpTb+LIAMYpVt2S9ZZ7qvIvoXGLXr9OjGuptntwJ6WKJ3\r
+ aKlIS701wBp0S0M89iACDOhhUBb/KFB1fbBsAMXInpCQbZWE8FI3L5qKJD5MEZmKWNWv\r
+ HiBck72zRikXjGQ/ckaCGKRYMhND+geMzC3Hyer2h289GTNF3WlS5VLoa3K6kjWo5X6+\r
+ +pBjXAakSkC0RqsKI20bt1D641+CkeK13yTvmzb2uNQURmnestn0dgBtlDpaObUYljee\r
+ 4ESg==\r
+X-Gm-Message-State:\r
+ ALoCoQngQ53VPER3gS113meSHt7aB5H9knDKtLQ7qrZQCFB84O3Ozesozuu03GIo8IgWKyGQ5H8J\r
+X-Received: by 10.194.62.107 with SMTP id x11mr4831597wjr.49.1411915265394;\r
+ Sun, 28 Sep 2014 07:41:05 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw2-58c36c-245.dhcp.inet.fi.\r
+ [88.195.108.245])\r
+ by mx.google.com with ESMTPSA id g6sm12706034wjf.13.2014.09.28.07.41.04\r
+ for <multiple recipients>\r
+ (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+ Sun, 28 Sep 2014 07:41:04 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 1/3] cli/insert: add fail path to add_file_to_database\r
+Date: Sun, 28 Sep 2014 17:40:57 +0300\r
+Message-Id:\r
+ <7b0d3b0e7357e2dd87ca48a559e6b7f661c1374c.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:11 -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 | 99 ++++++++++++++++++++++++++++++++++++--------------------\r
+ 1 file changed, 64 insertions(+), 35 deletions(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 5ef6e66d86e2..0ea438015dbe 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