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;
39 notmuch_bool_t verbose;
41 const char **new_tags;
42 size_t new_tags_length;
43 const char **new_ignore;
44 size_t new_ignore_length;
48 int added_messages, removed_messages, renamed_messages;
49 struct timeval tv_start;
51 _filename_list_t *removed_files;
52 _filename_list_t *removed_directories;
53 _filename_list_t *directory_mtimes;
55 notmuch_bool_t synchronize_flags;
58 static volatile sig_atomic_t do_print_progress = 0;
61 handle_sigalrm (unused (int signal))
63 do_print_progress = 1;
66 static volatile sig_atomic_t interrupted;
69 handle_sigint (unused (int sig))
71 static char msg[] = "Stopping... \n";
73 /* This write is "opportunistic", so it's okay to ignore the
74 * result. It is not required for correctness, and if it does
75 * fail or produce a short write, we want to get out of the signal
76 * handler as quickly as possible, not retry it. */
77 IGNORE_RESULT (write (2, msg, sizeof(msg)-1));
81 static _filename_list_t *
82 _filename_list_create (const void *ctx)
84 _filename_list_t *list;
86 list = talloc (ctx, _filename_list_t);
91 list->tail = &list->head;
97 static _filename_node_t *
98 _filename_list_add (_filename_list_t *list,
101 _filename_node_t *node = talloc (list, _filename_node_t);
105 node->filename = talloc_strdup (list, filename);
108 *(list->tail) = node;
109 list->tail = &node->next;
115 generic_print_progress (const char *action, const char *object,
116 struct timeval tv_start, unsigned processed, unsigned total)
118 struct timeval tv_now;
119 double elapsed_overall, rate_overall;
121 gettimeofday (&tv_now, NULL);
123 elapsed_overall = notmuch_time_elapsed (tv_start, tv_now);
124 rate_overall = processed / elapsed_overall;
126 printf ("%s %d ", action, processed);
129 printf ("of %d %s", total, object);
130 if (processed > 0 && elapsed_overall > 0.5) {
131 double time_remaining = ((total - processed) / rate_overall);
133 notmuch_time_print_formatted_seconds (time_remaining);
134 printf (" remaining)");
137 printf ("%s", object);
138 if (elapsed_overall > 0.5)
139 printf (" (%d %s/sec.)", (int) rate_overall, object);
141 printf (".\033[K\r");
147 dirent_sort_inode (const struct dirent **a, const struct dirent **b)
149 return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
153 dirent_sort_strcmp_name (const struct dirent **a, const struct dirent **b)
155 return strcmp ((*a)->d_name, (*b)->d_name);
158 /* Return the type of a directory entry relative to path as a stat(2)
159 * mode. Like stat, this follows symlinks. Returns -1 and sets errno
160 * if the file's type cannot be determined (which includes dangling
164 dirent_type (const char *path, const struct dirent *entry)
168 int err, saved_errno;
170 #ifdef _DIRENT_HAVE_D_TYPE
171 /* Mapping from d_type to stat mode_t. We omit DT_LNK so that
172 * we'll fall through to stat and get the real file type. */
173 static const mode_t modes[] = {
181 if (entry->d_type < ARRAY_SIZE(modes) && modes[entry->d_type])
182 return modes[entry->d_type];
185 abspath = talloc_asprintf (NULL, "%s/%s", path, entry->d_name);
190 err = stat(abspath, &statbuf);
192 talloc_free (abspath);
197 return statbuf.st_mode & S_IFMT;
200 /* Test if the directory looks like a Maildir directory.
202 * Search through the array of directory entries to see if we can find all
203 * three subdirectories typical for Maildir, that is "new", "cur", and "tmp".
205 * Return 1 if the directory looks like a Maildir and 0 otherwise.
208 _entries_resemble_maildir (const char *path, struct dirent **entries, int count)
212 for (i = 0; i < count; i++) {
213 if (dirent_type (path, entries[i]) != S_IFDIR)
216 if (strcmp(entries[i]->d_name, "new") == 0 ||
217 strcmp(entries[i]->d_name, "cur") == 0 ||
218 strcmp(entries[i]->d_name, "tmp") == 0)
229 /* Test if the file/directory is to be ignored.
231 static notmuch_bool_t
232 _entry_in_ignore_list (const char *entry, add_files_state_t *state)
236 for (i = 0; i < state->new_ignore_length; i++)
237 if (strcmp (entry, state->new_ignore[i]) == 0)
243 /* Examine 'path' recursively as follows:
245 * o Ask the filesystem for the mtime of 'path' (fs_mtime)
246 * o Ask the database for its timestamp of 'path' (db_mtime)
248 * o Ask the filesystem for files and directories within 'path'
249 * (via scandir and stored in fs_entries)
251 * o Pass 1: For each directory in fs_entries, recursively call into
252 * this same function.
254 * o Compare fs_mtime to db_mtime. If they are equivalent, terminate
255 * the algorithm at this point, (this directory has not been
256 * updated in the filesystem since the last database scan of PASS
259 * o Ask the database for files and directories within 'path'
260 * (db_files and db_subdirs)
262 * o Pass 2: Walk fs_entries simultaneously with db_files and
263 * db_subdirs. Look for one of three interesting cases:
265 * 1. Regular file in fs_entries and not in db_files
266 * This is a new file to add_message into the database.
268 * 2. Filename in db_files not in fs_entries.
269 * This is a file that has been removed from the mail store.
271 * 3. Directory in db_subdirs not in fs_entries
272 * This is a directory that has been removed from the mail store.
274 * Note that the addition of a directory is not interesting here,
275 * since that will have been taken care of in pass 1. Also, we
276 * don't immediately act on file/directory removal since we must
277 * ensure that in the case of a rename that the new filename is
278 * added before the old filename is removed, (so that no
279 * information is lost from the database).
281 * o Tell the database to update its time of 'path' to 'fs_mtime'
282 * if fs_mtime isn't the current wall-clock time.
284 static notmuch_status_t
285 add_files (notmuch_database_t *notmuch,
287 add_files_state_t *state)
290 struct dirent *entry = NULL;
292 time_t fs_mtime, db_mtime;
293 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
294 notmuch_message_t *message = NULL;
295 struct dirent **fs_entries = NULL;
296 int i, num_fs_entries = 0, entry_type;
297 notmuch_directory_t *directory;
298 notmuch_filenames_t *db_files = NULL;
299 notmuch_filenames_t *db_subdirs = NULL;
302 notmuch_bool_t is_maildir;
305 if (stat (path, &st)) {
306 fprintf (stderr, "Error reading directory %s: %s\n",
307 path, strerror (errno));
308 return NOTMUCH_STATUS_FILE_ERROR;
310 stat_time = time (NULL);
312 if (! S_ISDIR (st.st_mode)) {
313 fprintf (stderr, "Error: %s is not a directory.\n", path);
314 return NOTMUCH_STATUS_FILE_ERROR;
317 fs_mtime = st.st_mtime;
319 status = notmuch_database_get_directory (notmuch, path, &directory);
324 db_mtime = directory ? notmuch_directory_get_mtime (directory) : 0;
326 /* If the directory is unchanged from our last scan and has no
327 * sub-directories, then return without scanning it at all. In
328 * some situations, skipping the scan can substantially reduce the
329 * cost of notmuch new, especially since the huge numbers of files
330 * in Maildirs make scans expensive, but all files live in leaf
333 * To check for sub-directories, we borrow a trick from find,
334 * kpathsea, and many other UNIX tools: since a directory's link
335 * count is the number of sub-directories (specifically, their
336 * '..' entries) plus 2 (the link from the parent and the link for
337 * '.'). This check is safe even on weird file systems, since
338 * file systems that can't compute this will return 0 or 1. This
339 * is safe even on *really* weird file systems like HFS+ that
340 * mistakenly return the total number of directory entries, since
341 * that only inflates the count beyond 2.
343 if (directory && fs_mtime == db_mtime && st.st_nlink == 2) {
344 /* There's one catch: pass 1 below considers symlinks to
345 * directories to be directories, but these don't increase the
346 * file system link count. So, only bail early if the
347 * database agrees that there are no sub-directories. */
348 db_subdirs = notmuch_directory_get_child_directories (directory);
349 if (!notmuch_filenames_valid (db_subdirs))
351 notmuch_filenames_destroy (db_subdirs);
355 /* If the database knows about this directory, then we sort based
356 * on strcmp to match the database sorting. Otherwise, we can do
357 * inode-based sorting for faster filesystem operation. */
358 num_fs_entries = scandir (path, &fs_entries, 0,
360 dirent_sort_strcmp_name : dirent_sort_inode);
362 if (num_fs_entries == -1) {
363 fprintf (stderr, "Error opening directory %s: %s\n",
364 path, strerror (errno));
365 /* We consider this a fatal error because, if a user moved a
366 * message from another directory that we were able to scan
367 * into this directory, skipping this directory will cause
368 * that message to be lost. */
369 ret = NOTMUCH_STATUS_FILE_ERROR;
373 /* Pass 1: Recurse into all sub-directories. */
374 is_maildir = _entries_resemble_maildir (path, fs_entries, num_fs_entries);
376 for (i = 0; i < num_fs_entries; i++) {
380 entry = fs_entries[i];
382 /* Ignore any files/directories the user has configured to
383 * ignore. We do this before dirent_type both for performance
384 * and because we don't care if dirent_type fails on entries
385 * that are explicitly ignored.
387 if (_entry_in_ignore_list (entry->d_name, state)) {
389 printf ("(D) add_files_recursive, pass 1: explicitly ignoring %s/%s\n",
390 path, entry->d_name);
394 /* We only want to descend into directories (and symlinks to
396 entry_type = dirent_type (path, entry);
397 if (entry_type == -1) {
398 /* Be pessimistic, e.g. so we don't lose lots of mail just
399 * because a user broke a symlink. */
400 fprintf (stderr, "Error reading file %s/%s: %s\n",
401 path, entry->d_name, strerror (errno));
402 return NOTMUCH_STATUS_FILE_ERROR;
403 } else if (entry_type != S_IFDIR) {
407 /* Ignore special directories to avoid infinite recursion.
408 * Also ignore the .notmuch directory and any "tmp" directory
409 * that appears within a maildir.
411 if (strcmp (entry->d_name, ".") == 0 ||
412 strcmp (entry->d_name, "..") == 0 ||
413 (is_maildir && strcmp (entry->d_name, "tmp") == 0) ||
414 strcmp (entry->d_name, ".notmuch") == 0)
417 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
418 status = add_files (notmuch, next, state);
427 /* If the directory's modification time in the filesystem is the
428 * same as what we recorded in the database the last time we
429 * scanned it, then we can skip the second pass entirely.
431 * We test for strict equality here to avoid a bug that can happen
432 * if the system clock jumps backward, (preventing new mail from
433 * being discovered until the clock catches up and the directory
434 * is modified again).
436 if (directory && fs_mtime == db_mtime)
439 /* If the database has never seen this directory before, we can
440 * simply leave db_files and db_subdirs NULL. */
442 db_files = notmuch_directory_get_child_files (directory);
443 db_subdirs = notmuch_directory_get_child_directories (directory);
446 /* Pass 2: Scan for new files, removed files, and removed directories. */
447 for (i = 0; i < num_fs_entries; i++)
452 entry = fs_entries[i];
454 /* Ignore files & directories user has configured to be ignored */
455 if (_entry_in_ignore_list (entry->d_name, state)) {
457 printf ("(D) add_files_recursive, pass 2: explicitly ignoring %s/%s\n",
463 /* Check if we've walked past any names in db_files or
464 * db_subdirs. If so, these have been deleted. */
465 while (notmuch_filenames_valid (db_files) &&
466 strcmp (notmuch_filenames_get (db_files), entry->d_name) < 0)
468 char *absolute = talloc_asprintf (state->removed_files,
470 notmuch_filenames_get (db_files));
472 _filename_list_add (state->removed_files, absolute);
474 notmuch_filenames_move_to_next (db_files);
477 while (notmuch_filenames_valid (db_subdirs) &&
478 strcmp (notmuch_filenames_get (db_subdirs), entry->d_name) <= 0)
480 const char *filename = notmuch_filenames_get (db_subdirs);
482 if (strcmp (filename, entry->d_name) < 0)
484 char *absolute = talloc_asprintf (state->removed_directories,
485 "%s/%s", path, filename);
487 _filename_list_add (state->removed_directories, absolute);
490 notmuch_filenames_move_to_next (db_subdirs);
493 /* Only add regular files (and symlinks to regular files). */
494 entry_type = dirent_type (path, entry);
495 if (entry_type == -1) {
496 fprintf (stderr, "Error reading file %s/%s: %s\n",
497 path, entry->d_name, strerror (errno));
498 return NOTMUCH_STATUS_FILE_ERROR;
499 } else if (entry_type != S_IFREG) {
503 /* Don't add a file that we've added before. */
504 if (notmuch_filenames_valid (db_files) &&
505 strcmp (notmuch_filenames_get (db_files), entry->d_name) == 0)
507 notmuch_filenames_move_to_next (db_files);
511 /* We're now looking at a regular file that doesn't yet exist
512 * in the database, so add it. */
513 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
515 state->processed_files++;
517 if (state->verbose) {
518 if (state->output_is_a_tty)
522 state->processed_files,
526 putchar((state->output_is_a_tty) ? '\r' : '\n');
530 status = notmuch_database_begin_atomic (notmuch);
536 status = notmuch_database_add_message (notmuch, next, &message);
539 case NOTMUCH_STATUS_SUCCESS:
540 state->added_messages++;
541 notmuch_message_freeze (message);
542 for (tag=state->new_tags; *tag != NULL; tag++)
543 notmuch_message_add_tag (message, *tag);
544 if (state->synchronize_flags == TRUE)
545 notmuch_message_maildir_flags_to_tags (message);
546 notmuch_message_thaw (message);
548 /* Non-fatal issues (go on to next file) */
549 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
550 if (state->synchronize_flags == TRUE)
551 notmuch_message_maildir_flags_to_tags (message);
553 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
554 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
557 /* Fatal issues. Don't process anymore. */
558 case NOTMUCH_STATUS_READ_ONLY_DATABASE:
559 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
560 case NOTMUCH_STATUS_OUT_OF_MEMORY:
561 fprintf (stderr, "Error: %s. Halting processing.\n",
562 notmuch_status_to_string (status));
566 case NOTMUCH_STATUS_FILE_ERROR:
567 case NOTMUCH_STATUS_NULL_POINTER:
568 case NOTMUCH_STATUS_TAG_TOO_LONG:
569 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
570 case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
571 case NOTMUCH_STATUS_LAST_STATUS:
572 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
576 status = notmuch_database_end_atomic (notmuch);
583 notmuch_message_destroy (message);
587 if (do_print_progress) {
588 do_print_progress = 0;
589 generic_print_progress ("Processed", "files", state->tv_start,
590 state->processed_files, state->total_files);
600 /* Now that we've walked the whole filesystem list, anything left
601 * over in the database lists has been deleted. */
602 while (notmuch_filenames_valid (db_files))
604 char *absolute = talloc_asprintf (state->removed_files,
606 notmuch_filenames_get (db_files));
608 _filename_list_add (state->removed_files, absolute);
610 notmuch_filenames_move_to_next (db_files);
613 while (notmuch_filenames_valid (db_subdirs))
615 char *absolute = talloc_asprintf (state->removed_directories,
617 notmuch_filenames_get (db_subdirs));
619 _filename_list_add (state->removed_directories, absolute);
621 notmuch_filenames_move_to_next (db_subdirs);
624 /* If the directory's mtime is the same as the wall-clock time
625 * when we stat'ed the directory, we skip updating the mtime in
626 * the database because a message could be delivered later in this
627 * same second. This may lead to unnecessary re-scans, but it
628 * avoids overlooking messages. */
629 if (fs_mtime != stat_time)
630 _filename_list_add (state->directory_mtimes, path)->mtime = fs_mtime;
638 for (i = 0; i < num_fs_entries; i++)
639 free (fs_entries[i]);
644 notmuch_filenames_destroy (db_subdirs);
646 notmuch_filenames_destroy (db_files);
648 notmuch_directory_destroy (directory);
654 setup_progress_printing_timer (void)
656 struct sigaction action;
657 struct itimerval timerval;
659 /* Setup our handler for SIGALRM */
660 memset (&action, 0, sizeof (struct sigaction));
661 action.sa_handler = handle_sigalrm;
662 sigemptyset (&action.sa_mask);
663 action.sa_flags = SA_RESTART;
664 sigaction (SIGALRM, &action, NULL);
666 /* Then start a timer to send SIGALRM once per second. */
667 timerval.it_interval.tv_sec = 1;
668 timerval.it_interval.tv_usec = 0;
669 timerval.it_value.tv_sec = 1;
670 timerval.it_value.tv_usec = 0;
671 setitimer (ITIMER_REAL, &timerval, NULL);
675 stop_progress_printing_timer (void)
677 struct sigaction action;
678 struct itimerval timerval;
680 /* Now stop the timer. */
681 timerval.it_interval.tv_sec = 0;
682 timerval.it_interval.tv_usec = 0;
683 timerval.it_value.tv_sec = 0;
684 timerval.it_value.tv_usec = 0;
685 setitimer (ITIMER_REAL, &timerval, NULL);
687 /* And disable the signal handler. */
688 action.sa_handler = SIG_IGN;
689 sigaction (SIGALRM, &action, NULL);
693 /* XXX: This should be merged with the add_files function since it
694 * shares a lot of logic with it. */
695 /* Recursively count all regular files in path and all sub-directories
696 * of path. The result is added to *count (which should be
697 * initialized to zero by the top-level caller before calling
700 count_files (const char *path, int *count, add_files_state_t *state)
702 struct dirent *entry = NULL;
705 struct dirent **fs_entries = NULL;
706 int num_fs_entries = scandir (path, &fs_entries, 0, dirent_sort_inode);
709 if (num_fs_entries == -1) {
710 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
711 path, strerror (errno));
715 while (!interrupted) {
716 if (i == num_fs_entries)
719 entry = fs_entries[i++];
721 /* Ignore special directories to avoid infinite recursion.
722 * Also ignore the .notmuch directory and files/directories
723 * the user has configured to be ignored.
725 if (strcmp (entry->d_name, ".") == 0 ||
726 strcmp (entry->d_name, "..") == 0 ||
727 strcmp (entry->d_name, ".notmuch") == 0 ||
728 _entry_in_ignore_list (entry->d_name, state))
730 if (_entry_in_ignore_list (entry->d_name, state) && state->debug)
731 printf ("(D) count_files: explicitly ignoring %s/%s\n",
737 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
739 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
740 path, entry->d_name);
746 if (S_ISREG (st.st_mode)) {
748 if (*count % 1000 == 0) {
749 printf ("Found %d files so far.\r", *count);
752 } else if (S_ISDIR (st.st_mode)) {
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 notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
874 notmuch_database_t *notmuch;
875 add_files_state_t add_files_state;
877 struct timeval tv_now, tv_start;
881 char *dot_notmuch_path;
882 struct sigaction action;
886 notmuch_bool_t timer_is_active = FALSE;
887 notmuch_bool_t no_hooks = FALSE;
889 add_files_state.verbose = FALSE;
890 add_files_state.debug = FALSE;
891 add_files_state.output_is_a_tty = isatty (fileno (stdout));
893 notmuch_opt_desc_t options[] = {
894 { NOTMUCH_OPT_BOOLEAN, &add_files_state.verbose, "verbose", 'v', 0 },
895 { NOTMUCH_OPT_BOOLEAN, &add_files_state.debug, "debug", 'd', 0 },
896 { NOTMUCH_OPT_BOOLEAN, &no_hooks, "no-hooks", 'n', 0 },
900 opt_index = parse_arguments (argc, argv, options, 1);
902 /* diagnostics already printed */
906 add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);
907 add_files_state.new_ignore = notmuch_config_get_new_ignore (config, &add_files_state.new_ignore_length);
908 add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
909 db_path = notmuch_config_get_database_path (config);
912 ret = notmuch_run_hook (db_path, "pre-new");
917 dot_notmuch_path = talloc_asprintf (config, "%s/%s", db_path, ".notmuch");
919 if (stat (dot_notmuch_path, &st)) {
923 count_files (db_path, &count, &add_files_state);
927 printf ("Found %d total files (that's not much mail).\n", count);
928 if (notmuch_database_create (db_path, ¬much))
930 add_files_state.total_files = count;
932 if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
936 if (notmuch_database_needs_upgrade (notmuch)) {
937 printf ("Welcome to a new version of notmuch! Your database will now be upgraded.\n");
938 gettimeofday (&add_files_state.tv_start, NULL);
939 notmuch_database_upgrade (notmuch, upgrade_print_progress,
941 printf ("Your notmuch database has now been upgraded to database format version %u.\n",
942 notmuch_database_get_version (notmuch));
945 add_files_state.total_files = 0;
951 /* Setup our handler for SIGINT. We do this after having
952 * potentially done a database upgrade we this interrupt handler
954 memset (&action, 0, sizeof (struct sigaction));
955 action.sa_handler = handle_sigint;
956 sigemptyset (&action.sa_mask);
957 action.sa_flags = SA_RESTART;
958 sigaction (SIGINT, &action, NULL);
960 talloc_free (dot_notmuch_path);
961 dot_notmuch_path = NULL;
963 add_files_state.processed_files = 0;
964 add_files_state.added_messages = 0;
965 add_files_state.removed_messages = add_files_state.renamed_messages = 0;
966 gettimeofday (&add_files_state.tv_start, NULL);
968 add_files_state.removed_files = _filename_list_create (config);
969 add_files_state.removed_directories = _filename_list_create (config);
970 add_files_state.directory_mtimes = _filename_list_create (config);
972 if (! debugger_is_active () && add_files_state.output_is_a_tty
973 && ! add_files_state.verbose) {
974 setup_progress_printing_timer ();
975 timer_is_active = TRUE;
978 ret = add_files (notmuch, db_path, &add_files_state);
982 gettimeofday (&tv_start, NULL);
983 for (f = add_files_state.removed_files->head; f && !interrupted; f = f->next) {
984 ret = remove_filename (notmuch, f->filename, &add_files_state);
987 if (do_print_progress) {
988 do_print_progress = 0;
989 generic_print_progress ("Cleaned up", "messages",
990 tv_start, add_files_state.removed_messages + add_files_state.renamed_messages,
991 add_files_state.removed_files->count);
995 gettimeofday (&tv_start, NULL);
996 for (f = add_files_state.removed_directories->head, i = 0; f && !interrupted; f = f->next, i++) {
997 ret = _remove_directory (config, notmuch, f->filename, &add_files_state);
1000 if (do_print_progress) {
1001 do_print_progress = 0;
1002 generic_print_progress ("Cleaned up", "directories",
1004 add_files_state.removed_directories->count);
1008 for (f = add_files_state.directory_mtimes->head; f && !interrupted; f = f->next) {
1009 notmuch_status_t status;
1010 notmuch_directory_t *directory;
1011 status = notmuch_database_get_directory (notmuch, f->filename, &directory);
1012 if (status == NOTMUCH_STATUS_SUCCESS && directory) {
1013 notmuch_directory_set_mtime (directory, f->mtime);
1014 notmuch_directory_destroy (directory);
1019 talloc_free (add_files_state.removed_files);
1020 talloc_free (add_files_state.removed_directories);
1021 talloc_free (add_files_state.directory_mtimes);
1023 if (timer_is_active)
1024 stop_progress_printing_timer ();
1026 gettimeofday (&tv_now, NULL);
1027 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
1030 if (add_files_state.processed_files) {
1031 printf ("Processed %d %s in ", add_files_state.processed_files,
1032 add_files_state.processed_files == 1 ?
1033 "file" : "total files");
1034 notmuch_time_print_formatted_seconds (elapsed);
1036 printf (" (%d files/sec.).\033[K\n",
1037 (int) (add_files_state.processed_files / elapsed));
1039 printf (".\033[K\n");
1043 if (add_files_state.added_messages) {
1044 printf ("Added %d new %s to the database.",
1045 add_files_state.added_messages,
1046 add_files_state.added_messages == 1 ?
1047 "message" : "messages");
1049 printf ("No new mail.");
1052 if (add_files_state.removed_messages) {
1053 printf (" Removed %d %s.",
1054 add_files_state.removed_messages,
1055 add_files_state.removed_messages == 1 ? "message" : "messages");
1058 if (add_files_state.renamed_messages) {
1059 printf (" Detected %d file %s.",
1060 add_files_state.renamed_messages,
1061 add_files_state.renamed_messages == 1 ? "rename" : "renames");
1067 fprintf (stderr, "Note: A fatal error was encountered: %s\n",
1068 notmuch_status_to_string (ret));
1070 notmuch_database_destroy (notmuch);
1072 if (!no_hooks && !ret && !interrupted)
1073 ret = notmuch_run_hook (db_path, "post-new");
1075 return ret || interrupted;