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
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
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
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
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
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
69 Signed-off-by: Michal Sojka <sojkam1@fel.cvut.cz>
\r
71 lib/mailstore-files.c | 581 ++++++++++++++++++++++++++++++++++++++++++++
\r
72 notmuch-new.c | 648 +++++--------------------------------------------
\r
73 2 files changed, 637 insertions(+), 592 deletions(-)
\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
80 * Michal Sojka <sojkam1@fel.cvut.cz>
\r
83 +#define _GNU_SOURCE /* For asprintf() */
\r
84 #include "notmuch.h"
\r
85 #include "mailstore-private.h"
\r
86 +#include <dirent.h>
\r
88 +typedef struct _filename_node {
\r
90 + struct _filename_node *next;
\r
91 +} _filename_node_t;
\r
93 +typedef struct _filename_list {
\r
94 + _filename_node_t *head;
\r
95 + _filename_node_t **tail;
\r
96 +} _filename_list_t;
\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
103 +static _filename_list_t *
\r
104 +_filename_list_create (const void *ctx)
\r
106 + _filename_list_t *list;
\r
108 + list = talloc (ctx, _filename_list_t);
\r
109 + if (list == NULL)
\r
112 + list->head = NULL;
\r
113 + list->tail = &list->head;
\r
119 +_filename_list_add (_filename_list_t *list,
\r
120 + const char *filename)
\r
122 + _filename_node_t *node = talloc (list, _filename_node_t);
\r
124 + node->filename = talloc_strdup (list, filename);
\r
125 + node->next = NULL;
\r
127 + *(list->tail) = node;
\r
128 + list->tail = &node->next;
\r
132 +tag_inbox_and_unread (notmuch_message_t *message)
\r
134 + notmuch_message_add_tag (message, "inbox");
\r
135 + notmuch_message_add_tag (message, "unread");
\r
139 +dirent_sort_inode (const struct dirent **a, const struct dirent **b)
\r
141 + return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
\r
145 +dirent_sort_strcmp_name (const struct dirent **a, const struct dirent **b)
\r
147 + return strcmp ((*a)->d_name, (*b)->d_name);
\r
150 +/* Test if the directory looks like a Maildir directory.
\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
155 + * Return 1 if the directory looks like a Maildir and 0 otherwise.
\r
158 +_entries_resemble_maildir (struct dirent **entries, int count)
\r
160 + int i, found = 0;
\r
162 + for (i = 0; i < count; i++) {
\r
163 + if (entries[i]->d_type != DT_DIR && entries[i]->d_type != DT_UNKNOWN)
\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
180 +/* Examine 'path' recursively as follows:
\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
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
190 + * o Pass 1: For each directory in fs_entries, recursively call into
\r
191 + * this same function.
\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
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
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
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
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
213 + * o Tell the database to update its time of 'path' to 'fs_mtime'
\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
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
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
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
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
248 + fs_mtime = st.st_mtime;
\r
250 + directory = notmuch_database_get_directory (notmuch, path);
\r
251 + db_mtime = notmuch_directory_get_mtime (directory);
\r
253 + if (db_mtime == 0) {
\r
254 + new_directory = TRUE;
\r
256 + db_subdirs = NULL;
\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
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
268 + dirent_sort_inode : dirent_sort_strcmp_name);
\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
277 + /* Pass 1: Recurse into all sub-directories. */
\r
278 + is_maildir = _entries_resemble_maildir (fs_entries, num_fs_entries);
\r
280 + for (i = 0; i < num_fs_entries; i++) {
\r
281 + if (state->interrupted)
\r
284 + entry = fs_entries[i];
\r
286 + /* We only want to descend into directories.
\r
287 + * But symlinks can be to directories too, of course.
\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
293 + if (entry->d_type != DT_DIR &&
\r
294 + entry->d_type != DT_LNK &&
\r
295 + entry->d_type != DT_UNKNOWN)
\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
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
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
318 + talloc_free (next);
\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
327 + /* Pass 2: Scan for new files, removed files, and removed directories. */
\r
328 + for (i = 0; i < num_fs_entries; i++)
\r
330 + if (state->interrupted)
\r
333 + entry = fs_entries[i];
\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
340 + char *absolute = talloc_asprintf (priv->removed_files,
\r
342 + notmuch_filenames_get (db_files));
\r
344 + _filename_list_add (priv->removed_files, absolute);
\r
346 + notmuch_filenames_move_to_next (db_files);
\r
349 + while (notmuch_filenames_valid (db_subdirs) &&
\r
350 + strcmp (notmuch_filenames_get (db_subdirs), entry->d_name) <= 0)
\r
352 + const char *filename = notmuch_filenames_get (db_subdirs);
\r
354 + if (strcmp (filename, entry->d_name) < 0)
\r
356 + char *absolute = talloc_asprintf (priv->removed_directories,
\r
357 + "%s/%s", path, filename);
\r
359 + _filename_list_add (priv->removed_directories, absolute);
\r
362 + notmuch_filenames_move_to_next (db_subdirs);
\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
368 + * Similarly, if the file is of unknown type (due to filesytem
\r
369 + * limitations), then we also need to look closer.
\r
371 + * In either case, a stat does the trick.
\r
373 + if (entry->d_type == DT_LNK || entry->d_type == DT_UNKNOWN) {
\r
376 + next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
\r
377 + err = stat (next, &st);
\r
378 + talloc_free (next);
\r
381 + /* Don't emit an error for a link pointing nowhere, since
\r
382 + * the directory-traversal pass will have already done
\r
387 + if (! S_ISREG (st.st_mode))
\r
389 + } else if (entry->d_type != DT_REG) {
\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
397 + notmuch_filenames_move_to_next (db_files);
\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
405 + state->processed_files++;
\r
407 + if (state->verbose && state->print_verbose_cb)
\r
408 + state->print_verbose_cb(state, next);
\r
410 + status = notmuch_database_add_message (notmuch, next, &message);
\r
411 + switch (status) {
\r
413 + case NOTMUCH_STATUS_SUCCESS:
\r
414 + state->added_messages++;
\r
415 + tag_inbox_and_unread (message);
\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
421 + case NOTMUCH_STATUS_FILE_NOT_EMAIL:
\r
422 + fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
\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
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
444 + notmuch_message_destroy (message);
\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
454 + talloc_free (next);
\r
458 + if (state->interrupted)
\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
465 + char *absolute = talloc_asprintf (priv->removed_files,
\r
467 + notmuch_filenames_get (db_files));
\r
469 + _filename_list_add (priv->removed_files, absolute);
\r
471 + notmuch_filenames_move_to_next (db_files);
\r
474 + while (notmuch_filenames_valid (db_subdirs))
\r
476 + char *absolute = talloc_asprintf (priv->removed_directories,
\r
478 + notmuch_filenames_get (db_subdirs));
\r
480 + _filename_list_add (priv->removed_directories, absolute);
\r
482 + notmuch_filenames_move_to_next (db_subdirs);
\r
485 + if (! state->interrupted) {
\r
486 + status = notmuch_directory_set_mtime (directory, fs_mtime);
\r
487 + if (status && ret == NOTMUCH_STATUS_SUCCESS)
\r
493 + talloc_free (next);
\r
499 + free (fs_entries);
\r
501 + notmuch_filenames_destroy (db_subdirs);
\r
503 + notmuch_filenames_destroy (db_files);
\r
505 + notmuch_directory_destroy (directory);
\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
517 +count_files (notmuch_mailstore_t *mailstore,
\r
518 + const char *path, int *count,
\r
519 + volatile sig_atomic_t *interrupted)
\r
521 + struct dirent *entry = NULL;
\r
524 + struct dirent **fs_entries = NULL;
\r
525 + int num_fs_entries = scandir (path, &fs_entries, 0, dirent_sort_inode);
\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
535 + while (!*interrupted) {
\r
536 + if (i == num_fs_entries)
\r
539 + entry = fs_entries[i++];
\r
541 + /* Ignore special directories to avoid infinite recursion.
\r
542 + * Also ignore the .notmuch directory.
\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
553 + if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
\r
555 + fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
\r
556 + path, entry->d_name);
\r
560 + stat (next, &st);
\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
568 + } else if (S_ISDIR (st.st_mode)) {
\r
569 + count_files (mailstore, next, count, interrupted);
\r
579 + free (fs_entries);
\r
582 +/* Recursively remove all filenames from the database referring to
\r
583 + * 'path' (or to any of its children). */
\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
591 + notmuch_directory_t *directory;
\r
592 + notmuch_filenames_t *files, *subdirs;
\r
593 + notmuch_status_t status;
\r
596 + directory = notmuch_database_get_directory (notmuch, path);
\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
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
608 + *removed_files = *removed_files + 1;
\r
609 + talloc_free (absolute);
\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
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
622 + notmuch_directory_destroy (directory);
\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
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
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
639 + priv->removed_files = _filename_list_create (priv);
\r
640 + priv->removed_directories = _filename_list_create (priv);
\r
642 + ret = add_files_recursive(mailstore, path, indexing_ctx);
\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
651 + indexing_ctx->removed_files++;
\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
660 + talloc_free(priv);
\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
671 + .count_files = count_files,
\r
672 + .index_new = index_new,
\r
673 + .sync_tags = NULL,
\r
674 .open_file = open_file,
\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
682 #include <unistd.h>
\r
684 -typedef struct _filename_node {
\r
686 - struct _filename_node *next;
\r
687 -} _filename_node_t;
\r
689 -typedef struct _filename_list {
\r
690 - _filename_node_t *head;
\r
691 - _filename_node_t **tail;
\r
692 -} _filename_list_t;
\r
695 - int output_is_a_tty;
\r
699 - int processed_files;
\r
700 - int added_messages;
\r
701 - struct timeval tv_start;
\r
703 - _filename_list_t *removed_files;
\r
704 - _filename_list_t *removed_directories;
\r
705 -} add_files_state_t;
\r
707 -static volatile sig_atomic_t do_add_files_print_progress = 0;
\r
708 +notmuch_indexing_context_t *indexing_ctx;
\r
711 handle_sigalrm (unused (int signal))
\r
713 - do_add_files_print_progress = 1;
\r
714 + indexing_ctx->print_progress = 1;
\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
721 ignored = write(2, msg, sizeof(msg)-1);
\r
723 + indexing_ctx->interrupted = 1;
\r
726 -static _filename_list_t *
\r
727 -_filename_list_create (const void *ctx)
\r
729 - _filename_list_t *list;
\r
731 - list = talloc (ctx, _filename_list_t);
\r
732 - if (list == NULL)
\r
735 - list->head = NULL;
\r
736 - list->tail = &list->head;
\r
742 -_filename_list_add (_filename_list_t *list,
\r
743 - const char *filename)
\r
745 - _filename_node_t *node = talloc (list, _filename_node_t);
\r
747 - node->filename = talloc_strdup (list, filename);
\r
748 - node->next = NULL;
\r
750 - *(list->tail) = node;
\r
751 - list->tail = &node->next;
\r
755 -tag_inbox_and_unread (notmuch_message_t *message)
\r
757 - notmuch_message_add_tag (message, "inbox");
\r
758 - notmuch_message_add_tag (message, "unread");
\r
761 + struct timeval tv_start;
\r
762 + int output_is_a_tty;
\r
766 -add_files_print_progress (add_files_state_t *state)
\r
767 +add_files_print_progress (notmuch_indexing_context_t *state)
\r
769 + struct priv_ctx *state2 = state->priv;
\r
770 struct timeval tv_now;
\r
771 double elapsed_overall, rate_overall;
\r
773 gettimeofday (&tv_now, NULL);
\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
779 printf ("Processed %d", state->processed_files);
\r
780 @@ -128,396 +76,42 @@ add_files_print_progress (add_files_state_t *state)
\r
785 -dirent_sort_inode (const struct dirent **a, const struct dirent **b)
\r
787 - return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
\r
791 -dirent_sort_strcmp_name (const struct dirent **a, const struct dirent **b)
\r
793 - return strcmp ((*a)->d_name, (*b)->d_name);
\r
796 -/* Test if the directory looks like a Maildir directory.
\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
801 - * Return 1 if the directory looks like a Maildir and 0 otherwise.
\r
804 -_entries_resemble_maildir (struct dirent **entries, int count)
\r
806 - int i, found = 0;
\r
808 - for (i = 0; i < count; i++) {
\r
809 - if (entries[i]->d_type != DT_DIR && entries[i]->d_type != DT_UNKNOWN)
\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
825 -/* Examine 'path' recursively as follows:
\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
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
835 - * o Pass 1: For each directory in fs_entries, recursively call into
\r
836 - * this same function.
\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
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
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
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
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
858 - * o Tell the database to update its time of 'path' to 'fs_mtime'
\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
865 +add_files_print_verbose (notmuch_indexing_context_t *state, const char *filename)
\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
879 - notmuch_bool_t is_maildir, new_directory;
\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
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
893 - fs_mtime = st.st_mtime;
\r
895 - directory = notmuch_database_get_directory (notmuch, path);
\r
896 - db_mtime = notmuch_directory_get_mtime (directory);
\r
898 - if (db_mtime == 0) {
\r
899 - new_directory = TRUE;
\r
901 - db_subdirs = NULL;
\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
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
913 - dirent_sort_inode : dirent_sort_strcmp_name);
\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
922 - /* Pass 1: Recurse into all sub-directories. */
\r
923 - is_maildir = _entries_resemble_maildir (fs_entries, num_fs_entries);
\r
925 - for (i = 0; i < num_fs_entries; i++) {
\r
929 - entry = fs_entries[i];
\r
931 - /* We only want to descend into directories.
\r
932 - * But symlinks can be to directories too, of course.
\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
938 - if (entry->d_type != DT_DIR &&
\r
939 - entry->d_type != DT_LNK &&
\r
940 - entry->d_type != DT_UNKNOWN)
\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
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
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
963 - talloc_free (next);
\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
972 - /* Pass 2: Scan for new files, removed files, and removed directories. */
\r
973 - for (i = 0; i < num_fs_entries; i++)
\r
977 + struct priv_ctx *state2 = state->priv;
\r
978 + if (state2->output_is_a_tty)
\r
979 + printf("\r\033[K");
\r
981 - entry = fs_entries[i];
\r
982 + printf ("%i/%i: %s",
\r
983 + state->processed_files,
\r
984 + state->total_files,
\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
992 - char *absolute = talloc_asprintf (state->removed_files,
\r
994 - notmuch_filenames_get (db_files));
\r
996 - _filename_list_add (state->removed_files, absolute);
\r
998 - notmuch_filenames_move_to_next (db_files);
\r
1001 - while (notmuch_filenames_valid (db_subdirs) &&
\r
1002 - strcmp (notmuch_filenames_get (db_subdirs), entry->d_name) <= 0)
\r
1004 - const char *filename = notmuch_filenames_get (db_subdirs);
\r
1006 - if (strcmp (filename, entry->d_name) < 0)
\r
1008 - char *absolute = talloc_asprintf (state->removed_directories,
\r
1009 - "%s/%s", path, filename);
\r
1011 - _filename_list_add (state->removed_directories, absolute);
\r
1014 - notmuch_filenames_move_to_next (db_subdirs);
\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
1020 - * Similarly, if the file is of unknown type (due to filesytem
\r
1021 - * limitations), then we also need to look closer.
\r
1023 - * In either case, a stat does the trick.
\r
1025 - if (entry->d_type == DT_LNK || entry->d_type == DT_UNKNOWN) {
\r
1028 - next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
\r
1029 - err = stat (next, &st);
\r
1030 - talloc_free (next);
\r
1033 - /* Don't emit an error for a link pointing nowhere, since
\r
1034 - * the directory-traversal pass will have already done
\r
1039 - if (! S_ISREG (st.st_mode))
\r
1041 - } else if (entry->d_type != DT_REG) {
\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
1049 - notmuch_filenames_move_to_next (db_files);
\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
1057 - state->processed_files++;
\r
1059 - if (state->verbose) {
\r
1060 - if (state->output_is_a_tty)
\r
1061 - printf("\r\033[K");
\r
1063 - printf ("%i/%i: %s",
\r
1064 - state->processed_files,
\r
1065 - state->total_files,
\r
1068 - putchar((state->output_is_a_tty) ? '\r' : '\n');
\r
1069 - fflush (stdout);
\r
1072 - status = notmuch_database_add_message (notmuch, next, &message);
\r
1073 - switch (status) {
\r
1075 - case NOTMUCH_STATUS_SUCCESS:
\r
1076 - state->added_messages++;
\r
1077 - tag_inbox_and_unread (message);
\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
1083 - case NOTMUCH_STATUS_FILE_NOT_EMAIL:
\r
1084 - fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
\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
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
1106 - notmuch_message_destroy (message);
\r
1110 - if (do_add_files_print_progress) {
\r
1111 - do_add_files_print_progress = 0;
\r
1112 - add_files_print_progress (state);
\r
1115 - talloc_free (next);
\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
1123 - char *absolute = talloc_asprintf (state->removed_files,
\r
1125 - notmuch_filenames_get (db_files));
\r
1127 - _filename_list_add (state->removed_files, absolute);
\r
1129 - notmuch_filenames_move_to_next (db_files);
\r
1132 - while (notmuch_filenames_valid (db_subdirs))
\r
1134 - char *absolute = talloc_asprintf (state->removed_directories,
\r
1136 - notmuch_filenames_get (db_subdirs));
\r
1138 - _filename_list_add (state->removed_directories, absolute);
\r
1140 - notmuch_filenames_move_to_next (db_subdirs);
\r
1143 - if (! interrupted) {
\r
1144 - status = notmuch_directory_set_mtime (directory, fs_mtime);
\r
1145 - if (status && ret == NOTMUCH_STATUS_SUCCESS)
\r
1151 - talloc_free (next);
\r
1157 - free (fs_entries);
\r
1159 - notmuch_filenames_destroy (db_subdirs);
\r
1161 - notmuch_filenames_destroy (db_files);
\r
1163 - notmuch_directory_destroy (directory);
\r
1166 + putchar((state2->output_is_a_tty) ? '\r' : '\n');
\r
1167 + fflush (stdout);
\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
1177 - add_files_state_t *state)
\r
1178 + notmuch_indexing_context_t *state) /* FIXME: rename */
\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
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
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
1200 - status = add_files_recursive (notmuch, path, state);
\r
1201 + status = notmuch_mailstore_index_new (mailstore, path, state);
\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
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
1216 -count_files (const char *path, int *count)
\r
1218 - struct dirent *entry = NULL;
\r
1221 - struct dirent **fs_entries = NULL;
\r
1222 - int num_fs_entries = scandir (path, &fs_entries, 0, dirent_sort_inode);
\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
1231 - while (!interrupted) {
\r
1232 - if (i == num_fs_entries)
\r
1235 - entry = fs_entries[i++];
\r
1237 - /* Ignore special directories to avoid infinite recursion.
\r
1238 - * Also ignore the .notmuch directory.
\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
1249 - if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
\r
1251 - fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
\r
1252 - path, entry->d_name);
\r
1256 - stat (next, &st);
\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
1264 - } else if (S_ISDIR (st.st_mode)) {
\r
1265 - count_files (next, count);
\r
1275 - free (fs_entries);
\r
1279 upgrade_print_progress (void *closure,
\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
1286 printf ("Upgrading database: %.2f%% complete", progress * 100.0);
\r
1288 @@ -647,7 +173,7 @@ upgrade_print_progress (void *closure,
\r
1290 gettimeofday (&tv_now, NULL);
\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
1296 notmuch_time_print_formatted_seconds (time_remaining);
\r
1297 @@ -659,55 +185,13 @@ upgrade_print_progress (void *closure,
\r
1301 -/* Recursively remove all filenames from the database referring to
\r
1302 - * 'path' (or to any of its children). */
\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
1310 - notmuch_directory_t *directory;
\r
1311 - notmuch_filenames_t *files, *subdirs;
\r
1312 - notmuch_status_t status;
\r
1315 - directory = notmuch_database_get_directory (notmuch, path);
\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
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
1327 - *removed_files = *removed_files + 1;
\r
1328 - talloc_free (absolute);
\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
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
1341 - notmuch_directory_destroy (directory);
\r
1345 notmuch_new_command (void *ctx, int argc, char *argv[])
\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
1353 struct timeval tv_now;
\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
1363 + notmuch_mailstore_t *mailstore;
\r
1365 + indexing_ctx = &add_files_state;
\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
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
1377 db_path = notmuch_config_get_database_path (config);
\r
1378 + mailstore = notmuch_config_get_mailstore (config);
\r
1380 dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
\r
1382 @@ -744,24 +230,22 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
\r
1386 - count_files (db_path, &count);
\r
1387 + notmuch_mailstore_count_files (mailstore, db_path, &count, &interrupted);
\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
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
1402 if (notmuch == NULL)
\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
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
1426 - add_files_state.removed_files = _filename_list_create (ctx);
\r
1427 - add_files_state.removed_directories = _filename_list_create (ctx);
\r
1429 - ret = add_files (notmuch, db_path, &add_files_state);
\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
1438 - removed_files++;
\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
1445 + gettimeofday (&state2.tv_start, NULL);
\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
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
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
1461 - if (removed_files) {
\r
1462 + if (add_files_state.removed_files) {
\r
1463 printf (" Removed %d %s.",
\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
1470 - if (renamed_files) {
\r
1471 + if (add_files_state.renamed_files) {
\r
1472 printf (" Detected %d file %s.",
\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