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, ¬much)) 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