]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-new.c
new: Synchronize maildir flags eagerly.
[notmuch] / notmuch-new.c
index c7dfbb748fb9d50f878795acaabedae22677a383..598a2083ffaec689285878a6a4b873d8d6f4b245 100644 (file)
@@ -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;
@@ -465,11 +464,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",
@@ -724,6 +720,26 @@ upgrade_print_progress (void *closure,
     fflush (stdout);
 }
 
+/* Remove one message filename from the database. */
+static notmuch_status_t
+remove_filename (notmuch_database_t *notmuch,
+                const char *path,
+                add_files_state_t *add_files_state)
+{
+    notmuch_status_t status;
+    notmuch_message_t *message;
+    message = notmuch_database_find_message_by_filename (notmuch, path);
+    status = notmuch_database_remove_message (notmuch, path);
+    if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
+       add_files_state->renamed_messages++;
+       if (add_files_state->synchronize_flags == TRUE)
+           notmuch_message_maildir_flags_to_tags (message);
+    } else
+       add_files_state->removed_messages++;
+    notmuch_message_destroy (message);
+    return status;
+}
+
 /* Recursively remove all filenames from the database referring to
  * 'path' (or to any of its children). */
 static void
@@ -734,7 +750,6 @@ _remove_directory (void *ctx,
 {
     notmuch_directory_t *directory;
     notmuch_filenames_t *files, *subdirs;
-    notmuch_status_t status;
     char *absolute;
 
     directory = notmuch_database_get_directory (notmuch, path);
@@ -745,11 +760,7 @@ _remove_directory (void *ctx,
     {
        absolute = talloc_asprintf (ctx, "%s/%s", path,
                                    notmuch_filenames_get (files));
-       status = notmuch_database_remove_message (notmuch, absolute);
-       if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
-           add_files_state->renamed_messages++;
-       else
-           add_files_state->removed_messages++;
+       remove_filename (notmuch, absolute, add_files_state);
        talloc_free (absolute);
     }
 
@@ -780,7 +791,6 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
     char *dot_notmuch_path;
     struct sigaction action;
     _filename_node_t *f;
-    notmuch_status_t status;
     int i;
     notmuch_bool_t timer_is_active = FALSE;
 
@@ -801,7 +811,6 @@ 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);
 
     dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
@@ -869,11 +878,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 
     gettimeofday (&tv_start, NULL);
     for (f = add_files_state.removed_files->head; f && !interrupted; f = f->next) {
-       status = notmuch_database_remove_message (notmuch, f->filename);
-       if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
-           add_files_state.renamed_messages++;
-       else
-           add_files_state.removed_messages++;
+       remove_filename (notmuch, f->filename, &add_files_state);
        if (do_print_progress) {
            do_print_progress = 0;
            generic_print_progress ("Cleaned up", "messages",
@@ -906,32 +911,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 ();