+static void
+upgrade_print_progress (void *closure,
+ unsigned int count,
+ unsigned int total)
+{
+ add_files_state_t *state = closure;
+ struct timeval tv_now;
+ double elapsed_overall, rate_overall, time_remaining;
+
+ gettimeofday (&tv_now, NULL);
+
+ elapsed_overall = notmuch_time_elapsed (state->tv_start, tv_now);
+ rate_overall = count / elapsed_overall;
+ time_remaining = ((total - count) / rate_overall);
+
+ printf ("Upgraded %d of %d messages (", count, total);
+ notmuch_time_print_formatted_seconds (time_remaining);
+ printf (" remaining). \r");
+
+ fflush (stdout);
+}
+
+/* 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,
+ int *renamed_files,
+ int *removed_files)
+{
+ notmuch_directory_t *directory;
+ notmuch_filenames_t *files, *subdirs;
+ notmuch_status_t status;
+ char *absolute;
+
+ directory = notmuch_database_get_directory (notmuch, path);
+
+ for (files = notmuch_directory_get_child_files (directory);
+ notmuch_filenames_has_more (files);
+ notmuch_filenames_advance (files))
+ {
+ 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)
+ *renamed_files = *renamed_files + 1;
+ else
+ *removed_files = *removed_files + 1;
+ talloc_free (absolute);
+ }
+
+ for (subdirs = notmuch_directory_get_child_directories (directory);
+ notmuch_filenames_has_more (subdirs);
+ notmuch_filenames_advance (subdirs))
+ {
+ absolute = talloc_asprintf (ctx, "%s/%s", path,
+ notmuch_filenames_get (subdirs));
+ _remove_directory (ctx, notmuch, absolute, renamed_files, removed_files);
+ talloc_free (absolute);
+ }
+
+ notmuch_directory_destroy (directory);
+}
+