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;
55 static volatile sig_atomic_t do_print_progress = 0;
58 handle_sigalrm (unused (int signal))
60 do_print_progress = 1;
63 static volatile sig_atomic_t interrupted;
66 handle_sigint (unused (int sig))
68 static char msg[] = "Stopping... \n";
70 /* This write is "opportunistic", so it's okay to ignore the
71 * result. It is not required for correctness, and if it does
72 * fail or produce a short write, we want to get out of the signal
73 * handler as quickly as possible, not retry it. */
74 IGNORE_RESULT (write (2, msg, sizeof(msg)-1));
78 static _filename_list_t *
79 _filename_list_create (const void *ctx)
81 _filename_list_t *list;
83 list = talloc (ctx, _filename_list_t);
88 list->tail = &list->head;
94 static _filename_node_t *
95 _filename_list_add (_filename_list_t *list,
98 _filename_node_t *node = talloc (list, _filename_node_t);
102 node->filename = talloc_strdup (list, filename);
105 *(list->tail) = node;
106 list->tail = &node->next;
112 generic_print_progress (const char *action, const char *object,
113 struct timeval tv_start, unsigned processed, unsigned total)
115 struct timeval tv_now;
116 double elapsed_overall, rate_overall;
118 gettimeofday (&tv_now, NULL);
120 elapsed_overall = notmuch_time_elapsed (tv_start, tv_now);
121 rate_overall = processed / elapsed_overall;
123 printf ("%s %d ", action, processed);
126 printf ("of %d %s", total, object);
127 if (processed > 0 && elapsed_overall > 0.5) {
128 double time_remaining = ((total - processed) / rate_overall);
130 notmuch_time_print_formatted_seconds (time_remaining);
131 printf (" remaining)");
134 printf ("%s", object);
135 if (elapsed_overall > 0.5)
136 printf (" (%d %s/sec.)", (int) rate_overall, object);
138 printf (".\033[K\r");
144 dirent_sort_inode (const struct dirent **a, const struct dirent **b)
146 return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
150 dirent_sort_strcmp_name (const struct dirent **a, const struct dirent **b)
152 return strcmp ((*a)->d_name, (*b)->d_name);
155 /* Test if the directory looks like a Maildir directory.
157 * Search through the array of directory entries to see if we can find all
158 * three subdirectories typical for Maildir, that is "new", "cur", and "tmp".
160 * Return 1 if the directory looks like a Maildir and 0 otherwise.
163 _entries_resemble_maildir (struct dirent **entries, int count)
167 for (i = 0; i < count; i++) {
168 if (entries[i]->d_type != DT_DIR && entries[i]->d_type != DT_UNKNOWN)
171 if (strcmp(entries[i]->d_name, "new") == 0 ||
172 strcmp(entries[i]->d_name, "cur") == 0 ||
173 strcmp(entries[i]->d_name, "tmp") == 0)
184 /* Examine 'path' recursively as follows:
186 * o Ask the filesystem for the mtime of 'path' (fs_mtime)
187 * o Ask the database for its timestamp of 'path' (db_mtime)
189 * o Ask the filesystem for files and directories within 'path'
190 * (via scandir and stored in fs_entries)
192 * o Pass 1: For each directory in fs_entries, recursively call into
193 * this same function.
195 * o Compare fs_mtime to db_mtime. If they are equivalent, terminate
196 * the algorithm at this point, (this directory has not been
197 * updated in the filesystem since the last database scan of PASS
200 * o Ask the database for files and directories within 'path'
201 * (db_files and db_subdirs)
203 * o Pass 2: Walk fs_entries simultaneously with db_files and
204 * db_subdirs. Look for one of three interesting cases:
206 * 1. Regular file in fs_entries and not in db_files
207 * This is a new file to add_message into the database.
209 * 2. Filename in db_files not in fs_entries.
210 * This is a file that has been removed from the mail store.
212 * 3. Directory in db_subdirs not in fs_entries
213 * This is a directory that has been removed from the mail store.
215 * Note that the addition of a directory is not interesting here,
216 * since that will have been taken care of in pass 1. Also, we
217 * don't immediately act on file/directory removal since we must
218 * ensure that in the case of a rename that the new filename is
219 * added before the old filename is removed, (so that no
220 * information is lost from the database).
222 * o Tell the database to update its time of 'path' to 'fs_mtime'
223 * if fs_mtime isn't the current wall-clock time.
225 static notmuch_status_t
226 add_files_recursive (notmuch_database_t *notmuch,
228 add_files_state_t *state)
231 struct dirent *entry = NULL;
233 time_t fs_mtime, db_mtime;
234 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
235 notmuch_message_t *message = NULL;
236 struct dirent **fs_entries = NULL;
237 int i, num_fs_entries;
238 notmuch_directory_t *directory;
239 notmuch_filenames_t *db_files = NULL;
240 notmuch_filenames_t *db_subdirs = NULL;
243 notmuch_bool_t is_maildir, new_directory;
246 if (stat (path, &st)) {
247 fprintf (stderr, "Error reading directory %s: %s\n",
248 path, strerror (errno));
249 return NOTMUCH_STATUS_FILE_ERROR;
251 stat_time = time (NULL);
253 /* This is not an error since we may have recursed based on a
254 * symlink to a regular file, not a directory, and we don't know
255 * that until this stat. */
256 if (! S_ISDIR (st.st_mode))
257 return NOTMUCH_STATUS_SUCCESS;
259 fs_mtime = st.st_mtime;
261 directory = notmuch_database_get_directory (notmuch, path);
262 db_mtime = notmuch_directory_get_mtime (directory);
264 new_directory = db_mtime ? FALSE : TRUE;
266 /* XXX This is a temporary workaround. If we don't update the
267 * database mtime until after processing messages in this
268 * directory, then a 0 mtime is *not* sufficient to indicate that
269 * this directory has no messages or subdirs in the database (for
270 * example, if an earlier run skipped the mtime update because
271 * fs_mtime == stat_time, or was interrupted before updating the
272 * mtime at the end). To address this, we record a (bogus)
273 * non-zero value before processing any child messages so that a
274 * later run won't mistake this for a new directory (and, for
275 * example, fail to detect removed files and subdirs).
277 * A better solution would be for notmuch_database_get_directory
278 * to indicate if it really created a new directory or not, either
279 * by a new out-argument, or by recording this information and
280 * providing an accessor.
283 notmuch_directory_set_mtime (directory, -1);
285 /* If the database knows about this directory, then we sort based
286 * on strcmp to match the database sorting. Otherwise, we can do
287 * inode-based sorting for faster filesystem operation. */
288 num_fs_entries = scandir (path, &fs_entries, 0,
290 dirent_sort_inode : dirent_sort_strcmp_name);
292 if (num_fs_entries == -1) {
293 fprintf (stderr, "Error opening directory %s: %s\n",
294 path, strerror (errno));
295 ret = NOTMUCH_STATUS_FILE_ERROR;
299 /* Pass 1: Recurse into all sub-directories. */
300 is_maildir = _entries_resemble_maildir (fs_entries, num_fs_entries);
302 for (i = 0; i < num_fs_entries; i++) {
306 entry = fs_entries[i];
308 /* We only want to descend into directories.
309 * But symlinks can be to directories too, of course.
311 * And if the filesystem doesn't tell us the file type in the
312 * scandir results, then it might be a directory (and if not,
313 * then we'll stat and return immediately in the next level of
315 if (entry->d_type != DT_DIR &&
316 entry->d_type != DT_LNK &&
317 entry->d_type != DT_UNKNOWN)
322 /* Ignore special directories to avoid infinite recursion.
323 * Also ignore the .notmuch directory and any "tmp" directory
324 * that appears within a maildir.
326 /* XXX: Eventually we'll want more sophistication to let the
327 * user specify files to be ignored. */
328 if (strcmp (entry->d_name, ".") == 0 ||
329 strcmp (entry->d_name, "..") == 0 ||
330 (is_maildir && strcmp (entry->d_name, "tmp") == 0) ||
331 strcmp (entry->d_name, ".notmuch") ==0)
336 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
337 status = add_files_recursive (notmuch, next, state);
338 if (status && ret == NOTMUCH_STATUS_SUCCESS)
344 /* If the directory's modification time in the filesystem is the
345 * same as what we recorded in the database the last time we
346 * scanned it, then we can skip the second pass entirely.
348 * We test for strict equality here to avoid a bug that can happen
349 * if the system clock jumps backward, (preventing new mail from
350 * being discovered until the clock catches up and the directory
351 * is modified again).
353 if (fs_mtime == db_mtime)
356 /* new_directory means a directory that the database has never
357 * seen before. In that case, we can simply leave db_files and
358 * db_subdirs NULL. */
359 if (!new_directory) {
360 db_files = notmuch_directory_get_child_files (directory);
361 db_subdirs = notmuch_directory_get_child_directories (directory);
364 /* Pass 2: Scan for new files, removed files, and removed directories. */
365 for (i = 0; i < num_fs_entries; i++)
370 entry = fs_entries[i];
372 /* Check if we've walked past any names in db_files or
373 * db_subdirs. If so, these have been deleted. */
374 while (notmuch_filenames_valid (db_files) &&
375 strcmp (notmuch_filenames_get (db_files), entry->d_name) < 0)
377 char *absolute = talloc_asprintf (state->removed_files,
379 notmuch_filenames_get (db_files));
381 _filename_list_add (state->removed_files, absolute);
383 notmuch_filenames_move_to_next (db_files);
386 while (notmuch_filenames_valid (db_subdirs) &&
387 strcmp (notmuch_filenames_get (db_subdirs), entry->d_name) <= 0)
389 const char *filename = notmuch_filenames_get (db_subdirs);
391 if (strcmp (filename, entry->d_name) < 0)
393 char *absolute = talloc_asprintf (state->removed_directories,
394 "%s/%s", path, filename);
396 _filename_list_add (state->removed_directories, absolute);
399 notmuch_filenames_move_to_next (db_subdirs);
402 /* If we're looking at a symlink, we only want to add it if it
403 * links to a regular file, (and not to a directory, say).
405 * Similarly, if the file is of unknown type (due to filesystem
406 * limitations), then we also need to look closer.
408 * In either case, a stat does the trick.
410 if (entry->d_type == DT_LNK || entry->d_type == DT_UNKNOWN) {
413 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
414 err = stat (next, &st);
418 /* Don't emit an error for a link pointing nowhere, since
419 * the directory-traversal pass will have already done
424 if (! S_ISREG (st.st_mode))
426 } else if (entry->d_type != DT_REG) {
430 /* Don't add a file that we've added before. */
431 if (notmuch_filenames_valid (db_files) &&
432 strcmp (notmuch_filenames_get (db_files), entry->d_name) == 0)
434 notmuch_filenames_move_to_next (db_files);
438 /* We're now looking at a regular file that doesn't yet exist
439 * in the database, so add it. */
440 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
442 state->processed_files++;
444 if (state->verbose) {
445 if (state->output_is_a_tty)
449 state->processed_files,
453 putchar((state->output_is_a_tty) ? '\r' : '\n');
457 status = notmuch_database_begin_atomic (notmuch);
463 status = notmuch_database_add_message (notmuch, next, &message);
466 case NOTMUCH_STATUS_SUCCESS:
467 state->added_messages++;
468 notmuch_message_freeze (message);
469 for (tag=state->new_tags; *tag != NULL; tag++)
470 notmuch_message_add_tag (message, *tag);
471 if (state->synchronize_flags == TRUE)
472 notmuch_message_maildir_flags_to_tags (message);
473 notmuch_message_thaw (message);
475 /* Non-fatal issues (go on to next file) */
476 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
477 if (state->synchronize_flags == TRUE)
478 notmuch_message_maildir_flags_to_tags (message);
480 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
481 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
484 /* Fatal issues. Don't process anymore. */
485 case NOTMUCH_STATUS_READ_ONLY_DATABASE:
486 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
487 case NOTMUCH_STATUS_OUT_OF_MEMORY:
488 fprintf (stderr, "Error: %s. Halting processing.\n",
489 notmuch_status_to_string (status));
493 case NOTMUCH_STATUS_FILE_ERROR:
494 case NOTMUCH_STATUS_NULL_POINTER:
495 case NOTMUCH_STATUS_TAG_TOO_LONG:
496 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
497 case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
498 case NOTMUCH_STATUS_LAST_STATUS:
499 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
503 status = notmuch_database_end_atomic (notmuch);
510 notmuch_message_destroy (message);
514 if (do_print_progress) {
515 do_print_progress = 0;
516 generic_print_progress ("Processed", "files", state->tv_start,
517 state->processed_files, state->total_files);
527 /* Now that we've walked the whole filesystem list, anything left
528 * over in the database lists has been deleted. */
529 while (notmuch_filenames_valid (db_files))
531 char *absolute = talloc_asprintf (state->removed_files,
533 notmuch_filenames_get (db_files));
535 _filename_list_add (state->removed_files, absolute);
537 notmuch_filenames_move_to_next (db_files);
540 while (notmuch_filenames_valid (db_subdirs))
542 char *absolute = talloc_asprintf (state->removed_directories,
544 notmuch_filenames_get (db_subdirs));
546 _filename_list_add (state->removed_directories, absolute);
548 notmuch_filenames_move_to_next (db_subdirs);
551 /* If the directory's mtime is the same as the wall-clock time
552 * when we stat'ed the directory, we skip updating the mtime in
553 * the database because a message could be delivered later in this
554 * same second. This may lead to unnecessary re-scans, but it
555 * avoids overlooking messages. */
556 if (fs_mtime != stat_time)
557 _filename_list_add (state->directory_mtimes, path)->mtime = fs_mtime;
565 for (i = 0; i < num_fs_entries; i++)
566 free (fs_entries[i]);
571 notmuch_filenames_destroy (db_subdirs);
573 notmuch_filenames_destroy (db_files);
575 notmuch_directory_destroy (directory);
581 setup_progress_printing_timer (void)
583 struct sigaction action;
584 struct itimerval timerval;
586 /* Setup our handler for SIGALRM */
587 memset (&action, 0, sizeof (struct sigaction));
588 action.sa_handler = handle_sigalrm;
589 sigemptyset (&action.sa_mask);
590 action.sa_flags = SA_RESTART;
591 sigaction (SIGALRM, &action, NULL);
593 /* Then start a timer to send SIGALRM once per second. */
594 timerval.it_interval.tv_sec = 1;
595 timerval.it_interval.tv_usec = 0;
596 timerval.it_value.tv_sec = 1;
597 timerval.it_value.tv_usec = 0;
598 setitimer (ITIMER_REAL, &timerval, NULL);
602 stop_progress_printing_timer (void)
604 struct sigaction action;
605 struct itimerval timerval;
607 /* Now stop the timer. */
608 timerval.it_interval.tv_sec = 0;
609 timerval.it_interval.tv_usec = 0;
610 timerval.it_value.tv_sec = 0;
611 timerval.it_value.tv_usec = 0;
612 setitimer (ITIMER_REAL, &timerval, NULL);
614 /* And disable the signal handler. */
615 action.sa_handler = SIG_IGN;
616 sigaction (SIGALRM, &action, NULL);
620 /* This is the top-level entry point for add_files. It does a couple
621 * of error checks and then calls into the recursive function. */
622 static notmuch_status_t
623 add_files (notmuch_database_t *notmuch,
625 add_files_state_t *state)
627 notmuch_status_t status;
630 if (stat (path, &st)) {
631 fprintf (stderr, "Error reading directory %s: %s\n",
632 path, strerror (errno));
633 return NOTMUCH_STATUS_FILE_ERROR;
636 if (! S_ISDIR (st.st_mode)) {
637 fprintf (stderr, "Error: %s is not a directory.\n", path);
638 return NOTMUCH_STATUS_FILE_ERROR;
641 status = add_files_recursive (notmuch, path, state);
646 /* XXX: This should be merged with the add_files function since it
647 * shares a lot of logic with it. */
648 /* Recursively count all regular files in path and all sub-directories
649 * of path. The result is added to *count (which should be
650 * initialized to zero by the top-level caller before calling
653 count_files (const char *path, int *count)
655 struct dirent *entry = NULL;
658 struct dirent **fs_entries = NULL;
659 int num_fs_entries = scandir (path, &fs_entries, 0, dirent_sort_inode);
662 if (num_fs_entries == -1) {
663 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
664 path, strerror (errno));
668 while (!interrupted) {
669 if (i == num_fs_entries)
672 entry = fs_entries[i++];
674 /* Ignore special directories to avoid infinite recursion.
675 * Also ignore the .notmuch directory.
677 /* XXX: Eventually we'll want more sophistication to let the
678 * user specify files to be ignored. */
679 if (strcmp (entry->d_name, ".") == 0 ||
680 strcmp (entry->d_name, "..") == 0 ||
681 strcmp (entry->d_name, ".notmuch") == 0)
686 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
688 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
689 path, entry->d_name);
695 if (S_ISREG (st.st_mode)) {
697 if (*count % 1000 == 0) {
698 printf ("Found %d files so far.\r", *count);
701 } else if (S_ISDIR (st.st_mode)) {
702 count_files (next, count);
710 for (i = 0; i < num_fs_entries; i++)
711 free (fs_entries[i]);
718 upgrade_print_progress (void *closure,
721 add_files_state_t *state = closure;
723 printf ("Upgrading database: %.2f%% complete", progress * 100.0);
726 struct timeval tv_now;
727 double elapsed, time_remaining;
729 gettimeofday (&tv_now, NULL);
731 elapsed = notmuch_time_elapsed (state->tv_start, tv_now);
732 time_remaining = (elapsed / progress) * (1.0 - progress);
734 notmuch_time_print_formatted_seconds (time_remaining);
735 printf (" remaining)");
743 /* Remove one message filename from the database. */
744 static notmuch_status_t
745 remove_filename (notmuch_database_t *notmuch,
747 add_files_state_t *add_files_state)
749 notmuch_status_t status;
750 notmuch_message_t *message;
751 status = notmuch_database_begin_atomic (notmuch);
754 status = notmuch_database_find_message_by_filename (notmuch, path, &message);
755 if (status || message == NULL)
757 status = notmuch_database_remove_message (notmuch, path);
758 if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
759 add_files_state->renamed_messages++;
760 if (add_files_state->synchronize_flags == TRUE)
761 notmuch_message_maildir_flags_to_tags (message);
763 add_files_state->removed_messages++;
764 notmuch_message_destroy (message);
765 notmuch_database_end_atomic (notmuch);
769 /* Recursively remove all filenames from the database referring to
770 * 'path' (or to any of its children). */
772 _remove_directory (void *ctx,
773 notmuch_database_t *notmuch,
775 add_files_state_t *add_files_state)
777 notmuch_directory_t *directory;
778 notmuch_filenames_t *files, *subdirs;
781 directory = notmuch_database_get_directory (notmuch, path);
783 for (files = notmuch_directory_get_child_files (directory);
784 notmuch_filenames_valid (files);
785 notmuch_filenames_move_to_next (files))
787 absolute = talloc_asprintf (ctx, "%s/%s", path,
788 notmuch_filenames_get (files));
789 remove_filename (notmuch, absolute, add_files_state);
790 talloc_free (absolute);
793 for (subdirs = notmuch_directory_get_child_directories (directory);
794 notmuch_filenames_valid (subdirs);
795 notmuch_filenames_move_to_next (subdirs))
797 absolute = talloc_asprintf (ctx, "%s/%s", path,
798 notmuch_filenames_get (subdirs));
799 _remove_directory (ctx, notmuch, absolute, add_files_state);
800 talloc_free (absolute);
803 notmuch_directory_destroy (directory);
807 notmuch_new_command (void *ctx, int argc, char *argv[])
809 notmuch_config_t *config;
810 notmuch_database_t *notmuch;
811 add_files_state_t add_files_state;
813 struct timeval tv_now, tv_start;
817 char *dot_notmuch_path;
818 struct sigaction action;
821 notmuch_bool_t timer_is_active = FALSE;
822 notmuch_bool_t run_hooks = TRUE;
824 add_files_state.verbose = 0;
825 add_files_state.output_is_a_tty = isatty (fileno (stdout));
827 argc--; argv++; /* skip subcommand argument */
829 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
830 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
831 add_files_state.verbose = 1;
832 } else if (strcmp (argv[i], "--no-hooks") == 0) {
835 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
839 config = notmuch_config_open (ctx, NULL, NULL);
843 add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);
844 add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
845 db_path = notmuch_config_get_database_path (config);
848 ret = notmuch_run_hook (db_path, "pre-new");
853 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
855 if (stat (dot_notmuch_path, &st)) {
859 count_files (db_path, &count);
863 printf ("Found %d total files (that's not much mail).\n", count);
864 notmuch = notmuch_database_create (db_path);
865 add_files_state.total_files = count;
867 notmuch = notmuch_database_open (db_path,
868 NOTMUCH_DATABASE_MODE_READ_WRITE);
872 if (notmuch_database_needs_upgrade (notmuch)) {
873 printf ("Welcome to a new version of notmuch! Your database will now be upgraded.\n");
874 gettimeofday (&add_files_state.tv_start, NULL);
875 notmuch_database_upgrade (notmuch, upgrade_print_progress,
877 printf ("Your notmuch database has now been upgraded to database format version %u.\n",
878 notmuch_database_get_version (notmuch));
881 add_files_state.total_files = 0;
887 /* Setup our handler for SIGINT. We do this after having
888 * potentially done a database upgrade we this interrupt handler
890 memset (&action, 0, sizeof (struct sigaction));
891 action.sa_handler = handle_sigint;
892 sigemptyset (&action.sa_mask);
893 action.sa_flags = SA_RESTART;
894 sigaction (SIGINT, &action, NULL);
896 talloc_free (dot_notmuch_path);
897 dot_notmuch_path = NULL;
899 add_files_state.processed_files = 0;
900 add_files_state.added_messages = 0;
901 add_files_state.removed_messages = add_files_state.renamed_messages = 0;
902 gettimeofday (&add_files_state.tv_start, NULL);
904 add_files_state.removed_files = _filename_list_create (ctx);
905 add_files_state.removed_directories = _filename_list_create (ctx);
906 add_files_state.directory_mtimes = _filename_list_create (ctx);
908 if (! debugger_is_active () && add_files_state.output_is_a_tty
909 && ! add_files_state.verbose) {
910 setup_progress_printing_timer ();
911 timer_is_active = TRUE;
914 ret = add_files (notmuch, db_path, &add_files_state);
916 gettimeofday (&tv_start, NULL);
917 for (f = add_files_state.removed_files->head; f && !interrupted; f = f->next) {
918 remove_filename (notmuch, f->filename, &add_files_state);
919 if (do_print_progress) {
920 do_print_progress = 0;
921 generic_print_progress ("Cleaned up", "messages",
922 tv_start, add_files_state.removed_messages + add_files_state.renamed_messages,
923 add_files_state.removed_files->count);
927 gettimeofday (&tv_start, NULL);
928 for (f = add_files_state.removed_directories->head, i = 0; f && !interrupted; f = f->next, i++) {
929 _remove_directory (ctx, notmuch, f->filename, &add_files_state);
930 if (do_print_progress) {
931 do_print_progress = 0;
932 generic_print_progress ("Cleaned up", "directories",
934 add_files_state.removed_directories->count);
938 for (f = add_files_state.directory_mtimes->head; f && !interrupted; f = f->next) {
939 notmuch_directory_t *directory;
940 directory = notmuch_database_get_directory (notmuch, f->filename);
942 notmuch_directory_set_mtime (directory, f->mtime);
943 notmuch_directory_destroy (directory);
947 talloc_free (add_files_state.removed_files);
948 talloc_free (add_files_state.removed_directories);
949 talloc_free (add_files_state.directory_mtimes);
952 stop_progress_printing_timer ();
954 gettimeofday (&tv_now, NULL);
955 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
958 if (add_files_state.processed_files) {
959 printf ("Processed %d %s in ", add_files_state.processed_files,
960 add_files_state.processed_files == 1 ?
961 "file" : "total files");
962 notmuch_time_print_formatted_seconds (elapsed);
964 printf (" (%d files/sec.).\033[K\n",
965 (int) (add_files_state.processed_files / elapsed));
967 printf (".\033[K\n");
971 if (add_files_state.added_messages) {
972 printf ("Added %d new %s to the database.",
973 add_files_state.added_messages,
974 add_files_state.added_messages == 1 ?
975 "message" : "messages");
977 printf ("No new mail.");
980 if (add_files_state.removed_messages) {
981 printf (" Removed %d %s.",
982 add_files_state.removed_messages,
983 add_files_state.removed_messages == 1 ? "message" : "messages");
986 if (add_files_state.renamed_messages) {
987 printf (" Detected %d file %s.",
988 add_files_state.renamed_messages,
989 add_files_state.renamed_messages == 1 ? "rename" : "renames");
995 printf ("\nNote: At least one error was encountered: %s\n",
996 notmuch_status_to_string (ret));
999 notmuch_database_close (notmuch);
1001 if (run_hooks && !ret && !interrupted)
1002 ret = notmuch_run_hook (db_path, "post-new");
1004 return ret || interrupted;