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"
32 struct timeval tv_start;
35 static volatile sig_atomic_t do_add_files_print_progress = 0;
38 handle_sigalrm (unused (int signal))
40 do_add_files_print_progress = 1;
43 static volatile sig_atomic_t interrupted;
46 handle_sigint (unused (int sig))
49 static char msg[] = "Stopping... \n";
51 ignored = write(2, msg, sizeof(msg)-1);
56 tag_inbox_and_unread (notmuch_message_t *message)
58 notmuch_message_add_tag (message, "inbox");
59 notmuch_message_add_tag (message, "unread");
63 add_files_print_progress (add_files_state_t *state)
65 struct timeval tv_now;
66 double elapsed_overall, rate_overall;
68 gettimeofday (&tv_now, NULL);
70 elapsed_overall = notmuch_time_elapsed (state->tv_start, tv_now);
71 rate_overall = (state->processed_files) / elapsed_overall;
73 printf ("Processed %d", state->processed_files);
75 if (state->total_files) {
76 double time_remaining;
78 time_remaining = ((state->total_files - state->processed_files) /
80 printf (" of %d files (", state->total_files);
81 notmuch_time_print_formatted_seconds (time_remaining);
82 printf (" remaining). \r");
84 printf (" files (%d files/sec.) \r", (int) rate_overall);
90 static int ino_cmp(const struct dirent **a, const struct dirent **b)
92 return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
95 /* Test if the directory looks like a Maildir directory.
97 * Search through the array of directory entries to see if we can find all
98 * three subdirectories typical for Maildir, that is "new", "cur", and "tmp".
100 * Return 1 if the directory looks like a Maildir and 0 otherwise.
103 is_maildir (struct dirent **entries, int count)
107 for (i = 0; i < count; i++) {
108 if (entries[i]->d_type != DT_DIR) continue;
109 if (strcmp(entries[i]->d_name, "new") == 0 ||
110 strcmp(entries[i]->d_name, "cur") == 0 ||
111 strcmp(entries[i]->d_name, "tmp") == 0)
122 /* Examine 'path' recursively as follows:
124 * o Ask the filesystem for the mtime of 'path' (fs_mtime)
126 * o Ask the database for its timestamp of 'path' (db_mtime)
128 * o If 'fs_mtime' > 'db_mtime'
130 * o For each regular file directly within 'path', call
131 * add_message to add the file to the database.
133 * o For each sub-directory of path, recursively call into this
136 * o Tell the database to update its time of 'path' to 'fs_mtime'
138 * The 'struct stat *st' must point to a structure that has already
139 * been initialized for 'path' by calling stat().
141 static notmuch_status_t
142 add_files_recursive (notmuch_database_t *notmuch,
145 add_files_state_t *state)
148 struct dirent *entry = NULL;
150 time_t fs_mtime, db_mtime;
151 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
152 notmuch_message_t *message = NULL;
153 struct dirent **namelist = NULL;
155 notmuch_directory_t *directory;
157 fs_mtime = st->st_mtime;
159 directory = notmuch_database_get_directory (notmuch, path);
160 db_mtime = notmuch_directory_get_mtime (directory);
162 num_entries = scandir (path, &namelist, 0, ino_cmp);
164 if (num_entries == -1) {
165 fprintf (stderr, "Error opening directory %s: %s\n",
166 path, strerror (errno));
167 ret = NOTMUCH_STATUS_FILE_ERROR;
173 while (!interrupted) {
174 if (i == num_entries)
177 entry= namelist[i++];
179 /* If this directory hasn't been modified since the last
180 * add_files, then we only need to look further for
181 * sub-directories. */
182 if (fs_mtime <= db_mtime && entry->d_type == DT_REG)
185 /* Ignore special directories to avoid infinite recursion.
186 * Also ignore the .notmuch directory.
188 /* XXX: Eventually we'll want more sophistication to let the
189 * user specify files to be ignored. */
190 if (strcmp (entry->d_name, ".") == 0 ||
191 strcmp (entry->d_name, "..") == 0 ||
192 (entry->d_type == DT_DIR &&
193 (strcmp (entry->d_name, "tmp") == 0) &&
194 is_maildir (namelist, num_entries)) ||
195 strcmp (entry->d_name, ".notmuch") ==0)
200 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
202 if (stat (next, st)) {
207 /* The file was removed between scandir and now... */
210 /* We can't read this file so don't add it to the cache. */
214 fprintf (stderr, "Error reading %s: %s\n",
215 next, strerror (errno));
216 ret = NOTMUCH_STATUS_FILE_ERROR;
220 if (S_ISREG (st->st_mode)) {
221 state->processed_files++;
223 if (state->verbose) {
224 if (state->output_is_a_tty)
228 state->processed_files,
232 putchar((state->output_is_a_tty) ? '\r' : '\n');
236 status = notmuch_database_add_message (notmuch, next, &message);
239 case NOTMUCH_STATUS_SUCCESS:
240 state->added_messages++;
241 tag_inbox_and_unread (message);
243 /* Non-fatal issues (go on to next file) */
244 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
245 /* Stay silent on this one. */
247 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
248 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
251 /* Fatal issues. Don't process anymore. */
252 case NOTMUCH_STATUS_READONLY_DATABASE:
253 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
254 case NOTMUCH_STATUS_OUT_OF_MEMORY:
255 fprintf (stderr, "Error: %s. Halting processing.\n",
256 notmuch_status_to_string (status));
260 case NOTMUCH_STATUS_FILE_ERROR:
261 case NOTMUCH_STATUS_NULL_POINTER:
262 case NOTMUCH_STATUS_TAG_TOO_LONG:
263 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
264 case NOTMUCH_STATUS_LAST_STATUS:
265 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
270 notmuch_message_destroy (message);
274 if (do_add_files_print_progress) {
275 do_add_files_print_progress = 0;
276 add_files_print_progress (state);
278 } else if (S_ISDIR (st->st_mode)) {
279 status = add_files_recursive (notmuch, next, st, state);
280 if (status && ret == NOTMUCH_STATUS_SUCCESS)
289 status = notmuch_directory_set_mtime (directory, fs_mtime);
290 if (status && ret == NOTMUCH_STATUS_SUCCESS)
307 /* This is the top-level entry point for add_files. It does a couple
308 * of error checks, sets up the progress-printing timer and then calls
309 * into the recursive function. */
310 static notmuch_status_t
311 add_files (notmuch_database_t *notmuch,
313 add_files_state_t *state)
316 notmuch_status_t status;
317 struct sigaction action;
318 struct itimerval timerval;
319 notmuch_bool_t timer_is_active = FALSE;
321 if (stat (path, &st)) {
322 fprintf (stderr, "Error reading directory %s: %s\n",
323 path, strerror (errno));
324 return NOTMUCH_STATUS_FILE_ERROR;
327 if (! S_ISDIR (st.st_mode)) {
328 fprintf (stderr, "Error: %s is not a directory.\n", path);
329 return NOTMUCH_STATUS_FILE_ERROR;
332 if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {
333 /* Setup our handler for SIGALRM */
334 memset (&action, 0, sizeof (struct sigaction));
335 action.sa_handler = handle_sigalrm;
336 sigemptyset (&action.sa_mask);
337 action.sa_flags = SA_RESTART;
338 sigaction (SIGALRM, &action, NULL);
340 /* Then start a timer to send SIGALRM once per second. */
341 timerval.it_interval.tv_sec = 1;
342 timerval.it_interval.tv_usec = 0;
343 timerval.it_value.tv_sec = 1;
344 timerval.it_value.tv_usec = 0;
345 setitimer (ITIMER_REAL, &timerval, NULL);
347 timer_is_active = TRUE;
350 status = add_files_recursive (notmuch, path, &st, state);
352 if (timer_is_active) {
353 /* Now stop the timer. */
354 timerval.it_interval.tv_sec = 0;
355 timerval.it_interval.tv_usec = 0;
356 timerval.it_value.tv_sec = 0;
357 timerval.it_value.tv_usec = 0;
358 setitimer (ITIMER_REAL, &timerval, NULL);
360 /* And disable the signal handler. */
361 action.sa_handler = SIG_IGN;
362 sigaction (SIGALRM, &action, NULL);
368 /* XXX: This should be merged with the add_files function since it
369 * shares a lot of logic with it. */
370 /* Recursively count all regular files in path and all sub-directories
371 * of path. The result is added to *count (which should be
372 * initialized to zero by the top-level caller before calling
375 count_files (const char *path, int *count)
377 struct dirent *entry = NULL;
380 struct dirent **namelist = NULL;
381 int n_entries = scandir (path, &namelist, 0, ino_cmp);
384 if (n_entries == -1) {
385 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
386 path, strerror (errno));
390 while (!interrupted) {
394 entry= namelist[i++];
396 /* Ignore special directories to avoid infinite recursion.
397 * Also ignore the .notmuch directory.
399 /* XXX: Eventually we'll want more sophistication to let the
400 * user specify files to be ignored. */
401 if (strcmp (entry->d_name, ".") == 0 ||
402 strcmp (entry->d_name, "..") == 0 ||
403 strcmp (entry->d_name, ".notmuch") == 0)
408 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
410 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
411 path, entry->d_name);
417 if (S_ISREG (st.st_mode)) {
419 if (*count % 1000 == 0) {
420 printf ("Found %d files so far.\r", *count);
423 } else if (S_ISDIR (st.st_mode)) {
424 count_files (next, count);
438 notmuch_new_command (void *ctx, int argc, char *argv[])
440 notmuch_config_t *config;
441 notmuch_database_t *notmuch;
442 add_files_state_t add_files_state;
444 struct timeval tv_now;
448 char *dot_notmuch_path;
449 struct sigaction action;
452 add_files_state.verbose = 0;
453 add_files_state.output_is_a_tty = isatty (fileno (stdout));
455 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
456 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
457 add_files_state.verbose = 1;
459 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
464 /* Setup our handler for SIGINT */
465 memset (&action, 0, sizeof (struct sigaction));
466 action.sa_handler = handle_sigint;
467 sigemptyset (&action.sa_mask);
468 action.sa_flags = SA_RESTART;
469 sigaction (SIGINT, &action, NULL);
471 config = notmuch_config_open (ctx, NULL, NULL);
475 db_path = notmuch_config_get_database_path (config);
477 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
479 if (stat (dot_notmuch_path, &st)) {
483 count_files (db_path, &count);
487 printf ("Found %d total files (that's not much mail).\n", count);
488 notmuch = notmuch_database_create (db_path);
489 add_files_state.total_files = count;
491 notmuch = notmuch_database_open (db_path,
492 NOTMUCH_DATABASE_MODE_READ_WRITE);
493 add_files_state.total_files = 0;
499 talloc_free (dot_notmuch_path);
500 dot_notmuch_path = NULL;
502 add_files_state.processed_files = 0;
503 add_files_state.added_messages = 0;
504 gettimeofday (&add_files_state.tv_start, NULL);
506 ret = add_files (notmuch, db_path, &add_files_state);
508 gettimeofday (&tv_now, NULL);
509 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
511 if (add_files_state.processed_files) {
512 printf ("Processed %d %s in ", add_files_state.processed_files,
513 add_files_state.processed_files == 1 ?
514 "file" : "total files");
515 notmuch_time_print_formatted_seconds (elapsed);
517 printf (" (%d files/sec.). \n",
518 (int) (add_files_state.processed_files / elapsed));
523 if (add_files_state.added_messages) {
524 printf ("Added %d new %s to the database.\n",
525 add_files_state.added_messages,
526 add_files_state.added_messages == 1 ?
527 "message" : "messages");
529 printf ("No new mail.\n");
533 printf ("\nNote: At least one error was encountered: %s\n",
534 notmuch_status_to_string (ret));
537 notmuch_database_close (notmuch);
539 return ret || interrupted;