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 static volatile sig_atomic_t do_add_files_print_progress = 0;
28 handle_sigalrm (unused (int signal))
30 do_add_files_print_progress = 1;
33 static volatile sig_atomic_t interrupted;
36 handle_sigint (unused (int sig))
39 static char msg[] = "Stopping... \n";
41 ignored = write(2, msg, sizeof(msg)-1);
46 tag_inbox_and_unread (notmuch_message_t *message)
48 notmuch_message_add_tag (message, "inbox");
49 notmuch_message_add_tag (message, "unread");
53 add_files_print_progress (add_files_state_t *state)
55 struct timeval tv_now;
56 double elapsed_overall, rate_overall;
58 gettimeofday (&tv_now, NULL);
60 elapsed_overall = notmuch_time_elapsed (state->tv_start, tv_now);
61 rate_overall = (state->processed_files) / elapsed_overall;
63 printf ("Processed %d", state->processed_files);
65 if (state->total_files) {
66 double time_remaining;
68 time_remaining = ((state->total_files - state->processed_files) /
70 printf (" of %d files (", state->total_files);
71 notmuch_time_print_formatted_seconds (time_remaining);
72 printf (" remaining). \r");
74 printf (" files (%d files/sec.) \r", (int) rate_overall);
80 static int ino_cmp(const struct dirent **a, const struct dirent **b)
82 return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
85 /* Test if the directory looks like a Maildir directory.
87 * Search through the array of directory entries to see if we can find all
88 * three subdirectories typical for Maildir, that is "new", "cur", and "tmp".
90 * Return 1 if the directory looks like a Maildir and 0 otherwise.
93 is_maildir (struct dirent **entries, int count)
97 for (i = 0; i < count; i++) {
98 if (entries[i]->d_type != DT_DIR) continue;
99 if (strcmp(entries[i]->d_name, "new") == 0 ||
100 strcmp(entries[i]->d_name, "cur") == 0 ||
101 strcmp(entries[i]->d_name, "tmp") == 0)
112 /* Examine 'path' recursively as follows:
114 * o Ask the filesystem for the mtime of 'path' (path_mtime)
116 * o Ask the database for its timestamp of 'path' (path_dbtime)
118 * o If 'path_mtime' > 'path_dbtime'
120 * o For each regular file in 'path' with mtime newer than the
121 * 'path_dbtime' call add_message to add the file to the
124 * o For each sub-directory of path, recursively call into this
127 * o Tell the database to update its time of 'path' to 'path_mtime'
129 * The 'struct stat *st' must point to a structure that has already
130 * been initialized for 'path' by calling stat().
132 static notmuch_status_t
133 add_files_recursive (notmuch_database_t *notmuch,
136 add_files_state_t *state)
139 struct dirent *entry = NULL;
141 time_t path_mtime, path_dbtime;
142 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
143 notmuch_message_t *message = NULL;
144 struct dirent **namelist = NULL;
147 /* If we're told to, we bail out on encountering a read-only
148 * directory, (with this being a clear clue from the user to
149 * Notmuch that new mail won't be arriving there and we need not
151 if (state->ignore_read_only_directories &&
152 (st->st_mode & S_IWUSR) == 0)
154 state->saw_read_only_directory = TRUE;
158 path_mtime = st->st_mtime;
160 path_dbtime = notmuch_database_get_timestamp (notmuch, path);
161 num_entries = scandir (path, &namelist, 0, ino_cmp);
163 if (num_entries == -1) {
164 fprintf (stderr, "Error opening directory %s: %s\n",
165 path, strerror (errno));
166 ret = NOTMUCH_STATUS_FILE_ERROR;
172 while (!interrupted) {
173 if (i == num_entries)
176 entry= namelist[i++];
178 /* If this directory hasn't been modified since the last
179 * add_files, then we only need to look further for
180 * sub-directories. */
181 if (path_mtime <= path_dbtime && entry->d_type == DT_REG)
184 /* Ignore special directories to avoid infinite recursion.
185 * Also ignore the .notmuch directory.
187 /* XXX: Eventually we'll want more sophistication to let the
188 * user specify files to be ignored. */
189 if (strcmp (entry->d_name, ".") == 0 ||
190 strcmp (entry->d_name, "..") == 0 ||
191 (entry->d_type == DT_DIR &&
192 (strcmp (entry->d_name, "tmp") == 0) &&
193 is_maildir (namelist, num_entries)) ||
194 strcmp (entry->d_name, ".notmuch") ==0)
199 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
201 if (stat (next, st)) {
206 /* The file was removed between scandir and now... */
209 /* We can't read this file so don't add it to the cache. */
213 fprintf (stderr, "Error reading %s: %s\n",
214 next, strerror (errno));
215 ret = NOTMUCH_STATUS_FILE_ERROR;
219 if (S_ISREG (st->st_mode)) {
220 /* If the file hasn't been modified since the last
221 * add_files, then we need not look at it. */
222 if (path_dbtime == 0 || st->st_mtime > path_dbtime) {
223 state->processed_files++;
225 if (state->verbose) {
226 if (state->output_is_a_tty)
230 state->processed_files,
234 putchar((state->output_is_a_tty) ? '\r' : '\n');
238 status = notmuch_database_add_message (notmuch, next, &message);
241 case NOTMUCH_STATUS_SUCCESS:
242 state->added_messages++;
243 tag_inbox_and_unread (message);
245 /* Non-fatal issues (go on to next file) */
246 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
247 /* Stay silent on this one. */
249 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
250 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
253 /* Fatal issues. Don't process anymore. */
254 case NOTMUCH_STATUS_READONLY_DATABASE:
255 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
256 case NOTMUCH_STATUS_OUT_OF_MEMORY:
257 fprintf (stderr, "Error: %s. Halting processing.\n",
258 notmuch_status_to_string (status));
262 case NOTMUCH_STATUS_FILE_ERROR:
263 case NOTMUCH_STATUS_NULL_POINTER:
264 case NOTMUCH_STATUS_TAG_TOO_LONG:
265 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
266 case NOTMUCH_STATUS_LAST_STATUS:
267 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
272 notmuch_message_destroy (message);
276 if (do_add_files_print_progress) {
277 do_add_files_print_progress = 0;
278 add_files_print_progress (state);
281 } else if (S_ISDIR (st->st_mode)) {
282 status = add_files_recursive (notmuch, next, st, state);
283 if (status && ret == NOTMUCH_STATUS_SUCCESS)
291 status = notmuch_database_set_timestamp (notmuch, path, path_mtime);
292 if (status && ret == NOTMUCH_STATUS_SUCCESS)
308 /* This is the top-level entry point for add_files. It does a couple
309 * of error checks, sets up the progress-printing timer and then calls
310 * into the recursive function. */
312 add_files (notmuch_database_t *notmuch,
314 add_files_state_t *state)
317 notmuch_status_t status;
318 struct sigaction action;
319 struct itimerval timerval;
320 notmuch_bool_t timer_is_active = FALSE;
322 if (stat (path, &st)) {
323 fprintf (stderr, "Error reading directory %s: %s\n",
324 path, strerror (errno));
325 return NOTMUCH_STATUS_FILE_ERROR;
328 if (! S_ISDIR (st.st_mode)) {
329 fprintf (stderr, "Error: %s is not a directory.\n", path);
330 return NOTMUCH_STATUS_FILE_ERROR;
333 if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {
334 /* Setup our handler for SIGALRM */
335 memset (&action, 0, sizeof (struct sigaction));
336 action.sa_handler = handle_sigalrm;
337 sigemptyset (&action.sa_mask);
338 action.sa_flags = SA_RESTART;
339 sigaction (SIGALRM, &action, NULL);
341 /* Then start a timer to send SIGALRM once per second. */
342 timerval.it_interval.tv_sec = 1;
343 timerval.it_interval.tv_usec = 0;
344 timerval.it_value.tv_sec = 1;
345 timerval.it_value.tv_usec = 0;
346 setitimer (ITIMER_REAL, &timerval, NULL);
348 timer_is_active = TRUE;
351 status = add_files_recursive (notmuch, path, &st, state);
353 if (timer_is_active) {
354 /* Now stop the timer. */
355 timerval.it_interval.tv_sec = 0;
356 timerval.it_interval.tv_usec = 0;
357 timerval.it_value.tv_sec = 0;
358 timerval.it_value.tv_usec = 0;
359 setitimer (ITIMER_REAL, &timerval, NULL);
361 /* And disable the signal handler. */
362 action.sa_handler = SIG_IGN;
363 sigaction (SIGALRM, &action, NULL);
369 /* XXX: This should be merged with the add_files function since it
370 * shares a lot of logic with it. */
371 /* Recursively count all regular files in path and all sub-directories
372 * of path. The result is added to *count (which should be
373 * initialized to zero by the top-level caller before calling
376 count_files (const char *path, int *count)
378 struct dirent *entry = NULL;
381 struct dirent **namelist = NULL;
382 int n_entries = scandir (path, &namelist, 0, ino_cmp);
385 if (n_entries == -1) {
386 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
387 path, strerror (errno));
391 while (!interrupted) {
395 entry= namelist[i++];
397 /* Ignore special directories to avoid infinite recursion.
398 * Also ignore the .notmuch directory.
400 /* XXX: Eventually we'll want more sophistication to let the
401 * user specify files to be ignored. */
402 if (strcmp (entry->d_name, ".") == 0 ||
403 strcmp (entry->d_name, "..") == 0 ||
404 strcmp (entry->d_name, ".notmuch") == 0)
409 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
411 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
412 path, entry->d_name);
418 if (S_ISREG (st.st_mode)) {
420 if (*count % 1000 == 0) {
421 printf ("Found %d files so far.\r", *count);
424 } else if (S_ISDIR (st.st_mode)) {
425 count_files (next, count);
439 notmuch_new_command (void *ctx, int argc, char *argv[])
441 notmuch_config_t *config;
442 notmuch_database_t *notmuch;
443 add_files_state_t add_files_state;
445 struct timeval tv_now;
449 char *dot_notmuch_path;
450 struct sigaction action;
453 add_files_state.verbose = 0;
454 add_files_state.output_is_a_tty = isatty (fileno (stdout));
456 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
457 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
458 add_files_state.verbose = 1;
460 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
465 /* Setup our handler for SIGINT */
466 memset (&action, 0, sizeof (struct sigaction));
467 action.sa_handler = handle_sigint;
468 sigemptyset (&action.sa_mask);
469 action.sa_flags = SA_RESTART;
470 sigaction (SIGINT, &action, NULL);
472 config = notmuch_config_open (ctx, NULL, NULL);
476 db_path = notmuch_config_get_database_path (config);
478 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
480 if (stat (dot_notmuch_path, &st)) {
484 count_files (db_path, &count);
488 printf ("Found %d total files. \n", count);
489 notmuch = notmuch_database_create (db_path);
490 add_files_state.ignore_read_only_directories = FALSE;
491 add_files_state.total_files = count;
493 notmuch = notmuch_database_open (db_path,
494 NOTMUCH_DATABASE_MODE_READ_WRITE);
495 add_files_state.ignore_read_only_directories = TRUE;
496 add_files_state.total_files = 0;
502 talloc_free (dot_notmuch_path);
503 dot_notmuch_path = NULL;
505 add_files_state.saw_read_only_directory = FALSE;
506 add_files_state.processed_files = 0;
507 add_files_state.added_messages = 0;
508 gettimeofday (&add_files_state.tv_start, NULL);
510 ret = add_files (notmuch, db_path, &add_files_state);
512 gettimeofday (&tv_now, NULL);
513 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
515 if (add_files_state.processed_files) {
516 printf ("Processed %d %s in ", add_files_state.processed_files,
517 add_files_state.processed_files == 1 ?
518 "file" : "total files");
519 notmuch_time_print_formatted_seconds (elapsed);
521 printf (" (%d files/sec.). \n",
522 (int) (add_files_state.processed_files / elapsed));
527 if (add_files_state.added_messages) {
528 printf ("Added %d new %s to the database (not much, really).\n",
529 add_files_state.added_messages,
530 add_files_state.added_messages == 1 ?
531 "message" : "messages");
533 printf ("No new mail---and that's not much.\n");
536 if (elapsed > 1 && ! add_files_state.saw_read_only_directory) {
537 printf ("\nTip: If you have any sub-directories that are archives (that is,\n"
538 "they will never receive new mail), marking these directories as\n"
539 "read-only (chmod u-w /path/to/dir) will make \"notmuch new\"\n"
540 "much more efficient (it won't even look in those directories).\n");
544 printf ("\nNote: At least one error was encountered: %s\n",
545 notmuch_status_to_string (ret));
548 notmuch_database_close (notmuch);
550 return ret || interrupted;