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 For each sub-directory of path, recursively call into this
131 * o If 'fs_mtime' > 'db_mtime'
133 * o For each regular file directly within 'path', call
134 * add_message to add the file to the database.
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 **fs_entries = NULL;
153 int i, num_fs_entries;
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_fs_entries = scandir (path, &fs_entries, 0, ino_cmp);
175 if (num_fs_entries == -1) {
176 fprintf (stderr, "Error opening directory %s: %s\n",
177 path, strerror (errno));
178 ret = NOTMUCH_STATUS_FILE_ERROR;
182 /* First, recurse into all sub-directories. */
183 for (i = 0; i < num_fs_entries; i++) {
187 entry = fs_entries[i];
189 if (entry->d_type != DT_DIR)
192 /* Ignore special directories to avoid infinite recursion.
193 * Also ignore the .notmuch directory.
195 /* XXX: Eventually we'll want more sophistication to let the
196 * user specify files to be ignored. */
197 if (strcmp (entry->d_name, ".") == 0 ||
198 strcmp (entry->d_name, "..") == 0 ||
199 (entry->d_type == DT_DIR &&
200 (strcmp (entry->d_name, "tmp") == 0) &&
201 is_maildir (fs_entries, num_fs_entries)) ||
202 strcmp (entry->d_name, ".notmuch") ==0)
207 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
208 status = add_files_recursive (notmuch, next, state);
209 if (status && ret == NOTMUCH_STATUS_SUCCESS)
215 /* If this directory hasn't been modified since the last
216 * add_files, then we can skip the second pass where we look for
217 * new files in this directory. */
218 if (fs_mtime <= db_mtime)
221 /* Second, scan the regular files in this directory. */
222 for (i = 0; i < num_fs_entries; i++) {
226 entry = fs_entries[i];
228 if (entry->d_type != DT_REG)
231 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
233 state->processed_files++;
235 if (state->verbose) {
236 if (state->output_is_a_tty)
240 state->processed_files,
244 putchar((state->output_is_a_tty) ? '\r' : '\n');
248 status = notmuch_database_add_message (notmuch, next, &message);
251 case NOTMUCH_STATUS_SUCCESS:
252 state->added_messages++;
253 tag_inbox_and_unread (message);
255 /* Non-fatal issues (go on to next file) */
256 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
257 /* Stay silent on this one. */
259 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
260 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
263 /* Fatal issues. Don't process anymore. */
264 case NOTMUCH_STATUS_READONLY_DATABASE:
265 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
266 case NOTMUCH_STATUS_OUT_OF_MEMORY:
267 fprintf (stderr, "Error: %s. Halting processing.\n",
268 notmuch_status_to_string (status));
272 case NOTMUCH_STATUS_FILE_ERROR:
273 case NOTMUCH_STATUS_NULL_POINTER:
274 case NOTMUCH_STATUS_TAG_TOO_LONG:
275 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
276 case NOTMUCH_STATUS_LAST_STATUS:
277 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
282 notmuch_message_destroy (message);
286 if (do_add_files_print_progress) {
287 do_add_files_print_progress = 0;
288 add_files_print_progress (state);
296 status = notmuch_directory_set_mtime (directory, fs_mtime);
297 if (status && ret == NOTMUCH_STATUS_SUCCESS)
314 /* This is the top-level entry point for add_files. It does a couple
315 * of error checks, sets up the progress-printing timer and then calls
316 * into the recursive function. */
317 static notmuch_status_t
318 add_files (notmuch_database_t *notmuch,
320 add_files_state_t *state)
322 notmuch_status_t status;
323 struct sigaction action;
324 struct itimerval timerval;
325 notmuch_bool_t timer_is_active = FALSE;
327 if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {
328 /* Setup our handler for SIGALRM */
329 memset (&action, 0, sizeof (struct sigaction));
330 action.sa_handler = handle_sigalrm;
331 sigemptyset (&action.sa_mask);
332 action.sa_flags = SA_RESTART;
333 sigaction (SIGALRM, &action, NULL);
335 /* Then start a timer to send SIGALRM once per second. */
336 timerval.it_interval.tv_sec = 1;
337 timerval.it_interval.tv_usec = 0;
338 timerval.it_value.tv_sec = 1;
339 timerval.it_value.tv_usec = 0;
340 setitimer (ITIMER_REAL, &timerval, NULL);
342 timer_is_active = TRUE;
345 status = add_files_recursive (notmuch, path, state);
347 if (timer_is_active) {
348 /* Now stop the timer. */
349 timerval.it_interval.tv_sec = 0;
350 timerval.it_interval.tv_usec = 0;
351 timerval.it_value.tv_sec = 0;
352 timerval.it_value.tv_usec = 0;
353 setitimer (ITIMER_REAL, &timerval, NULL);
355 /* And disable the signal handler. */
356 action.sa_handler = SIG_IGN;
357 sigaction (SIGALRM, &action, NULL);
363 /* XXX: This should be merged with the add_files function since it
364 * shares a lot of logic with it. */
365 /* Recursively count all regular files in path and all sub-directories
366 * of path. The result is added to *count (which should be
367 * initialized to zero by the top-level caller before calling
370 count_files (const char *path, int *count)
372 struct dirent *entry = NULL;
375 struct dirent **fs_entries = NULL;
376 int num_fs_entries = scandir (path, &fs_entries, 0, ino_cmp);
379 if (num_fs_entries == -1) {
380 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
381 path, strerror (errno));
385 while (!interrupted) {
386 if (i == num_fs_entries)
389 entry = fs_entries[i++];
391 /* Ignore special directories to avoid infinite recursion.
392 * Also ignore the .notmuch directory.
394 /* XXX: Eventually we'll want more sophistication to let the
395 * user specify files to be ignored. */
396 if (strcmp (entry->d_name, ".") == 0 ||
397 strcmp (entry->d_name, "..") == 0 ||
398 strcmp (entry->d_name, ".notmuch") == 0)
403 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
405 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
406 path, entry->d_name);
412 if (S_ISREG (st.st_mode)) {
414 if (*count % 1000 == 0) {
415 printf ("Found %d files so far.\r", *count);
418 } else if (S_ISDIR (st.st_mode)) {
419 count_files (next, count);
433 notmuch_new_command (void *ctx, int argc, char *argv[])
435 notmuch_config_t *config;
436 notmuch_database_t *notmuch;
437 add_files_state_t add_files_state;
439 struct timeval tv_now;
443 char *dot_notmuch_path;
444 struct sigaction action;
447 add_files_state.verbose = 0;
448 add_files_state.output_is_a_tty = isatty (fileno (stdout));
450 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
451 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
452 add_files_state.verbose = 1;
454 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
459 /* Setup our handler for SIGINT */
460 memset (&action, 0, sizeof (struct sigaction));
461 action.sa_handler = handle_sigint;
462 sigemptyset (&action.sa_mask);
463 action.sa_flags = SA_RESTART;
464 sigaction (SIGINT, &action, NULL);
466 config = notmuch_config_open (ctx, NULL, NULL);
470 db_path = notmuch_config_get_database_path (config);
472 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
474 if (stat (dot_notmuch_path, &st)) {
478 count_files (db_path, &count);
482 printf ("Found %d total files (that's not much mail).\n", count);
483 notmuch = notmuch_database_create (db_path);
484 add_files_state.total_files = count;
486 notmuch = notmuch_database_open (db_path,
487 NOTMUCH_DATABASE_MODE_READ_WRITE);
488 add_files_state.total_files = 0;
494 talloc_free (dot_notmuch_path);
495 dot_notmuch_path = NULL;
497 add_files_state.processed_files = 0;
498 add_files_state.added_messages = 0;
499 gettimeofday (&add_files_state.tv_start, NULL);
501 ret = add_files (notmuch, db_path, &add_files_state);
503 gettimeofday (&tv_now, NULL);
504 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
506 if (add_files_state.processed_files) {
507 printf ("Processed %d %s in ", add_files_state.processed_files,
508 add_files_state.processed_files == 1 ?
509 "file" : "total files");
510 notmuch_time_print_formatted_seconds (elapsed);
512 printf (" (%d files/sec.). \n",
513 (int) (add_files_state.processed_files / elapsed));
518 if (add_files_state.added_messages) {
519 printf ("Added %d new %s to the database.\n",
520 add_files_state.added_messages,
521 add_files_state.added_messages == 1 ?
522 "message" : "messages");
524 printf ("No new mail.\n");
528 printf ("\nNote: At least one error was encountered: %s\n",
529 notmuch_status_to_string (ret));
532 notmuch_database_close (notmuch);
534 return ret || interrupted;