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 /* Examine 'path' recursively as follows:
85 * o Ask the filesystem for the mtime of 'path' (path_mtime)
87 * o Ask the database for its timestamp of 'path' (path_dbtime)
89 * o If 'path_mtime' > 'path_dbtime'
91 * o For each regular file in 'path' with mtime newer than the
92 * 'path_dbtime' call add_message to add the file to the
95 * o For each sub-directory of path, recursively call into this
98 * o Tell the database to update its time of 'path' to 'path_mtime'
100 * The 'struct stat *st' must point to a structure that has already
101 * been initialized for 'path' by calling stat().
103 static notmuch_status_t
104 add_files_recursive (notmuch_database_t *notmuch,
107 add_files_state_t *state)
110 struct dirent *entry = NULL;
112 time_t path_mtime, path_dbtime;
113 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
114 notmuch_message_t *message = NULL;
115 struct dirent **namelist = NULL;
118 /* If we're told to, we bail out on encountering a read-only
119 * directory, (with this being a clear clue from the user to
120 * Notmuch that new mail won't be arriving there and we need not
122 if (state->ignore_read_only_directories &&
123 (st->st_mode & S_IWUSR) == 0)
125 state->saw_read_only_directory = TRUE;
129 path_mtime = st->st_mtime;
131 path_dbtime = notmuch_database_get_timestamp (notmuch, path);
132 num_entries = scandir (path, &namelist, 0, ino_cmp);
134 if (num_entries == -1) {
135 fprintf (stderr, "Error opening directory %s: %s\n",
136 path, strerror (errno));
137 ret = NOTMUCH_STATUS_FILE_ERROR;
143 while (!interrupted) {
144 if (i == num_entries)
147 entry= namelist[i++];
149 /* If this directory hasn't been modified since the last
150 * add_files, then we only need to look further for
151 * sub-directories. */
152 if (path_mtime <= path_dbtime && entry->d_type != DT_DIR)
155 /* Ignore special directories to avoid infinite recursion.
156 * Also ignore the .notmuch directory.
158 /* XXX: Eventually we'll want more sophistication to let the
159 * user specify files to be ignored. */
160 if (strcmp (entry->d_name, ".") == 0 ||
161 strcmp (entry->d_name, "..") == 0 ||
162 strcmp (entry->d_name, "tmp") == 0 ||
163 strcmp (entry->d_name, ".notmuch") ==0)
168 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
170 if (stat (next, st)) {
171 fprintf (stderr, "Error reading %s: %s\n",
172 next, strerror (errno));
173 ret = NOTMUCH_STATUS_FILE_ERROR;
177 if (S_ISREG (st->st_mode)) {
178 /* If the file hasn't been modified since the last
179 * add_files, then we need not look at it. */
180 if (path_dbtime == 0 || st->st_mtime > path_dbtime) {
181 state->processed_files++;
183 if (state->verbose) {
184 if (state->output_is_a_tty)
188 state->processed_files,
192 putchar((state->output_is_a_tty) ? '\r' : '\n');
196 status = notmuch_database_add_message (notmuch, next, &message);
199 case NOTMUCH_STATUS_SUCCESS:
200 state->added_messages++;
201 tag_inbox_and_unread (message);
203 /* Non-fatal issues (go on to next file) */
204 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
205 /* Stay silent on this one. */
207 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
208 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
211 /* Fatal issues. Don't process anymore. */
212 case NOTMUCH_STATUS_READONLY_DATABASE:
213 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
214 case NOTMUCH_STATUS_OUT_OF_MEMORY:
215 fprintf (stderr, "Error: %s. Halting processing.\n",
216 notmuch_status_to_string (status));
220 case NOTMUCH_STATUS_FILE_ERROR:
221 case NOTMUCH_STATUS_NULL_POINTER:
222 case NOTMUCH_STATUS_TAG_TOO_LONG:
223 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
224 case NOTMUCH_STATUS_LAST_STATUS:
225 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
230 notmuch_message_destroy (message);
234 if (do_add_files_print_progress) {
235 do_add_files_print_progress = 0;
236 add_files_print_progress (state);
239 } else if (S_ISDIR (st->st_mode)) {
240 status = add_files_recursive (notmuch, next, st, state);
241 if (status && ret == NOTMUCH_STATUS_SUCCESS)
249 status = notmuch_database_set_timestamp (notmuch, path, path_mtime);
250 if (status && ret == NOTMUCH_STATUS_SUCCESS)
266 /* This is the top-level entry point for add_files. It does a couple
267 * of error checks, sets up the progress-printing timer and then calls
268 * into the recursive function. */
270 add_files (notmuch_database_t *notmuch,
272 add_files_state_t *state)
275 notmuch_status_t status;
276 struct sigaction action;
277 struct itimerval timerval;
278 notmuch_bool_t timer_is_active = FALSE;
280 if (stat (path, &st)) {
281 fprintf (stderr, "Error reading directory %s: %s\n",
282 path, strerror (errno));
283 return NOTMUCH_STATUS_FILE_ERROR;
286 if (! S_ISDIR (st.st_mode)) {
287 fprintf (stderr, "Error: %s is not a directory.\n", path);
288 return NOTMUCH_STATUS_FILE_ERROR;
291 if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {
292 /* Setup our handler for SIGALRM */
293 memset (&action, 0, sizeof (struct sigaction));
294 action.sa_handler = handle_sigalrm;
295 sigemptyset (&action.sa_mask);
296 action.sa_flags = SA_RESTART;
297 sigaction (SIGALRM, &action, NULL);
299 /* Then start a timer to send SIGALRM once per second. */
300 timerval.it_interval.tv_sec = 1;
301 timerval.it_interval.tv_usec = 0;
302 timerval.it_value.tv_sec = 1;
303 timerval.it_value.tv_usec = 0;
304 setitimer (ITIMER_REAL, &timerval, NULL);
306 timer_is_active = TRUE;
309 status = add_files_recursive (notmuch, path, &st, state);
311 if (timer_is_active) {
312 /* Now stop the timer. */
313 timerval.it_interval.tv_sec = 0;
314 timerval.it_interval.tv_usec = 0;
315 timerval.it_value.tv_sec = 0;
316 timerval.it_value.tv_usec = 0;
317 setitimer (ITIMER_REAL, &timerval, NULL);
319 /* And disable the signal handler. */
320 action.sa_handler = SIG_IGN;
321 sigaction (SIGALRM, &action, NULL);
327 /* XXX: This should be merged with the add_files function since it
328 * shares a lot of logic with it. */
329 /* Recursively count all regular files in path and all sub-directories
330 * of path. The result is added to *count (which should be
331 * initialized to zero by the top-level caller before calling
334 count_files (const char *path, int *count)
336 struct dirent *entry = NULL;
339 struct dirent **namelist = NULL;
340 int n_entries = scandir (path, &namelist, 0, ino_cmp);
343 if (n_entries == -1) {
344 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
345 path, strerror (errno));
349 while (!interrupted) {
353 entry= namelist[i++];
355 /* Ignore special directories to avoid infinite recursion.
356 * Also ignore the .notmuch directory.
358 /* XXX: Eventually we'll want more sophistication to let the
359 * user specify files to be ignored. */
360 if (strcmp (entry->d_name, ".") == 0 ||
361 strcmp (entry->d_name, "..") == 0 ||
362 strcmp (entry->d_name, ".notmuch") == 0)
367 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
369 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
370 path, entry->d_name);
376 if (S_ISREG (st.st_mode)) {
378 if (*count % 1000 == 0) {
379 printf ("Found %d files so far.\r", *count);
382 } else if (S_ISDIR (st.st_mode)) {
383 count_files (next, count);
397 notmuch_new_command (void *ctx, int argc, char *argv[])
399 notmuch_config_t *config;
400 notmuch_database_t *notmuch;
401 add_files_state_t add_files_state;
403 struct timeval tv_now;
407 char *dot_notmuch_path;
408 struct sigaction action;
411 add_files_state.verbose = 0;
412 add_files_state.output_is_a_tty = isatty (fileno (stdout));
414 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
415 if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
416 add_files_state.verbose = 1;
418 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
423 /* Setup our handler for SIGINT */
424 memset (&action, 0, sizeof (struct sigaction));
425 action.sa_handler = handle_sigint;
426 sigemptyset (&action.sa_mask);
427 action.sa_flags = SA_RESTART;
428 sigaction (SIGINT, &action, NULL);
430 config = notmuch_config_open (ctx, NULL, NULL);
434 db_path = notmuch_config_get_database_path (config);
436 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
438 if (stat (dot_notmuch_path, &st)) {
442 count_files (db_path, &count);
446 printf ("Found %d total files. \n", count);
447 notmuch = notmuch_database_create (db_path);
448 add_files_state.ignore_read_only_directories = FALSE;
449 add_files_state.total_files = count;
451 notmuch = notmuch_database_open (db_path,
452 NOTMUCH_DATABASE_MODE_READ_WRITE);
453 add_files_state.ignore_read_only_directories = TRUE;
454 add_files_state.total_files = 0;
460 talloc_free (dot_notmuch_path);
461 dot_notmuch_path = NULL;
463 add_files_state.saw_read_only_directory = FALSE;
464 add_files_state.processed_files = 0;
465 add_files_state.added_messages = 0;
466 gettimeofday (&add_files_state.tv_start, NULL);
468 ret = add_files (notmuch, db_path, &add_files_state);
470 gettimeofday (&tv_now, NULL);
471 elapsed = notmuch_time_elapsed (add_files_state.tv_start,
473 if (add_files_state.processed_files) {
474 printf ("Processed %d %s in ", add_files_state.processed_files,
475 add_files_state.processed_files == 1 ?
476 "file" : "total files");
477 notmuch_time_print_formatted_seconds (elapsed);
479 printf (" (%d files/sec.). \n",
480 (int) (add_files_state.processed_files / elapsed));
485 if (add_files_state.added_messages) {
486 printf ("Added %d new %s to the database (not much, really).\n",
487 add_files_state.added_messages,
488 add_files_state.added_messages == 1 ?
489 "message" : "messages");
491 printf ("No new mail---and that's not much.\n");
494 if (elapsed > 1 && ! add_files_state.saw_read_only_directory) {
495 printf ("\nTip: If you have any sub-directories that are archives (that is,\n"
496 "they will never receive new mail), marking these directories as\n"
497 "read-only (chmod u-w /path/to/dir) will make \"notmuch new\"\n"
498 "much more efficient (it won't even look in those directories).\n");
502 printf ("\nNote: At least one error was encountered: %s\n",
503 notmuch_status_to_string (ret));
506 notmuch_database_close (notmuch);
508 return ret || interrupted;