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