unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH 1/3] cli: add insert --must-index option
@ 2013-07-21  0:07 Peter Wang
  2013-07-21  0:07 ` [PATCH 2/3] man: document insert --must-index Peter Wang
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Peter Wang @ 2013-07-21  0:07 UTC (permalink / raw)
  To: notmuch

This option causes notmuch insert to fail as a whole if the message
failed to be added to the notmuch database.  The new message file
will be deleted from disk, and a distinct status code (2) returned.
---
 notmuch-insert.c | 76 ++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 46 insertions(+), 30 deletions(-)

diff --git a/notmuch-insert.c b/notmuch-insert.c
index 2207b1e..505b647 100644
--- a/notmuch-insert.c
+++ b/notmuch-insert.c
@@ -28,6 +28,10 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
+#define INSERT_EXIT_SUCCESS	0
+#define INSERT_EXIT_FAIL_WRITE	1
+#define INSERT_EXIT_FAIL_INDEX	2
+
 static volatile sig_atomic_t interrupted;
 
 static void
@@ -293,12 +297,13 @@ copy_stdin (int fdin, int fdout)
 
 /* Add the specified message file to the notmuch database, applying tags.
  * The file is renamed to encode notmuch tags as maildir flags. */
-static void
+static notmuch_status_t
 add_file_to_database (notmuch_database_t *notmuch, const char *path,
 		      tag_op_list_t *tag_ops)
 {
     notmuch_message_t *message;
     notmuch_status_t status;
+    notmuch_status_t sync;
 
     status = notmuch_database_add_message (notmuch, path, &message);
     switch (status) {
@@ -318,47 +323,52 @@ add_file_to_database (notmuch_database_t *notmuch, const char *path,
     case NOTMUCH_STATUS_LAST_STATUS:
 	fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n",
 		 path, notmuch_status_to_string (status));
-	return;
+	return status;
     }
 
     if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
 	/* Don't change tags of an existing message. */
-	status = notmuch_message_tags_to_maildir_flags (message);
-	if (status != NOTMUCH_STATUS_SUCCESS)
+	sync = notmuch_message_tags_to_maildir_flags (message);
+	if (sync != NOTMUCH_STATUS_SUCCESS)
 	    fprintf (stderr, "Error: failed to sync tags to maildir flags\n");
     } else {
 	tag_op_list_apply (message, tag_ops, TAG_FLAG_MAILDIR_SYNC);
     }
 
     notmuch_message_destroy (message);
+
+    return status;
 }
 
-static notmuch_bool_t
+static int
 insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,
-		const char *dir, tag_op_list_t *tag_ops)
+		const char *dir, tag_op_list_t *tag_ops,
+		notmuch_bool_t must_index)
 {
     char *tmppath;
     char *newpath;
     char *newdir;
     int fdout;
-    char *cleanup_path;
+    notmuch_status_t status;
 
     fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir);
     if (fdout < 0)
-	return FALSE;
+	return INSERT_EXIT_FAIL_WRITE;
 
-    cleanup_path = tmppath;
-
-    if (! copy_stdin (fdin, fdout))
-	goto FAIL;
+    if (! copy_stdin (fdin, fdout)) {
+	close (fdout);
+	unlink (tmppath);
+	return INSERT_EXIT_FAIL_WRITE;
+    }
 
     if (fsync (fdout) != 0) {
 	fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno));
-	goto FAIL;
+	close (fdout);
+	unlink (tmppath);
+	return INSERT_EXIT_FAIL_WRITE;
     }
 
     close (fdout);
-    fdout = -1;
 
     /* Atomically move the new message file from the Maildir 'tmp' directory
      * to the 'new' directory.  We follow the Dovecot recommendation to
@@ -367,25 +377,28 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,
      */
     if (rename (tmppath, newpath) != 0) {
 	fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno));
-	goto FAIL;
+	unlink (tmppath);
+	return INSERT_EXIT_FAIL_WRITE;
     }
 
-    cleanup_path = newpath;
-
-    if (! sync_dir (newdir))
-	goto FAIL;
+    if (! sync_dir (newdir)) {
+	unlink (newpath);
+	return INSERT_EXIT_FAIL_WRITE;
+    }
 
-    /* Even if adding the message to the notmuch database fails,
-     * the message is on disk and we consider the delivery completed. */
-    add_file_to_database (notmuch, newpath, tag_ops);
+    status = add_file_to_database (notmuch, newpath, tag_ops);
 
-    return TRUE;
+    /* If must_index is TRUE, then indexing must succeed.  Otherwise, we
+     * consider the delivery completed as long as the message is on disk. */
+    if (must_index &&
+	status != NOTMUCH_STATUS_SUCCESS &&
+	status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
+    {
+	unlink (newpath);
+	return INSERT_EXIT_FAIL_INDEX;
+    }
 
-  FAIL:
-    if (fdout >= 0)
-	close (fdout);
-    unlink (cleanup_path);
-    return FALSE;
+    return INSERT_EXIT_SUCCESS;
 }
 
 int
@@ -400,6 +413,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
     char *query_string = NULL;
     const char *folder = NULL;
     notmuch_bool_t create_folder = FALSE;
+    notmuch_bool_t must_index = FALSE;
     const char *maildir;
     int opt_index;
     unsigned int i;
@@ -408,6 +422,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
     notmuch_opt_desc_t options[] = {
 	{ NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },
 	{ NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },
+	{ NOTMUCH_OPT_BOOLEAN, &must_index, "must-index", 0, 0 },
 	{ NOTMUCH_OPT_END, 0, 0, 0, 0 }
     };
 
@@ -471,9 +486,10 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
 			       NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
 	return 1;
 
-    ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops);
+    ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops,
+			  must_index);
 
     notmuch_database_destroy (notmuch);
 
-    return (ret) ? 0 : 1;
+    return ret;
 }
-- 
1.7.12.1

^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2013-10-24 10:19 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-21  0:07 [PATCH 1/3] cli: add insert --must-index option Peter Wang
2013-07-21  0:07 ` [PATCH 2/3] man: document insert --must-index Peter Wang
2013-07-21  0:07 ` [PATCH 3/3] test: test " Peter Wang
2013-07-21  8:31 ` [PATCH 1/3] cli: add insert --must-index option Mark Walters
2013-07-27  5:15   ` Peter Wang
2013-09-10  8:06     ` Mark Walters
2013-09-11 14:13       ` Peter Wang
2013-10-10 10:41         ` David Bremner
2013-10-10 12:30           ` Tomi Ollila
2013-10-10 14:15             ` David Bremner
2013-10-23 19:05               ` Tomi Ollila
2013-10-23 19:32                 ` Austin Clements
2013-10-23 21:34                   ` Tomi Ollila
2013-10-24  0:05                   ` David Bremner
2013-10-24 10:19                     ` Tomi Ollila

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).