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;
45 enum verbosity verbosity;
47 const char **new_tags;
48 size_t new_tags_length;
49 const char **new_ignore;
50 size_t new_ignore_length;
54 int added_messages, removed_messages, renamed_messages;
55 struct timeval tv_start;
57 _filename_list_t *removed_files;
58 _filename_list_t *removed_directories;
59 _filename_list_t *directory_mtimes;
61 notmuch_bool_t synchronize_flags;
64 static volatile sig_atomic_t do_print_progress = 0;
67 handle_sigalrm (unused (int signal))
69 do_print_progress = 1;
72 static volatile sig_atomic_t interrupted;
75 handle_sigint (unused (int sig))
77 static char msg[] = "Stopping... \n";
79 /* This write is "opportunistic", so it's okay to ignore the
80 * result. It is not required for correctness, and if it does
81 * fail or produce a short write, we want to get out of the signal
82 * handler as quickly as possible, not retry it. */
83 IGNORE_RESULT (write (2, msg, sizeof(msg)-1));
87 static _filename_list_t *
88 _filename_list_create (const void *ctx)
90 _filename_list_t *list;
92 list = talloc (ctx, _filename_list_t);
97 list->tail = &list->head;
103 static _filename_node_t *
104 _filename_list_add (_filename_list_t *list,
105 const char *filename)
107 _filename_node_t *node = talloc (list, _filename_node_t);
111 node->filename = talloc_strdup (list, filename);
114 *(list->tail) = node;
115 list->tail = &node->next;
121 generic_print_progress (const char *action, const char *object,
122 struct timeval tv_start, unsigned processed, unsigned total)
124 struct timeval tv_now;
125 double elapsed_overall, rate_overall;
127 gettimeofday (&tv_now, NULL);
129 elapsed_overall = notmuch_time_elapsed (tv_start, tv_now);
130 rate_overall = processed / elapsed_overall;
132 printf ("%s %d ", action, processed);
135 printf ("of %d %s", total, object);
136 if (processed > 0 && elapsed_overall > 0.5) {
137 double time_remaining = ((total - processed) / rate_overall);
139 notmuch_time_print_formatted_seconds (time_remaining);
140 printf (" remaining)");
143 printf ("%s", object);
144 if (elapsed_overall > 0.5)
145 printf (" (%d %s/sec.)", (int) rate_overall, object);
147 printf (".\033[K\r");
153 dirent_sort_inode (const struct dirent **a, const struct dirent **b)
155 return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
159 dirent_sort_strcmp_name (const struct dirent **a, const struct dirent **b)
161 return strcmp ((*a)->d_name, (*b)->d_name);
164 /* Return the type of a directory entry relative to path as a stat(2)
165 * mode. Like stat, this follows symlinks. Returns -1 and sets errno
166 * if the file's type cannot be determined (which includes dangling
170 dirent_type (const char *path, const struct dirent *entry)
174 int err, saved_errno;
176 #ifdef _DIRENT_HAVE_D_TYPE
177 /* Mapping from d_type to stat mode_t. We omit DT_LNK so that
178 * we'll fall through to stat and get the real file type. */
179 static const mode_t modes[] = {
187 if (entry->d_type < ARRAY_SIZE(modes) && modes[entry->d_type])
188 return modes[entry->d_type];
191 abspath = talloc_asprintf (NULL, "%s/%s", path, entry->d_name);
196 err = stat(abspath, &statbuf);
198 talloc_free (abspath);
203 return statbuf.st_mode & S_IFMT;
206 /* Test if the directory looks like a Maildir directory.
208 * Search through the array of directory entries to see if we can find all
209 * three subdirectories typical for Maildir, that is "new", "cur", and "tmp".
211 * Return 1 if the directory looks like a Maildir and 0 otherwise.
214 _entries_resemble_maildir (const char *path, struct dirent **entries, int count)
218 for (i = 0; i < count; i++) {
219 if (dirent_type (path, entries[i]) != S_IFDIR)
222 if (strcmp(entries[i]->d_name, "new") == 0 ||
223 strcmp(entries[i]->d_name, "cur") == 0 ||
224 strcmp(entries[i]->d_name, "tmp") == 0)
235 /* Test if the file/directory is to be ignored.
237 static notmuch_bool_t
238 _entry_in_ignore_list (const char *entry, add_files_state_t *state)
242 for (i = 0; i < state->new_ignore_length; i++)
243 if (strcmp (entry, state->new_ignore[i]) == 0)
249 /* Add a single file to the database. */
250 static notmuch_status_t
251 add_file (notmuch_database_t *notmuch, const char *filename,
252 add_files_state_t *state)
254 notmuch_message_t *message = NULL;
256 notmuch_status_t status;
258 status = notmuch_database_begin_atomic (notmuch);
262 status = notmuch_database_add_message (notmuch, filename, &message);
265 case NOTMUCH_STATUS_SUCCESS:
266 state->added_messages++;
267 notmuch_message_freeze (message);
268 for (tag = state->new_tags; *tag != NULL; tag++)
269 notmuch_message_add_tag (message, *tag);
270 if (state->synchronize_flags)
271 notmuch_message_maildir_flags_to_tags (message);
272 notmuch_message_thaw (message);
274 /* Non-fatal issues (go on to next file). */
275 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
276 if (state->synchronize_flags)
277 notmuch_message_maildir_flags_to_tags (message);
279 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
280 fprintf (stderr, "Note: Ignoring non-mail file: %s\n", filename);
282 /* Fatal issues. Don't process anymore. */
283 case NOTMUCH_STATUS_READ_ONLY_DATABASE:
284 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
285 case NOTMUCH_STATUS_OUT_OF_MEMORY:
286 fprintf (stderr, "Error: %s. Halting processing.\n",
287 notmuch_status_to_string (status));
290 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
294 status = notmuch_database_end_atomic (notmuch);
298 notmuch_message_destroy (message);
303 /* Examine 'path' recursively as follows:
305 * o Ask the filesystem for the mtime of 'path' (fs_mtime)
306 * o Ask the database for its timestamp of 'path' (db_mtime)
308 * o Ask the filesystem for files and directories within 'path'
309 * (via scandir and stored in fs_entries)
311 * o Pass 1: For each directory in fs_entries, recursively call into
312 * this same function.
314 * o Compare fs_mtime to db_mtime. If they are equivalent, terminate
315 * the algorithm at this point, (this directory has not been
316 * updated in the filesystem since the last database scan of PASS
319 * o Ask the database for files and directories within 'path'
320 * (db_files and db_subdirs)
322 * o Pass 2: Walk fs_entries simultaneously with db_files and
323 * db_subdirs. Look for one of three interesting cases:
325 * 1. Regular file in fs_entries and not in db_files
326 * This is a new file to add_message into the database.
328 * 2. Filename in db_files not in fs_entries.
329 * This is a file that has been removed from the mail store.
331 * 3. Directory in db_subdirs not in fs_entries
332 * This is a directory that has been removed from the mail store.
334 * Note that the addition of a directory is not interesting here,
335 * since that will have been taken care of in pass 1. Also, we
336 * don't immediately act on file/directory removal since we must
337 * ensure that in the case of a rename that the new filename is
338 * added before the old filename is removed, (so that no
339 * information is lost from the database).
341 * o Tell the database to update its time of 'path' to 'fs_mtime'
342 * if fs_mtime isn't the current wall-clock time.
344 static notmuch_status_t
345 add_files (notmuch_database_t *notmuch,
347 add_files_state_t *state)
350 struct dirent *entry = NULL;
352 time_t fs_mtime, db_mtime;
353 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
354 struct dirent **fs_entries = NULL;
355 int i, num_fs_entries = 0, entry_type;
356 notmuch_directory_t *directory;
357 notmuch_filenames_t *db_files = NULL;
358 notmuch_filenames_t *db_subdirs = NULL;
361 notmuch_bool_t is_maildir;
363 if (stat (path, &st)) {
364 fprintf (stderr, "Error reading directory %s: %s\n",
365 path, strerror (errno));
366 return NOTMUCH_STATUS_FILE_ERROR;
368 stat_time = time (NULL);
370 if (! S_ISDIR (st.st_mode)) {
371 fprintf (stderr, "Error: %s is not a directory.\n", path);
372 return NOTMUCH_STATUS_FILE_ERROR;
375 fs_mtime = st.st_mtime;
377 status = notmuch_database_get_directory (notmuch, path, &directory);
382 db_mtime = directory ? notmuch_directory_get_mtime (directory) : 0;
384 /* If the directory is unchanged from our last scan and has no
385 * sub-directories, then return without scanning it at all. In
386 * some situations, skipping the scan can substantially reduce the
387 * cost of notmuch new, especially since the huge numbers of files
388 * in Maildirs make scans expensive, but all files live in leaf
391 * To check for sub-directories, we borrow a trick from find,
392 * kpathsea, and many other UNIX tools: since a directory's link
393 * count is the number of sub-directories (specifically, their
394 * '..' entries) plus 2 (the link from the parent and the link for
395 * '.'). This check is safe even on weird file systems, since
396 * file systems that can't compute this will return 0 or 1. This
397 * is safe even on *really* weird file systems like HFS+ that
398 * mistakenly return the total number of directory entries, since
399 * that only inflates the count beyond 2.
401 if (directory && fs_mtime == db_mtime && st.st_nlink == 2) {
402 /* There's one catch: pass 1 below considers symlinks to
403 * directories to be directories, but these don't increase the
404 * file system link count. So, only bail early if the
405 * database agrees that there are no sub-directories. */
406 db_subdirs = notmuch_directory_get_child_directories (directory);
407 if (!notmuch_filenames_valid (db_subdirs))
409 notmuch_filenames_destroy (db_subdirs);
413 /* If the database knows about this directory, then we sort based
414 * on strcmp to match the database sorting. Otherwise, we can do
415 * inode-based sorting for faster filesystem operation. */
416 num_fs_entries = scandir (path, &fs_entries, 0,
418 dirent_sort_strcmp_name : dirent_sort_inode);
420 if (num_fs_entries == -1) {
421 fprintf (stderr, "Error opening directory %s: %s\n",
422 path, strerror (errno));
423 /* We consider this a fatal error because, if a user moved a
424 * message from another directory that we were able to scan
425 * into this directory, skipping this directory will cause
426 * that message to be lost. */
427 ret = NOTMUCH_STATUS_FILE_ERROR;
431 /* Pass 1: Recurse into all sub-directories. */
432 is_maildir = _entries_resemble_maildir (path, fs_entries, num_fs_entries);
434 for (i = 0; i < num_fs_entries; i++) {
438 entry = fs_entries[i];
440 /* Ignore any files/directories the user has configured to
441 * ignore. We do this before dirent_type both for performance
442 * and because we don't care if dirent_type fails on entries
443 * that are explicitly ignored.
445 if (_entry_in_ignore_list (entry->d_name, state)) {
447 printf ("(D) add_files_recursive, pass 1: explicitly ignoring %s/%s\n",
448 path, entry->d_name);
452 /* We only want to descend into directories (and symlinks to
454 entry_type = dirent_type (path, entry);
455 if (entry_type == -1) {
456 /* Be pessimistic, e.g. so we don't lose lots of mail just
457 * because a user broke a symlink. */
458 fprintf (stderr, "Error reading file %s/%s: %s\n",
459 path, entry->d_name, strerror (errno));
460 return NOTMUCH_STATUS_FILE_ERROR;
461 } else if (entry_type != S_IFDIR) {
465 /* Ignore special directories to avoid infinite recursion.
466 * Also ignore the .notmuch directory and any "tmp" directory
467 * that appears within a maildir.
469 if (strcmp (entry->d_name, ".") == 0 ||
470 strcmp (entry->d_name, "..") == 0 ||
471 (is_maildir && strcmp (entry->d_name, "tmp") == 0) ||
472 strcmp (entry->d_name, ".notmuch") == 0)
475 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
476 status = add_files (notmuch, next, state);
485 /* If the directory's modification time in the filesystem is the
486 * same as what we recorded in the database the last time we
487 * scanned it, then we can skip the second pass entirely.
489 * We test for strict equality here to avoid a bug that can happen
490 * if the system clock jumps backward, (preventing new mail from
491 * being discovered until the clock catches up and the directory
492 * is modified again).
494 if (directory && fs_mtime == db_mtime)
497 /* If the database has never seen this directory before, we can
498 * simply leave db_files and db_subdirs NULL. */
500 db_files = notmuch_directory_get_child_files (directory);
501 db_subdirs = notmuch_directory_get_child_directories (directory);
504 /* Pass 2: Scan for new files, removed files, and removed directories. */
505 for (i = 0; i < num_fs_entries; i++)
510 entry = fs_entries[i];
512 /* Ignore files & directories user has configured to be ignored */
513 if (_entry_in_ignore_list (entry->d_name, state)) {
515 printf ("(D) add_files_recursive, pass 2: explicitly ignoring %s/%s\n",
521 /* Check if we've walked past any names in db_files or
522 * db_subdirs. If so, these have been deleted. */
523 while (notmuch_filenames_valid (db_files) &&
524 strcmp (notmuch_filenames_get (db_files), entry->d_name) < 0)
526 char *absolute = talloc_asprintf (state->removed_files,
528 notmuch_filenames_get (db_files));
530 _filename_list_add (state->removed_files, absolute);
532 notmuch_filenames_move_to_next (db_files);
535 while (notmuch_filenames_valid (db_subdirs) &&
536 strcmp (notmuch_filenames_get (db_subdirs), entry->d_name) <= 0)
538 const char *filename = notmuch_filenames_get (db_subdirs);
540 if (strcmp (filename, entry->d_name) < 0)
542 char *absolute = talloc_asprintf (state->removed_directories,
543 "%s/%s", path, filename);
545 _filename_list_add (state->removed_directories, absolute);
548 notmuch_filenames_move_to_next (db_subdirs);
551 /* Only add regular files (and symlinks to regular files). */
552 entry_type = dirent_type (path, entry);
553 if (entry_type == -1) {
554 fprintf (stderr, "Error reading file %s/%s: %s\n",
555 path, entry->d_name, strerror (errno));
556 return NOTMUCH_STATUS_FILE_ERROR;
557 } else if (entry_type != S_IFREG) {
561 /* Don't add a file that we've added before. */
562 if (notmuch_filenames_valid (db_files) &&
563 strcmp (notmuch_filenames_get (db_files), entry->d_name) == 0)
565 notmuch_filenames_move_to_next (db_files);
569 /* We're now looking at a regular file that doesn't yet exist
570 * in the database, so add it. */
571 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
573 state->processed_files++;
575 if (state->verbosity >= VERBOSITY_VERBOSE) {
576 if (state->output_is_a_tty)
579 printf ("%i/%i: %s", state->processed_files, state->total_files,
582 putchar((state->output_is_a_tty) ? '\r' : '\n');
586 status = add_file (notmuch, next, state);
592 if (do_print_progress) {
593 do_print_progress = 0;
594 generic_print_progress ("Processed", "files", state->tv_start,
595 state->processed_files, state->total_files);
605 /* Now that we've walked the whole filesystem list, anything left
606 * over in the database lists has been deleted. */
607 while (notmuch_filenames_valid (db_files))
609 char *absolute = talloc_asprintf (state->removed_files,
611 notmuch_filenames_get (db_files));
613 _filename_list_add (state->removed_files, absolute);
615 notmuch_filenames_move_to_next (db_files);
618 while (notmuch_filenames_valid (db_subdirs))
620 char *absolute = talloc_asprintf (state->removed_directories,
622 notmuch_filenames_get (db_subdirs));
624 _filename_list_add (state->removed_directories, absolute);
626 notmuch_filenames_move_to_next (db_subdirs);
629 /* If the directory's mtime is the same as the wall-clock time
630 * when we stat'ed the directory, we skip updating the mtime in
631 * the database because a message could be delivered later in this
632 * same second. This may lead to unnecessary re-scans, but it
633 * avoids overlooking messages. */
634 if (fs_mtime != stat_time)
635 _filename_list_add (state->directory_mtimes, path)->mtime = fs_mtime;
643 for (i = 0; i < num_fs_entries; i++)
644 free (fs_entries[i]);
649 notmuch_filenames_destroy (db_subdirs);
651 notmuch_filenames_destroy (db_files);
653 notmuch_directory_destroy (directory);
659 setup_progress_printing_timer (void)
661 struct sigaction action;
662 struct itimerval timerval;
664 /* Setup our handler for SIGALRM */
665 memset (&action, 0, sizeof (struct sigaction));
666 action.sa_handler = handle_sigalrm;
667 sigemptyset (&action.sa_mask);
668 action.sa_flags = SA_RESTART;
669 sigaction (SIGALRM, &action, NULL);
671 /* Then start a timer to send SIGALRM once per second. */
672 timerval.it_interval.tv_sec = 1;
673 timerval.it_interval.tv_usec = 0;
674 timerval.it_value.tv_sec = 1;
675 timerval.it_value.tv_usec = 0;
676 setitimer (ITIMER_REAL, &timerval, NULL);
680 stop_progress_printing_timer (void)
682 struct sigaction action;
683 struct itimerval timerval;
685 /* Now stop the timer. */
686 timerval.it_interval.tv_sec = 0;
687 timerval.it_interval.tv_usec = 0;
688 timerval.it_value.tv_sec = 0;
689 timerval.it_value.tv_usec = 0;
690 setitimer (ITIMER_REAL, &timerval, NULL);
692 /* And disable the signal handler. */
693 action.sa_handler = SIG_IGN;
694 sigaction (SIGALRM, &action, NULL);
698 /* XXX: This should be merged with the add_files function since it
699 * shares a lot of logic with it. */
700 /* Recursively count all regular files in path and all sub-directories
701 * of path. The result is added to *count (which should be
702 * initialized to zero by the top-level caller before calling
705 count_files (const char *path, int *count, add_files_state_t *state)
707 struct dirent *entry = NULL;
709 struct dirent **fs_entries = NULL;
710 int num_fs_entries = scandir (path, &fs_entries, 0, dirent_sort_inode);
713 if (num_fs_entries == -1) {
714 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
715 path, strerror (errno));
719 for (i = 0; i < num_fs_entries && ! interrupted; i++) {
720 entry = fs_entries[i];
722 /* Ignore special directories to avoid infinite recursion.
723 * Also ignore the .notmuch directory and files/directories
724 * the user has configured to be ignored.
726 if (strcmp (entry->d_name, ".") == 0 ||
727 strcmp (entry->d_name, "..") == 0 ||
728 strcmp (entry->d_name, ".notmuch") == 0 ||
729 _entry_in_ignore_list (entry->d_name, state))
731 if (state->debug && _entry_in_ignore_list (entry->d_name, state))
732 printf ("(D) count_files: explicitly ignoring %s/%s\n",
738 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
740 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
741 path, entry->d_name);
745 entry_type = dirent_type (path, entry);
746 if (entry_type == S_IFREG) {
748 if (*count % 1000 == 0 && state->verbosity >= VERBOSITY_NORMAL) {
749 printf ("Found %d files so far.\r", *count);
752 } else if (entry_type == S_IFDIR) {
753 count_files (next, count, state);
761 for (i = 0; i < num_fs_entries; i++)
762 free (fs_entries[i]);
769 upgrade_print_progress (void *closure,
772 add_files_state_t *state = closure;
774 printf ("Upgrading database: %.2f%% complete", progress * 100.0);
777 struct timeval tv_now;
778 double elapsed, time_remaining;
780 gettimeofday (&tv_now, NULL);
782 elapsed = notmuch_time_elapsed (state->tv_start, tv_now);
783 time_remaining = (elapsed / progress) * (1.0 - progress);
785 notmuch_time_print_formatted_seconds (time_remaining);
786 printf (" remaining)");
794 /* Remove one message filename from the database. */
795 static notmuch_status_t
796 remove_filename (notmuch_database_t *notmuch,
798 add_files_state_t *add_files_state)
800 notmuch_status_t status;
801 notmuch_message_t *message;
802 status = notmuch_database_begin_atomic (notmuch);
805 status = notmuch_database_find_message_by_filename (notmuch, path, &message);
806 if (status || message == NULL)
809 status = notmuch_database_remove_message (notmuch, path);
810 if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
811 add_files_state->renamed_messages++;
812 if (add_files_state->synchronize_flags == TRUE)
813 notmuch_message_maildir_flags_to_tags (message);
814 status = NOTMUCH_STATUS_SUCCESS;
815 } else if (status == NOTMUCH_STATUS_SUCCESS) {
816 add_files_state->removed_messages++;
818 notmuch_message_destroy (message);
821 notmuch_database_end_atomic (notmuch);
825 /* Recursively remove all filenames from the database referring to
826 * 'path' (or to any of its children). */
827 static notmuch_status_t
828 _remove_directory (void *ctx,
829 notmuch_database_t *notmuch,
831 add_files_state_t *add_files_state)
833 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
834 notmuch_directory_t *directory;
835 notmuch_filenames_t *files, *subdirs;
838 status = notmuch_database_get_directory (notmuch, path, &directory);
839 if (status || !directory)
842 for (files = notmuch_directory_get_child_files (directory);
843 notmuch_filenames_valid (files);
844 notmuch_filenames_move_to_next (files))
846 absolute = talloc_asprintf (ctx, "%s/%s", path,
847 notmuch_filenames_get (files));
848 status = remove_filename (notmuch, absolute, add_files_state);
849 talloc_free (absolute);
854 for (subdirs = notmuch_directory_get_child_directories (directory);
855 notmuch_filenames_valid (subdirs);
856 notmuch_filenames_move_to_next (subdirs))
858 absolute = talloc_asprintf (ctx, "%s/%s", path,
859 notmuch_filenames_get (subdirs));
860 status = _remove_directory (ctx, notmuch, absolute, add_files_state);
861 talloc_free (absolute);
867 notmuch_directory_destroy (directory);
872 print_results (const add_files_state_t *state)
875 struct timeval tv_now;
877 gettimeofday (&tv_now, NULL);
878 elapsed = notmuch_time_elapsed (state->tv_start, tv_now);
880 if (state->processed_files) {
881 printf ("Processed %d %s in ", state->processed_files,
882 state->processed_files == 1 ? "file" : "total files");
883 notmuch_time_print_formatted_seconds (elapsed);
885 printf (" (%d files/sec.).\033[K\n",
886 (int) (state->processed_files / elapsed));
888 printf (".\033[K\n");
891 if (state->added_messages)
892 printf ("Added %d new %s to the database.", state->added_messages,
893 state->added_messages == 1 ? "message" : "messages");
895 printf ("No new mail.");
897 if (state->removed_messages)
898 printf (" Removed %d %s.", state->removed_messages,
899 state->removed_messages == 1 ? "message" : "messages");
901 if (state->renamed_messages)
902 printf (" Detected %d file %s.", state->renamed_messages,
903 state->renamed_messages == 1 ? "rename" : "renames");
909 notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
911 notmuch_database_t *notmuch;
912 add_files_state_t add_files_state;
913 struct timeval tv_start;
917 char *dot_notmuch_path;
918 struct sigaction action;
922 notmuch_bool_t timer_is_active = FALSE;
923 notmuch_bool_t no_hooks = FALSE;
924 notmuch_bool_t quiet = FALSE, verbose = FALSE;
926 add_files_state.verbosity = VERBOSITY_NORMAL;
927 add_files_state.debug = FALSE;
928 add_files_state.output_is_a_tty = isatty (fileno (stdout));
930 notmuch_opt_desc_t options[] = {
931 { NOTMUCH_OPT_BOOLEAN, &quiet, "quiet", 'q', 0 },
932 { NOTMUCH_OPT_BOOLEAN, &verbose, "verbose", 'v', 0 },
933 { NOTMUCH_OPT_BOOLEAN, &add_files_state.debug, "debug", 'd', 0 },
934 { NOTMUCH_OPT_BOOLEAN, &no_hooks, "no-hooks", 'n', 0 },
938 opt_index = parse_arguments (argc, argv, options, 1);
942 /* quiet trumps verbose */
944 add_files_state.verbosity = VERBOSITY_QUIET;
946 add_files_state.verbosity = VERBOSITY_VERBOSE;
948 add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);
949 add_files_state.new_ignore = notmuch_config_get_new_ignore (config, &add_files_state.new_ignore_length);
950 add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
951 db_path = notmuch_config_get_database_path (config);
954 ret = notmuch_run_hook (db_path, "pre-new");
959 dot_notmuch_path = talloc_asprintf (config, "%s/%s", db_path, ".notmuch");
961 if (stat (dot_notmuch_path, &st)) {
965 count_files (db_path, &count, &add_files_state);
969 if (add_files_state.verbosity >= VERBOSITY_NORMAL)
970 printf ("Found %d total files (that's not much mail).\n", count);
971 if (notmuch_database_create (db_path, ¬much))
973 add_files_state.total_files = count;
975 if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
979 if (notmuch_database_needs_upgrade (notmuch)) {
980 if (add_files_state.verbosity >= VERBOSITY_NORMAL)
981 printf ("Welcome to a new version of notmuch! Your database will now be upgraded.\n");
982 gettimeofday (&add_files_state.tv_start, NULL);
983 notmuch_database_upgrade (notmuch,
984 add_files_state.verbosity >= VERBOSITY_NORMAL ? upgrade_print_progress : NULL,
986 if (add_files_state.verbosity >= VERBOSITY_NORMAL)
987 printf ("Your notmuch database has now been upgraded to database format version %u.\n",
988 notmuch_database_get_version (notmuch));
991 add_files_state.total_files = 0;
997 /* Setup our handler for SIGINT. We do this after having
998 * potentially done a database upgrade we this interrupt handler
1000 memset (&action, 0, sizeof (struct sigaction));
1001 action.sa_handler = handle_sigint;
1002 sigemptyset (&action.sa_mask);
1003 action.sa_flags = SA_RESTART;
1004 sigaction (SIGINT, &action, NULL);
1006 talloc_free (dot_notmuch_path);
1007 dot_notmuch_path = NULL;
1009 add_files_state.processed_files = 0;
1010 add_files_state.added_messages = 0;
1011 add_files_state.removed_messages = add_files_state.renamed_messages = 0;
1012 gettimeofday (&add_files_state.tv_start, NULL);
1014 add_files_state.removed_files = _filename_list_create (config);
1015 add_files_state.removed_directories = _filename_list_create (config);
1016 add_files_state.directory_mtimes = _filename_list_create (config);
1018 if (add_files_state.verbosity == VERBOSITY_NORMAL &&
1019 add_files_state.output_is_a_tty && ! debugger_is_active ()) {
1020 setup_progress_printing_timer ();
1021 timer_is_active = TRUE;
1024 ret = add_files (notmuch, db_path, &add_files_state);
1028 gettimeofday (&tv_start, NULL);
1029 for (f = add_files_state.removed_files->head; f && !interrupted; f = f->next) {
1030 ret = remove_filename (notmuch, f->filename, &add_files_state);
1033 if (do_print_progress) {
1034 do_print_progress = 0;
1035 generic_print_progress ("Cleaned up", "messages",
1036 tv_start, add_files_state.removed_messages + add_files_state.renamed_messages,
1037 add_files_state.removed_files->count);
1041 gettimeofday (&tv_start, NULL);
1042 for (f = add_files_state.removed_directories->head, i = 0; f && !interrupted; f = f->next, i++) {
1043 ret = _remove_directory (config, notmuch, f->filename, &add_files_state);
1046 if (do_print_progress) {
1047 do_print_progress = 0;
1048 generic_print_progress ("Cleaned up", "directories",
1050 add_files_state.removed_directories->count);
1054 for (f = add_files_state.directory_mtimes->head; f && !interrupted; f = f->next) {
1055 notmuch_status_t status;
1056 notmuch_directory_t *directory;
1057 status = notmuch_database_get_directory (notmuch, f->filename, &directory);
1058 if (status == NOTMUCH_STATUS_SUCCESS && directory) {
1059 notmuch_directory_set_mtime (directory, f->mtime);
1060 notmuch_directory_destroy (directory);
1065 talloc_free (add_files_state.removed_files);
1066 talloc_free (add_files_state.removed_directories);
1067 talloc_free (add_files_state.directory_mtimes);
1069 if (timer_is_active)
1070 stop_progress_printing_timer ();
1072 if (add_files_state.verbosity >= VERBOSITY_NORMAL)
1073 print_results (&add_files_state);
1076 fprintf (stderr, "Note: A fatal error was encountered: %s\n",
1077 notmuch_status_to_string (ret));
1079 notmuch_database_destroy (notmuch);
1081 if (!no_hooks && !ret && !interrupted)
1082 ret = notmuch_run_hook (db_path, "post-new");
1084 return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;