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 _entries_resemble_maildir (struct dirent **entries, int count)
107 for (i = 0; i < count; i++) {
108 if (entries[i]->d_type != DT_DIR)
111 if (strcmp(entries[i]->d_name, "new") == 0 ||
112 strcmp(entries[i]->d_name, "cur") == 0 ||
113 strcmp(entries[i]->d_name, "tmp") == 0)
124 /* Examine 'path' recursively as follows:
126 * o Ask the filesystem for the mtime of 'path' (fs_mtime)
128 * o Ask the database for its timestamp of 'path' (db_mtime)
130 * o For each sub-directory of path, recursively call into this
133 * o If 'fs_mtime' > 'db_mtime'
135 * o For each regular file directly within 'path', call
136 * add_message to add the file to the database.
138 * o Tell the database to update its time of 'path' to 'fs_mtime'
140 * The 'struct stat *st' must point to a structure that has already
141 * been initialized for 'path' by calling stat().
143 static notmuch_status_t
144 add_files_recursive (notmuch_database_t *notmuch,
146 add_files_state_t *state)
149 struct dirent *entry = NULL;
151 time_t fs_mtime, db_mtime;
152 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
153 notmuch_message_t *message = NULL;
154 struct dirent **fs_entries = NULL;
155 int i, num_fs_entries;
156 notmuch_directory_t *directory;
158 notmuch_bool_t is_maildir;
160 if (stat (path, &st)) {
161 fprintf (stderr, "Error reading directory %s: %s\n",
162 path, strerror (errno));
163 return NOTMUCH_STATUS_FILE_ERROR;
166 if (! S_ISDIR (st.st_mode)) {
167 fprintf (stderr, "Error: %s is not a directory.\n", path);
168 return NOTMUCH_STATUS_FILE_ERROR;
171 fs_mtime = st.st_mtime;
173 directory = notmuch_database_get_directory (notmuch, path);
174 db_mtime = notmuch_directory_get_mtime (directory);
176 num_fs_entries = scandir (path, &fs_entries, 0, ino_cmp);
178 if (num_fs_entries == -1) {
179 fprintf (stderr, "Error opening directory %s: %s\n",
180 path, strerror (errno));
181 ret = NOTMUCH_STATUS_FILE_ERROR;
185 /* First, recurse into all sub-directories. */
186 is_maildir = _entries_resemble_maildir (fs_entries, num_fs_entries);
188 for (i = 0; i < num_fs_entries; i++) {
192 entry = fs_entries[i];
194 if (entry->d_type != DT_DIR)
197 /* Ignore special directories to avoid infinite recursion.
198 * Also ignore the .notmuch directory and any "tmp" directory
199 * that appears within a maildir.
201 /* XXX: Eventually we'll want more sophistication to let the
202 * user specify files to be ignored. */
203 if (strcmp (entry->d_name, ".") == 0 ||
204 strcmp (entry->d_name, "..") == 0 ||
205 (is_maildir && strcmp (entry->d_name, "tmp") == 0) ||
206 strcmp (entry->d_name, ".notmuch") ==0)
211 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
212 status = add_files_recursive (notmuch, next, state);
213 if (status && ret == NOTMUCH_STATUS_SUCCESS)
219 /* If this directory hasn't been modified since the last
220 * add_files, then we can skip the second pass where we look for
221 * new files in this directory. */
222 if (fs_mtime <= db_mtime)
225 /* Second, scan the regular files in this directory. */
226 for (i = 0; i < num_fs_entries; i++) {
230 entry = fs_entries[i];
232 if (entry->d_type != DT_REG)
235 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
237 state->processed_files++;
239 if (state->verbose) {
240 if (state->output_is_a_tty)
244 state->processed_files,
248 putchar((state->output_is_a_tty) ? '\r' : '\n');
252 status = notmuch_database_add_message (notmuch, next, &message);
255 case NOTMUCH_STATUS_SUCCESS:
256 state->added_messages++;
257 tag_inbox_and_unread (message);
259 /* Non-fatal issues (go on to next file) */
260 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
261 /* Stay silent on this one. */
263 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
264 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
267 /* Fatal issues. Don't process anymore. */
268 case NOTMUCH_STATUS_READONLY_DATABASE:
269 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
270 case NOTMUCH_STATUS_OUT_OF_MEMORY:
271 fprintf (stderr, "Error: %s. Halting processing.\n",
272 notmuch_status_to_string (status));
276 case NOTMUCH_STATUS_FILE_ERROR:
277 case NOTMUCH_STATUS_NULL_POINTER:
278 case NOTMUCH_STATUS_TAG_TOO_LONG:
279 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
280 case NOTMUCH_STATUS_LAST_STATUS:
281 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
286 notmuch_message_destroy (message);
290 if (do_add_files_print_progress) {
291 do_add_files_print_progress = 0;
292 add_files_print_progress (state);
300 status = notmuch_directory_set_mtime (directory, fs_mtime);
301 if (status && ret == NOTMUCH_STATUS_SUCCESS)
318 /* This is the top-level entry point for add_files. It does a couple
319 * of error checks, sets up the progress-printing timer and then calls
320 * into the recursive function. */
321 static notmuch_status_t
322 add_files (notmuch_database_t *notmuch,
324 add_files_state_t *state)
326 notmuch_status_t status;
327 struct sigaction action;
328 struct itimerval timerval;
329 notmuch_bool_t timer_is_active = FALSE;
331 if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {
332 /* Setup our handler for SIGALRM */
333 memset (&action, 0, sizeof (struct sigaction));
334 action.sa_handler = handle_sigalrm;
335 sigemptyset (&action.sa_mask);
336 action.sa_flags = SA_RESTART;
337 sigaction (SIGALRM, &action, NULL);
339 /* Then start a timer to send SIGALRM once per second. */
340 timerval.it_interval.tv_sec = 1;
341 timerval.it_interval.tv_usec = 0;
342 timerval.it_value.tv_sec = 1;
343 timerval.it_value.tv_usec = 0;
344 setitimer (ITIMER_REAL, &timerval, NULL);
346 timer_is_active = TRUE;
349 status = add_files_recursive (notmuch, path, state);
351 if (timer_is_active) {
352 /* Now stop the timer. */
353 timerval.it_interval.tv_sec = 0;
354 timerval.it_interval.tv_usec = 0;
355 timerval.it_value.tv_sec = 0;
356 timerval.it_value.tv_usec = 0;
357 setitimer (ITIMER_REAL, &timerval, NULL);
359 /* And disable the signal handler. */
360 action.sa_handler = SIG_IGN;
361 sigaction (SIGALRM, &action, NULL);
367 /* XXX: This should be merged with the add_files function since it
368 * shares a lot of logic with it. */
369 /* Recursively count all regular files in path and all sub-directories
370 * of path. The result is added to *count (which should be
371 * initialized to zero by the top-level caller before calling
374 count_files (const char *path, int *count)
376 struct dirent *entry = NULL;
379 struct dirent **fs_entries = NULL;
380 int num_fs_entries = scandir (path, &fs_entries, 0, ino_cmp);
383 if (num_fs_entries == -1) {
384 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
385 path, strerror (errno));
389 while (!interrupted) {
390 if (i == num_fs_entries)
393 entry = fs_entries[i++];
395 /* Ignore special directories to avoid infinite recursion.
396 * Also ignore the .notmuch directory.
398 /* XXX: Eventually we'll want more sophistication to let the
399 * user specify files to be ignored. */
400 if (strcmp (entry->d_name, ".") == 0 ||
401 strcmp (entry->d_name, "..") == 0 ||
402 strcmp (entry->d_name, ".notmuch") == 0)
407 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
409 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
410 path, entry->d_name);
416 if (S_ISREG (st.st_mode)) {
418 if (*count % 1000 == 0) {
419 printf ("Found %d files so far.\r", *count);
422 } else if (S_ISDIR (st.st_mode)) {
423 count_files (next, count);
437 notmuch_new_command (void *ctx, int argc, char *argv[])
439 notmuch_config_t *config;
440 notmuch_database_t *notmuch;
441 add_files_state_t add_files_state;
443 struct timeval tv_now;
447 char *dot_notmuch_path;
448 struct sigaction action;
451 add_files_state.verbose = 0;
452 add_files_state.output_is_a_tty = isatty (fileno (stdout));
454 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
455 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
456 add_files_state.verbose = 1;
458 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
463 /* Setup our handler for SIGINT */
464 memset (&action, 0, sizeof (struct sigaction));
465 action.sa_handler = handle_sigint;
466 sigemptyset (&action.sa_mask);
467 action.sa_flags = SA_RESTART;
468 sigaction (SIGINT, &action, NULL);
470 config = notmuch_config_open (ctx, NULL, NULL);
474 db_path = notmuch_config_get_database_path (config);
476 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
478 if (stat (dot_notmuch_path, &st)) {
482 count_files (db_path, &count);
486 printf ("Found %d total files (that's not much mail).\n", count);
487 notmuch = notmuch_database_create (db_path);
488 add_files_state.total_files = count;
490 notmuch = notmuch_database_open (db_path,
491 NOTMUCH_DATABASE_MODE_READ_WRITE);
492 add_files_state.total_files = 0;
498 talloc_free (dot_notmuch_path);
499 dot_notmuch_path = NULL;
501 add_files_state.processed_files = 0;
502 add_files_state.added_messages = 0;
503 gettimeofday (&add_files_state.tv_start, NULL);
505 ret = add_files (notmuch, db_path, &add_files_state);
507 gettimeofday (&tv_now, NULL);
508 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
510 if (add_files_state.processed_files) {
511 printf ("Processed %d %s in ", add_files_state.processed_files,
512 add_files_state.processed_files == 1 ?
513 "file" : "total files");
514 notmuch_time_print_formatted_seconds (elapsed);
516 printf (" (%d files/sec.). \n",
517 (int) (add_files_state.processed_files / elapsed));
522 if (add_files_state.added_messages) {
523 printf ("Added %d new %s to the database.\n",
524 add_files_state.added_messages,
525 add_files_state.added_messages == 1 ?
526 "message" : "messages");
528 printf ("No new mail.\n");
532 printf ("\nNote: At least one error was encountered: %s\n",
533 notmuch_status_to_string (ret));
536 notmuch_database_close (notmuch);
538 return ret || interrupted;