1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
25 typedef struct _filename_node {
28 struct _filename_node *next;
31 typedef struct _filename_list {
33 _filename_node_t *head;
34 _filename_node_t **tail;
40 const char **new_tags;
41 size_t new_tags_length;
45 int added_messages, removed_messages, renamed_messages;
46 struct timeval tv_start;
48 _filename_list_t *removed_files;
49 _filename_list_t *removed_directories;
50 _filename_list_t *directory_mtimes;
52 notmuch_bool_t synchronize_flags;
53 _filename_list_t *message_ids_to_sync;
56 static volatile sig_atomic_t do_print_progress = 0;
59 handle_sigalrm (unused (int signal))
61 do_print_progress = 1;
64 static volatile sig_atomic_t interrupted;
67 handle_sigint (unused (int sig))
69 static char msg[] = "Stopping... \n";
71 write(2, msg, sizeof(msg)-1);
75 static _filename_list_t *
76 _filename_list_create (const void *ctx)
78 _filename_list_t *list;
80 list = talloc (ctx, _filename_list_t);
85 list->tail = &list->head;
91 static _filename_node_t *
92 _filename_list_add (_filename_list_t *list,
95 _filename_node_t *node = talloc (list, _filename_node_t);
99 node->filename = talloc_strdup (list, filename);
102 *(list->tail) = node;
103 list->tail = &node->next;
109 generic_print_progress (const char *action, const char *object,
110 struct timeval tv_start, unsigned processed, unsigned total)
112 struct timeval tv_now;
113 double elapsed_overall, rate_overall;
115 gettimeofday (&tv_now, NULL);
117 elapsed_overall = notmuch_time_elapsed (tv_start, tv_now);
118 rate_overall = processed / elapsed_overall;
120 printf ("%s %d ", action, processed);
123 printf ("of %d %s", total, object);
124 if (processed > 0 && elapsed_overall > 0.5) {
125 double time_remaining = ((total - processed) / rate_overall);
127 notmuch_time_print_formatted_seconds (time_remaining);
128 printf (" remaining)");
131 printf ("%s", object);
132 if (elapsed_overall > 0.5)
133 printf (" (%d %s/sec.)", (int) rate_overall, object);
135 printf (".\033[K\r");
141 dirent_sort_inode (const struct dirent **a, const struct dirent **b)
143 return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
147 dirent_sort_strcmp_name (const struct dirent **a, const struct dirent **b)
149 return strcmp ((*a)->d_name, (*b)->d_name);
152 /* Test if the directory looks like a Maildir directory.
154 * Search through the array of directory entries to see if we can find all
155 * three subdirectories typical for Maildir, that is "new", "cur", and "tmp".
157 * Return 1 if the directory looks like a Maildir and 0 otherwise.
160 _entries_resemble_maildir (struct dirent **entries, int count)
164 for (i = 0; i < count; i++) {
165 if (entries[i]->d_type != DT_DIR && entries[i]->d_type != DT_UNKNOWN)
168 if (strcmp(entries[i]->d_name, "new") == 0 ||
169 strcmp(entries[i]->d_name, "cur") == 0 ||
170 strcmp(entries[i]->d_name, "tmp") == 0)
181 /* Examine 'path' recursively as follows:
183 * o Ask the filesystem for the mtime of 'path' (fs_mtime)
184 * o Ask the database for its timestamp of 'path' (db_mtime)
186 * o Ask the filesystem for files and directories within 'path'
187 * (via scandir and stored in fs_entries)
189 * o Pass 1: For each directory in fs_entries, recursively call into
190 * this same function.
192 * o Compare fs_mtime to db_mtime. If they are equivalent, terminate
193 * the algorithm at this point, (this directory has not been
194 * updated in the filesystem since the last database scan of PASS
197 * o Ask the database for files and directories within 'path'
198 * (db_files and db_subdirs)
200 * o Pass 2: Walk fs_entries simultaneously with db_files and
201 * db_subdirs. Look for one of three interesting cases:
203 * 1. Regular file in fs_entries and not in db_files
204 * This is a new file to add_message into the database.
206 * 2. Filename in db_files not in fs_entries.
207 * This is a file that has been removed from the mail store.
209 * 3. Directory in db_subdirs not in fs_entries
210 * This is a directory that has been removed from the mail store.
212 * Note that the addition of a directory is not interesting here,
213 * since that will have been taken care of in pass 1. Also, we
214 * don't immediately act on file/directory removal since we must
215 * ensure that in the case of a rename that the new filename is
216 * added before the old filename is removed, (so that no
217 * information is lost from the database).
219 * o Tell the database to update its time of 'path' to 'fs_mtime'
220 * if fs_mtime isn't the current wall-clock time.
222 static notmuch_status_t
223 add_files_recursive (notmuch_database_t *notmuch,
225 add_files_state_t *state)
228 struct dirent *entry = NULL;
230 time_t fs_mtime, db_mtime;
231 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
232 notmuch_message_t *message = NULL;
233 struct dirent **fs_entries = NULL;
234 int i, num_fs_entries;
235 notmuch_directory_t *directory;
236 notmuch_filenames_t *db_files = NULL;
237 notmuch_filenames_t *db_subdirs = NULL;
240 notmuch_bool_t is_maildir, new_directory;
243 if (stat (path, &st)) {
244 fprintf (stderr, "Error reading directory %s: %s\n",
245 path, strerror (errno));
246 return NOTMUCH_STATUS_FILE_ERROR;
248 stat_time = time (NULL);
250 /* This is not an error since we may have recursed based on a
251 * symlink to a regular file, not a directory, and we don't know
252 * that until this stat. */
253 if (! S_ISDIR (st.st_mode))
254 return NOTMUCH_STATUS_SUCCESS;
256 fs_mtime = st.st_mtime;
258 directory = notmuch_database_get_directory (notmuch, path);
259 db_mtime = notmuch_directory_get_mtime (directory);
261 new_directory = db_mtime ? FALSE : TRUE;
263 /* XXX This is a temporary workaround. If we don't update the
264 * database mtime until after processing messages in this
265 * directory, then a 0 mtime is *not* sufficient to indicate that
266 * this directory has no messages or subdirs in the database (for
267 * example, if an earlier run skipped the mtime update because
268 * fs_mtime == stat_time, or was interrupted before updating the
269 * mtime at the end). To address this, we record a (bogus)
270 * non-zero value before processing any child messages so that a
271 * later run won't mistake this for a new directory (and, for
272 * example, fail to detect removed files and subdirs).
274 * A better solution would be for notmuch_database_get_directory
275 * to indicate if it really created a new directory or not, either
276 * by a new out-argument, or by recording this information and
277 * providing an accessor.
280 notmuch_directory_set_mtime (directory, -1);
282 /* If the database knows about this directory, then we sort based
283 * on strcmp to match the database sorting. Otherwise, we can do
284 * inode-based sorting for faster filesystem operation. */
285 num_fs_entries = scandir (path, &fs_entries, 0,
287 dirent_sort_inode : dirent_sort_strcmp_name);
289 if (num_fs_entries == -1) {
290 fprintf (stderr, "Error opening directory %s: %s\n",
291 path, strerror (errno));
292 ret = NOTMUCH_STATUS_FILE_ERROR;
296 /* Pass 1: Recurse into all sub-directories. */
297 is_maildir = _entries_resemble_maildir (fs_entries, num_fs_entries);
299 for (i = 0; i < num_fs_entries; i++) {
303 entry = fs_entries[i];
305 /* We only want to descend into directories.
306 * But symlinks can be to directories too, of course.
308 * And if the filesystem doesn't tell us the file type in the
309 * scandir results, then it might be a directory (and if not,
310 * then we'll stat and return immediately in the next level of
312 if (entry->d_type != DT_DIR &&
313 entry->d_type != DT_LNK &&
314 entry->d_type != DT_UNKNOWN)
319 /* Ignore special directories to avoid infinite recursion.
320 * Also ignore the .notmuch directory and any "tmp" directory
321 * that appears within a maildir.
323 /* XXX: Eventually we'll want more sophistication to let the
324 * user specify files to be ignored. */
325 if (strcmp (entry->d_name, ".") == 0 ||
326 strcmp (entry->d_name, "..") == 0 ||
327 (is_maildir && strcmp (entry->d_name, "tmp") == 0) ||
328 strcmp (entry->d_name, ".notmuch") ==0)
333 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
334 status = add_files_recursive (notmuch, next, state);
335 if (status && ret == NOTMUCH_STATUS_SUCCESS)
341 /* If the directory's modification time in the filesystem is the
342 * same as what we recorded in the database the last time we
343 * scanned it, then we can skip the second pass entirely.
345 * We test for strict equality here to avoid a bug that can happen
346 * if the system clock jumps backward, (preventing new mail from
347 * being discovered until the clock catches up and the directory
348 * is modified again).
350 if (fs_mtime == db_mtime)
353 /* new_directory means a directory that the database has never
354 * seen before. In that case, we can simply leave db_files and
355 * db_subdirs NULL. */
356 if (!new_directory) {
357 db_files = notmuch_directory_get_child_files (directory);
358 db_subdirs = notmuch_directory_get_child_directories (directory);
361 /* Pass 2: Scan for new files, removed files, and removed directories. */
362 for (i = 0; i < num_fs_entries; i++)
367 entry = fs_entries[i];
369 /* Check if we've walked past any names in db_files or
370 * db_subdirs. If so, these have been deleted. */
371 while (notmuch_filenames_valid (db_files) &&
372 strcmp (notmuch_filenames_get (db_files), entry->d_name) < 0)
374 char *absolute = talloc_asprintf (state->removed_files,
376 notmuch_filenames_get (db_files));
378 _filename_list_add (state->removed_files, absolute);
380 notmuch_filenames_move_to_next (db_files);
383 while (notmuch_filenames_valid (db_subdirs) &&
384 strcmp (notmuch_filenames_get (db_subdirs), entry->d_name) <= 0)
386 const char *filename = notmuch_filenames_get (db_subdirs);
388 if (strcmp (filename, entry->d_name) < 0)
390 char *absolute = talloc_asprintf (state->removed_directories,
391 "%s/%s", path, filename);
393 _filename_list_add (state->removed_directories, absolute);
396 notmuch_filenames_move_to_next (db_subdirs);
399 /* If we're looking at a symlink, we only want to add it if it
400 * links to a regular file, (and not to a directory, say).
402 * Similarly, if the file is of unknown type (due to filesystem
403 * limitations), then we also need to look closer.
405 * In either case, a stat does the trick.
407 if (entry->d_type == DT_LNK || entry->d_type == DT_UNKNOWN) {
410 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
411 err = stat (next, &st);
415 /* Don't emit an error for a link pointing nowhere, since
416 * the directory-traversal pass will have already done
421 if (! S_ISREG (st.st_mode))
423 } else if (entry->d_type != DT_REG) {
427 /* Don't add a file that we've added before. */
428 if (notmuch_filenames_valid (db_files) &&
429 strcmp (notmuch_filenames_get (db_files), entry->d_name) == 0)
431 notmuch_filenames_move_to_next (db_files);
435 /* We're now looking at a regular file that doesn't yet exist
436 * in the database, so add it. */
437 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
439 state->processed_files++;
441 if (state->verbose) {
442 if (state->output_is_a_tty)
446 state->processed_files,
450 putchar((state->output_is_a_tty) ? '\r' : '\n');
454 status = notmuch_database_add_message (notmuch, next, &message);
457 case NOTMUCH_STATUS_SUCCESS:
458 state->added_messages++;
459 notmuch_message_freeze (message);
460 for (tag=state->new_tags; *tag != NULL; tag++)
461 notmuch_message_add_tag (message, *tag);
462 if (state->synchronize_flags == TRUE)
463 notmuch_message_maildir_flags_to_tags (message);
464 notmuch_message_thaw (message);
466 /* Non-fatal issues (go on to next file) */
467 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
468 /* Defer sync of maildir flags until after old filenames
469 * are removed in the case of a rename. */
470 if (state->synchronize_flags == TRUE)
471 _filename_list_add (state->message_ids_to_sync,
472 notmuch_message_get_message_id (message));
474 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
475 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
478 /* Fatal issues. Don't process anymore. */
479 case NOTMUCH_STATUS_READ_ONLY_DATABASE:
480 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
481 case NOTMUCH_STATUS_OUT_OF_MEMORY:
482 fprintf (stderr, "Error: %s. Halting processing.\n",
483 notmuch_status_to_string (status));
487 case NOTMUCH_STATUS_FILE_ERROR:
488 case NOTMUCH_STATUS_NULL_POINTER:
489 case NOTMUCH_STATUS_TAG_TOO_LONG:
490 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
491 case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
492 case NOTMUCH_STATUS_LAST_STATUS:
493 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
498 notmuch_message_destroy (message);
502 if (do_print_progress) {
503 do_print_progress = 0;
504 generic_print_progress ("Processed", "files", state->tv_start,
505 state->processed_files, state->total_files);
515 /* Now that we've walked the whole filesystem list, anything left
516 * over in the database lists has been deleted. */
517 while (notmuch_filenames_valid (db_files))
519 char *absolute = talloc_asprintf (state->removed_files,
521 notmuch_filenames_get (db_files));
523 _filename_list_add (state->removed_files, absolute);
525 notmuch_filenames_move_to_next (db_files);
528 while (notmuch_filenames_valid (db_subdirs))
530 char *absolute = talloc_asprintf (state->removed_directories,
532 notmuch_filenames_get (db_subdirs));
534 _filename_list_add (state->removed_directories, absolute);
536 notmuch_filenames_move_to_next (db_subdirs);
539 /* If the directory's mtime is the same as the wall-clock time
540 * when we stat'ed the directory, we skip updating the mtime in
541 * the database because a message could be delivered later in this
542 * same second. This may lead to unnecessary re-scans, but it
543 * avoids overlooking messages. */
544 if (fs_mtime != stat_time)
545 _filename_list_add (state->directory_mtimes, path)->mtime = fs_mtime;
557 notmuch_filenames_destroy (db_subdirs);
559 notmuch_filenames_destroy (db_files);
561 notmuch_directory_destroy (directory);
567 setup_progress_printing_timer (void)
569 struct sigaction action;
570 struct itimerval timerval;
572 /* Setup our handler for SIGALRM */
573 memset (&action, 0, sizeof (struct sigaction));
574 action.sa_handler = handle_sigalrm;
575 sigemptyset (&action.sa_mask);
576 action.sa_flags = SA_RESTART;
577 sigaction (SIGALRM, &action, NULL);
579 /* Then start a timer to send SIGALRM once per second. */
580 timerval.it_interval.tv_sec = 1;
581 timerval.it_interval.tv_usec = 0;
582 timerval.it_value.tv_sec = 1;
583 timerval.it_value.tv_usec = 0;
584 setitimer (ITIMER_REAL, &timerval, NULL);
588 stop_progress_printing_timer (void)
590 struct sigaction action;
591 struct itimerval timerval;
593 /* Now stop the timer. */
594 timerval.it_interval.tv_sec = 0;
595 timerval.it_interval.tv_usec = 0;
596 timerval.it_value.tv_sec = 0;
597 timerval.it_value.tv_usec = 0;
598 setitimer (ITIMER_REAL, &timerval, NULL);
600 /* And disable the signal handler. */
601 action.sa_handler = SIG_IGN;
602 sigaction (SIGALRM, &action, NULL);
606 /* This is the top-level entry point for add_files. It does a couple
607 * of error checks and then calls into the recursive function. */
608 static notmuch_status_t
609 add_files (notmuch_database_t *notmuch,
611 add_files_state_t *state)
613 notmuch_status_t status;
616 if (stat (path, &st)) {
617 fprintf (stderr, "Error reading directory %s: %s\n",
618 path, strerror (errno));
619 return NOTMUCH_STATUS_FILE_ERROR;
622 if (! S_ISDIR (st.st_mode)) {
623 fprintf (stderr, "Error: %s is not a directory.\n", path);
624 return NOTMUCH_STATUS_FILE_ERROR;
627 status = add_files_recursive (notmuch, path, state);
632 /* XXX: This should be merged with the add_files function since it
633 * shares a lot of logic with it. */
634 /* Recursively count all regular files in path and all sub-directories
635 * of path. The result is added to *count (which should be
636 * initialized to zero by the top-level caller before calling
639 count_files (const char *path, int *count)
641 struct dirent *entry = NULL;
644 struct dirent **fs_entries = NULL;
645 int num_fs_entries = scandir (path, &fs_entries, 0, dirent_sort_inode);
648 if (num_fs_entries == -1) {
649 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
650 path, strerror (errno));
654 while (!interrupted) {
655 if (i == num_fs_entries)
658 entry = fs_entries[i++];
660 /* Ignore special directories to avoid infinite recursion.
661 * Also ignore the .notmuch directory.
663 /* XXX: Eventually we'll want more sophistication to let the
664 * user specify files to be ignored. */
665 if (strcmp (entry->d_name, ".") == 0 ||
666 strcmp (entry->d_name, "..") == 0 ||
667 strcmp (entry->d_name, ".notmuch") == 0)
672 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
674 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
675 path, entry->d_name);
681 if (S_ISREG (st.st_mode)) {
683 if (*count % 1000 == 0) {
684 printf ("Found %d files so far.\r", *count);
687 } else if (S_ISDIR (st.st_mode)) {
688 count_files (next, count);
702 upgrade_print_progress (void *closure,
705 add_files_state_t *state = closure;
707 printf ("Upgrading database: %.2f%% complete", progress * 100.0);
710 struct timeval tv_now;
711 double elapsed, time_remaining;
713 gettimeofday (&tv_now, NULL);
715 elapsed = notmuch_time_elapsed (state->tv_start, tv_now);
716 time_remaining = (elapsed / progress) * (1.0 - progress);
718 notmuch_time_print_formatted_seconds (time_remaining);
719 printf (" remaining)");
727 /* Recursively remove all filenames from the database referring to
728 * 'path' (or to any of its children). */
730 _remove_directory (void *ctx,
731 notmuch_database_t *notmuch,
733 add_files_state_t *add_files_state)
735 notmuch_directory_t *directory;
736 notmuch_filenames_t *files, *subdirs;
737 notmuch_status_t status;
740 directory = notmuch_database_get_directory (notmuch, path);
742 for (files = notmuch_directory_get_child_files (directory);
743 notmuch_filenames_valid (files);
744 notmuch_filenames_move_to_next (files))
746 absolute = talloc_asprintf (ctx, "%s/%s", path,
747 notmuch_filenames_get (files));
748 status = notmuch_database_remove_message (notmuch, absolute);
749 if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
750 add_files_state->renamed_messages++;
752 add_files_state->removed_messages++;
753 talloc_free (absolute);
756 for (subdirs = notmuch_directory_get_child_directories (directory);
757 notmuch_filenames_valid (subdirs);
758 notmuch_filenames_move_to_next (subdirs))
760 absolute = talloc_asprintf (ctx, "%s/%s", path,
761 notmuch_filenames_get (subdirs));
762 _remove_directory (ctx, notmuch, absolute, add_files_state);
763 talloc_free (absolute);
766 notmuch_directory_destroy (directory);
770 notmuch_new_command (void *ctx, int argc, char *argv[])
772 notmuch_config_t *config;
773 notmuch_database_t *notmuch;
774 add_files_state_t add_files_state;
776 struct timeval tv_now, tv_start;
780 char *dot_notmuch_path;
781 struct sigaction action;
783 notmuch_status_t status;
785 notmuch_bool_t timer_is_active = FALSE;
787 add_files_state.verbose = 0;
788 add_files_state.output_is_a_tty = isatty (fileno (stdout));
790 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
791 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
792 add_files_state.verbose = 1;
794 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
798 config = notmuch_config_open (ctx, NULL, NULL);
802 add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);
803 add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
804 add_files_state.message_ids_to_sync = _filename_list_create (ctx);
805 db_path = notmuch_config_get_database_path (config);
807 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
809 if (stat (dot_notmuch_path, &st)) {
813 count_files (db_path, &count);
817 printf ("Found %d total files (that's not much mail).\n", count);
818 notmuch = notmuch_database_create (db_path);
819 add_files_state.total_files = count;
821 notmuch = notmuch_database_open (db_path,
822 NOTMUCH_DATABASE_MODE_READ_WRITE);
826 if (notmuch_database_needs_upgrade (notmuch)) {
827 printf ("Welcome to a new version of notmuch! Your database will now be upgraded.\n");
828 gettimeofday (&add_files_state.tv_start, NULL);
829 notmuch_database_upgrade (notmuch, upgrade_print_progress,
831 printf ("Your notmuch database has now been upgraded to database format version %u.\n",
832 notmuch_database_get_version (notmuch));
835 add_files_state.total_files = 0;
841 /* Setup our handler for SIGINT. We do this after having
842 * potentially done a database upgrade we this interrupt handler
844 memset (&action, 0, sizeof (struct sigaction));
845 action.sa_handler = handle_sigint;
846 sigemptyset (&action.sa_mask);
847 action.sa_flags = SA_RESTART;
848 sigaction (SIGINT, &action, NULL);
850 talloc_free (dot_notmuch_path);
851 dot_notmuch_path = NULL;
853 add_files_state.processed_files = 0;
854 add_files_state.added_messages = 0;
855 add_files_state.removed_messages = add_files_state.renamed_messages = 0;
856 gettimeofday (&add_files_state.tv_start, NULL);
858 add_files_state.removed_files = _filename_list_create (ctx);
859 add_files_state.removed_directories = _filename_list_create (ctx);
860 add_files_state.directory_mtimes = _filename_list_create (ctx);
862 if (! debugger_is_active () && add_files_state.output_is_a_tty
863 && ! add_files_state.verbose) {
864 setup_progress_printing_timer ();
865 timer_is_active = TRUE;
868 ret = add_files (notmuch, db_path, &add_files_state);
870 gettimeofday (&tv_start, NULL);
871 for (f = add_files_state.removed_files->head; f && !interrupted; f = f->next) {
872 status = notmuch_database_remove_message (notmuch, f->filename);
873 if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
874 add_files_state.renamed_messages++;
876 add_files_state.removed_messages++;
877 if (do_print_progress) {
878 do_print_progress = 0;
879 generic_print_progress ("Cleaned up", "messages",
880 tv_start, add_files_state.removed_messages + add_files_state.renamed_messages,
881 add_files_state.removed_files->count);
885 gettimeofday (&tv_start, NULL);
886 for (f = add_files_state.removed_directories->head, i = 0; f && !interrupted; f = f->next, i++) {
887 _remove_directory (ctx, notmuch, f->filename, &add_files_state);
888 if (do_print_progress) {
889 do_print_progress = 0;
890 generic_print_progress ("Cleaned up", "directories",
892 add_files_state.removed_directories->count);
896 for (f = add_files_state.directory_mtimes->head; f && !interrupted; f = f->next) {
897 notmuch_directory_t *directory;
898 directory = notmuch_database_get_directory (notmuch, f->filename);
900 notmuch_directory_set_mtime (directory, f->mtime);
901 notmuch_directory_destroy (directory);
905 talloc_free (add_files_state.removed_files);
906 talloc_free (add_files_state.removed_directories);
907 talloc_free (add_files_state.directory_mtimes);
909 /* Now that removals are done (hence the database is aware of all
910 * renames), we can synchronize maildir_flags to tags for all
911 * messages that had new filenames appear on this run. */
912 gettimeofday (&tv_start, NULL);
913 if (add_files_state.synchronize_flags) {
914 _filename_node_t *node;
915 notmuch_message_t *message;
916 for (node = add_files_state.message_ids_to_sync->head, i = 0;
918 node = node->next, i++)
920 message = notmuch_database_find_message (notmuch, node->filename);
921 notmuch_message_maildir_flags_to_tags (message);
922 notmuch_message_destroy (message);
923 if (do_print_progress) {
924 do_print_progress = 0;
925 generic_print_progress (
926 "Synchronized tags for", "messages",
927 tv_start, i, add_files_state.message_ids_to_sync->count);
932 talloc_free (add_files_state.message_ids_to_sync);
933 add_files_state.message_ids_to_sync = NULL;
936 stop_progress_printing_timer ();
938 gettimeofday (&tv_now, NULL);
939 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
942 if (add_files_state.processed_files) {
943 printf ("Processed %d %s in ", add_files_state.processed_files,
944 add_files_state.processed_files == 1 ?
945 "file" : "total files");
946 notmuch_time_print_formatted_seconds (elapsed);
948 printf (" (%d files/sec.).\033[K\n",
949 (int) (add_files_state.processed_files / elapsed));
951 printf (".\033[K\n");
955 if (add_files_state.added_messages) {
956 printf ("Added %d new %s to the database.",
957 add_files_state.added_messages,
958 add_files_state.added_messages == 1 ?
959 "message" : "messages");
961 printf ("No new mail.");
964 if (add_files_state.removed_messages) {
965 printf (" Removed %d %s.",
966 add_files_state.removed_messages,
967 add_files_state.removed_messages == 1 ? "message" : "messages");
970 if (add_files_state.renamed_messages) {
971 printf (" Detected %d file %s.",
972 add_files_state.renamed_messages,
973 add_files_state.renamed_messages == 1 ? "rename" : "renames");
979 printf ("\nNote: At least one error was encountered: %s\n",
980 notmuch_status_to_string (ret));
983 notmuch_database_close (notmuch);
985 return ret || interrupted;