[notmuch] [PATCH 2/4] Convert mailstore abstraction
[notmuch-archives.git] / 30 / 72a673eeadceb9f290a39385ee6a6138c4ff20
1 Return-Path: <sojkam1@fel.cvut.cz>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 154F7404964\r
6         for <notmuch@notmuchmail.org>; Thu, 18 Mar 2010 08:41:03 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.272\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.272 tagged_above=-999 required=5 tests=[AWL=0.327,\r
12         BAYES_00=-2.599] autolearn=ham\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id QOzVBAr5IYSK for <notmuch@notmuchmail.org>;\r
16         Thu, 18 Mar 2010 08:40:57 -0700 (PDT)\r
17 Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
18         by olra.theworths.org (Postfix) with ESMTP id E86A040495F\r
19         for <notmuch@notmuchmail.org>; Thu, 18 Mar 2010 08:40:54 -0700 (PDT)\r
20 Received: from localhost (unknown [192.168.200.4])\r
21         by max.feld.cvut.cz (Postfix) with ESMTP id 542C419F3401;\r
22         Thu, 18 Mar 2010 16:40:54 +0100 (CET)\r
23 X-Virus-Scanned: IMAP AMAVIS\r
24 Received: from max.feld.cvut.cz ([192.168.200.1])\r
25         by localhost (styx.feld.cvut.cz [192.168.200.4]) (amavisd-new,\r
26         port 10044)\r
27         with ESMTP id jEwR7Ce5rT+J; Thu, 18 Mar 2010 16:40:51 +0100 (CET)\r
28 Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
29         by max.feld.cvut.cz (Postfix) with ESMTP id 6262719F3406;\r
30         Thu, 18 Mar 2010 16:40:51 +0100 (CET)\r
31 Received: from steelpick.localdomain (k335-30.felk.cvut.cz [147.32.86.30])\r
32         (Authenticated sender: sojkam1)\r
33         by imap.feld.cvut.cz (Postfix) with ESMTPSA id 37B5DFA005;\r
34         Thu, 18 Mar 2010 16:40:51 +0100 (CET)\r
35 Received: from wsh by steelpick.localdomain with local (Exim 4.71)\r
36         (envelope-from <sojkam1@fel.cvut.cz>)\r
37         id 1NsHq6-0005Hj-Uu; Thu, 18 Mar 2010 16:40:51 +0100\r
38 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
39 To: notmuch@notmuchmail.org\r
40 Date: Thu, 18 Mar 2010 16:39:38 +0100\r
41 Message-Id: <1268926780-20045-3-git-send-email-sojkam1@fel.cvut.cz>\r
42 X-Mailer: git-send-email 1.7.0\r
43 In-Reply-To: <1268926780-20045-1-git-send-email-sojkam1@fel.cvut.cz>\r
44 References: <1268926780-20045-1-git-send-email-sojkam1@fel.cvut.cz>\r
45 Subject: [notmuch] [PATCH 2/4] Convert mailstore abstraction\r
46 X-BeenThere: notmuch@notmuchmail.org\r
47 X-Mailman-Version: 2.1.13\r
48 Precedence: list\r
49 List-Id: "Use and development of the notmuch mail system."\r
50         <notmuch.notmuchmail.org>\r
51 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
52         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
53 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
54 List-Post: <mailto:notmuch@notmuchmail.org>\r
55 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
56 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
57         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
58 X-List-Received-Date: Thu, 18 Mar 2010 15:41:03 -0000\r
59 \r
60 The code for detection of new files in the mailstore and their\r
61 addition to the database is moved from notmuch-new.c to\r
62 lib/mailstore-files.c, where it is called by the abstract mailstore\r
63 interface.\r
64 \r
65 The code was changed to allow the progress reporting function to be\r
66 implemented outside of notmuch library.\r
67 \r
68 Signed-off-by: Michal Sojka <sojkam1@fel.cvut.cz>\r
69 ---\r
70  lib/mailstore-files.c |  590 +++++++++++++++++++++++++++++++++++++++++++++++\r
71  notmuch-new.c         |  611 ++-----------------------------------------------\r
72  2 files changed, 615 insertions(+), 586 deletions(-)\r
73 \r
74 diff --git a/lib/mailstore-files.c b/lib/mailstore-files.c\r
75 index 92d7f5d..ace2664 100644\r
76 --- a/lib/mailstore-files.c\r
77 +++ b/lib/mailstore-files.c\r
78 @@ -20,9 +20,596 @@\r
79   *         Michal Sojka <sojkam1@fel.cvut.cz>\r
80   */\r
81  \r
82 +#define _GNU_SOURCE            /* For asprintf() */\r
83  #include "notmuch.h"\r
84  #include "mailstore-private.h"\r
85 +#include <dirent.h>\r
86  \r
87 +typedef struct _filename_node {\r
88 +    char *filename;\r
89 +    struct _filename_node *next;\r
90 +} _filename_node_t;\r
91 +\r
92 +typedef struct _filename_list {\r
93 +    _filename_node_t *head;\r
94 +    _filename_node_t **tail;\r
95 +} _filename_list_t;\r
96 +\r
97 +typedef struct _indexing_context_priv {\r
98 +    _filename_list_t *removed_files;\r
99 +    _filename_list_t *removed_directories;\r
100 +} _indexing_context_priv_t;\r
101 +\r
102 +static _filename_list_t *\r
103 +_filename_list_create (const void *ctx)\r
104 +{\r
105 +    _filename_list_t *list;\r
106 +\r
107 +    list = talloc (ctx, _filename_list_t);\r
108 +    if (list == NULL)\r
109 +       return NULL;\r
110 +\r
111 +    list->head = NULL;\r
112 +    list->tail = &list->head;\r
113 +\r
114 +    return list;\r
115 +}\r
116 +\r
117 +static void\r
118 +_filename_list_add (_filename_list_t *list,\r
119 +                   const char *filename)\r
120 +{\r
121 +    _filename_node_t *node = talloc (list, _filename_node_t);\r
122 +\r
123 +    node->filename = talloc_strdup (list, filename);\r
124 +    node->next = NULL;\r
125 +\r
126 +    *(list->tail) = node;\r
127 +    list->tail = &node->next;\r
128 +}\r
129 +\r
130 +static void\r
131 +tag_inbox_and_unread (notmuch_message_t *message)\r
132 +{\r
133 +    notmuch_message_add_tag (message, "inbox");\r
134 +    notmuch_message_add_tag (message, "unread");\r
135 +}\r
136 +\r
137 +static int\r
138 +dirent_sort_inode (const struct dirent **a, const struct dirent **b)\r
139 +{\r
140 +    return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;\r
141 +}\r
142 +\r
143 +static int\r
144 +dirent_sort_strcmp_name (const struct dirent **a, const struct dirent **b)\r
145 +{\r
146 +    return strcmp ((*a)->d_name, (*b)->d_name);\r
147 +}\r
148 +\r
149 +/* Test if the directory looks like a Maildir directory.\r
150 + *\r
151 + * Search through the array of directory entries to see if we can find all\r
152 + * three subdirectories typical for Maildir, that is "new", "cur", and "tmp".\r
153 + *\r
154 + * Return 1 if the directory looks like a Maildir and 0 otherwise.\r
155 + */\r
156 +static int\r
157 +_entries_resemble_maildir (struct dirent **entries, int count)\r
158 +{\r
159 +    int i, found = 0;\r
160 +\r
161 +    for (i = 0; i < count; i++) {\r
162 +       if (entries[i]->d_type != DT_DIR && entries[i]->d_type != DT_UNKNOWN)\r
163 +           continue;\r
164 +\r
165 +       if (strcmp(entries[i]->d_name, "new") == 0 ||\r
166 +           strcmp(entries[i]->d_name, "cur") == 0 ||\r
167 +           strcmp(entries[i]->d_name, "tmp") == 0)\r
168 +       {\r
169 +           found++;\r
170 +           if (found == 3)\r
171 +               return 1;\r
172 +       }\r
173 +    }\r
174 +\r
175 +    return 0;\r
176 +}\r
177 +\r
178 +\r
179 +/* Examine 'path' recursively as follows:\r
180 + *\r
181 + *   o Ask the filesystem for the mtime of 'path' (fs_mtime)\r
182 + *   o Ask the database for its timestamp of 'path' (db_mtime)\r
183 + *\r
184 + *   o Ask the filesystem for files and directories within 'path'\r
185 + *     (via scandir and stored in fs_entries)\r
186 + *   o Ask the database for files and directories within 'path'\r
187 + *     (db_files and db_subdirs)\r
188 + *\r
189 + *   o Pass 1: For each directory in fs_entries, recursively call into\r
190 + *     this same function.\r
191 + *\r
192 + *   o Pass 2: If 'fs_mtime' > 'db_mtime', then walk fs_entries\r
193 + *     simultaneously with db_files and db_subdirs. Look for one of\r
194 + *     three interesting cases:\r
195 + *\r
196 + *        1. Regular file in fs_entries and not in db_files\r
197 + *            This is a new file to add_message into the database.\r
198 + *\r
199 + *         2. Filename in db_files not in fs_entries.\r
200 + *            This is a file that has been removed from the mail store.\r
201 + *\r
202 + *         3. Directory in db_subdirs not in fs_entries\r
203 + *            This is a directory that has been removed from the mail store.\r
204 + *\r
205 + *     Note that the addition of a directory is not interesting here,\r
206 + *     since that will have been taken care of in pass 1. Also, we\r
207 + *     don't immediately act on file/directory removal since we must\r
208 + *     ensure that in the case of a rename that the new filename is\r
209 + *     added before the old filename is removed, (so that no\r
210 + *     information is lost from the database).\r
211 + *\r
212 + *   o Tell the database to update its time of 'path' to 'fs_mtime'\r
213 + */\r
214 +static notmuch_status_t\r
215 +add_files_recursive (notmuch_mailstore_t *mailstore,\r
216 +                    const char *path,\r
217 +                    notmuch_indexing_context_t *state)\r
218 +{\r
219 +    DIR *dir = NULL;\r
220 +    struct dirent *entry = NULL;\r
221 +    char *next = NULL;\r
222 +    time_t fs_mtime, db_mtime;\r
223 +    notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;\r
224 +    notmuch_message_t *message = NULL;\r
225 +    struct dirent **fs_entries = NULL;\r
226 +    int i, num_fs_entries;\r
227 +    notmuch_directory_t *directory;\r
228 +    notmuch_filenames_t *db_files = NULL;\r
229 +    notmuch_filenames_t *db_subdirs = NULL;\r
230 +    struct stat st;\r
231 +    notmuch_bool_t is_maildir, new_directory;\r
232 +    _indexing_context_priv_t *priv = state->priv;\r
233 +    notmuch_database_t *notmuch = mailstore->notmuch;\r
234 +\r
235 +    if (stat (path, &st)) {\r
236 +       fprintf (stderr, "Error reading directory %s: %s\n",\r
237 +                path, strerror (errno));\r
238 +       return NOTMUCH_STATUS_FILE_ERROR;\r
239 +    }\r
240 +\r
241 +    /* This is not an error since we may have recursed based on a\r
242 +     * symlink to a regular file, not a directory, and we don't know\r
243 +     * that until this stat. */\r
244 +    if (! S_ISDIR (st.st_mode))\r
245 +       return NOTMUCH_STATUS_SUCCESS;\r
246 +\r
247 +    fs_mtime = st.st_mtime;\r
248 +\r
249 +    directory = notmuch_database_get_directory (notmuch, path);\r
250 +    db_mtime = notmuch_directory_get_mtime (directory);\r
251 +\r
252 +    if (db_mtime == 0) {\r
253 +       new_directory = TRUE;\r
254 +       db_files = NULL;\r
255 +       db_subdirs = NULL;\r
256 +    } else {\r
257 +       new_directory = FALSE;\r
258 +       db_files = notmuch_directory_get_child_files (directory);\r
259 +       db_subdirs = notmuch_directory_get_child_directories (directory);\r
260 +    }\r
261 +\r
262 +    /* If the database knows about this directory, then we sort based\r
263 +     * on strcmp to match the database sorting. Otherwise, we can do\r
264 +     * inode-based sorting for faster filesystem operation. */\r
265 +    num_fs_entries = scandir (path, &fs_entries, 0,\r
266 +                             new_directory ?\r
267 +                             dirent_sort_inode : dirent_sort_strcmp_name);\r
268 +\r
269 +    if (num_fs_entries == -1) {\r
270 +       fprintf (stderr, "Error opening directory %s: %s\n",\r
271 +                path, strerror (errno));\r
272 +       ret = NOTMUCH_STATUS_FILE_ERROR;\r
273 +       goto DONE;\r
274 +    }\r
275 +\r
276 +    /* Pass 1: Recurse into all sub-directories. */\r
277 +    is_maildir = _entries_resemble_maildir (fs_entries, num_fs_entries);\r
278 +\r
279 +    for (i = 0; i < num_fs_entries; i++) {\r
280 +       if (state->interrupted)\r
281 +           break;\r
282 +\r
283 +       entry = fs_entries[i];\r
284 +\r
285 +       /* We only want to descend into directories.\r
286 +        * But symlinks can be to directories too, of course.\r
287 +        *\r
288 +        * And if the filesystem doesn't tell us the file type in the\r
289 +        * scandir results, then it might be a directory (and if not,\r
290 +        * then we'll stat and return immediately in the next level of\r
291 +        * recursion). */\r
292 +       if (entry->d_type != DT_DIR &&\r
293 +           entry->d_type != DT_LNK &&\r
294 +           entry->d_type != DT_UNKNOWN)\r
295 +       {\r
296 +           continue;\r
297 +       }\r
298 +\r
299 +       /* Ignore special directories to avoid infinite recursion.\r
300 +        * Also ignore the .notmuch directory and any "tmp" directory\r
301 +        * that appears within a maildir.\r
302 +        */\r
303 +       /* XXX: Eventually we'll want more sophistication to let the\r
304 +        * user specify files to be ignored. */\r
305 +       if (strcmp (entry->d_name, ".") == 0 ||\r
306 +           strcmp (entry->d_name, "..") == 0 ||\r
307 +           (is_maildir && strcmp (entry->d_name, "tmp") == 0) ||\r
308 +           strcmp (entry->d_name, ".notmuch") ==0)\r
309 +       {\r
310 +           continue;\r
311 +       }\r
312 +\r
313 +       next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
314 +       status = add_files_recursive (mailstore, next, state);\r
315 +       if (status && ret == NOTMUCH_STATUS_SUCCESS)\r
316 +           ret = status;\r
317 +       talloc_free (next);\r
318 +       next = NULL;\r
319 +    }\r
320 +\r
321 +    /* If this directory hasn't been modified since the last\r
322 +     * "notmuch new", then we can skip the second pass entirely. */\r
323 +    if (fs_mtime <= db_mtime)\r
324 +       goto DONE;\r
325 +\r
326 +    /* Pass 2: Scan for new files, removed files, and removed directories. */\r
327 +    for (i = 0; i < num_fs_entries; i++)\r
328 +    {\r
329 +       if (state->interrupted)\r
330 +           break;\r
331 +\r
332 +        entry = fs_entries[i];\r
333 +\r
334 +       /* Check if we've walked past any names in db_files or\r
335 +        * db_subdirs. If so, these have been deleted. */\r
336 +       while (notmuch_filenames_valid (db_files) &&\r
337 +              strcmp (notmuch_filenames_get (db_files), entry->d_name) < 0)\r
338 +       {\r
339 +           char *absolute = talloc_asprintf (priv->removed_files,\r
340 +                                             "%s/%s", path,\r
341 +                                             notmuch_filenames_get (db_files));\r
342 +\r
343 +           _filename_list_add (priv->removed_files, absolute);\r
344 +\r
345 +           notmuch_filenames_move_to_next (db_files);\r
346 +       }\r
347 +\r
348 +       while (notmuch_filenames_valid (db_subdirs) &&\r
349 +              strcmp (notmuch_filenames_get (db_subdirs), entry->d_name) <= 0)\r
350 +       {\r
351 +           const char *filename = notmuch_filenames_get (db_subdirs);\r
352 +\r
353 +           if (strcmp (filename, entry->d_name) < 0)\r
354 +           {\r
355 +               char *absolute = talloc_asprintf (priv->removed_directories,\r
356 +                                                 "%s/%s", path, filename);\r
357 +\r
358 +               _filename_list_add (priv->removed_directories, absolute);\r
359 +           }\r
360 +\r
361 +           notmuch_filenames_move_to_next (db_subdirs);\r
362 +       }\r
363 +\r
364 +       /* If we're looking at a symlink, we only want to add it if it\r
365 +        * links to a regular file, (and not to a directory, say).\r
366 +        *\r
367 +        * Similarly, if the file is of unknown type (due to filesytem\r
368 +        * limitations), then we also need to look closer.\r
369 +        *\r
370 +        * In either case, a stat does the trick.\r
371 +        */\r
372 +       if (entry->d_type == DT_LNK || entry->d_type == DT_UNKNOWN) {\r
373 +           int err;\r
374 +\r
375 +           next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
376 +           err = stat (next, &st);\r
377 +           talloc_free (next);\r
378 +           next = NULL;\r
379 +\r
380 +           /* Don't emit an error for a link pointing nowhere, since\r
381 +            * the directory-traversal pass will have already done\r
382 +            * that. */\r
383 +           if (err)\r
384 +               continue;\r
385 +\r
386 +           if (! S_ISREG (st.st_mode))\r
387 +               continue;\r
388 +       } else if (entry->d_type != DT_REG) {\r
389 +           continue;\r
390 +       }\r
391 +\r
392 +       /* Don't add a file that we've added before. */\r
393 +       if (notmuch_filenames_valid (db_files) &&\r
394 +           strcmp (notmuch_filenames_get (db_files), entry->d_name) == 0)\r
395 +       {\r
396 +           notmuch_filenames_move_to_next (db_files);\r
397 +           continue;\r
398 +       }\r
399 +\r
400 +       /* We're now looking at a regular file that doesn't yet exist\r
401 +        * in the database, so add it. */\r
402 +       next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
403 +\r
404 +       state->processed_files++;\r
405 +\r
406 +       if (state->verbose) {\r
407 +           if (state->output_is_a_tty)\r
408 +               printf("\r\033[K");\r
409 +\r
410 +           printf ("%i/%i: %s",\r
411 +                   state->processed_files,\r
412 +                   state->total_files,\r
413 +                   next);\r
414 +\r
415 +           putchar((state->output_is_a_tty) ? '\r' : '\n');\r
416 +           fflush (stdout);\r
417 +       }\r
418 +\r
419 +       status = notmuch_database_add_message (notmuch, next, &message);\r
420 +       switch (status) {\r
421 +       /* success */\r
422 +       case NOTMUCH_STATUS_SUCCESS:\r
423 +           state->added_messages++;\r
424 +           tag_inbox_and_unread (message);\r
425 +           break;\r
426 +       /* Non-fatal issues (go on to next file) */\r
427 +       case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
428 +           /* Stay silent on this one. */\r
429 +           break;\r
430 +       case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
431 +           fprintf (stderr, "Note: Ignoring non-mail file: %s\n",\r
432 +                    next);\r
433 +           break;\r
434 +       /* Fatal issues. Don't process anymore. */\r
435 +       case NOTMUCH_STATUS_READ_ONLY_DATABASE:\r
436 +       case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
437 +       case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
438 +           fprintf (stderr, "Error: %s. Halting processing.\n",\r
439 +                    notmuch_status_to_string (status));\r
440 +           ret = status;\r
441 +           goto DONE;\r
442 +       default:\r
443 +       case NOTMUCH_STATUS_FILE_ERROR:\r
444 +       case NOTMUCH_STATUS_NULL_POINTER:\r
445 +       case NOTMUCH_STATUS_TAG_TOO_LONG:\r
446 +       case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:\r
447 +       case NOTMUCH_STATUS_LAST_STATUS:\r
448 +           INTERNAL_ERROR ("add_message returned unexpected value: %d",  status);\r
449 +           goto DONE;\r
450 +       }\r
451 +\r
452 +       if (message) {\r
453 +           notmuch_message_destroy (message);\r
454 +           message = NULL;\r
455 +       }\r
456 +\r
457 +       if (state->print_progress &&\r
458 +           state->print_progress_cb) {\r
459 +           state->print_progress = 0;\r
460 +           state->print_progress_cb (state);\r
461 +       }\r
462 +\r
463 +       talloc_free (next);\r
464 +       next = NULL;\r
465 +    }\r
466 +\r
467 +    /* FIXME: Handle interrupted - there might be data loss */\r
468 +\r
469 +    /* Now that we've walked the whole filesystem list, anything left\r
470 +     * over in the database lists has been deleted. */\r
471 +    while (notmuch_filenames_valid (db_files))\r
472 +    {\r
473 +       char *absolute = talloc_asprintf (priv->removed_files,\r
474 +                                         "%s/%s", path,\r
475 +                                         notmuch_filenames_get (db_files));\r
476 +\r
477 +       _filename_list_add (priv->removed_files, absolute);\r
478 +\r
479 +       notmuch_filenames_move_to_next (db_files);\r
480 +    }\r
481 +\r
482 +    while (notmuch_filenames_valid (db_subdirs))\r
483 +    {\r
484 +       char *absolute = talloc_asprintf (priv->removed_directories,\r
485 +                                         "%s/%s", path,\r
486 +                                         notmuch_filenames_get (db_subdirs));\r
487 +\r
488 +       _filename_list_add (priv->removed_directories, absolute);\r
489 +\r
490 +       notmuch_filenames_move_to_next (db_subdirs);\r
491 +    }\r
492 +\r
493 +    if (! state->interrupted) {\r
494 +       status = notmuch_directory_set_mtime (directory, fs_mtime);\r
495 +       if (status && ret == NOTMUCH_STATUS_SUCCESS)\r
496 +           ret = status;\r
497 +    }\r
498 +\r
499 +DONE:\r
500 +    if (next)\r
501 +       talloc_free (next);\r
502 +    if (entry)\r
503 +       free (entry);\r
504 +    if (dir)\r
505 +       closedir (dir);\r
506 +    if (fs_entries)\r
507 +       free (fs_entries);\r
508 +    if (db_subdirs)\r
509 +       notmuch_filenames_destroy (db_subdirs);\r
510 +    if (db_files)\r
511 +       notmuch_filenames_destroy (db_files);\r
512 +    if (directory)\r
513 +       notmuch_directory_destroy (directory);\r
514 +\r
515 +    return ret;\r
516 +}\r
517 +\r
518 +/* XXX: This should be merged with the add_files function since it\r
519 + * shares a lot of logic with it. */\r
520 +/* Recursively count all regular files in path and all sub-directories\r
521 + * of path.  The result is added to *count (which should be\r
522 + * initialized to zero by the top-level caller before calling\r
523 + * count_files). */\r
524 +static void\r
525 +count_files (notmuch_mailstore_t *mailstore,\r
526 +            const char *path, int *count,\r
527 +            volatile sig_atomic_t *interrupted)\r
528 +{\r
529 +    struct dirent *entry = NULL;\r
530 +    char *next;\r
531 +    struct stat st;\r
532 +    struct dirent **fs_entries = NULL;\r
533 +    int num_fs_entries = scandir (path, &fs_entries, 0, dirent_sort_inode);\r
534 +    int i = 0;\r
535 +\r
536 +    (void)mailstore;\r
537 +    if (num_fs_entries == -1) {\r
538 +       fprintf (stderr, "Warning: failed to open directory %s: %s\n",\r
539 +                path, strerror (errno));\r
540 +       goto DONE;\r
541 +    }\r
542 +\r
543 +    while (!*interrupted) {\r
544 +        if (i == num_fs_entries)\r
545 +           break;\r
546 +\r
547 +        entry = fs_entries[i++];\r
548 +\r
549 +       /* Ignore special directories to avoid infinite recursion.\r
550 +        * Also ignore the .notmuch directory.\r
551 +        */\r
552 +       /* XXX: Eventually we'll want more sophistication to let the\r
553 +        * user specify files to be ignored. */\r
554 +       if (strcmp (entry->d_name, ".") == 0 ||\r
555 +           strcmp (entry->d_name, "..") == 0 ||\r
556 +           strcmp (entry->d_name, ".notmuch") == 0)\r
557 +       {\r
558 +           continue;\r
559 +       }\r
560 +\r
561 +       if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {\r
562 +           next = NULL;\r
563 +           fprintf (stderr, "Error descending from %s to %s: Out of memory\n",\r
564 +                    path, entry->d_name);\r
565 +           continue;\r
566 +       }\r
567 +\r
568 +       stat (next, &st);\r
569 +\r
570 +       if (S_ISREG (st.st_mode)) {\r
571 +           *count = *count + 1;\r
572 +           if (*count % 1000 == 0) {\r
573 +               printf ("Found %d files so far.\r", *count);\r
574 +               fflush (stdout);\r
575 +           }\r
576 +       } else if (S_ISDIR (st.st_mode)) {\r
577 +           count_files (mailstore, next, count, interrupted);\r
578 +       }\r
579 +\r
580 +       free (next);\r
581 +    }\r
582 +\r
583 +DONE:\r
584 +    if (entry)\r
585 +       free (entry);\r
586 +    if (fs_entries)\r
587 +        free (fs_entries);\r
588 +}\r
589 +\r
590 +/* Recursively remove all filenames from the database referring to\r
591 + * 'path' (or to any of its children). */\r
592 +static void\r
593 +_remove_directory (void *ctx,\r
594 +                  notmuch_database_t *notmuch,\r
595 +                  const char *path,\r
596 +                  int *renamed_files,\r
597 +                  int *removed_files)\r
598 +{\r
599 +    notmuch_directory_t *directory;\r
600 +    notmuch_filenames_t *files, *subdirs;\r
601 +    notmuch_status_t status;\r
602 +    char *absolute;\r
603 +\r
604 +    directory = notmuch_database_get_directory (notmuch, path);\r
605 +\r
606 +    for (files = notmuch_directory_get_child_files (directory);\r
607 +        notmuch_filenames_valid (files);\r
608 +        notmuch_filenames_move_to_next (files))\r
609 +    {\r
610 +       absolute = talloc_asprintf (ctx, "%s/%s", path,\r
611 +                                   notmuch_filenames_get (files));\r
612 +       status = notmuch_database_remove_message (notmuch, absolute);\r
613 +       if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)\r
614 +           *renamed_files = *renamed_files + 1;\r
615 +       else\r
616 +           *removed_files = *removed_files + 1;\r
617 +       talloc_free (absolute);\r
618 +    }\r
619 +\r
620 +    for (subdirs = notmuch_directory_get_child_directories (directory);\r
621 +        notmuch_filenames_valid (subdirs);\r
622 +        notmuch_filenames_move_to_next (subdirs))\r
623 +    {\r
624 +       absolute = talloc_asprintf (ctx, "%s/%s", path,\r
625 +                                   notmuch_filenames_get (subdirs));\r
626 +       _remove_directory (ctx, notmuch, absolute, renamed_files, removed_files);\r
627 +       talloc_free (absolute);\r
628 +    }\r
629 +\r
630 +    notmuch_directory_destroy (directory);\r
631 +}\r
632 +\r
633 +static notmuch_private_status_t\r
634 +index_new(notmuch_mailstore_t *mailstore, const char* path,\r
635 +         notmuch_indexing_context_t *indexing_ctx)\r
636 +{\r
637 +    _indexing_context_priv_t *priv;\r
638 +    _filename_node_t *f;\r
639 +    notmuch_status_t status, ret;\r
640 +    notmuch_database_t *notmuch = mailstore->notmuch;\r
641 +\r
642 +    priv = talloc(NULL, _indexing_context_priv_t);\r
643 +    indexing_ctx->priv = priv;\r
644 +    if (priv == NULL)\r
645 +       return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
646 +\r
647 +    priv->removed_files = _filename_list_create (priv);\r
648 +    priv->removed_directories = _filename_list_create (priv);\r
649 +\r
650 +    ret = add_files_recursive(mailstore, path, indexing_ctx);\r
651 +\r
652 +    indexing_ctx->removed_files = 0;\r
653 +    indexing_ctx->renamed_files = 0;\r
654 +    for (f = priv->removed_files->head; f; f = f->next) {\r
655 +       status = notmuch_database_remove_message (notmuch, f->filename);\r
656 +       if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)\r
657 +           indexing_ctx->renamed_files++;\r
658 +       else\r
659 +           indexing_ctx->removed_files++;\r
660 +    }\r
661 +\r
662 +    for (f = priv->removed_directories->head; f; f = f->next) {\r
663 +       _remove_directory (priv, notmuch, f->filename,\r
664 +                          &indexing_ctx->renamed_files,\r
665 +                          &indexing_ctx->removed_files);\r
666 +    }\r
667 +\r
668 +    talloc_free(priv);\r
669 +\r
670 +    return ret;\r
671 +}\r
672  \r
673  static FILE *\r
674  open_file(notmuch_mailstore_t *mailstore, const char *filename)\r
675 @@ -39,5 +626,8 @@ open_file(notmuch_mailstore_t *mailstore, const char *filename)\r
676  /* Original notmuch mail store */\r
677  struct _notmuch_mailstore mailstore_files = {\r
678      .type = "files",\r
679 +    .count_files = count_files,\r
680 +    .index_new = index_new,\r
681 +    .sync_tags = NULL,\r
682      .open_file = open_file,\r
683  };\r
684 diff --git a/notmuch-new.c b/notmuch-new.c\r
685 index 2d0ba6c..ede0859 100644\r
686 --- a/notmuch-new.c\r
687 +++ b/notmuch-new.c\r
688 @@ -22,35 +22,12 @@\r
689  \r
690  #include <unistd.h>\r
691  \r
692 -typedef struct _filename_node {\r
693 -    char *filename;\r
694 -    struct _filename_node *next;\r
695 -} _filename_node_t;\r
696 -\r
697 -typedef struct _filename_list {\r
698 -    _filename_node_t *head;\r
699 -    _filename_node_t **tail;\r
700 -} _filename_list_t;\r
701 -\r
702 -typedef struct {\r
703 -    int output_is_a_tty;\r
704 -    int verbose;\r
705 -\r
706 -    int total_files;\r
707 -    int processed_files;\r
708 -    int added_messages;\r
709 -    struct timeval tv_start;\r
710 -\r
711 -    _filename_list_t *removed_files;\r
712 -    _filename_list_t *removed_directories;\r
713 -} add_files_state_t;\r
714 -\r
715 -static volatile sig_atomic_t do_add_files_print_progress = 0;\r
716 +notmuch_indexing_context_t *indexing_ctx;\r
717  \r
718  static void\r
719  handle_sigalrm (unused (int signal))\r
720  {\r
721 -    do_add_files_print_progress = 1;\r
722 +    indexing_ctx->print_progress = 1;\r
723  }\r
724  \r
725  static volatile sig_atomic_t interrupted;\r
726 @@ -62,46 +39,11 @@ handle_sigint (unused (int sig))\r
727      static char msg[] = "Stopping...         \n";\r
728  \r
729      ignored = write(2, msg, sizeof(msg)-1);\r
730 -    interrupted = 1;\r
731 -}\r
732 -\r
733 -static _filename_list_t *\r
734 -_filename_list_create (const void *ctx)\r
735 -{\r
736 -    _filename_list_t *list;\r
737 -\r
738 -    list = talloc (ctx, _filename_list_t);\r
739 -    if (list == NULL)\r
740 -       return NULL;\r
741 -\r
742 -    list->head = NULL;\r
743 -    list->tail = &list->head;\r
744 -\r
745 -    return list;\r
746 -}\r
747 -\r
748 -static void\r
749 -_filename_list_add (_filename_list_t *list,\r
750 -                   const char *filename)\r
751 -{\r
752 -    _filename_node_t *node = talloc (list, _filename_node_t);\r
753 -\r
754 -    node->filename = talloc_strdup (list, filename);\r
755 -    node->next = NULL;\r
756 -\r
757 -    *(list->tail) = node;\r
758 -    list->tail = &node->next;\r
759 +    indexing_ctx->interrupted = 1;\r
760  }\r
761  \r
762  static void\r
763 -tag_inbox_and_unread (notmuch_message_t *message)\r
764 -{\r
765 -    notmuch_message_add_tag (message, "inbox");\r
766 -    notmuch_message_add_tag (message, "unread");\r
767 -}\r
768 -\r
769 -static void\r
770 -add_files_print_progress (add_files_state_t *state)\r
771 +add_files_print_progress (notmuch_indexing_context_t *state)\r
772  {\r
773      struct timeval tv_now;\r
774      double elapsed_overall, rate_overall;\r
775 @@ -128,388 +70,13 @@ add_files_print_progress (add_files_state_t *state)\r
776      fflush (stdout);\r
777  }\r
778  \r
779 -static int\r
780 -dirent_sort_inode (const struct dirent **a, const struct dirent **b)\r
781 -{\r
782 -    return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;\r
783 -}\r
784 -\r
785 -static int\r
786 -dirent_sort_strcmp_name (const struct dirent **a, const struct dirent **b)\r
787 -{\r
788 -    return strcmp ((*a)->d_name, (*b)->d_name);\r
789 -}\r
790 -\r
791 -/* Test if the directory looks like a Maildir directory.\r
792 - *\r
793 - * Search through the array of directory entries to see if we can find all\r
794 - * three subdirectories typical for Maildir, that is "new", "cur", and "tmp".\r
795 - *\r
796 - * Return 1 if the directory looks like a Maildir and 0 otherwise.\r
797 - */\r
798 -static int\r
799 -_entries_resemble_maildir (struct dirent **entries, int count)\r
800 -{\r
801 -    int i, found = 0;\r
802 -\r
803 -    for (i = 0; i < count; i++) {\r
804 -       if (entries[i]->d_type != DT_DIR && entries[i]->d_type != DT_UNKNOWN)\r
805 -           continue;\r
806 -\r
807 -       if (strcmp(entries[i]->d_name, "new") == 0 ||\r
808 -           strcmp(entries[i]->d_name, "cur") == 0 ||\r
809 -           strcmp(entries[i]->d_name, "tmp") == 0)\r
810 -       {\r
811 -           found++;\r
812 -           if (found == 3)\r
813 -               return 1;\r
814 -       }\r
815 -    }\r
816 -\r
817 -    return 0;\r
818 -}\r
819 -\r
820 -/* Examine 'path' recursively as follows:\r
821 - *\r
822 - *   o Ask the filesystem for the mtime of 'path' (fs_mtime)\r
823 - *   o Ask the database for its timestamp of 'path' (db_mtime)\r
824 - *\r
825 - *   o Ask the filesystem for files and directories within 'path'\r
826 - *     (via scandir and stored in fs_entries)\r
827 - *   o Ask the database for files and directories within 'path'\r
828 - *     (db_files and db_subdirs)\r
829 - *\r
830 - *   o Pass 1: For each directory in fs_entries, recursively call into\r
831 - *     this same function.\r
832 - *\r
833 - *   o Pass 2: If 'fs_mtime' > 'db_mtime', then walk fs_entries\r
834 - *     simultaneously with db_files and db_subdirs. Look for one of\r
835 - *     three interesting cases:\r
836 - *\r
837 - *        1. Regular file in fs_entries and not in db_files\r
838 - *            This is a new file to add_message into the database.\r
839 - *\r
840 - *         2. Filename in db_files not in fs_entries.\r
841 - *            This is a file that has been removed from the mail store.\r
842 - *\r
843 - *         3. Directory in db_subdirs not in fs_entries\r
844 - *            This is a directory that has been removed from the mail store.\r
845 - *\r
846 - *     Note that the addition of a directory is not interesting here,\r
847 - *     since that will have been taken care of in pass 1. Also, we\r
848 - *     don't immediately act on file/directory removal since we must\r
849 - *     ensure that in the case of a rename that the new filename is\r
850 - *     added before the old filename is removed, (so that no\r
851 - *     information is lost from the database).\r
852 - *\r
853 - *   o Tell the database to update its time of 'path' to 'fs_mtime'\r
854 - */\r
855 -static notmuch_status_t\r
856 -add_files_recursive (notmuch_database_t *notmuch,\r
857 -                    const char *path,\r
858 -                    add_files_state_t *state)\r
859 -{\r
860 -    DIR *dir = NULL;\r
861 -    struct dirent *entry = NULL;\r
862 -    char *next = NULL;\r
863 -    time_t fs_mtime, db_mtime;\r
864 -    notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;\r
865 -    notmuch_message_t *message = NULL;\r
866 -    struct dirent **fs_entries = NULL;\r
867 -    int i, num_fs_entries;\r
868 -    notmuch_directory_t *directory;\r
869 -    notmuch_filenames_t *db_files = NULL;\r
870 -    notmuch_filenames_t *db_subdirs = NULL;\r
871 -    struct stat st;\r
872 -    notmuch_bool_t is_maildir, new_directory;\r
873 -\r
874 -    if (stat (path, &st)) {\r
875 -       fprintf (stderr, "Error reading directory %s: %s\n",\r
876 -                path, strerror (errno));\r
877 -       return NOTMUCH_STATUS_FILE_ERROR;\r
878 -    }\r
879 -\r
880 -    /* This is not an error since we may have recursed based on a\r
881 -     * symlink to a regular file, not a directory, and we don't know\r
882 -     * that until this stat. */\r
883 -    if (! S_ISDIR (st.st_mode))\r
884 -       return NOTMUCH_STATUS_SUCCESS;\r
885 -\r
886 -    fs_mtime = st.st_mtime;\r
887 -\r
888 -    directory = notmuch_database_get_directory (notmuch, path);\r
889 -    db_mtime = notmuch_directory_get_mtime (directory);\r
890 -\r
891 -    if (db_mtime == 0) {\r
892 -       new_directory = TRUE;\r
893 -       db_files = NULL;\r
894 -       db_subdirs = NULL;\r
895 -    } else {\r
896 -       new_directory = FALSE;\r
897 -       db_files = notmuch_directory_get_child_files (directory);\r
898 -       db_subdirs = notmuch_directory_get_child_directories (directory);\r
899 -    }\r
900 -\r
901 -    /* If the database knows about this directory, then we sort based\r
902 -     * on strcmp to match the database sorting. Otherwise, we can do\r
903 -     * inode-based sorting for faster filesystem operation. */\r
904 -    num_fs_entries = scandir (path, &fs_entries, 0,\r
905 -                             new_directory ?\r
906 -                             dirent_sort_inode : dirent_sort_strcmp_name);\r
907 -\r
908 -    if (num_fs_entries == -1) {\r
909 -       fprintf (stderr, "Error opening directory %s: %s\n",\r
910 -                path, strerror (errno));\r
911 -       ret = NOTMUCH_STATUS_FILE_ERROR;\r
912 -       goto DONE;\r
913 -    }\r
914 -\r
915 -    /* Pass 1: Recurse into all sub-directories. */\r
916 -    is_maildir = _entries_resemble_maildir (fs_entries, num_fs_entries);\r
917 -\r
918 -    for (i = 0; i < num_fs_entries; i++) {\r
919 -       if (interrupted)\r
920 -           break;\r
921 -\r
922 -       entry = fs_entries[i];\r
923 -\r
924 -       /* We only want to descend into directories.\r
925 -        * But symlinks can be to directories too, of course.\r
926 -        *\r
927 -        * And if the filesystem doesn't tell us the file type in the\r
928 -        * scandir results, then it might be a directory (and if not,\r
929 -        * then we'll stat and return immediately in the next level of\r
930 -        * recursion). */\r
931 -       if (entry->d_type != DT_DIR &&\r
932 -           entry->d_type != DT_LNK &&\r
933 -           entry->d_type != DT_UNKNOWN)\r
934 -       {\r
935 -           continue;\r
936 -       }\r
937 -\r
938 -       /* Ignore special directories to avoid infinite recursion.\r
939 -        * Also ignore the .notmuch directory and any "tmp" directory\r
940 -        * that appears within a maildir.\r
941 -        */\r
942 -       /* XXX: Eventually we'll want more sophistication to let the\r
943 -        * user specify files to be ignored. */\r
944 -       if (strcmp (entry->d_name, ".") == 0 ||\r
945 -           strcmp (entry->d_name, "..") == 0 ||\r
946 -           (is_maildir && strcmp (entry->d_name, "tmp") == 0) ||\r
947 -           strcmp (entry->d_name, ".notmuch") ==0)\r
948 -       {\r
949 -           continue;\r
950 -       }\r
951 -\r
952 -       next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
953 -       status = add_files_recursive (notmuch, next, state);\r
954 -       if (status && ret == NOTMUCH_STATUS_SUCCESS)\r
955 -           ret = status;\r
956 -       talloc_free (next);\r
957 -       next = NULL;\r
958 -    }\r
959 -\r
960 -    /* If this directory hasn't been modified since the last\r
961 -     * "notmuch new", then we can skip the second pass entirely. */\r
962 -    if (fs_mtime <= db_mtime)\r
963 -       goto DONE;\r
964 -\r
965 -    /* Pass 2: Scan for new files, removed files, and removed directories. */\r
966 -    for (i = 0; i < num_fs_entries; i++)\r
967 -    {\r
968 -       if (interrupted)\r
969 -           break;\r
970 -\r
971 -        entry = fs_entries[i];\r
972 -\r
973 -       /* Check if we've walked past any names in db_files or\r
974 -        * db_subdirs. If so, these have been deleted. */\r
975 -       while (notmuch_filenames_valid (db_files) &&\r
976 -              strcmp (notmuch_filenames_get (db_files), entry->d_name) < 0)\r
977 -       {\r
978 -           char *absolute = talloc_asprintf (state->removed_files,\r
979 -                                             "%s/%s", path,\r
980 -                                             notmuch_filenames_get (db_files));\r
981 -\r
982 -           _filename_list_add (state->removed_files, absolute);\r
983 -\r
984 -           notmuch_filenames_move_to_next (db_files);\r
985 -       }\r
986 -\r
987 -       while (notmuch_filenames_valid (db_subdirs) &&\r
988 -              strcmp (notmuch_filenames_get (db_subdirs), entry->d_name) <= 0)\r
989 -       {\r
990 -           const char *filename = notmuch_filenames_get (db_subdirs);\r
991 -\r
992 -           if (strcmp (filename, entry->d_name) < 0)\r
993 -           {\r
994 -               char *absolute = talloc_asprintf (state->removed_directories,\r
995 -                                                 "%s/%s", path, filename);\r
996 -\r
997 -               _filename_list_add (state->removed_directories, absolute);\r
998 -           }\r
999 -\r
1000 -           notmuch_filenames_move_to_next (db_subdirs);\r
1001 -       }\r
1002 -\r
1003 -       /* If we're looking at a symlink, we only want to add it if it\r
1004 -        * links to a regular file, (and not to a directory, say).\r
1005 -        *\r
1006 -        * Similarly, if the file is of unknown type (due to filesytem\r
1007 -        * limitations), then we also need to look closer.\r
1008 -        *\r
1009 -        * In either case, a stat does the trick.\r
1010 -        */\r
1011 -       if (entry->d_type == DT_LNK || entry->d_type == DT_UNKNOWN) {\r
1012 -           int err;\r
1013 -\r
1014 -           next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
1015 -           err = stat (next, &st);\r
1016 -           talloc_free (next);\r
1017 -           next = NULL;\r
1018 -\r
1019 -           /* Don't emit an error for a link pointing nowhere, since\r
1020 -            * the directory-traversal pass will have already done\r
1021 -            * that. */\r
1022 -           if (err)\r
1023 -               continue;\r
1024 -\r
1025 -           if (! S_ISREG (st.st_mode))\r
1026 -               continue;\r
1027 -       } else if (entry->d_type != DT_REG) {\r
1028 -           continue;\r
1029 -       }\r
1030 -\r
1031 -       /* Don't add a file that we've added before. */\r
1032 -       if (notmuch_filenames_valid (db_files) &&\r
1033 -           strcmp (notmuch_filenames_get (db_files), entry->d_name) == 0)\r
1034 -       {\r
1035 -           notmuch_filenames_move_to_next (db_files);\r
1036 -           continue;\r
1037 -       }\r
1038 -\r
1039 -       /* We're now looking at a regular file that doesn't yet exist\r
1040 -        * in the database, so add it. */\r
1041 -       next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
1042 -\r
1043 -       state->processed_files++;\r
1044 -\r
1045 -       if (state->verbose) {\r
1046 -           if (state->output_is_a_tty)\r
1047 -               printf("\r\033[K");\r
1048 -\r
1049 -           printf ("%i/%i: %s",\r
1050 -                   state->processed_files,\r
1051 -                   state->total_files,\r
1052 -                   next);\r
1053 -\r
1054 -           putchar((state->output_is_a_tty) ? '\r' : '\n');\r
1055 -           fflush (stdout);\r
1056 -       }\r
1057 -\r
1058 -       status = notmuch_database_add_message (notmuch, next, &message);\r
1059 -       switch (status) {\r
1060 -       /* success */\r
1061 -       case NOTMUCH_STATUS_SUCCESS:\r
1062 -           state->added_messages++;\r
1063 -           tag_inbox_and_unread (message);\r
1064 -           break;\r
1065 -       /* Non-fatal issues (go on to next file) */\r
1066 -       case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
1067 -           /* Stay silent on this one. */\r
1068 -           break;\r
1069 -       case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
1070 -           fprintf (stderr, "Note: Ignoring non-mail file: %s\n",\r
1071 -                    next);\r
1072 -           break;\r
1073 -       /* Fatal issues. Don't process anymore. */\r
1074 -       case NOTMUCH_STATUS_READ_ONLY_DATABASE:\r
1075 -       case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
1076 -       case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
1077 -           fprintf (stderr, "Error: %s. Halting processing.\n",\r
1078 -                    notmuch_status_to_string (status));\r
1079 -           ret = status;\r
1080 -           goto DONE;\r
1081 -       default:\r
1082 -       case NOTMUCH_STATUS_FILE_ERROR:\r
1083 -       case NOTMUCH_STATUS_NULL_POINTER:\r
1084 -       case NOTMUCH_STATUS_TAG_TOO_LONG:\r
1085 -       case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:\r
1086 -       case NOTMUCH_STATUS_LAST_STATUS:\r
1087 -           INTERNAL_ERROR ("add_message returned unexpected value: %d",  status);\r
1088 -           goto DONE;\r
1089 -       }\r
1090 -\r
1091 -       if (message) {\r
1092 -           notmuch_message_destroy (message);\r
1093 -           message = NULL;\r
1094 -       }\r
1095 -\r
1096 -       if (do_add_files_print_progress) {\r
1097 -           do_add_files_print_progress = 0;\r
1098 -           add_files_print_progress (state);\r
1099 -       }\r
1100 -\r
1101 -       talloc_free (next);\r
1102 -       next = NULL;\r
1103 -    }\r
1104 -\r
1105 -    /* Now that we've walked the whole filesystem list, anything left\r
1106 -     * over in the database lists has been deleted. */\r
1107 -    while (notmuch_filenames_valid (db_files))\r
1108 -    {\r
1109 -       char *absolute = talloc_asprintf (state->removed_files,\r
1110 -                                         "%s/%s", path,\r
1111 -                                         notmuch_filenames_get (db_files));\r
1112 -\r
1113 -       _filename_list_add (state->removed_files, absolute);\r
1114 -\r
1115 -       notmuch_filenames_move_to_next (db_files);\r
1116 -    }\r
1117 -\r
1118 -    while (notmuch_filenames_valid (db_subdirs))\r
1119 -    {\r
1120 -       char *absolute = talloc_asprintf (state->removed_directories,\r
1121 -                                         "%s/%s", path,\r
1122 -                                         notmuch_filenames_get (db_subdirs));\r
1123 -\r
1124 -       _filename_list_add (state->removed_directories, absolute);\r
1125 -\r
1126 -       notmuch_filenames_move_to_next (db_subdirs);\r
1127 -    }\r
1128 -\r
1129 -    if (! interrupted) {\r
1130 -       status = notmuch_directory_set_mtime (directory, fs_mtime);\r
1131 -       if (status && ret == NOTMUCH_STATUS_SUCCESS)\r
1132 -           ret = status;\r
1133 -    }\r
1134 -\r
1135 -  DONE:\r
1136 -    if (next)\r
1137 -       talloc_free (next);\r
1138 -    if (entry)\r
1139 -       free (entry);\r
1140 -    if (dir)\r
1141 -       closedir (dir);\r
1142 -    if (fs_entries)\r
1143 -       free (fs_entries);\r
1144 -    if (db_subdirs)\r
1145 -       notmuch_filenames_destroy (db_subdirs);\r
1146 -    if (db_files)\r
1147 -       notmuch_filenames_destroy (db_files);\r
1148 -    if (directory)\r
1149 -       notmuch_directory_destroy (directory);\r
1150 -\r
1151 -    return ret;\r
1152 -}\r
1153 -\r
1154  /* This is the top-level entry point for add_files. It does a couple\r
1155   * of error checks, sets up the progress-printing timer and then calls\r
1156   * into the recursive function. */\r
1157  static notmuch_status_t\r
1158 -add_files (notmuch_database_t *notmuch,\r
1159 +add_files (notmuch_mailstore_t *mailstore,\r
1160            const char *path,\r
1161 -          add_files_state_t *state)\r
1162 +          notmuch_indexing_context_t *state) /* FIXME: rename */\r
1163  {\r
1164      notmuch_status_t status;\r
1165      struct sigaction action;\r
1166 @@ -517,6 +84,9 @@ add_files (notmuch_database_t *notmuch,\r
1167      notmuch_bool_t timer_is_active = FALSE;\r
1168      struct stat st;\r
1169  \r
1170 +    state->print_progress = 0;\r
1171 +    state->print_progress_cb = add_files_print_progress;\r
1172 +\r
1173      if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {\r
1174         /* Setup our handler for SIGALRM */\r
1175         memset (&action, 0, sizeof (struct sigaction));\r
1176 @@ -546,7 +116,7 @@ add_files (notmuch_database_t *notmuch,\r
1177         return NOTMUCH_STATUS_FILE_ERROR;\r
1178      }\r
1179  \r
1180 -    status = add_files_recursive (notmuch, path, state);\r
1181 +    status = notmuch_mailstore_index_new (mailstore, path, state);\r
1182  \r
1183      if (timer_is_active) {\r
1184         /* Now stop the timer. */\r
1185 @@ -564,80 +134,11 @@ add_files (notmuch_database_t *notmuch,\r
1186      return status;\r
1187  }\r
1188  \r
1189 -/* XXX: This should be merged with the add_files function since it\r
1190 - * shares a lot of logic with it. */\r
1191 -/* Recursively count all regular files in path and all sub-directories\r
1192 - * of path.  The result is added to *count (which should be\r
1193 - * initialized to zero by the top-level caller before calling\r
1194 - * count_files). */\r
1195 -static void\r
1196 -count_files (const char *path, int *count)\r
1197 -{\r
1198 -    struct dirent *entry = NULL;\r
1199 -    char *next;\r
1200 -    struct stat st;\r
1201 -    struct dirent **fs_entries = NULL;\r
1202 -    int num_fs_entries = scandir (path, &fs_entries, 0, dirent_sort_inode);\r
1203 -    int i = 0;\r
1204 -\r
1205 -    if (num_fs_entries == -1) {\r
1206 -       fprintf (stderr, "Warning: failed to open directory %s: %s\n",\r
1207 -                path, strerror (errno));\r
1208 -       goto DONE;\r
1209 -    }\r
1210 -\r
1211 -    while (!interrupted) {\r
1212 -        if (i == num_fs_entries)\r
1213 -           break;\r
1214 -\r
1215 -        entry = fs_entries[i++];\r
1216 -\r
1217 -       /* Ignore special directories to avoid infinite recursion.\r
1218 -        * Also ignore the .notmuch directory.\r
1219 -        */\r
1220 -       /* XXX: Eventually we'll want more sophistication to let the\r
1221 -        * user specify files to be ignored. */\r
1222 -       if (strcmp (entry->d_name, ".") == 0 ||\r
1223 -           strcmp (entry->d_name, "..") == 0 ||\r
1224 -           strcmp (entry->d_name, ".notmuch") == 0)\r
1225 -       {\r
1226 -           continue;\r
1227 -       }\r
1228 -\r
1229 -       if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {\r
1230 -           next = NULL;\r
1231 -           fprintf (stderr, "Error descending from %s to %s: Out of memory\n",\r
1232 -                    path, entry->d_name);\r
1233 -           continue;\r
1234 -       }\r
1235 -\r
1236 -       stat (next, &st);\r
1237 -\r
1238 -       if (S_ISREG (st.st_mode)) {\r
1239 -           *count = *count + 1;\r
1240 -           if (*count % 1000 == 0) {\r
1241 -               printf ("Found %d files so far.\r", *count);\r
1242 -               fflush (stdout);\r
1243 -           }\r
1244 -       } else if (S_ISDIR (st.st_mode)) {\r
1245 -           count_files (next, count);\r
1246 -       }\r
1247 -\r
1248 -       free (next);\r
1249 -    }\r
1250 -\r
1251 -  DONE:\r
1252 -    if (entry)\r
1253 -       free (entry);\r
1254 -    if (fs_entries)\r
1255 -        free (fs_entries);\r
1256 -}\r
1257 -\r
1258  static void\r
1259  upgrade_print_progress (void *closure,\r
1260                         double progress)\r
1261  {\r
1262 -    add_files_state_t *state = closure;\r
1263 +    notmuch_indexing_context_t *state = closure;\r
1264  \r
1265      printf ("Upgrading database: %.2f%% complete", progress * 100.0);\r
1266  \r
1267 @@ -659,55 +160,12 @@ upgrade_print_progress (void *closure,\r
1268      fflush (stdout);\r
1269  }\r
1270  \r
1271 -/* Recursively remove all filenames from the database referring to\r
1272 - * 'path' (or to any of its children). */\r
1273 -static void\r
1274 -_remove_directory (void *ctx,\r
1275 -                  notmuch_database_t *notmuch,\r
1276 -                  const char *path,\r
1277 -                  int *renamed_files,\r
1278 -                  int *removed_files)\r
1279 -{\r
1280 -    notmuch_directory_t *directory;\r
1281 -    notmuch_filenames_t *files, *subdirs;\r
1282 -    notmuch_status_t status;\r
1283 -    char *absolute;\r
1284 -\r
1285 -    directory = notmuch_database_get_directory (notmuch, path);\r
1286 -\r
1287 -    for (files = notmuch_directory_get_child_files (directory);\r
1288 -        notmuch_filenames_valid (files);\r
1289 -        notmuch_filenames_move_to_next (files))\r
1290 -    {\r
1291 -       absolute = talloc_asprintf (ctx, "%s/%s", path,\r
1292 -                                   notmuch_filenames_get (files));\r
1293 -       status = notmuch_database_remove_message (notmuch, absolute);\r
1294 -       if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)\r
1295 -           *renamed_files = *renamed_files + 1;\r
1296 -       else\r
1297 -           *removed_files = *removed_files + 1;\r
1298 -       talloc_free (absolute);\r
1299 -    }\r
1300 -\r
1301 -    for (subdirs = notmuch_directory_get_child_directories (directory);\r
1302 -        notmuch_filenames_valid (subdirs);\r
1303 -        notmuch_filenames_move_to_next (subdirs))\r
1304 -    {\r
1305 -       absolute = talloc_asprintf (ctx, "%s/%s", path,\r
1306 -                                   notmuch_filenames_get (subdirs));\r
1307 -       _remove_directory (ctx, notmuch, absolute, renamed_files, removed_files);\r
1308 -       talloc_free (absolute);\r
1309 -    }\r
1310 -\r
1311 -    notmuch_directory_destroy (directory);\r
1312 -}\r
1313 -\r
1314  int\r
1315  notmuch_new_command (void *ctx, int argc, char *argv[])\r
1316  {\r
1317      notmuch_config_t *config;\r
1318      notmuch_database_t *notmuch;\r
1319 -    add_files_state_t add_files_state;\r
1320 +    notmuch_indexing_context_t add_files_state; /* FIXME: Rename */\r
1321      double elapsed;\r
1322      struct timeval tv_now;\r
1323      int ret = 0;\r
1324 @@ -715,10 +173,10 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
1325      const char *db_path;\r
1326      char *dot_notmuch_path;\r
1327      struct sigaction action;\r
1328 -    _filename_node_t *f;\r
1329 -    int renamed_files, removed_files;\r
1330 -    notmuch_status_t status;\r
1331      int i;\r
1332 +    notmuch_mailstore_t *mailstore;\r
1333 +\r
1334 +    indexing_ctx = &add_files_state;\r
1335  \r
1336      add_files_state.verbose = 0;\r
1337      add_files_state.output_is_a_tty = isatty (fileno (stdout));\r
1338 @@ -737,6 +195,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
1339         return 1;\r
1340  \r
1341      db_path = notmuch_config_get_database_path (config);\r
1342 +    mailstore = notmuch_config_get_mailstore (config);\r
1343  \r
1344      dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");\r
1345  \r
1346 @@ -744,7 +203,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
1347         int count;\r
1348  \r
1349         count = 0;\r
1350 -       count_files (db_path, &count);\r
1351 +       notmuch_mailstore_count_files (mailstore, db_path, &count, &interrupted);\r
1352         if (interrupted)\r
1353             return 1;\r
1354  \r
1355 @@ -777,6 +236,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
1356      /* Setup our handler for SIGINT. We do this after having\r
1357       * potentially done a database upgrade we this interrupt handler\r
1358       * won't support. */\r
1359 +    add_files_state.interrupted = 0;\r
1360      memset (&action, 0, sizeof (struct sigaction));\r
1361      action.sa_handler = handle_sigint;\r
1362      sigemptyset (&action.sa_mask);\r
1363 @@ -790,28 +250,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
1364      add_files_state.added_messages = 0;\r
1365      gettimeofday (&add_files_state.tv_start, NULL);\r
1366  \r
1367 -    add_files_state.removed_files = _filename_list_create (ctx);\r
1368 -    add_files_state.removed_directories = _filename_list_create (ctx);\r
1369 -\r
1370 -    ret = add_files (notmuch, db_path, &add_files_state);\r
1371 -\r
1372 -    removed_files = 0;\r
1373 -    renamed_files = 0;\r
1374 -    for (f = add_files_state.removed_files->head; f; f = f->next) {\r
1375 -       status = notmuch_database_remove_message (notmuch, f->filename);\r
1376 -       if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)\r
1377 -           renamed_files++;\r
1378 -       else\r
1379 -           removed_files++;\r
1380 -    }\r
1381 -\r
1382 -    for (f = add_files_state.removed_directories->head; f; f = f->next) {\r
1383 -       _remove_directory (ctx, notmuch, f->filename,\r
1384 -                          &renamed_files, &removed_files);\r
1385 -    }\r
1386 -\r
1387 -    talloc_free (add_files_state.removed_files);\r
1388 -    talloc_free (add_files_state.removed_directories);\r
1389 +    ret = add_files (mailstore, db_path, &add_files_state);\r
1390  \r
1391      gettimeofday (&tv_now, NULL);\r
1392      elapsed = notmuch_time_elapsed (add_files_state.tv_start,\r
1393 @@ -839,16 +278,16 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
1394         printf ("No new mail.");\r
1395      }\r
1396  \r
1397 -    if (removed_files) {\r
1398 +    if (add_files_state.removed_files) {\r
1399         printf (" Removed %d %s.",\r
1400 -               removed_files,\r
1401 -               removed_files == 1 ? "message" : "messages");\r
1402 +               add_files_state.removed_files,\r
1403 +               add_files_state.removed_files == 1 ? "message" : "messages");\r
1404      }\r
1405  \r
1406 -    if (renamed_files) {\r
1407 +    if (add_files_state.renamed_files) {\r
1408         printf (" Detected %d file %s.",\r
1409 -               renamed_files,\r
1410 -               renamed_files == 1 ? "rename" : "renames");\r
1411 +               add_files_state.renamed_files,\r
1412 +               add_files_state.renamed_files == 1 ? "rename" : "renames");\r
1413      }\r
1414  \r
1415      printf ("\n");\r
1416 -- \r
1417 1.7.0\r
1418 \r