]> git.notmuchmail.org Git - notmuch/blob - add-files.c
7b9639b7d33ab8b76eba15977aa9297a82915878
[notmuch] / add-files.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22
23 static volatile sig_atomic_t do_add_files_print_progress = 0;
24
25 static void
26 handle_sigalrm (unused (int signal))
27 {
28     do_add_files_print_progress = 1;
29 }
30
31 static void
32 add_files_print_progress (add_files_state_t *state)
33 {
34     struct timeval tv_now;
35     double elapsed_overall, rate_overall;
36
37     gettimeofday (&tv_now, NULL);
38
39     elapsed_overall = notmuch_time_elapsed (state->tv_start, tv_now);
40     rate_overall = (state->processed_files) / elapsed_overall;
41
42     printf ("Processed %d", state->processed_files);
43
44     if (state->total_files) {
45         double time_remaining;
46
47         time_remaining = ((state->total_files - state->processed_files) /
48                           rate_overall);
49         printf (" of %d files (", state->total_files);
50         notmuch_time_print_formatted_seconds (time_remaining);
51         printf (" remaining).      \r");
52     } else {
53         printf (" files (%d files/sec.)    \r", (int) rate_overall);
54     }
55
56     fflush (stdout);
57 }
58
59 /* Examine 'path' recursively as follows:
60  *
61  *   o Ask the filesystem for the mtime of 'path' (path_mtime)
62  *
63  *   o Ask the database for its timestamp of 'path' (path_dbtime)
64  *
65  *   o If 'path_mtime' > 'path_dbtime'
66  *
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
69  *         database.
70  *
71  *       o For each sub-directory of path, recursively call into this
72  *         same function.
73  *
74  *   o Tell the database to update its time of 'path' to 'path_mtime'
75  *
76  * The 'struct stat *st' must point to a structure that has already
77  * been initialized for 'path' by calling stat().
78  */
79 static notmuch_status_t
80 add_files_recursive (notmuch_database_t *notmuch,
81                      const char *path,
82                      struct stat *st,
83                      add_files_state_t *state)
84 {
85     DIR *dir = NULL;
86     struct dirent *e, *entry = NULL;
87     int entry_length;
88     int err;
89     char *next = NULL;
90     time_t path_mtime, path_dbtime;
91     notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
92     notmuch_message_t *message = NULL, **closure;
93
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
97      * look. */
98     if (state->ignore_read_only_directories &&
99         (st->st_mode & S_IWUSR) == 0)
100     {
101         state->saw_read_only_directory = TRUE;
102         goto DONE;
103     }
104
105     path_mtime = st->st_mtime;
106
107     path_dbtime = notmuch_database_get_timestamp (notmuch, path);
108
109     dir = opendir (path);
110     if (dir == NULL) {
111         fprintf (stderr, "Error opening directory %s: %s\n",
112                  path, strerror (errno));
113         ret = NOTMUCH_STATUS_FILE_ERROR;
114         goto DONE;
115     }
116
117     entry_length = offsetof (struct dirent, d_name) +
118         pathconf (path, _PC_NAME_MAX) + 1;
119     entry = malloc (entry_length);
120
121     while (1) {
122         err = readdir_r (dir, entry, &e);
123         if (err) {
124             fprintf (stderr, "Error reading directory: %s\n",
125                      strerror (errno));
126             ret = NOTMUCH_STATUS_FILE_ERROR;
127             goto DONE;
128         }
129
130         if (e == NULL)
131             break;
132
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)
137             continue;
138
139         /* Ignore special directories to avoid infinite recursion.
140          * Also ignore the .notmuch directory.
141          */
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)
147         {
148             continue;
149         }
150
151         next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
152
153         if (stat (next, st)) {
154             fprintf (stderr, "Error reading %s: %s\n",
155                      next, strerror (errno));
156             ret = NOTMUCH_STATUS_FILE_ERROR;
157             continue;
158         }
159
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++;
165
166                 if (state->callback)
167                     closure = &message;
168                 else
169                     closure = NULL;
170
171                 status = notmuch_database_add_message (notmuch, next, closure);
172                 switch (status) {
173                     /* success */
174                     case NOTMUCH_STATUS_SUCCESS:
175                         state->added_messages++;
176                         if (state->callback)
177                             (state->callback) (message);
178                         break;
179                     /* Non-fatal issues (go on to next file) */
180                     case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
181                         /* Stay silent on this one. */
182                         break;
183                     case NOTMUCH_STATUS_FILE_NOT_EMAIL:
184                         fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
185                                  next);
186                         break;
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));
192                         ret = status;
193                         goto DONE;
194                     default:
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);
201                         goto DONE;
202                 }
203
204                 if (message) {
205                     notmuch_message_destroy (message);
206                     message = NULL;
207                 }
208
209                 if (do_add_files_print_progress) {
210                     do_add_files_print_progress = 0;
211                     add_files_print_progress (state);
212                 }
213             }
214         } else if (S_ISDIR (st->st_mode)) {
215             status = add_files_recursive (notmuch, next, st, state);
216             if (status && ret == NOTMUCH_STATUS_SUCCESS)
217                 ret = status;
218         }
219
220         talloc_free (next);
221         next = NULL;
222     }
223
224     status = notmuch_database_set_timestamp (notmuch, path, path_mtime);
225     if (status && ret == NOTMUCH_STATUS_SUCCESS)
226         ret = status;
227
228   DONE:
229     if (next)
230         talloc_free (next);
231     if (entry)
232         free (entry);
233     if (dir)
234         closedir (dir);
235
236     return ret;
237 }
238
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. */
242 notmuch_status_t
243 add_files (notmuch_database_t *notmuch,
244            const char *path,
245            add_files_state_t *state)
246 {
247     struct stat st;
248     notmuch_status_t status;
249     struct sigaction action;
250     struct itimerval timerval;
251
252     if (stat (path, &st)) {
253         fprintf (stderr, "Error reading directory %s: %s\n",
254                  path, strerror (errno));
255         return NOTMUCH_STATUS_FILE_ERROR;
256     }
257
258     if (! S_ISDIR (st.st_mode)) {
259         fprintf (stderr, "Error: %s is not a directory.\n", path);
260         return NOTMUCH_STATUS_FILE_ERROR;
261     }
262
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);
269
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);
276
277     status = add_files_recursive (notmuch, path, &st, state);
278
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);
285
286     /* And disable the signal handler. */
287     action.sa_handler = SIG_IGN;
288     sigaction (SIGALRM, &action, NULL);
289
290     return status;
291 }