]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-new.c
uncrustify.cfg: label indent, some known types, not, # and ##
[notmuch] / notmuch-new.c
index 598a2083ffaec689285878a6a4b873d8d6f4b245..a569a54454560ef33cb3fdad41c484658f514f35 100644 (file)
@@ -67,7 +67,11 @@ handle_sigint (unused (int sig))
 {
     static char msg[] = "Stopping...         \n";
 
-    write(2, msg, sizeof(msg)-1);
+    /* This write is "opportunistic", so it's okay to ignore the
+     * result.  It is not required for correctness, and if it does
+     * fail or produce a short write, we want to get out of the signal
+     * handler as quickly as possible, not retry it. */
+    IGNORE_RESULT (write (2, msg, sizeof(msg)-1));
     interrupted = 1;
 }
 
@@ -450,6 +454,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 */
@@ -490,6 +500,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;
@@ -728,7 +744,12 @@ remove_filename (notmuch_database_t *notmuch,
 {
     notmuch_status_t status;
     notmuch_message_t *message;
-    message = notmuch_database_find_message_by_filename (notmuch, path);
+    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) {
        add_files_state->renamed_messages++;
@@ -737,6 +758,7 @@ remove_filename (notmuch_database_t *notmuch,
     } else
        add_files_state->removed_messages++;
     notmuch_message_destroy (message);
+    notmuch_database_end_atomic (notmuch);
     return status;
 }
 
@@ -793,13 +815,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;
@@ -813,6 +840,12 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
     add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
     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)) {
@@ -961,5 +994,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;
 }