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"
23 static volatile sig_atomic_t do_add_files_print_progress = 0;
26 handle_sigalrm (unused (int signal))
28 do_add_files_print_progress = 1;
32 add_files_print_progress (add_files_state_t *state)
34 struct timeval tv_now;
35 double elapsed_overall, rate_overall;
37 gettimeofday (&tv_now, NULL);
39 elapsed_overall = notmuch_time_elapsed (state->tv_start, tv_now);
40 rate_overall = (state->processed_files) / elapsed_overall;
42 printf ("Processed %d", state->processed_files);
44 if (state->total_files) {
45 double time_remaining;
47 time_remaining = ((state->total_files - state->processed_files) /
49 printf (" of %d files (", state->total_files);
50 notmuch_time_print_formatted_seconds (time_remaining);
51 printf (" remaining). \r");
53 printf (" files (%d files/sec.) \r", (int) rate_overall);
59 /* Examine 'path' recursively as follows:
61 * o Ask the filesystem for the mtime of 'path' (path_mtime)
63 * o Ask the database for its timestamp of 'path' (path_dbtime)
65 * o If 'path_mtime' > 'path_dbtime'
67 * o For each regular file in 'path' with mtime newer than the
68 * 'path_dbtime' call add_message to add the file to the
71 * o For each sub-directory of path, recursively call into this
74 * o Tell the database to update its time of 'path' to 'path_mtime'
76 * The 'struct stat *st' must point to a structure that has already
77 * been initialized for 'path' by calling stat().
79 static notmuch_status_t
80 add_files_recursive (notmuch_database_t *notmuch,
83 add_files_state_t *state)
86 struct dirent *e, *entry = NULL;
90 time_t path_mtime, path_dbtime;
91 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
92 notmuch_message_t *message = NULL, **closure;
94 /* If we're told to, we bail out on encountering a read-only
95 * directory, (with this being a clear clue from the user to
96 * Notmuch that new mail won't be arriving there and we need not
98 if (state->ignore_read_only_directories &&
99 (st->st_mode & S_IWUSR) == 0)
101 state->saw_read_only_directory = TRUE;
105 path_mtime = st->st_mtime;
107 path_dbtime = notmuch_database_get_timestamp (notmuch, path);
109 dir = opendir (path);
111 fprintf (stderr, "Error opening directory %s: %s\n",
112 path, strerror (errno));
113 ret = NOTMUCH_STATUS_FILE_ERROR;
117 entry_length = offsetof (struct dirent, d_name) +
118 pathconf (path, _PC_NAME_MAX) + 1;
119 entry = malloc (entry_length);
122 err = readdir_r (dir, entry, &e);
124 fprintf (stderr, "Error reading directory: %s\n",
126 ret = NOTMUCH_STATUS_FILE_ERROR;
133 /* If this directory hasn't been modified since the last
134 * add_files, then we only need to look further for
135 * sub-directories. */
136 if (path_mtime <= path_dbtime && entry->d_type != DT_DIR)
139 /* Ignore special directories to avoid infinite recursion.
140 * Also ignore the .notmuch directory.
142 /* XXX: Eventually we'll want more sophistication to let the
143 * user specify files to be ignored. */
144 if (strcmp (entry->d_name, ".") == 0 ||
145 strcmp (entry->d_name, "..") == 0 ||
146 strcmp (entry->d_name, ".notmuch") ==0)
151 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
153 if (stat (next, st)) {
154 fprintf (stderr, "Error reading %s: %s\n",
155 next, strerror (errno));
156 ret = NOTMUCH_STATUS_FILE_ERROR;
160 if (S_ISREG (st->st_mode)) {
161 /* If the file hasn't been modified since the last
162 * add_files, then we need not look at it. */
163 if (st->st_mtime > path_dbtime) {
164 state->processed_files++;
171 status = notmuch_database_add_message (notmuch, next, closure);
174 case NOTMUCH_STATUS_SUCCESS:
175 state->added_messages++;
177 (state->callback) (message);
179 /* Non-fatal issues (go on to next file) */
180 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
181 /* Stay silent on this one. */
183 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
184 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
187 /* Fatal issues. Don't process anymore. */
188 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
189 case NOTMUCH_STATUS_OUT_OF_MEMORY:
190 fprintf (stderr, "Error: %s. Halting processing.\n",
191 notmuch_status_to_string (status));
195 case NOTMUCH_STATUS_FILE_ERROR:
196 case NOTMUCH_STATUS_NULL_POINTER:
197 case NOTMUCH_STATUS_TAG_TOO_LONG:
198 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
199 case NOTMUCH_STATUS_LAST_STATUS:
200 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
205 notmuch_message_destroy (message);
209 if (do_add_files_print_progress) {
210 do_add_files_print_progress = 0;
211 add_files_print_progress (state);
214 } else if (S_ISDIR (st->st_mode)) {
215 status = add_files_recursive (notmuch, next, st, state);
216 if (status && ret == NOTMUCH_STATUS_SUCCESS)
224 status = notmuch_database_set_timestamp (notmuch, path, path_mtime);
225 if (status && ret == NOTMUCH_STATUS_SUCCESS)
239 /* This is the top-level entry point for add_files. It does a couple
240 * of error checks, sets up the progress-printing timer and then calls
241 * into the recursive function. */
243 add_files (notmuch_database_t *notmuch,
245 add_files_state_t *state)
248 notmuch_status_t status;
249 struct sigaction action;
250 struct itimerval timerval;
252 if (stat (path, &st)) {
253 fprintf (stderr, "Error reading directory %s: %s\n",
254 path, strerror (errno));
255 return NOTMUCH_STATUS_FILE_ERROR;
258 if (! S_ISDIR (st.st_mode)) {
259 fprintf (stderr, "Error: %s is not a directory.\n", path);
260 return NOTMUCH_STATUS_FILE_ERROR;
263 /* Setup our handler for SIGALRM */
264 memset (&action, 0, sizeof (struct sigaction));
265 action.sa_handler = handle_sigalrm;
266 sigemptyset (&action.sa_mask);
267 action.sa_flags = SA_RESTART;
268 sigaction (SIGALRM, &action, NULL);
270 /* Then start a timer to send SIGALRM once per second. */
271 timerval.it_interval.tv_sec = 1;
272 timerval.it_interval.tv_usec = 0;
273 timerval.it_value.tv_sec = 1;
274 timerval.it_value.tv_usec = 0;
275 setitimer (ITIMER_REAL, &timerval, NULL);
277 status = add_files_recursive (notmuch, path, &st, state);
279 /* Now stop the timer. */
280 timerval.it_interval.tv_sec = 0;
281 timerval.it_interval.tv_usec = 0;
282 timerval.it_value.tv_sec = 0;
283 timerval.it_value.tv_usec = 0;
284 setitimer (ITIMER_REAL, &timerval, NULL);
286 /* And disable the signal handler. */
287 action.sa_handler = SIG_IGN;
288 sigaction (SIGALRM, &action, NULL);