--- /dev/null
+Return-Path: <novalazy@gmail.com>\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 64F50431FDC\r
+ for <notmuch@notmuchmail.org>; Wed, 16 Apr 2014 06:00:43 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_FROM=0.001, 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 agypQVppRV2o for <notmuch@notmuchmail.org>;\r
+ Wed, 16 Apr 2014 06:00:36 -0700 (PDT)\r
+Received: from mail-pa0-f42.google.com (mail-pa0-f42.google.com\r
+ [209.85.220.42]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id CC304431FDE\r
+ for <notmuch@notmuchmail.org>; Wed, 16 Apr 2014 06:00:08 -0700 (PDT)\r
+Received: by mail-pa0-f42.google.com with SMTP id fb1so10923726pad.15\r
+ for <notmuch@notmuchmail.org>; Wed, 16 Apr 2014 06:00:08 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references;\r
+ bh=+HXB+LTFJM7wRO+0IUZtPIGOPE0vuuNVAiMcp6D0URY=;\r
+ b=xwFlwQtDx3+GR7nzOj50kSJpSQe9T4LCfBtxVQt5ffcLhrogq/uxmEr++0zCr0EjMb\r
+ uWW9NTCQbI0H2K6mDTVkiC2QYe4J1G9dwGXfHK8OT67Ipx7vNzPD4WBRJh5kSiT5hdNz\r
+ rChslUwKHAUGCHYWur8gxtpfXFJRXyL/vtXY3NHBtsfeVlZhInAjCBKYM4lzwtwRLbni\r
+ qMMjZvdOr7IRi9FJl8291ZfDUwiZHxW0ehi7j1Ekzso16uC0fGZ6ze7G2Blnh+ayvbuM\r
+ offBDzj48qvBeQcINLgVDlTaWatbUXazOgM1UejLnFRSl8ADB2Q9dIM6Hor++XX25dW+\r
+ sGMA==\r
+X-Received: by 10.68.143.196 with SMTP id sg4mr8185642pbb.155.1397653208034;\r
+ Wed, 16 Apr 2014 06:00:08 -0700 (PDT)\r
+Received: from localhost (215.42.233.220.static.exetel.com.au.\r
+ [220.233.42.215]) by mx.google.com with ESMTPSA id\r
+ hw8sm46903968pbc.62.2014.04.16.06.00.06 for <multiple recipients>\r
+ (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+ Wed, 16 Apr 2014 06:00:07 -0700 (PDT)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 06/10] cli: refactor insert\r
+Date: Wed, 16 Apr 2014 22:59:21 +1000\r
+Message-Id: <1397653165-15620-7-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.8.4\r
+In-Reply-To: <1397653165-15620-1-git-send-email-novalazy@gmail.com>\r
+References: <1397653165-15620-1-git-send-email-novalazy@gmail.com>\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: Wed, 16 Apr 2014 13:00:43 -0000\r
+\r
+Change insert_message into write_message and move its responsibilities\r
+for indexing the message into the main function, to simplify the control\r
+flow.\r
+---\r
+ notmuch-insert.c | 63 +++++++++++++++++++++++++++-----------------------------\r
+ 1 file changed, 30 insertions(+), 33 deletions(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 6752fc8..7db4f73 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -338,59 +338,48 @@ add_file_to_database (notmuch_database_t *notmuch, const char *path,\r
+ }\r
+ \r
+ static notmuch_bool_t\r
+-insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,\r
+- const char *dir, tag_op_list_t *tag_ops,\r
+- notmuch_bool_t synchronize_flags)\r
++write_message (void *ctx, int fdin, const char *dir, char **newpath)\r
+ {\r
+ char *tmppath;\r
+- char *newpath;\r
+ char *newdir;\r
+ int fdout;\r
+- char *cleanup_path;\r
+ \r
+- fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir);\r
++ fdout = maildir_open_tmp_file (ctx, dir, &tmppath, newpath, &newdir);\r
+ if (fdout < 0)\r
+ return FALSE;\r
+ \r
+- cleanup_path = tmppath;\r
+-\r
+- if (! copy_stdin (fdin, fdout))\r
+- goto FAIL;\r
++ if (! copy_stdin (fdin, fdout)) {\r
++ close (fdout);\r
++ unlink (tmppath);\r
++ return FALSE;\r
++ }\r
+ \r
+ if (fsync (fdout) != 0) {\r
+ fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno));\r
+- goto FAIL;\r
++ close (fdout);\r
++ unlink (tmppath);\r
++ return FALSE;\r
+ }\r
+ \r
+ close (fdout);\r
+- fdout = -1;\r
+ \r
+ /* Atomically move the new message file from the Maildir 'tmp' directory\r
+ * to the 'new' directory. We follow the Dovecot recommendation to\r
+ * simply use rename() instead of link() and unlink().\r
+ * See also: http://wiki.dovecot.org/MailboxFormat/Maildir#Mail_delivery\r
+ */\r
+- if (rename (tmppath, newpath) != 0) {\r
++ if (rename (tmppath, *newpath) != 0) {\r
+ fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno));\r
+- goto FAIL;\r
++ unlink (tmppath);\r
++ return FALSE;\r
+ }\r
+ \r
+- cleanup_path = newpath;\r
+-\r
+- if (! sync_dir (newdir))\r
+- goto FAIL;\r
+-\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, synchronize_flags);\r
++ if (! sync_dir (newdir)) {\r
++ unlink (*newpath);\r
++ return FALSE;\r
++ }\r
+ \r
+ return TRUE;\r
+-\r
+- FAIL:\r
+- if (fdout >= 0)\r
+- close (fdout);\r
+- unlink (cleanup_path);\r
+- return FALSE;\r
+ }\r
+ \r
+ int\r
+@@ -407,9 +396,9 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ notmuch_bool_t create_folder = FALSE;\r
+ notmuch_bool_t synchronize_flags;\r
+ const char *maildir;\r
++ char *newpath;\r
+ int opt_index;\r
+ unsigned int i;\r
+- notmuch_bool_t ret;\r
+ \r
+ notmuch_opt_desc_t options[] = {\r
+ { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },\r
+@@ -484,10 +473,18 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))\r
+ return EXIT_FAILURE;\r
+ \r
+- ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops,\r
+- synchronize_flags);\r
++ /* Write the message to the Maildir new directory. */\r
++ if (! write_message (config, STDIN_FILENO, maildir, &newpath)) {\r
++ notmuch_database_destroy (notmuch);\r
++ return EXIT_FAILURE;\r
++ }\r
+ \r
+- notmuch_database_destroy (notmuch);\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
+ \r
+- return ret ? EXIT_SUCCESS : EXIT_FAILURE;\r
++ notmuch_database_destroy (notmuch);\r
++ return EXIT_SUCCESS;\r
+ }\r
+-- \r
+1.8.4\r
+\r