X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-insert.c;h=90fe3bad2bfb7291d3d81e6deafb50e6b62d913e;hp=0ea438015dbe283111a6c3569d2840cd84c0e94c;hb=19c09d870f18f549df3d9f5a3a30a20f7279b07a;hpb=5df46a3d9efcc201700b02e70b11808fcc40beab diff --git a/notmuch-insert.c b/notmuch-insert.c index 0ea43801..90fe3bad 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -197,7 +197,7 @@ tempfilename (const void *ctx) gettimeofday (&tv, NULL); filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s", - tv.tv_sec, tv.tv_usec, pid, hostname); + (long) tv.tv_sec, (long) tv.tv_usec, pid, hostname); if (! filename) fprintf (stderr, "Error: %s\n", strerror (ENOMEM)); @@ -443,6 +443,7 @@ add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops, int notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) { + notmuch_status_t status, close_status; notmuch_database_t *notmuch; struct sigaction action; const char *db_path; @@ -452,6 +453,8 @@ 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 keep = FALSE; + notmuch_bool_t no_hooks = FALSE; notmuch_bool_t synchronize_flags; const char *maildir; char *newpath; @@ -461,6 +464,8 @@ 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, &keep, "keep", 0, 0 }, + { NOTMUCH_OPT_BOOLEAN, &no_hooks, "no-hooks", 'n', 0 }, { NOTMUCH_OPT_END, 0, 0, 0, 0 } }; @@ -535,11 +540,37 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) return EXIT_FAILURE; } - /* Add the message to the index. - * Even if adding the message to the notmuch database fails, - * the message is on disk and we consider the delivery completed. */ - add_file (notmuch, newpath, tag_ops, synchronize_flags, TRUE); + /* Index the message. */ + status = add_file (notmuch, newpath, tag_ops, synchronize_flags, keep); + + /* Commit changes. */ + close_status = notmuch_database_destroy (notmuch); + if (close_status) { + /* Hold on to the first error, if any. */ + if (! status) + status = close_status; + fprintf (stderr, "%s: failed to commit database changes: %s\n", + keep ? "Warning" : "Error", + notmuch_status_to_string (close_status)); + } + + if (status) { + if (keep) { + status = NOTMUCH_STATUS_SUCCESS; + } else { + /* If maildir flag sync failed, this might fail. */ + if (unlink (newpath)) { + fprintf (stderr, "Warning: failed to remove '%s' from maildir " + "after errors: %s. Please run 'notmuch new' to fix.\n", + newpath, strerror (errno)); + } + } + } + + if (! no_hooks && status == NOTMUCH_STATUS_SUCCESS) { + /* Ignore hook failures. */ + notmuch_run_hook (db_path, "post-insert"); + } - notmuch_database_destroy (notmuch); - return EXIT_SUCCESS; + return status ? EXIT_FAILURE : EXIT_SUCCESS; }