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))
38 static char msg[] = "Stopping... \n";
39 write(2, msg, sizeof(msg)-1);
44 tag_inbox_and_unread (notmuch_message_t *message)
46 notmuch_message_add_tag (message, "inbox");
47 notmuch_message_add_tag (message, "unread");
51 add_files_print_progress (add_files_state_t *state)
53 struct timeval tv_now;
54 double elapsed_overall, rate_overall;
56 gettimeofday (&tv_now, NULL);
58 elapsed_overall = notmuch_time_elapsed (state->tv_start, tv_now);
59 rate_overall = (state->processed_files) / elapsed_overall;
61 printf ("Processed %d", state->processed_files);
63 if (state->total_files) {
64 double time_remaining;
66 time_remaining = ((state->total_files - state->processed_files) /
68 printf (" of %d files (", state->total_files);
69 notmuch_time_print_formatted_seconds (time_remaining);
70 printf (" remaining). \r");
72 printf (" files (%d files/sec.) \r", (int) rate_overall);
78 static int ino_cmp(const struct dirent **a, const struct dirent **b)
80 return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
83 /* Test if the directory looks like a Maildir directory.
85 * Search through the array of directory entries to see if we can find all
86 * three subdirectories typical for Maildir, that is "new", "cur", and "tmp".
88 * Return 1 if the directory looks like a Maildir and 0 otherwise.
91 is_maildir (struct dirent **entries, int count)
95 for (i = 0; i < count; i++) {
96 if (entries[i]->d_type != DT_DIR) continue;
97 if (strcmp(entries[i]->d_name, "new") == 0 ||
98 strcmp(entries[i]->d_name, "cur") == 0 ||
99 strcmp(entries[i]->d_name, "tmp") == 0)
110 /* Examine 'path' recursively as follows:
112 * o Ask the filesystem for the mtime of 'path' (path_mtime)
114 * o Ask the database for its timestamp of 'path' (path_dbtime)
116 * o If 'path_mtime' > 'path_dbtime'
118 * o For each regular file in 'path' with mtime newer than the
119 * 'path_dbtime' call add_message to add the file to the
122 * o For each sub-directory of path, recursively call into this
125 * o Tell the database to update its time of 'path' to 'path_mtime'
127 * The 'struct stat *st' must point to a structure that has already
128 * been initialized for 'path' by calling stat().
130 static notmuch_status_t
131 add_files_recursive (notmuch_database_t *notmuch,
134 add_files_state_t *state)
137 struct dirent *entry = NULL;
139 time_t path_mtime, path_dbtime;
140 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
141 notmuch_message_t *message = NULL;
142 struct dirent **namelist = NULL;
145 /* If we're told to, we bail out on encountering a read-only
146 * directory, (with this being a clear clue from the user to
147 * Notmuch that new mail won't be arriving there and we need not
149 if (state->ignore_read_only_directories &&
150 (st->st_mode & S_IWUSR) == 0)
152 state->saw_read_only_directory = TRUE;
156 path_mtime = st->st_mtime;
158 path_dbtime = notmuch_database_get_timestamp (notmuch, path);
159 num_entries = scandir (path, &namelist, 0, ino_cmp);
161 if (num_entries == -1) {
162 fprintf (stderr, "Error opening directory %s: %s\n",
163 path, strerror (errno));
164 ret = NOTMUCH_STATUS_FILE_ERROR;
170 while (!interrupted) {
171 if (i == num_entries)
174 entry= namelist[i++];
176 /* If this directory hasn't been modified since the last
177 * add_files, then we only need to look further for
178 * sub-directories. */
179 if (path_mtime <= path_dbtime && entry->d_type == DT_REG)
182 /* Ignore special directories to avoid infinite recursion.
183 * Also ignore the .notmuch directory.
185 /* XXX: Eventually we'll want more sophistication to let the
186 * user specify files to be ignored. */
187 if (strcmp (entry->d_name, ".") == 0 ||
188 strcmp (entry->d_name, "..") == 0 ||
189 (entry->d_type == DT_DIR &&
190 (strcmp (entry->d_name, "tmp") == 0) &&
191 is_maildir (namelist, num_entries)) ||
192 strcmp (entry->d_name, ".notmuch") ==0)
197 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
199 if (stat (next, st)) {
200 fprintf (stderr, "Error reading %s: %s\n",
201 next, strerror (errno));
202 ret = NOTMUCH_STATUS_FILE_ERROR;
206 if (S_ISREG (st->st_mode)) {
207 /* If the file hasn't been modified since the last
208 * add_files, then we need not look at it. */
209 if (path_dbtime == 0 || st->st_mtime > path_dbtime) {
210 state->processed_files++;
212 if (state->verbose) {
213 if (state->output_is_a_tty)
217 state->processed_files,
221 putchar((state->output_is_a_tty) ? '\r' : '\n');
225 status = notmuch_database_add_message (notmuch, next, &message);
228 case NOTMUCH_STATUS_SUCCESS:
229 state->added_messages++;
230 tag_inbox_and_unread (message);
232 /* Non-fatal issues (go on to next file) */
233 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
234 /* Stay silent on this one. */
236 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
237 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
240 /* Fatal issues. Don't process anymore. */
241 case NOTMUCH_STATUS_READONLY_DATABASE:
242 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
243 case NOTMUCH_STATUS_OUT_OF_MEMORY:
244 fprintf (stderr, "Error: %s. Halting processing.\n",
245 notmuch_status_to_string (status));
249 case NOTMUCH_STATUS_FILE_ERROR:
250 case NOTMUCH_STATUS_NULL_POINTER:
251 case NOTMUCH_STATUS_TAG_TOO_LONG:
252 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
253 case NOTMUCH_STATUS_LAST_STATUS:
254 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
259 notmuch_message_destroy (message);
263 if (do_add_files_print_progress) {
264 do_add_files_print_progress = 0;
265 add_files_print_progress (state);
268 } else if (S_ISDIR (st->st_mode)) {
269 status = add_files_recursive (notmuch, next, st, state);
270 if (status && ret == NOTMUCH_STATUS_SUCCESS)
278 status = notmuch_database_set_timestamp (notmuch, path, path_mtime);
279 if (status && ret == NOTMUCH_STATUS_SUCCESS)
295 /* This is the top-level entry point for add_files. It does a couple
296 * of error checks, sets up the progress-printing timer and then calls
297 * into the recursive function. */
299 add_files (notmuch_database_t *notmuch,
301 add_files_state_t *state)
304 notmuch_status_t status;
305 struct sigaction action;
306 struct itimerval timerval;
307 notmuch_bool_t timer_is_active = FALSE;
309 if (stat (path, &st)) {
310 fprintf (stderr, "Error reading directory %s: %s\n",
311 path, strerror (errno));
312 return NOTMUCH_STATUS_FILE_ERROR;
315 if (! S_ISDIR (st.st_mode)) {
316 fprintf (stderr, "Error: %s is not a directory.\n", path);
317 return NOTMUCH_STATUS_FILE_ERROR;
320 if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {
321 /* Setup our handler for SIGALRM */
322 memset (&action, 0, sizeof (struct sigaction));
323 action.sa_handler = handle_sigalrm;
324 sigemptyset (&action.sa_mask);
325 action.sa_flags = SA_RESTART;
326 sigaction (SIGALRM, &action, NULL);
328 /* Then start a timer to send SIGALRM once per second. */
329 timerval.it_interval.tv_sec = 1;
330 timerval.it_interval.tv_usec = 0;
331 timerval.it_value.tv_sec = 1;
332 timerval.it_value.tv_usec = 0;
333 setitimer (ITIMER_REAL, &timerval, NULL);
335 timer_is_active = TRUE;
338 status = add_files_recursive (notmuch, path, &st, state);
340 if (timer_is_active) {
341 /* Now stop the timer. */
342 timerval.it_interval.tv_sec = 0;
343 timerval.it_interval.tv_usec = 0;
344 timerval.it_value.tv_sec = 0;
345 timerval.it_value.tv_usec = 0;
346 setitimer (ITIMER_REAL, &timerval, NULL);
348 /* And disable the signal handler. */
349 action.sa_handler = SIG_IGN;
350 sigaction (SIGALRM, &action, NULL);
356 /* XXX: This should be merged with the add_files function since it
357 * shares a lot of logic with it. */
358 /* Recursively count all regular files in path and all sub-directories
359 * of path. The result is added to *count (which should be
360 * initialized to zero by the top-level caller before calling
363 count_files (const char *path, int *count)
365 struct dirent *entry = NULL;
368 struct dirent **namelist = NULL;
369 int n_entries = scandir (path, &namelist, 0, ino_cmp);
372 if (n_entries == -1) {
373 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
374 path, strerror (errno));
378 while (!interrupted) {
382 entry= namelist[i++];
384 /* Ignore special directories to avoid infinite recursion.
385 * Also ignore the .notmuch directory.
387 /* XXX: Eventually we'll want more sophistication to let the
388 * user specify files to be ignored. */
389 if (strcmp (entry->d_name, ".") == 0 ||
390 strcmp (entry->d_name, "..") == 0 ||
391 strcmp (entry->d_name, ".notmuch") == 0)
396 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
398 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
399 path, entry->d_name);
405 if (S_ISREG (st.st_mode)) {
407 if (*count % 1000 == 0) {
408 printf ("Found %d files so far.\r", *count);
411 } else if (S_ISDIR (st.st_mode)) {
412 count_files (next, count);
426 notmuch_new_command (void *ctx, int argc, char *argv[])
428 notmuch_config_t *config;
429 notmuch_database_t *notmuch;
430 add_files_state_t add_files_state;
432 struct timeval tv_now;
436 char *dot_notmuch_path;
437 struct sigaction action;
440 add_files_state.verbose = 0;
441 add_files_state.output_is_a_tty = isatty (fileno (stdout));
443 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
444 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
445 add_files_state.verbose = 1;
447 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
452 /* Setup our handler for SIGINT */
453 memset (&action, 0, sizeof (struct sigaction));
454 action.sa_handler = handle_sigint;
455 sigemptyset (&action.sa_mask);
456 action.sa_flags = SA_RESTART;
457 sigaction (SIGINT, &action, NULL);
459 config = notmuch_config_open (ctx, NULL, NULL);
463 db_path = notmuch_config_get_database_path (config);
465 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
467 if (stat (dot_notmuch_path, &st)) {
471 count_files (db_path, &count);
475 printf ("Found %d total files. \n", count);
476 notmuch = notmuch_database_create (db_path);
477 add_files_state.ignore_read_only_directories = FALSE;
478 add_files_state.total_files = count;
480 notmuch = notmuch_database_open (db_path,
481 NOTMUCH_DATABASE_MODE_READ_WRITE);
482 add_files_state.ignore_read_only_directories = TRUE;
483 add_files_state.total_files = 0;
489 talloc_free (dot_notmuch_path);
490 dot_notmuch_path = NULL;
492 add_files_state.saw_read_only_directory = FALSE;
493 add_files_state.processed_files = 0;
494 add_files_state.added_messages = 0;
495 gettimeofday (&add_files_state.tv_start, NULL);
497 ret = add_files (notmuch, db_path, &add_files_state);
499 gettimeofday (&tv_now, NULL);
500 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
502 if (add_files_state.processed_files) {
503 printf ("Processed %d %s in ", add_files_state.processed_files,
504 add_files_state.processed_files == 1 ?
505 "file" : "total files");
506 notmuch_time_print_formatted_seconds (elapsed);
508 printf (" (%d files/sec.). \n",
509 (int) (add_files_state.processed_files / elapsed));
514 if (add_files_state.added_messages) {
515 printf ("Added %d new %s to the database (not much, really).\n",
516 add_files_state.added_messages,
517 add_files_state.added_messages == 1 ?
518 "message" : "messages");
520 printf ("No new mail---and that's not much.\n");
523 if (elapsed > 1 && ! add_files_state.saw_read_only_directory) {
524 printf ("\nTip: If you have any sub-directories that are archives (that is,\n"
525 "they will never receive new mail), marking these directories as\n"
526 "read-only (chmod u-w /path/to/dir) will make \"notmuch new\"\n"
527 "much more efficient (it won't even look in those directories).\n");
531 printf ("\nNote: At least one error was encountered: %s\n",
532 notmuch_status_to_string (ret));
535 notmuch_database_close (notmuch);
537 return ret || interrupted;