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))
38 static char msg[] = "Stopping... \n";
39 write(2, msg, sizeof(msg)-1);
44 tag_inbox_and_unread (notmuch_message_t *message)
46 notmuch_message_add_tag (message, "inbox");
47 notmuch_message_add_tag (message, "unread");
51 add_files_print_progress (add_files_state_t *state)
53 struct timeval tv_now;
54 double elapsed_overall, rate_overall;
56 gettimeofday (&tv_now, NULL);
58 elapsed_overall = notmuch_time_elapsed (state->tv_start, tv_now);
59 rate_overall = (state->processed_files) / elapsed_overall;
61 printf ("Processed %d", state->processed_files);
63 if (state->total_files) {
64 double time_remaining;
66 time_remaining = ((state->total_files - state->processed_files) /
68 printf (" of %d files (", state->total_files);
69 notmuch_time_print_formatted_seconds (time_remaining);
70 printf (" remaining). \r");
72 printf (" files (%d files/sec.) \r", (int) rate_overall);
78 static int ino_cmp(const struct dirent **a, const struct dirent **b)
80 return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
83 /* Test if the directory looks like a Maildir directory.
85 * Search through the array of directory entries to see if we can find all
86 * three subdirectories typical for Maildir, that is "new", "cur", and "tmp".
88 * Return 1 if the directory looks like a Maildir and 0 otherwise.
90 static int is_maildir (struct dirent **entries, int count)
94 for (i = 0; i < count; i++) {
95 if (entries[i]->d_type != DT_DIR) continue;
96 if (strcmp(entries[i]->d_name, "new") == 0 ||
97 strcmp(entries[i]->d_name, "cur") == 0 ||
98 strcmp(entries[i]->d_name, "tmp") == 0)
101 if (found == 3) return 1;
108 /* Examine 'path' recursively as follows:
110 * o Ask the filesystem for the mtime of 'path' (path_mtime)
112 * o Ask the database for its timestamp of 'path' (path_dbtime)
114 * o If 'path_mtime' > 'path_dbtime'
116 * o For each regular file in 'path' with mtime newer than the
117 * 'path_dbtime' call add_message to add the file to the
120 * o For each sub-directory of path, recursively call into this
123 * o Tell the database to update its time of 'path' to 'path_mtime'
125 * The 'struct stat *st' must point to a structure that has already
126 * been initialized for 'path' by calling stat().
128 static notmuch_status_t
129 add_files_recursive (notmuch_database_t *notmuch,
132 add_files_state_t *state)
135 struct dirent *entry = NULL;
137 time_t path_mtime, path_dbtime;
138 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
139 notmuch_message_t *message = NULL;
140 struct dirent **namelist = NULL;
143 /* If we're told to, we bail out on encountering a read-only
144 * directory, (with this being a clear clue from the user to
145 * Notmuch that new mail won't be arriving there and we need not
147 if (state->ignore_read_only_directories &&
148 (st->st_mode & S_IWUSR) == 0)
150 state->saw_read_only_directory = TRUE;
154 path_mtime = st->st_mtime;
156 path_dbtime = notmuch_database_get_timestamp (notmuch, path);
157 num_entries = scandir (path, &namelist, 0, ino_cmp);
159 if (num_entries == -1) {
160 fprintf (stderr, "Error opening directory %s: %s\n",
161 path, strerror (errno));
162 ret = NOTMUCH_STATUS_FILE_ERROR;
168 while (!interrupted) {
169 if (i == num_entries)
172 entry= namelist[i++];
174 /* If this directory hasn't been modified since the last
175 * add_files, then we only need to look further for
176 * sub-directories. */
177 if (path_mtime <= path_dbtime && entry->d_type == DT_REG)
180 /* Ignore special directories to avoid infinite recursion.
181 * Also ignore the .notmuch directory.
183 /* XXX: Eventually we'll want more sophistication to let the
184 * user specify files to be ignored. */
185 if (strcmp (entry->d_name, ".") == 0 ||
186 strcmp (entry->d_name, "..") == 0 ||
187 (entry->d_type == DT_DIR &&
188 (strcmp (entry->d_name, "tmp") == 0) &&
189 is_maildir (namelist, num_entries)) ||
190 strcmp (entry->d_name, ".notmuch") ==0)
195 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
197 if (stat (next, st)) {
198 fprintf (stderr, "Error reading %s: %s\n",
199 next, strerror (errno));
200 ret = NOTMUCH_STATUS_FILE_ERROR;
204 if (S_ISREG (st->st_mode)) {
205 /* If the file hasn't been modified since the last
206 * add_files, then we need not look at it. */
207 if (path_dbtime == 0 || st->st_mtime > path_dbtime) {
208 state->processed_files++;
210 if (state->verbose) {
211 if (state->output_is_a_tty)
215 state->processed_files,
219 putchar((state->output_is_a_tty) ? '\r' : '\n');
223 status = notmuch_database_add_message (notmuch, next, &message);
226 case NOTMUCH_STATUS_SUCCESS:
227 state->added_messages++;
228 tag_inbox_and_unread (message);
230 /* Non-fatal issues (go on to next file) */
231 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
232 /* Stay silent on this one. */
234 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
235 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
238 /* Fatal issues. Don't process anymore. */
239 case NOTMUCH_STATUS_READONLY_DATABASE:
240 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
241 case NOTMUCH_STATUS_OUT_OF_MEMORY:
242 fprintf (stderr, "Error: %s. Halting processing.\n",
243 notmuch_status_to_string (status));
247 case NOTMUCH_STATUS_FILE_ERROR:
248 case NOTMUCH_STATUS_NULL_POINTER:
249 case NOTMUCH_STATUS_TAG_TOO_LONG:
250 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
251 case NOTMUCH_STATUS_LAST_STATUS:
252 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
257 notmuch_message_destroy (message);
261 if (do_add_files_print_progress) {
262 do_add_files_print_progress = 0;
263 add_files_print_progress (state);
266 } else if (S_ISDIR (st->st_mode)) {
267 status = add_files_recursive (notmuch, next, st, state);
268 if (status && ret == NOTMUCH_STATUS_SUCCESS)
276 status = notmuch_database_set_timestamp (notmuch, path, path_mtime);
277 if (status && ret == NOTMUCH_STATUS_SUCCESS)
293 /* This is the top-level entry point for add_files. It does a couple
294 * of error checks, sets up the progress-printing timer and then calls
295 * into the recursive function. */
297 add_files (notmuch_database_t *notmuch,
299 add_files_state_t *state)
302 notmuch_status_t status;
303 struct sigaction action;
304 struct itimerval timerval;
305 notmuch_bool_t timer_is_active = FALSE;
307 if (stat (path, &st)) {
308 fprintf (stderr, "Error reading directory %s: %s\n",
309 path, strerror (errno));
310 return NOTMUCH_STATUS_FILE_ERROR;
313 if (! S_ISDIR (st.st_mode)) {
314 fprintf (stderr, "Error: %s is not a directory.\n", path);
315 return NOTMUCH_STATUS_FILE_ERROR;
318 if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {
319 /* Setup our handler for SIGALRM */
320 memset (&action, 0, sizeof (struct sigaction));
321 action.sa_handler = handle_sigalrm;
322 sigemptyset (&action.sa_mask);
323 action.sa_flags = SA_RESTART;
324 sigaction (SIGALRM, &action, NULL);
326 /* Then start a timer to send SIGALRM once per second. */
327 timerval.it_interval.tv_sec = 1;
328 timerval.it_interval.tv_usec = 0;
329 timerval.it_value.tv_sec = 1;
330 timerval.it_value.tv_usec = 0;
331 setitimer (ITIMER_REAL, &timerval, NULL);
333 timer_is_active = TRUE;
336 status = add_files_recursive (notmuch, path, &st, state);
338 if (timer_is_active) {
339 /* Now stop the timer. */
340 timerval.it_interval.tv_sec = 0;
341 timerval.it_interval.tv_usec = 0;
342 timerval.it_value.tv_sec = 0;
343 timerval.it_value.tv_usec = 0;
344 setitimer (ITIMER_REAL, &timerval, NULL);
346 /* And disable the signal handler. */
347 action.sa_handler = SIG_IGN;
348 sigaction (SIGALRM, &action, NULL);
354 /* XXX: This should be merged with the add_files function since it
355 * shares a lot of logic with it. */
356 /* Recursively count all regular files in path and all sub-directories
357 * of path. The result is added to *count (which should be
358 * initialized to zero by the top-level caller before calling
361 count_files (const char *path, int *count)
363 struct dirent *entry = NULL;
366 struct dirent **namelist = NULL;
367 int n_entries = scandir (path, &namelist, 0, ino_cmp);
370 if (n_entries == -1) {
371 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
372 path, strerror (errno));
376 while (!interrupted) {
380 entry= namelist[i++];
382 /* Ignore special directories to avoid infinite recursion.
383 * Also ignore the .notmuch directory.
385 /* XXX: Eventually we'll want more sophistication to let the
386 * user specify files to be ignored. */
387 if (strcmp (entry->d_name, ".") == 0 ||
388 strcmp (entry->d_name, "..") == 0 ||
389 strcmp (entry->d_name, ".notmuch") == 0)
394 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
396 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
397 path, entry->d_name);
403 if (S_ISREG (st.st_mode)) {
405 if (*count % 1000 == 0) {
406 printf ("Found %d files so far.\r", *count);
409 } else if (S_ISDIR (st.st_mode)) {
410 count_files (next, count);
424 notmuch_new_command (void *ctx, int argc, char *argv[])
426 notmuch_config_t *config;
427 notmuch_database_t *notmuch;
428 add_files_state_t add_files_state;
430 struct timeval tv_now;
434 char *dot_notmuch_path;
435 struct sigaction action;
438 add_files_state.verbose = 0;
439 add_files_state.output_is_a_tty = isatty (fileno (stdout));
441 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
442 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
443 add_files_state.verbose = 1;
445 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
450 /* Setup our handler for SIGINT */
451 memset (&action, 0, sizeof (struct sigaction));
452 action.sa_handler = handle_sigint;
453 sigemptyset (&action.sa_mask);
454 action.sa_flags = SA_RESTART;
455 sigaction (SIGINT, &action, NULL);
457 config = notmuch_config_open (ctx, NULL, NULL);
461 db_path = notmuch_config_get_database_path (config);
463 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
465 if (stat (dot_notmuch_path, &st)) {
469 count_files (db_path, &count);
473 printf ("Found %d total files. \n", count);
474 notmuch = notmuch_database_create (db_path);
475 add_files_state.ignore_read_only_directories = FALSE;
476 add_files_state.total_files = count;
478 notmuch = notmuch_database_open (db_path,
479 NOTMUCH_DATABASE_MODE_READ_WRITE);
480 add_files_state.ignore_read_only_directories = TRUE;
481 add_files_state.total_files = 0;
487 talloc_free (dot_notmuch_path);
488 dot_notmuch_path = NULL;
490 add_files_state.saw_read_only_directory = FALSE;
491 add_files_state.processed_files = 0;
492 add_files_state.added_messages = 0;
493 gettimeofday (&add_files_state.tv_start, NULL);
495 ret = add_files (notmuch, db_path, &add_files_state);
497 gettimeofday (&tv_now, NULL);
498 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
500 if (add_files_state.processed_files) {
501 printf ("Processed %d %s in ", add_files_state.processed_files,
502 add_files_state.processed_files == 1 ?
503 "file" : "total files");
504 notmuch_time_print_formatted_seconds (elapsed);
506 printf (" (%d files/sec.). \n",
507 (int) (add_files_state.processed_files / elapsed));
512 if (add_files_state.added_messages) {
513 printf ("Added %d new %s to the database (not much, really).\n",
514 add_files_state.added_messages,
515 add_files_state.added_messages == 1 ?
516 "message" : "messages");
518 printf ("No new mail---and that's not much.\n");
521 if (elapsed > 1 && ! add_files_state.saw_read_only_directory) {
522 printf ("\nTip: If you have any sub-directories that are archives (that is,\n"
523 "they will never receive new mail), marking these directories as\n"
524 "read-only (chmod u-w /path/to/dir) will make \"notmuch new\"\n"
525 "much more efficient (it won't even look in those directories).\n");
529 printf ("\nNote: At least one error was encountered: %s\n",
530 notmuch_status_to_string (ret));
533 notmuch_database_close (notmuch);
535 return ret || interrupted;