]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-new.c
Use notmuch_database_destroy instead of notmuch_database_close
[notmuch] / notmuch-new.c
index 4f13535c75d92f5755502125462e54464c613497..3ff630478c613848f342ddbdb42fa625484254d0 100644 (file)
@@ -308,6 +308,10 @@ add_files_recursive (notmuch_database_t *notmuch,
     if (num_fs_entries == -1) {
        fprintf (stderr, "Error opening directory %s: %s\n",
                 path, strerror (errno));
+       /* We consider this a fatal error because, if a user moved a
+        * message from another directory that we were able to scan
+        * into this directory, skipping this directory will cause
+        * that message to be lost. */
        ret = NOTMUCH_STATUS_FILE_ERROR;
        goto DONE;
     }
@@ -351,8 +355,10 @@ add_files_recursive (notmuch_database_t *notmuch,
 
        next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
        status = add_files_recursive (notmuch, next, state);
-       if (status && ret == NOTMUCH_STATUS_SUCCESS)
+       if (status) {
            ret = status;
+           goto DONE;
+       }
        talloc_free (next);
        next = NULL;
     }
@@ -773,27 +779,33 @@ remove_filename (notmuch_database_t *notmuch,
        return status;
     status = notmuch_database_find_message_by_filename (notmuch, path, &message);
     if (status || message == NULL)
-       return status;
+       goto DONE;
+
     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
+       status = NOTMUCH_STATUS_SUCCESS;
+    } else if (status == NOTMUCH_STATUS_SUCCESS) {
        add_files_state->removed_messages++;
+    }
     notmuch_message_destroy (message);
+
+  DONE:
     notmuch_database_end_atomic (notmuch);
     return status;
 }
 
 /* Recursively remove all filenames from the database referring to
  * 'path' (or to any of its children). */
-static void
+static notmuch_status_t
 _remove_directory (void *ctx,
                   notmuch_database_t *notmuch,
                   const char *path,
                   add_files_state_t *add_files_state)
 {
+    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
     notmuch_directory_t *directory;
     notmuch_filenames_t *files, *subdirs;
     char *absolute;
@@ -806,8 +818,10 @@ _remove_directory (void *ctx,
     {
        absolute = talloc_asprintf (ctx, "%s/%s", path,
                                    notmuch_filenames_get (files));
-       remove_filename (notmuch, absolute, add_files_state);
+       status = remove_filename (notmuch, absolute, add_files_state);
        talloc_free (absolute);
+       if (status)
+           goto DONE;
     }
 
     for (subdirs = notmuch_directory_get_child_directories (directory);
@@ -816,11 +830,15 @@ _remove_directory (void *ctx,
     {
        absolute = talloc_asprintf (ctx, "%s/%s", path,
                                    notmuch_filenames_get (subdirs));
-       _remove_directory (ctx, notmuch, absolute, add_files_state);
+       status = _remove_directory (ctx, notmuch, absolute, add_files_state);
        talloc_free (absolute);
+       if (status)
+           goto DONE;
     }
 
+  DONE:
     notmuch_directory_destroy (directory);
+    return status;
 }
 
 int
@@ -933,10 +951,14 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
     }
 
     ret = add_files (notmuch, db_path, &add_files_state);
+    if (ret)
+       goto DONE;
 
     gettimeofday (&tv_start, NULL);
     for (f = add_files_state.removed_files->head; f && !interrupted; f = f->next) {
-       remove_filename (notmuch, f->filename, &add_files_state);
+       ret = remove_filename (notmuch, f->filename, &add_files_state);
+       if (ret)
+           goto DONE;
        if (do_print_progress) {
            do_print_progress = 0;
            generic_print_progress ("Cleaned up", "messages",
@@ -947,7 +969,9 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 
     gettimeofday (&tv_start, NULL);
     for (f = add_files_state.removed_directories->head, i = 0; f && !interrupted; f = f->next, i++) {
-       _remove_directory (ctx, notmuch, f->filename, &add_files_state);
+       ret = _remove_directory (ctx, notmuch, f->filename, &add_files_state);
+       if (ret)
+           goto DONE;
        if (do_print_progress) {
            do_print_progress = 0;
            generic_print_progress ("Cleaned up", "directories",
@@ -965,6 +989,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
        }
     }
 
+  DONE:
     talloc_free (add_files_state.removed_files);
     talloc_free (add_files_state.removed_directories);
     talloc_free (add_files_state.directory_mtimes);
@@ -1012,12 +1037,11 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 
     printf ("\n");
 
-    if (ret) {
-       printf ("\nNote: At least one error was encountered: %s\n",
-               notmuch_status_to_string (ret));
-    }
+    if (ret)
+       fprintf (stderr, "Note: A fatal error was encountered: %s\n",
+                notmuch_status_to_string (ret));
 
-    notmuch_database_close (notmuch);
+    notmuch_database_destroy (notmuch);
 
     if (run_hooks && !ret && !interrupted)
        ret = notmuch_run_hook (db_path, "post-new");