X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-new.c;h=3512de727734ad48025f3f88972587fed91d838b;hp=455ac10323f4c28c34f0bba5b5fcc3e9336c5fda;hb=baa2c9721d850ea95857f44ba0b44147c80f7998;hpb=191c4ae693c35ecd9e905e64c7619734171c4a8a diff --git a/notmuch-new.c b/notmuch-new.c index 455ac103..3512de72 100644 --- a/notmuch-new.c +++ b/notmuch-new.c @@ -50,7 +50,6 @@ typedef struct { _filename_list_t *directory_mtimes; notmuch_bool_t synchronize_flags; - _filename_list_t *message_ids_to_sync; } add_files_state_t; static volatile sig_atomic_t do_print_progress = 0; @@ -68,7 +67,7 @@ handle_sigint (unused (int sig)) { static char msg[] = "Stopping... \n"; - write(2, msg, sizeof(msg)-1); + (void) write(2, msg, sizeof(msg)-1); interrupted = 1; } @@ -451,6 +450,12 @@ add_files_recursive (notmuch_database_t *notmuch, fflush (stdout); } + status = notmuch_database_begin_atomic (notmuch); + if (status) { + ret = status; + goto DONE; + } + status = notmuch_database_add_message (notmuch, next, &message); switch (status) { /* success */ @@ -465,11 +470,8 @@ add_files_recursive (notmuch_database_t *notmuch, break; /* Non-fatal issues (go on to next file) */ case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: - /* Defer sync of maildir flags until after old filenames - * are removed in the case of a rename. */ if (state->synchronize_flags == TRUE) - _filename_list_add (state->message_ids_to_sync, - notmuch_message_get_message_id (message)); + notmuch_message_maildir_flags_to_tags (message); break; case NOTMUCH_STATUS_FILE_NOT_EMAIL: fprintf (stderr, "Note: Ignoring non-mail file: %s\n", @@ -494,6 +496,12 @@ add_files_recursive (notmuch_database_t *notmuch, goto DONE; } + status = notmuch_database_end_atomic (notmuch); + if (status) { + ret = status; + goto DONE; + } + if (message) { notmuch_message_destroy (message); message = NULL; @@ -731,11 +739,22 @@ remove_filename (notmuch_database_t *notmuch, add_files_state_t *add_files_state) { notmuch_status_t status; + notmuch_message_t *message; + status = notmuch_database_begin_atomic (notmuch); + if (status) + return status; + status = notmuch_database_find_message_by_filename (notmuch, path, &message); + if (status || message == NULL) + return status; status = notmuch_database_remove_message (notmuch, path); - if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) + if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) { add_files_state->renamed_messages++; - else + if (add_files_state->synchronize_flags == TRUE) + notmuch_message_maildir_flags_to_tags (message); + } else add_files_state->removed_messages++; + notmuch_message_destroy (message); + notmuch_database_end_atomic (notmuch); return status; } @@ -792,13 +811,18 @@ notmuch_new_command (void *ctx, int argc, char *argv[]) _filename_node_t *f; int i; notmuch_bool_t timer_is_active = FALSE; + notmuch_bool_t run_hooks = TRUE; add_files_state.verbose = 0; add_files_state.output_is_a_tty = isatty (fileno (stdout)); + argc--; argv++; /* skip subcommand argument */ + for (i = 0; i < argc && argv[i][0] == '-'; i++) { if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) { add_files_state.verbose = 1; + } else if (strcmp (argv[i], "--no-hooks") == 0) { + run_hooks = FALSE; } else { fprintf (stderr, "Unrecognized option: %s\n", argv[i]); return 1; @@ -810,9 +834,14 @@ notmuch_new_command (void *ctx, int argc, char *argv[]) add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length); add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config); - add_files_state.message_ids_to_sync = _filename_list_create (ctx); db_path = notmuch_config_get_database_path (config); + if (run_hooks) { + ret = notmuch_run_hook (db_path, "pre-new"); + if (ret) + return ret; + } + dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch"); if (stat (dot_notmuch_path, &st)) { @@ -911,32 +940,6 @@ notmuch_new_command (void *ctx, int argc, char *argv[]) talloc_free (add_files_state.removed_directories); talloc_free (add_files_state.directory_mtimes); - /* Now that removals are done (hence the database is aware of all - * renames), we can synchronize maildir_flags to tags for all - * messages that had new filenames appear on this run. */ - gettimeofday (&tv_start, NULL); - if (add_files_state.synchronize_flags) { - _filename_node_t *node; - notmuch_message_t *message; - for (node = add_files_state.message_ids_to_sync->head, i = 0; - node; - node = node->next, i++) - { - message = notmuch_database_find_message (notmuch, node->filename); - notmuch_message_maildir_flags_to_tags (message); - notmuch_message_destroy (message); - if (do_print_progress) { - do_print_progress = 0; - generic_print_progress ( - "Synchronized tags for", "messages", - tv_start, i, add_files_state.message_ids_to_sync->count); - } - } - } - - talloc_free (add_files_state.message_ids_to_sync); - add_files_state.message_ids_to_sync = NULL; - if (timer_is_active) stop_progress_printing_timer (); @@ -987,5 +990,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[]) notmuch_database_close (notmuch); + if (run_hooks && !ret && !interrupted) + ret = notmuch_run_hook (db_path, "post-new"); + return ret || interrupted; }