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 path_mtime = st->st_mtime;
149 path_dbtime = notmuch_database_get_timestamp (notmuch, path);
150 num_entries = scandir (path, &namelist, 0, ino_cmp);
152 if (num_entries == -1) {
153 fprintf (stderr, "Error opening directory %s: %s\n",
154 path, strerror (errno));
155 ret = NOTMUCH_STATUS_FILE_ERROR;
161 while (!interrupted) {
162 if (i == num_entries)
165 entry= namelist[i++];
167 /* If this directory hasn't been modified since the last
168 * add_files, then we only need to look further for
169 * sub-directories. */
170 if (path_mtime <= path_dbtime && entry->d_type == DT_REG)
173 /* Ignore special directories to avoid infinite recursion.
174 * Also ignore the .notmuch directory.
176 /* XXX: Eventually we'll want more sophistication to let the
177 * user specify files to be ignored. */
178 if (strcmp (entry->d_name, ".") == 0 ||
179 strcmp (entry->d_name, "..") == 0 ||
180 (entry->d_type == DT_DIR &&
181 (strcmp (entry->d_name, "tmp") == 0) &&
182 is_maildir (namelist, num_entries)) ||
183 strcmp (entry->d_name, ".notmuch") ==0)
188 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
190 if (stat (next, st)) {
195 /* The file was removed between scandir and now... */
198 /* We can't read this file so don't add it to the cache. */
202 fprintf (stderr, "Error reading %s: %s\n",
203 next, strerror (errno));
204 ret = NOTMUCH_STATUS_FILE_ERROR;
208 if (S_ISREG (st->st_mode)) {
209 /* If the file hasn't been modified since the last
210 * add_files, then we need not look at it. */
211 if (path_dbtime == 0 || st->st_mtime > path_dbtime) {
212 state->processed_files++;
214 if (state->verbose) {
215 if (state->output_is_a_tty)
219 state->processed_files,
223 putchar((state->output_is_a_tty) ? '\r' : '\n');
227 status = notmuch_database_add_message (notmuch, next, &message);
230 case NOTMUCH_STATUS_SUCCESS:
231 state->added_messages++;
232 tag_inbox_and_unread (message);
234 /* Non-fatal issues (go on to next file) */
235 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
236 /* Stay silent on this one. */
238 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
239 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
242 /* Fatal issues. Don't process anymore. */
243 case NOTMUCH_STATUS_READONLY_DATABASE:
244 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
245 case NOTMUCH_STATUS_OUT_OF_MEMORY:
246 fprintf (stderr, "Error: %s. Halting processing.\n",
247 notmuch_status_to_string (status));
251 case NOTMUCH_STATUS_FILE_ERROR:
252 case NOTMUCH_STATUS_NULL_POINTER:
253 case NOTMUCH_STATUS_TAG_TOO_LONG:
254 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
255 case NOTMUCH_STATUS_LAST_STATUS:
256 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
261 notmuch_message_destroy (message);
265 if (do_add_files_print_progress) {
266 do_add_files_print_progress = 0;
267 add_files_print_progress (state);
270 } else if (S_ISDIR (st->st_mode)) {
271 status = add_files_recursive (notmuch, next, st, state);
272 if (status && ret == NOTMUCH_STATUS_SUCCESS)
280 status = notmuch_database_set_timestamp (notmuch, path, path_mtime);
281 if (status && ret == NOTMUCH_STATUS_SUCCESS)
297 /* This is the top-level entry point for add_files. It does a couple
298 * of error checks, sets up the progress-printing timer and then calls
299 * into the recursive function. */
301 add_files (notmuch_database_t *notmuch,
303 add_files_state_t *state)
306 notmuch_status_t status;
307 struct sigaction action;
308 struct itimerval timerval;
309 notmuch_bool_t timer_is_active = FALSE;
311 if (stat (path, &st)) {
312 fprintf (stderr, "Error reading directory %s: %s\n",
313 path, strerror (errno));
314 return NOTMUCH_STATUS_FILE_ERROR;
317 if (! S_ISDIR (st.st_mode)) {
318 fprintf (stderr, "Error: %s is not a directory.\n", path);
319 return NOTMUCH_STATUS_FILE_ERROR;
322 if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {
323 /* Setup our handler for SIGALRM */
324 memset (&action, 0, sizeof (struct sigaction));
325 action.sa_handler = handle_sigalrm;
326 sigemptyset (&action.sa_mask);
327 action.sa_flags = SA_RESTART;
328 sigaction (SIGALRM, &action, NULL);
330 /* Then start a timer to send SIGALRM once per second. */
331 timerval.it_interval.tv_sec = 1;
332 timerval.it_interval.tv_usec = 0;
333 timerval.it_value.tv_sec = 1;
334 timerval.it_value.tv_usec = 0;
335 setitimer (ITIMER_REAL, &timerval, NULL);
337 timer_is_active = TRUE;
340 status = add_files_recursive (notmuch, path, &st, state);
342 if (timer_is_active) {
343 /* Now stop the timer. */
344 timerval.it_interval.tv_sec = 0;
345 timerval.it_interval.tv_usec = 0;
346 timerval.it_value.tv_sec = 0;
347 timerval.it_value.tv_usec = 0;
348 setitimer (ITIMER_REAL, &timerval, NULL);
350 /* And disable the signal handler. */
351 action.sa_handler = SIG_IGN;
352 sigaction (SIGALRM, &action, NULL);
358 /* XXX: This should be merged with the add_files function since it
359 * shares a lot of logic with it. */
360 /* Recursively count all regular files in path and all sub-directories
361 * of path. The result is added to *count (which should be
362 * initialized to zero by the top-level caller before calling
365 count_files (const char *path, int *count)
367 struct dirent *entry = NULL;
370 struct dirent **namelist = NULL;
371 int n_entries = scandir (path, &namelist, 0, ino_cmp);
374 if (n_entries == -1) {
375 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
376 path, strerror (errno));
380 while (!interrupted) {
384 entry= namelist[i++];
386 /* Ignore special directories to avoid infinite recursion.
387 * Also ignore the .notmuch directory.
389 /* XXX: Eventually we'll want more sophistication to let the
390 * user specify files to be ignored. */
391 if (strcmp (entry->d_name, ".") == 0 ||
392 strcmp (entry->d_name, "..") == 0 ||
393 strcmp (entry->d_name, ".notmuch") == 0)
398 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
400 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
401 path, entry->d_name);
407 if (S_ISREG (st.st_mode)) {
409 if (*count % 1000 == 0) {
410 printf ("Found %d files so far.\r", *count);
413 } else if (S_ISDIR (st.st_mode)) {
414 count_files (next, count);
428 notmuch_new_command (void *ctx, int argc, char *argv[])
430 notmuch_config_t *config;
431 notmuch_database_t *notmuch;
432 add_files_state_t add_files_state;
434 struct timeval tv_now;
438 char *dot_notmuch_path;
439 struct sigaction action;
442 add_files_state.verbose = 0;
443 add_files_state.output_is_a_tty = isatty (fileno (stdout));
445 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
446 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
447 add_files_state.verbose = 1;
449 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
454 /* Setup our handler for SIGINT */
455 memset (&action, 0, sizeof (struct sigaction));
456 action.sa_handler = handle_sigint;
457 sigemptyset (&action.sa_mask);
458 action.sa_flags = SA_RESTART;
459 sigaction (SIGINT, &action, NULL);
461 config = notmuch_config_open (ctx, NULL, NULL);
465 db_path = notmuch_config_get_database_path (config);
467 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
469 if (stat (dot_notmuch_path, &st)) {
473 count_files (db_path, &count);
477 printf ("Found %d total files (that's not much mail).\n", count);
478 notmuch = notmuch_database_create (db_path);
479 add_files_state.total_files = count;
481 notmuch = notmuch_database_open (db_path,
482 NOTMUCH_DATABASE_MODE_READ_WRITE);
483 add_files_state.total_files = 0;
489 talloc_free (dot_notmuch_path);
490 dot_notmuch_path = NULL;
492 add_files_state.processed_files = 0;
493 add_files_state.added_messages = 0;
494 gettimeofday (&add_files_state.tv_start, NULL);
496 ret = add_files (notmuch, db_path, &add_files_state);
498 gettimeofday (&tv_now, NULL);
499 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
501 if (add_files_state.processed_files) {
502 printf ("Processed %d %s in ", add_files_state.processed_files,
503 add_files_state.processed_files == 1 ?
504 "file" : "total files");
505 notmuch_time_print_formatted_seconds (elapsed);
507 printf (" (%d files/sec.). \n",
508 (int) (add_files_state.processed_files / elapsed));
513 if (add_files_state.added_messages) {
514 printf ("Added %d new %s to the database.\n",
515 add_files_state.added_messages,
516 add_files_state.added_messages == 1 ?
517 "message" : "messages");
519 printf ("No new mail.\n");
523 printf ("\nNote: At least one error was encountered: %s\n",
524 notmuch_status_to_string (ret));
527 notmuch_database_close (notmuch);
529 return ret || interrupted;