+static void
+upgrade_print_progress (void *closure,
+ double progress)
+{
+ add_files_state_t *state = closure;
+
+ printf ("Upgrading database: %.2f%% complete", progress * 100.0);
+
+ if (progress > 0) {
+ struct timeval tv_now;
+ double elapsed, time_remaining;
+
+ gettimeofday (&tv_now, NULL);
+
+ elapsed = notmuch_time_elapsed (state->tv_start, tv_now);
+ time_remaining = (elapsed / progress) * (1.0 - progress);
+ printf (" (");
+ notmuch_time_print_formatted_seconds (time_remaining);
+ printf (" remaining)");
+ }
+
+ printf (". \r");
+
+ 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;
+ 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++;
+ 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;
+}
+
+/* Recursively remove all filenames from the database referring to
+ * 'path' (or to any of its children). */
+static void
+_remove_directory (void *ctx,
+ notmuch_database_t *notmuch,
+ const char *path,
+ add_files_state_t *add_files_state)
+{
+ notmuch_directory_t *directory;
+ notmuch_filenames_t *files, *subdirs;
+ char *absolute;
+
+ directory = notmuch_database_get_directory (notmuch, path);
+
+ for (files = notmuch_directory_get_child_files (directory);
+ notmuch_filenames_valid (files);
+ notmuch_filenames_move_to_next (files))
+ {
+ absolute = talloc_asprintf (ctx, "%s/%s", path,
+ notmuch_filenames_get (files));
+ remove_filename (notmuch, absolute, add_files_state);
+ talloc_free (absolute);
+ }
+
+ for (subdirs = notmuch_directory_get_child_directories (directory);
+ notmuch_filenames_valid (subdirs);
+ notmuch_filenames_move_to_next (subdirs))
+ {
+ absolute = talloc_asprintf (ctx, "%s/%s", path,
+ notmuch_filenames_get (subdirs));
+ _remove_directory (ctx, notmuch, absolute, add_files_state);
+ talloc_free (absolute);
+ }
+
+ notmuch_directory_destroy (directory);
+}
+