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;
42 const char **new_ignore;
43 size_t new_ignore_length;
47 int added_messages, removed_messages, renamed_messages;
48 struct timeval tv_start;
50 _filename_list_t *removed_files;
51 _filename_list_t *removed_directories;
52 _filename_list_t *directory_mtimes;
54 notmuch_bool_t synchronize_flags;
57 static volatile sig_atomic_t do_print_progress = 0;
60 handle_sigalrm (unused (int signal))
62 do_print_progress = 1;
65 static volatile sig_atomic_t interrupted;
68 handle_sigint (unused (int sig))
70 static char msg[] = "Stopping... \n";
72 /* This write is "opportunistic", so it's okay to ignore the
73 * result. It is not required for correctness, and if it does
74 * fail or produce a short write, we want to get out of the signal
75 * handler as quickly as possible, not retry it. */
76 IGNORE_RESULT (write (2, msg, sizeof(msg)-1));
80 static _filename_list_t *
81 _filename_list_create (const void *ctx)
83 _filename_list_t *list;
85 list = talloc (ctx, _filename_list_t);
90 list->tail = &list->head;
96 static _filename_node_t *
97 _filename_list_add (_filename_list_t *list,
100 _filename_node_t *node = talloc (list, _filename_node_t);
104 node->filename = talloc_strdup (list, filename);
107 *(list->tail) = node;
108 list->tail = &node->next;
114 generic_print_progress (const char *action, const char *object,
115 struct timeval tv_start, unsigned processed, unsigned total)
117 struct timeval tv_now;
118 double elapsed_overall, rate_overall;
120 gettimeofday (&tv_now, NULL);
122 elapsed_overall = notmuch_time_elapsed (tv_start, tv_now);
123 rate_overall = processed / elapsed_overall;
125 printf ("%s %d ", action, processed);
128 printf ("of %d %s", total, object);
129 if (processed > 0 && elapsed_overall > 0.5) {
130 double time_remaining = ((total - processed) / rate_overall);
132 notmuch_time_print_formatted_seconds (time_remaining);
133 printf (" remaining)");
136 printf ("%s", object);
137 if (elapsed_overall > 0.5)
138 printf (" (%d %s/sec.)", (int) rate_overall, object);
140 printf (".\033[K\r");
146 dirent_sort_inode (const struct dirent **a, const struct dirent **b)
148 return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
152 dirent_sort_strcmp_name (const struct dirent **a, const struct dirent **b)
154 return strcmp ((*a)->d_name, (*b)->d_name);
157 /* Test if the directory looks like a Maildir directory.
159 * Search through the array of directory entries to see if we can find all
160 * three subdirectories typical for Maildir, that is "new", "cur", and "tmp".
162 * Return 1 if the directory looks like a Maildir and 0 otherwise.
165 _entries_resemble_maildir (struct dirent **entries, int count)
169 for (i = 0; i < count; i++) {
170 if (entries[i]->d_type != DT_DIR && entries[i]->d_type != DT_UNKNOWN)
173 if (strcmp(entries[i]->d_name, "new") == 0 ||
174 strcmp(entries[i]->d_name, "cur") == 0 ||
175 strcmp(entries[i]->d_name, "tmp") == 0)
186 /* Test if the file/directory is to be ignored.
188 static notmuch_bool_t
189 _entry_in_ignore_list (const char *entry, add_files_state_t *state)
193 for (i = 0; i < state->new_ignore_length; i++)
194 if (strcmp (entry, state->new_ignore[i]) == 0)
200 /* Examine 'path' recursively as follows:
202 * o Ask the filesystem for the mtime of 'path' (fs_mtime)
203 * o Ask the database for its timestamp of 'path' (db_mtime)
205 * o Ask the filesystem for files and directories within 'path'
206 * (via scandir and stored in fs_entries)
208 * o Pass 1: For each directory in fs_entries, recursively call into
209 * this same function.
211 * o Compare fs_mtime to db_mtime. If they are equivalent, terminate
212 * the algorithm at this point, (this directory has not been
213 * updated in the filesystem since the last database scan of PASS
216 * o Ask the database for files and directories within 'path'
217 * (db_files and db_subdirs)
219 * o Pass 2: Walk fs_entries simultaneously with db_files and
220 * db_subdirs. Look for one of three interesting cases:
222 * 1. Regular file in fs_entries and not in db_files
223 * This is a new file to add_message into the database.
225 * 2. Filename in db_files not in fs_entries.
226 * This is a file that has been removed from the mail store.
228 * 3. Directory in db_subdirs not in fs_entries
229 * This is a directory that has been removed from the mail store.
231 * Note that the addition of a directory is not interesting here,
232 * since that will have been taken care of in pass 1. Also, we
233 * don't immediately act on file/directory removal since we must
234 * ensure that in the case of a rename that the new filename is
235 * added before the old filename is removed, (so that no
236 * information is lost from the database).
238 * o Tell the database to update its time of 'path' to 'fs_mtime'
239 * if fs_mtime isn't the current wall-clock time.
241 static notmuch_status_t
242 add_files_recursive (notmuch_database_t *notmuch,
244 add_files_state_t *state)
247 struct dirent *entry = NULL;
249 time_t fs_mtime, db_mtime;
250 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
251 notmuch_message_t *message = NULL;
252 struct dirent **fs_entries = NULL;
253 int i, num_fs_entries;
254 notmuch_directory_t *directory;
255 notmuch_filenames_t *db_files = NULL;
256 notmuch_filenames_t *db_subdirs = NULL;
259 notmuch_bool_t is_maildir, new_directory;
262 if (stat (path, &st)) {
263 fprintf (stderr, "Error reading directory %s: %s\n",
264 path, strerror (errno));
265 return NOTMUCH_STATUS_FILE_ERROR;
267 stat_time = time (NULL);
269 /* This is not an error since we may have recursed based on a
270 * symlink to a regular file, not a directory, and we don't know
271 * that until this stat. */
272 if (! S_ISDIR (st.st_mode))
273 return NOTMUCH_STATUS_SUCCESS;
275 fs_mtime = st.st_mtime;
277 directory = notmuch_database_get_directory (notmuch, path);
278 db_mtime = notmuch_directory_get_mtime (directory);
280 new_directory = db_mtime ? FALSE : TRUE;
282 /* XXX This is a temporary workaround. If we don't update the
283 * database mtime until after processing messages in this
284 * directory, then a 0 mtime is *not* sufficient to indicate that
285 * this directory has no messages or subdirs in the database (for
286 * example, if an earlier run skipped the mtime update because
287 * fs_mtime == stat_time, or was interrupted before updating the
288 * mtime at the end). To address this, we record a (bogus)
289 * non-zero value before processing any child messages so that a
290 * later run won't mistake this for a new directory (and, for
291 * example, fail to detect removed files and subdirs).
293 * A better solution would be for notmuch_database_get_directory
294 * to indicate if it really created a new directory or not, either
295 * by a new out-argument, or by recording this information and
296 * providing an accessor.
299 notmuch_directory_set_mtime (directory, -1);
301 /* If the database knows about this directory, then we sort based
302 * on strcmp to match the database sorting. Otherwise, we can do
303 * inode-based sorting for faster filesystem operation. */
304 num_fs_entries = scandir (path, &fs_entries, 0,
306 dirent_sort_inode : dirent_sort_strcmp_name);
308 if (num_fs_entries == -1) {
309 fprintf (stderr, "Error opening directory %s: %s\n",
310 path, strerror (errno));
311 ret = NOTMUCH_STATUS_FILE_ERROR;
315 /* Pass 1: Recurse into all sub-directories. */
316 is_maildir = _entries_resemble_maildir (fs_entries, num_fs_entries);
318 for (i = 0; i < num_fs_entries; i++) {
322 entry = fs_entries[i];
324 /* We only want to descend into directories.
325 * But symlinks can be to directories too, of course.
327 * And if the filesystem doesn't tell us the file type in the
328 * scandir results, then it might be a directory (and if not,
329 * then we'll stat and return immediately in the next level of
331 if (entry->d_type != DT_DIR &&
332 entry->d_type != DT_LNK &&
333 entry->d_type != DT_UNKNOWN)
338 /* Ignore special directories to avoid infinite recursion.
339 * Also ignore the .notmuch directory, any "tmp" directory
340 * that appears within a maildir and files/directories
341 * the user has configured to be ignored.
343 if (strcmp (entry->d_name, ".") == 0 ||
344 strcmp (entry->d_name, "..") == 0 ||
345 (is_maildir && strcmp (entry->d_name, "tmp") == 0) ||
346 strcmp (entry->d_name, ".notmuch") == 0 ||
347 _entry_in_ignore_list (entry->d_name, state))
352 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
353 status = add_files_recursive (notmuch, next, state);
354 if (status && ret == NOTMUCH_STATUS_SUCCESS)
360 /* If the directory's modification time in the filesystem is the
361 * same as what we recorded in the database the last time we
362 * scanned it, then we can skip the second pass entirely.
364 * We test for strict equality here to avoid a bug that can happen
365 * if the system clock jumps backward, (preventing new mail from
366 * being discovered until the clock catches up and the directory
367 * is modified again).
369 if (fs_mtime == db_mtime)
372 /* new_directory means a directory that the database has never
373 * seen before. In that case, we can simply leave db_files and
374 * db_subdirs NULL. */
375 if (!new_directory) {
376 db_files = notmuch_directory_get_child_files (directory);
377 db_subdirs = notmuch_directory_get_child_directories (directory);
380 /* Pass 2: Scan for new files, removed files, and removed directories. */
381 for (i = 0; i < num_fs_entries; i++)
386 entry = fs_entries[i];
388 /* Ignore files & directories user has configured to be ignored */
389 if (_entry_in_ignore_list (entry->d_name, state))
392 /* Check if we've walked past any names in db_files or
393 * db_subdirs. If so, these have been deleted. */
394 while (notmuch_filenames_valid (db_files) &&
395 strcmp (notmuch_filenames_get (db_files), entry->d_name) < 0)
397 char *absolute = talloc_asprintf (state->removed_files,
399 notmuch_filenames_get (db_files));
401 _filename_list_add (state->removed_files, absolute);
403 notmuch_filenames_move_to_next (db_files);
406 while (notmuch_filenames_valid (db_subdirs) &&
407 strcmp (notmuch_filenames_get (db_subdirs), entry->d_name) <= 0)
409 const char *filename = notmuch_filenames_get (db_subdirs);
411 if (strcmp (filename, entry->d_name) < 0)
413 char *absolute = talloc_asprintf (state->removed_directories,
414 "%s/%s", path, filename);
416 _filename_list_add (state->removed_directories, absolute);
419 notmuch_filenames_move_to_next (db_subdirs);
422 /* If we're looking at a symlink, we only want to add it if it
423 * links to a regular file, (and not to a directory, say).
425 * Similarly, if the file is of unknown type (due to filesystem
426 * limitations), then we also need to look closer.
428 * In either case, a stat does the trick.
430 if (entry->d_type == DT_LNK || entry->d_type == DT_UNKNOWN) {
433 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
434 err = stat (next, &st);
438 /* Don't emit an error for a link pointing nowhere, since
439 * the directory-traversal pass will have already done
444 if (! S_ISREG (st.st_mode))
446 } else if (entry->d_type != DT_REG) {
450 /* Don't add a file that we've added before. */
451 if (notmuch_filenames_valid (db_files) &&
452 strcmp (notmuch_filenames_get (db_files), entry->d_name) == 0)
454 notmuch_filenames_move_to_next (db_files);
458 /* We're now looking at a regular file that doesn't yet exist
459 * in the database, so add it. */
460 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
462 state->processed_files++;
464 if (state->verbose) {
465 if (state->output_is_a_tty)
469 state->processed_files,
473 putchar((state->output_is_a_tty) ? '\r' : '\n');
477 status = notmuch_database_begin_atomic (notmuch);
483 status = notmuch_database_add_message (notmuch, next, &message);
486 case NOTMUCH_STATUS_SUCCESS:
487 state->added_messages++;
488 notmuch_message_freeze (message);
489 for (tag=state->new_tags; *tag != NULL; tag++)
490 notmuch_message_add_tag (message, *tag);
491 if (state->synchronize_flags == TRUE)
492 notmuch_message_maildir_flags_to_tags (message);
493 notmuch_message_thaw (message);
495 /* Non-fatal issues (go on to next file) */
496 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
497 if (state->synchronize_flags == TRUE)
498 notmuch_message_maildir_flags_to_tags (message);
500 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
501 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
504 /* Fatal issues. Don't process anymore. */
505 case NOTMUCH_STATUS_READ_ONLY_DATABASE:
506 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
507 case NOTMUCH_STATUS_OUT_OF_MEMORY:
508 fprintf (stderr, "Error: %s. Halting processing.\n",
509 notmuch_status_to_string (status));
513 case NOTMUCH_STATUS_FILE_ERROR:
514 case NOTMUCH_STATUS_NULL_POINTER:
515 case NOTMUCH_STATUS_TAG_TOO_LONG:
516 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
517 case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
518 case NOTMUCH_STATUS_LAST_STATUS:
519 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
523 status = notmuch_database_end_atomic (notmuch);
530 notmuch_message_destroy (message);
534 if (do_print_progress) {
535 do_print_progress = 0;
536 generic_print_progress ("Processed", "files", state->tv_start,
537 state->processed_files, state->total_files);
547 /* Now that we've walked the whole filesystem list, anything left
548 * over in the database lists has been deleted. */
549 while (notmuch_filenames_valid (db_files))
551 char *absolute = talloc_asprintf (state->removed_files,
553 notmuch_filenames_get (db_files));
555 _filename_list_add (state->removed_files, absolute);
557 notmuch_filenames_move_to_next (db_files);
560 while (notmuch_filenames_valid (db_subdirs))
562 char *absolute = talloc_asprintf (state->removed_directories,
564 notmuch_filenames_get (db_subdirs));
566 _filename_list_add (state->removed_directories, absolute);
568 notmuch_filenames_move_to_next (db_subdirs);
571 /* If the directory's mtime is the same as the wall-clock time
572 * when we stat'ed the directory, we skip updating the mtime in
573 * the database because a message could be delivered later in this
574 * same second. This may lead to unnecessary re-scans, but it
575 * avoids overlooking messages. */
576 if (fs_mtime != stat_time)
577 _filename_list_add (state->directory_mtimes, path)->mtime = fs_mtime;
585 for (i = 0; i < num_fs_entries; i++)
586 free (fs_entries[i]);
591 notmuch_filenames_destroy (db_subdirs);
593 notmuch_filenames_destroy (db_files);
595 notmuch_directory_destroy (directory);
601 setup_progress_printing_timer (void)
603 struct sigaction action;
604 struct itimerval timerval;
606 /* Setup our handler for SIGALRM */
607 memset (&action, 0, sizeof (struct sigaction));
608 action.sa_handler = handle_sigalrm;
609 sigemptyset (&action.sa_mask);
610 action.sa_flags = SA_RESTART;
611 sigaction (SIGALRM, &action, NULL);
613 /* Then start a timer to send SIGALRM once per second. */
614 timerval.it_interval.tv_sec = 1;
615 timerval.it_interval.tv_usec = 0;
616 timerval.it_value.tv_sec = 1;
617 timerval.it_value.tv_usec = 0;
618 setitimer (ITIMER_REAL, &timerval, NULL);
622 stop_progress_printing_timer (void)
624 struct sigaction action;
625 struct itimerval timerval;
627 /* Now stop the timer. */
628 timerval.it_interval.tv_sec = 0;
629 timerval.it_interval.tv_usec = 0;
630 timerval.it_value.tv_sec = 0;
631 timerval.it_value.tv_usec = 0;
632 setitimer (ITIMER_REAL, &timerval, NULL);
634 /* And disable the signal handler. */
635 action.sa_handler = SIG_IGN;
636 sigaction (SIGALRM, &action, NULL);
640 /* This is the top-level entry point for add_files. It does a couple
641 * of error checks and then calls into the recursive function. */
642 static notmuch_status_t
643 add_files (notmuch_database_t *notmuch,
645 add_files_state_t *state)
647 notmuch_status_t status;
650 if (stat (path, &st)) {
651 fprintf (stderr, "Error reading directory %s: %s\n",
652 path, strerror (errno));
653 return NOTMUCH_STATUS_FILE_ERROR;
656 if (! S_ISDIR (st.st_mode)) {
657 fprintf (stderr, "Error: %s is not a directory.\n", path);
658 return NOTMUCH_STATUS_FILE_ERROR;
661 status = add_files_recursive (notmuch, path, state);
666 /* XXX: This should be merged with the add_files function since it
667 * shares a lot of logic with it. */
668 /* Recursively count all regular files in path and all sub-directories
669 * of path. The result is added to *count (which should be
670 * initialized to zero by the top-level caller before calling
673 count_files (const char *path, int *count, add_files_state_t *state)
675 struct dirent *entry = NULL;
678 struct dirent **fs_entries = NULL;
679 int num_fs_entries = scandir (path, &fs_entries, 0, dirent_sort_inode);
682 if (num_fs_entries == -1) {
683 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
684 path, strerror (errno));
688 while (!interrupted) {
689 if (i == num_fs_entries)
692 entry = fs_entries[i++];
694 /* Ignore special directories to avoid infinite recursion.
695 * Also ignore the .notmuch directory and files/directories
696 * the user has configured to be ignored.
698 if (strcmp (entry->d_name, ".") == 0 ||
699 strcmp (entry->d_name, "..") == 0 ||
700 strcmp (entry->d_name, ".notmuch") == 0 ||
701 _entry_in_ignore_list (entry->d_name, state))
706 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
708 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
709 path, entry->d_name);
715 if (S_ISREG (st.st_mode)) {
717 if (*count % 1000 == 0) {
718 printf ("Found %d files so far.\r", *count);
721 } else if (S_ISDIR (st.st_mode)) {
722 count_files (next, count, state);
730 for (i = 0; i < num_fs_entries; i++)
731 free (fs_entries[i]);
738 upgrade_print_progress (void *closure,
741 add_files_state_t *state = closure;
743 printf ("Upgrading database: %.2f%% complete", progress * 100.0);
746 struct timeval tv_now;
747 double elapsed, time_remaining;
749 gettimeofday (&tv_now, NULL);
751 elapsed = notmuch_time_elapsed (state->tv_start, tv_now);
752 time_remaining = (elapsed / progress) * (1.0 - progress);
754 notmuch_time_print_formatted_seconds (time_remaining);
755 printf (" remaining)");
763 /* Remove one message filename from the database. */
764 static notmuch_status_t
765 remove_filename (notmuch_database_t *notmuch,
767 add_files_state_t *add_files_state)
769 notmuch_status_t status;
770 notmuch_message_t *message;
771 status = notmuch_database_begin_atomic (notmuch);
774 status = notmuch_database_find_message_by_filename (notmuch, path, &message);
775 if (status || message == NULL)
777 status = notmuch_database_remove_message (notmuch, path);
778 if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
779 add_files_state->renamed_messages++;
780 if (add_files_state->synchronize_flags == TRUE)
781 notmuch_message_maildir_flags_to_tags (message);
783 add_files_state->removed_messages++;
784 notmuch_message_destroy (message);
785 notmuch_database_end_atomic (notmuch);
789 /* Recursively remove all filenames from the database referring to
790 * 'path' (or to any of its children). */
792 _remove_directory (void *ctx,
793 notmuch_database_t *notmuch,
795 add_files_state_t *add_files_state)
797 notmuch_directory_t *directory;
798 notmuch_filenames_t *files, *subdirs;
801 directory = notmuch_database_get_directory (notmuch, path);
803 for (files = notmuch_directory_get_child_files (directory);
804 notmuch_filenames_valid (files);
805 notmuch_filenames_move_to_next (files))
807 absolute = talloc_asprintf (ctx, "%s/%s", path,
808 notmuch_filenames_get (files));
809 remove_filename (notmuch, absolute, add_files_state);
810 talloc_free (absolute);
813 for (subdirs = notmuch_directory_get_child_directories (directory);
814 notmuch_filenames_valid (subdirs);
815 notmuch_filenames_move_to_next (subdirs))
817 absolute = talloc_asprintf (ctx, "%s/%s", path,
818 notmuch_filenames_get (subdirs));
819 _remove_directory (ctx, notmuch, absolute, add_files_state);
820 talloc_free (absolute);
823 notmuch_directory_destroy (directory);
827 notmuch_new_command (void *ctx, int argc, char *argv[])
829 notmuch_config_t *config;
830 notmuch_database_t *notmuch;
831 add_files_state_t add_files_state;
833 struct timeval tv_now, tv_start;
837 char *dot_notmuch_path;
838 struct sigaction action;
841 notmuch_bool_t timer_is_active = FALSE;
842 notmuch_bool_t run_hooks = TRUE;
844 add_files_state.verbose = 0;
845 add_files_state.output_is_a_tty = isatty (fileno (stdout));
847 argc--; argv++; /* skip subcommand argument */
849 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
850 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
851 add_files_state.verbose = 1;
852 } else if (strcmp (argv[i], "--no-hooks") == 0) {
855 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
859 config = notmuch_config_open (ctx, NULL, NULL);
863 add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);
864 add_files_state.new_ignore = notmuch_config_get_new_ignore (config, &add_files_state.new_ignore_length);
865 add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
866 db_path = notmuch_config_get_database_path (config);
869 ret = notmuch_run_hook (db_path, "pre-new");
874 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
876 if (stat (dot_notmuch_path, &st)) {
880 count_files (db_path, &count, &add_files_state);
884 printf ("Found %d total files (that's not much mail).\n", count);
885 notmuch = notmuch_database_create (db_path);
886 add_files_state.total_files = count;
888 notmuch = notmuch_database_open (db_path,
889 NOTMUCH_DATABASE_MODE_READ_WRITE);
893 if (notmuch_database_needs_upgrade (notmuch)) {
894 printf ("Welcome to a new version of notmuch! Your database will now be upgraded.\n");
895 gettimeofday (&add_files_state.tv_start, NULL);
896 notmuch_database_upgrade (notmuch, upgrade_print_progress,
898 printf ("Your notmuch database has now been upgraded to database format version %u.\n",
899 notmuch_database_get_version (notmuch));
902 add_files_state.total_files = 0;
908 /* Setup our handler for SIGINT. We do this after having
909 * potentially done a database upgrade we this interrupt handler
911 memset (&action, 0, sizeof (struct sigaction));
912 action.sa_handler = handle_sigint;
913 sigemptyset (&action.sa_mask);
914 action.sa_flags = SA_RESTART;
915 sigaction (SIGINT, &action, NULL);
917 talloc_free (dot_notmuch_path);
918 dot_notmuch_path = NULL;
920 add_files_state.processed_files = 0;
921 add_files_state.added_messages = 0;
922 add_files_state.removed_messages = add_files_state.renamed_messages = 0;
923 gettimeofday (&add_files_state.tv_start, NULL);
925 add_files_state.removed_files = _filename_list_create (ctx);
926 add_files_state.removed_directories = _filename_list_create (ctx);
927 add_files_state.directory_mtimes = _filename_list_create (ctx);
929 if (! debugger_is_active () && add_files_state.output_is_a_tty
930 && ! add_files_state.verbose) {
931 setup_progress_printing_timer ();
932 timer_is_active = TRUE;
935 ret = add_files (notmuch, db_path, &add_files_state);
937 gettimeofday (&tv_start, NULL);
938 for (f = add_files_state.removed_files->head; f && !interrupted; f = f->next) {
939 remove_filename (notmuch, f->filename, &add_files_state);
940 if (do_print_progress) {
941 do_print_progress = 0;
942 generic_print_progress ("Cleaned up", "messages",
943 tv_start, add_files_state.removed_messages + add_files_state.renamed_messages,
944 add_files_state.removed_files->count);
948 gettimeofday (&tv_start, NULL);
949 for (f = add_files_state.removed_directories->head, i = 0; f && !interrupted; f = f->next, i++) {
950 _remove_directory (ctx, notmuch, f->filename, &add_files_state);
951 if (do_print_progress) {
952 do_print_progress = 0;
953 generic_print_progress ("Cleaned up", "directories",
955 add_files_state.removed_directories->count);
959 for (f = add_files_state.directory_mtimes->head; f && !interrupted; f = f->next) {
960 notmuch_directory_t *directory;
961 directory = notmuch_database_get_directory (notmuch, f->filename);
963 notmuch_directory_set_mtime (directory, f->mtime);
964 notmuch_directory_destroy (directory);
968 talloc_free (add_files_state.removed_files);
969 talloc_free (add_files_state.removed_directories);
970 talloc_free (add_files_state.directory_mtimes);
973 stop_progress_printing_timer ();
975 gettimeofday (&tv_now, NULL);
976 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
979 if (add_files_state.processed_files) {
980 printf ("Processed %d %s in ", add_files_state.processed_files,
981 add_files_state.processed_files == 1 ?
982 "file" : "total files");
983 notmuch_time_print_formatted_seconds (elapsed);
985 printf (" (%d files/sec.).\033[K\n",
986 (int) (add_files_state.processed_files / elapsed));
988 printf (".\033[K\n");
992 if (add_files_state.added_messages) {
993 printf ("Added %d new %s to the database.",
994 add_files_state.added_messages,
995 add_files_state.added_messages == 1 ?
996 "message" : "messages");
998 printf ("No new mail.");
1001 if (add_files_state.removed_messages) {
1002 printf (" Removed %d %s.",
1003 add_files_state.removed_messages,
1004 add_files_state.removed_messages == 1 ? "message" : "messages");
1007 if (add_files_state.renamed_messages) {
1008 printf (" Detected %d file %s.",
1009 add_files_state.renamed_messages,
1010 add_files_state.renamed_messages == 1 ? "rename" : "renames");
1016 printf ("\nNote: At least one error was encountered: %s\n",
1017 notmuch_status_to_string (ret));
1020 notmuch_database_close (notmuch);
1022 if (run_hooks && !ret && !interrupted)
1023 ret = notmuch_run_hook (db_path, "post-new");
1025 return ret || interrupted;