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,
144 add_files_state_t *state)
147 struct dirent *entry = NULL;
149 time_t fs_mtime, db_mtime;
150 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
151 notmuch_message_t *message = NULL;
152 struct dirent **namelist = NULL;
154 notmuch_directory_t *directory;
157 if (stat (path, &st)) {
158 fprintf (stderr, "Error reading directory %s: %s\n",
159 path, strerror (errno));
160 return NOTMUCH_STATUS_FILE_ERROR;
163 if (! S_ISDIR (st.st_mode)) {
164 fprintf (stderr, "Error: %s is not a directory.\n", path);
165 return NOTMUCH_STATUS_FILE_ERROR;
168 fs_mtime = st.st_mtime;
170 directory = notmuch_database_get_directory (notmuch, path);
171 db_mtime = notmuch_directory_get_mtime (directory);
173 num_entries = scandir (path, &namelist, 0, ino_cmp);
175 if (num_entries == -1) {
176 fprintf (stderr, "Error opening directory %s: %s\n",
177 path, strerror (errno));
178 ret = NOTMUCH_STATUS_FILE_ERROR;
184 while (!interrupted) {
185 if (i == num_entries)
188 entry= namelist[i++];
190 /* If this directory hasn't been modified since the last
191 * add_files, then we only need to look further for
192 * sub-directories. */
193 if (fs_mtime <= db_mtime && entry->d_type == DT_REG)
196 /* Ignore special directories to avoid infinite recursion.
197 * Also ignore the .notmuch directory.
199 /* XXX: Eventually we'll want more sophistication to let the
200 * user specify files to be ignored. */
201 if (strcmp (entry->d_name, ".") == 0 ||
202 strcmp (entry->d_name, "..") == 0 ||
203 (entry->d_type == DT_DIR &&
204 (strcmp (entry->d_name, "tmp") == 0) &&
205 is_maildir (namelist, num_entries)) ||
206 strcmp (entry->d_name, ".notmuch") ==0)
211 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
213 if (entry->d_type == DT_REG) {
214 state->processed_files++;
216 if (state->verbose) {
217 if (state->output_is_a_tty)
221 state->processed_files,
225 putchar((state->output_is_a_tty) ? '\r' : '\n');
229 status = notmuch_database_add_message (notmuch, next, &message);
232 case NOTMUCH_STATUS_SUCCESS:
233 state->added_messages++;
234 tag_inbox_and_unread (message);
236 /* Non-fatal issues (go on to next file) */
237 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
238 /* Stay silent on this one. */
240 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
241 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
244 /* Fatal issues. Don't process anymore. */
245 case NOTMUCH_STATUS_READONLY_DATABASE:
246 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
247 case NOTMUCH_STATUS_OUT_OF_MEMORY:
248 fprintf (stderr, "Error: %s. Halting processing.\n",
249 notmuch_status_to_string (status));
253 case NOTMUCH_STATUS_FILE_ERROR:
254 case NOTMUCH_STATUS_NULL_POINTER:
255 case NOTMUCH_STATUS_TAG_TOO_LONG:
256 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
257 case NOTMUCH_STATUS_LAST_STATUS:
258 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
263 notmuch_message_destroy (message);
267 if (do_add_files_print_progress) {
268 do_add_files_print_progress = 0;
269 add_files_print_progress (state);
271 } else if (entry->d_type == DT_DIR) {
272 status = add_files_recursive (notmuch, next, state);
273 if (status && ret == NOTMUCH_STATUS_SUCCESS)
282 status = notmuch_directory_set_mtime (directory, fs_mtime);
283 if (status && ret == NOTMUCH_STATUS_SUCCESS)
300 /* This is the top-level entry point for add_files. It does a couple
301 * of error checks, sets up the progress-printing timer and then calls
302 * into the recursive function. */
303 static notmuch_status_t
304 add_files (notmuch_database_t *notmuch,
306 add_files_state_t *state)
308 notmuch_status_t status;
309 struct sigaction action;
310 struct itimerval timerval;
311 notmuch_bool_t timer_is_active = FALSE;
313 if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {
314 /* Setup our handler for SIGALRM */
315 memset (&action, 0, sizeof (struct sigaction));
316 action.sa_handler = handle_sigalrm;
317 sigemptyset (&action.sa_mask);
318 action.sa_flags = SA_RESTART;
319 sigaction (SIGALRM, &action, NULL);
321 /* Then start a timer to send SIGALRM once per second. */
322 timerval.it_interval.tv_sec = 1;
323 timerval.it_interval.tv_usec = 0;
324 timerval.it_value.tv_sec = 1;
325 timerval.it_value.tv_usec = 0;
326 setitimer (ITIMER_REAL, &timerval, NULL);
328 timer_is_active = TRUE;
331 status = add_files_recursive (notmuch, path, state);
333 if (timer_is_active) {
334 /* Now stop the timer. */
335 timerval.it_interval.tv_sec = 0;
336 timerval.it_interval.tv_usec = 0;
337 timerval.it_value.tv_sec = 0;
338 timerval.it_value.tv_usec = 0;
339 setitimer (ITIMER_REAL, &timerval, NULL);
341 /* And disable the signal handler. */
342 action.sa_handler = SIG_IGN;
343 sigaction (SIGALRM, &action, NULL);
349 /* XXX: This should be merged with the add_files function since it
350 * shares a lot of logic with it. */
351 /* Recursively count all regular files in path and all sub-directories
352 * of path. The result is added to *count (which should be
353 * initialized to zero by the top-level caller before calling
356 count_files (const char *path, int *count)
358 struct dirent *entry = NULL;
361 struct dirent **namelist = NULL;
362 int n_entries = scandir (path, &namelist, 0, ino_cmp);
365 if (n_entries == -1) {
366 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
367 path, strerror (errno));
371 while (!interrupted) {
375 entry= namelist[i++];
377 /* Ignore special directories to avoid infinite recursion.
378 * Also ignore the .notmuch directory.
380 /* XXX: Eventually we'll want more sophistication to let the
381 * user specify files to be ignored. */
382 if (strcmp (entry->d_name, ".") == 0 ||
383 strcmp (entry->d_name, "..") == 0 ||
384 strcmp (entry->d_name, ".notmuch") == 0)
389 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
391 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
392 path, entry->d_name);
398 if (S_ISREG (st.st_mode)) {
400 if (*count % 1000 == 0) {
401 printf ("Found %d files so far.\r", *count);
404 } else if (S_ISDIR (st.st_mode)) {
405 count_files (next, count);
419 notmuch_new_command (void *ctx, int argc, char *argv[])
421 notmuch_config_t *config;
422 notmuch_database_t *notmuch;
423 add_files_state_t add_files_state;
425 struct timeval tv_now;
429 char *dot_notmuch_path;
430 struct sigaction action;
433 add_files_state.verbose = 0;
434 add_files_state.output_is_a_tty = isatty (fileno (stdout));
436 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
437 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
438 add_files_state.verbose = 1;
440 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
445 /* Setup our handler for SIGINT */
446 memset (&action, 0, sizeof (struct sigaction));
447 action.sa_handler = handle_sigint;
448 sigemptyset (&action.sa_mask);
449 action.sa_flags = SA_RESTART;
450 sigaction (SIGINT, &action, NULL);
452 config = notmuch_config_open (ctx, NULL, NULL);
456 db_path = notmuch_config_get_database_path (config);
458 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
460 if (stat (dot_notmuch_path, &st)) {
464 count_files (db_path, &count);
468 printf ("Found %d total files (that's not much mail).\n", count);
469 notmuch = notmuch_database_create (db_path);
470 add_files_state.total_files = count;
472 notmuch = notmuch_database_open (db_path,
473 NOTMUCH_DATABASE_MODE_READ_WRITE);
474 add_files_state.total_files = 0;
480 talloc_free (dot_notmuch_path);
481 dot_notmuch_path = NULL;
483 add_files_state.processed_files = 0;
484 add_files_state.added_messages = 0;
485 gettimeofday (&add_files_state.tv_start, NULL);
487 ret = add_files (notmuch, db_path, &add_files_state);
489 gettimeofday (&tv_now, NULL);
490 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
492 if (add_files_state.processed_files) {
493 printf ("Processed %d %s in ", add_files_state.processed_files,
494 add_files_state.processed_files == 1 ?
495 "file" : "total files");
496 notmuch_time_print_formatted_seconds (elapsed);
498 printf (" (%d files/sec.). \n",
499 (int) (add_files_state.processed_files / elapsed));
504 if (add_files_state.added_messages) {
505 printf ("Added %d new %s to the database.\n",
506 add_files_state.added_messages,
507 add_files_state.added_messages == 1 ?
508 "message" : "messages");
510 printf ("No new mail.\n");
514 printf ("\nNote: At least one error was encountered: %s\n",
515 notmuch_status_to_string (ret));
518 notmuch_database_close (notmuch);
520 return ret || interrupted;