--- /dev/null
+Return-Path: <glasse@cs.rpi.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id AB64D431E62\r
+ for <notmuch@notmuchmail.org>; Wed, 15 Feb 2012 14:09:45 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.54\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.54 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ RCVD_IN_BL_SPAMCOP_NET=1.246, RCVD_IN_DNSWL_MED=-2.3,\r
+ RCVD_IN_SORBS_WEB=0.614] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id JYgxVZaFZw+r for <notmuch@notmuchmail.org>;\r
+ Wed, 15 Feb 2012 14:09:44 -0800 (PST)\r
+Received: from cliffclavin.cs.rpi.edu (cliffclavin.cs.rpi.edu\r
+ [128.113.126.25]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS\r
+ id 89E9941EB90 for <notmuch@notmuchmail.org>; Wed, 15 Feb 2012 14:09:44 -0800\r
+ (PST)\r
+X-Hash:\r
+ S|1e6fe4067216137514005175a8428c8dbfb39661|ccff6b3b9d4b3c6d4682b599774abdc5\r
+X-Countries: Cameroon, United States\r
+X-SMTP-From: accepted <glasse@cs.rpi.edu> [195.24.209.20] [195.24.209.20]\r
+ (localhost) {Cameroon}\r
+DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=cs.rpi.edu; h=from\r
+ :to:cc:subject:date:message-id:in-reply-to:references; s=\r
+ default; i=glasse@cs.rpi.edu; t=1329343782; x=1329948582; l=7459;\r
+ bh=8Cc1hoc5j0AGxRBD+Gl5gEhg5BE=; b=oz09gGRXEWGVjSqcmGq9Tew\r
+ SQLBvC1fQTydWumrdxMFTgBAXRDhTAZotLp/7co0a818olWbiIhsXmjvIioVh9Ys\r
+ Cq3y5SCZ+i3a8AZCuXJDj8d0HS2JYm/0p4dovv6eZnTKiQVnYoiFVrKcyuUYiIfC\r
+ Z3OLXYZYcWvZp+Rwdc7g=\r
+DomainKey-Signature: a=rsa-sha1; c=nofws; d=cs.rpi.edu; h=from:to:cc\r
+ :subject:date:message-id:in-reply-to:references; q=dns; s=\r
+ default; b=B5tH8TFV4j4s5tN9wbaaaLGaOYa2qn/cRAHl2hnHLnYdMwM1qZAUj\r
+ EIc606a0CA6j1M4K6jB3Zqywl6BwydEXQABE96dotp4Ymka59dh1Lxhhtlnv5X+m\r
+ ZdFFpXTWBMGc22WFqCHZ6AIgaDancPN/+z4+Usq8EwfxVqG6NZ9X2s=\r
+X-Spam-Info: -2.7; ALL_TRUSTED,AWL,BAYES_00\r
+X-Spam-Scanned-By: cliffclavin.cs.rpi.edu using SpamAssassin 3.2.5 (hard limit\r
+ 15)\r
+Authentication-Results: cliffclavin.cs.rpi.edu;\r
+ DKIM=neutral (none) header.from=glasse@cs.rpi.edu;\r
+ SPF=neutral (mfrom;\r
+ Mechanism '?all' matched) smtp.mail=glasse@cs.rpi.edu\r
+X-Auth-Passed: cliffclavin.cs.rpi.edu:q1FM9Dkb008829 Auth:glasse\r
+X-Virus-Scanned-By: cliffclavin.cs.rpi.edu\r
+Received: from localhost ([195.24.209.20]) (authenticated bits=0)\r
+ by cliffclavin.cs.rpi.edu (8.14.3/8.14.3) with ESMTP id q1FM9Dkb008829\r
+ (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO);\r
+ Wed, 15 Feb 2012 17:09:30 -0500 (EST)\r
+ (envelope-from glasse@cs.rpi.edu)\r
+From: Ethan Glasser-Camp <glasse@cs.rpi.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC PATCH 07/13] notmuch-new: pull out useful bits of\r
+ add_files_recursive\r
+Date: Wed, 15 Feb 2012 17:02:00 -0500\r
+Message-Id: <1329343326-16410-8-git-send-email-glasse@cs.rpi.edu>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <1329343326-16410-1-git-send-email-glasse@cs.rpi.edu>\r
+References: <1329343326-16410-1-git-send-email-glasse@cs.rpi.edu>\r
+X-Scanned-By: MIMEDefang 2.67 on 128.113.126.25\r
+Cc: Ethan Glasser-Camp <ethan@betacantrips.com>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 15 Feb 2012 22:09:45 -0000\r
+\r
+From: Ethan Glasser-Camp <ethan@betacantrips.com>\r
+\r
+This is part of notmuch-new refactor phase 1: make add_files stuff\r
+safe for other backends. add_files_recursive is essentially a\r
+maildir-crawling function that periodically adds files to the database\r
+or adds filenames to remove_files or remove_directory lists. I don't\r
+see an easy way to adapt add_files_recursive for other backends who\r
+might not have concepts of directories with other directories inside\r
+of them, so instead just provide an add_files method for each backend.\r
+\r
+This patch pulls some bits out of add_files_recursive which will be\r
+useful for other backends: two reporting functions\r
+_report_before_adding_file and _report_added_file, as well as\r
+_add_message, which actually does the message adding.\r
+\r
+Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>\r
+---\r
+ notmuch-new.c | 193 +++++++++++++++++++++++++++++++++++----------------------\r
+ 1 files changed, 120 insertions(+), 73 deletions(-)\r
+\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 355dde4..dbdfbb6 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -181,6 +181,123 @@ _entries_resemble_maildir (struct dirent **entries, int count)\r
+ return 0;\r
+ }\r
+ \r
++/* Progress-reporting function.\r
++ *\r
++ * Can be used by any mailstore-crawling function that wants to alert\r
++ * users what message it's about to add. Subsequent errors will be due\r
++ * to this message ;)\r
++ */\r
++static void\r
++_report_before_adding_file (add_files_state_t *state, const char *filename)\r
++{\r
++ state->processed_files++;\r
++\r
++ if (state->verbose) {\r
++ if (state->output_is_a_tty)\r
++ printf("\r\033[K");\r
++\r
++ printf ("%i/%i: %s",\r
++ state->processed_files,\r
++ state->total_files,\r
++ filename);\r
++\r
++ putchar((state->output_is_a_tty) ? '\r' : '\n');\r
++ fflush (stdout);\r
++ }\r
++}\r
++\r
++/* Progress-reporting function.\r
++ *\r
++ * Call this to respond to the signal handler for SIGALRM.\r
++ */\r
++static void\r
++_report_added_file (add_files_state_t *state)\r
++{\r
++ if (do_print_progress) {\r
++ do_print_progress = 0;\r
++ generic_print_progress ("Processed", "files", state->tv_start,\r
++ state->processed_files, state->total_files);\r
++ }\r
++}\r
++\r
++\r
++/* Atomically handles adding a message to the database.\r
++ *\r
++ * Should be used by any mailstore-crawling function that finds a new\r
++ * message to add.\r
++ */\r
++static notmuch_status_t\r
++_add_message (add_files_state_t *state, notmuch_database_t *notmuch,\r
++ const char *filename)\r
++{\r
++ notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;\r
++ notmuch_message_t *message;\r
++ const char **tag;\r
++\r
++ status = notmuch_database_begin_atomic (notmuch);\r
++ if (status) {\r
++ ret = status;\r
++ goto DONE;\r
++ }\r
++\r
++ status = notmuch_database_add_message (notmuch, filename, &message);\r
++\r
++ switch (status) {\r
++ /* success */\r
++ case NOTMUCH_STATUS_SUCCESS:\r
++ state->added_messages++;\r
++ notmuch_message_freeze (message);\r
++ for (tag=state->new_tags; *tag != NULL; tag++)\r
++ notmuch_message_add_tag (message, *tag);\r
++ if (state->synchronize_flags == TRUE)\r
++ notmuch_message_maildir_flags_to_tags (message);\r
++ notmuch_message_thaw (message);\r
++ break;\r
++ /* Non-fatal issues (go on to next file) */\r
++ case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
++ if (state->synchronize_flags == TRUE)\r
++ notmuch_message_maildir_flags_to_tags (message);\r
++ break;\r
++ case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
++ fprintf (stderr, "Note: Ignoring non-mail file: %s\n",\r
++ filename);\r
++ break;\r
++ /* Fatal issues. Don't process anymore. */\r
++ case NOTMUCH_STATUS_READ_ONLY_DATABASE:\r
++ case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
++ case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
++ fprintf (stderr, "Error: %s. Halting processing.\n",\r
++ notmuch_status_to_string (status));\r
++ ret = status;\r
++ goto DONE;\r
++ default:\r
++ case NOTMUCH_STATUS_FILE_ERROR:\r
++ case NOTMUCH_STATUS_NULL_POINTER:\r
++ case NOTMUCH_STATUS_TAG_TOO_LONG:\r
++ case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:\r
++ case NOTMUCH_STATUS_UNBALANCED_ATOMIC:\r
++ case NOTMUCH_STATUS_LAST_STATUS:\r
++ INTERNAL_ERROR ("add_message returned unexpected value: %d", status);\r
++ ret = status;\r
++ goto DONE;\r
++ }\r
++\r
++ status = notmuch_database_end_atomic (notmuch);\r
++ if (status) {\r
++ ret = status;\r
++ goto DONE;\r
++ }\r
++\r
++ DONE:\r
++ if (message) {\r
++ notmuch_message_destroy (message);\r
++ message = NULL;\r
++ }\r
++\r
++ return ret;\r
++}\r
++\r
++\r
+ /* Examine 'path' recursively as follows:\r
+ *\r
+ * o Ask the filesystem for the mtime of 'path' (fs_mtime)\r
+@@ -232,7 +349,6 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ char *next = NULL;\r
+ time_t fs_mtime, db_mtime;\r
+ notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;\r
+- notmuch_message_t *message = NULL;\r
+ struct dirent **fs_entries = NULL;\r
+ int i, num_fs_entries;\r
+ notmuch_directory_t *directory;\r
+@@ -241,7 +357,6 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ time_t stat_time;\r
+ struct stat st;\r
+ notmuch_bool_t is_maildir, new_directory;\r
+- const char **tag;\r
+ \r
+ if (stat (path, &st)) {\r
+ fprintf (stderr, "Error reading directory %s: %s\n",\r
+@@ -439,83 +554,15 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ * in the database, so add it. */\r
+ next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
+ \r
+- state->processed_files++;\r
+-\r
+- if (state->verbose) {\r
+- if (state->output_is_a_tty)\r
+- printf("\r\033[K");\r
++ _report_before_adding_file (state, next);\r
+ \r
+- printf ("%i/%i: %s",\r
+- state->processed_files,\r
+- state->total_files,\r
+- next);\r
+-\r
+- putchar((state->output_is_a_tty) ? '\r' : '\n');\r
+- fflush (stdout);\r
+- }\r
+-\r
+- status = notmuch_database_begin_atomic (notmuch);\r
++ status = _add_message (state, notmuch, next);\r
+ if (status) {\r
+ ret = status;\r
+ goto DONE;\r
+ }\r
+ \r
+- status = notmuch_database_add_message (notmuch, next, &message);\r
+- switch (status) {\r
+- /* success */\r
+- case NOTMUCH_STATUS_SUCCESS:\r
+- state->added_messages++;\r
+- notmuch_message_freeze (message);\r
+- for (tag=state->new_tags; *tag != NULL; tag++)\r
+- notmuch_message_add_tag (message, *tag);\r
+- if (state->synchronize_flags == TRUE)\r
+- notmuch_message_maildir_flags_to_tags (message);\r
+- notmuch_message_thaw (message);\r
+- break;\r
+- /* Non-fatal issues (go on to next file) */\r
+- case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
+- if (state->synchronize_flags == TRUE)\r
+- notmuch_message_maildir_flags_to_tags (message);\r
+- break;\r
+- case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
+- fprintf (stderr, "Note: Ignoring non-mail file: %s\n",\r
+- next);\r
+- break;\r
+- /* Fatal issues. Don't process anymore. */\r
+- case NOTMUCH_STATUS_READ_ONLY_DATABASE:\r
+- case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
+- case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
+- fprintf (stderr, "Error: %s. Halting processing.\n",\r
+- notmuch_status_to_string (status));\r
+- ret = status;\r
+- goto DONE;\r
+- default:\r
+- case NOTMUCH_STATUS_FILE_ERROR:\r
+- case NOTMUCH_STATUS_NULL_POINTER:\r
+- case NOTMUCH_STATUS_TAG_TOO_LONG:\r
+- case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:\r
+- case NOTMUCH_STATUS_UNBALANCED_ATOMIC:\r
+- case NOTMUCH_STATUS_LAST_STATUS:\r
+- INTERNAL_ERROR ("add_message returned unexpected value: %d", status);\r
+- goto DONE;\r
+- }\r
+-\r
+- status = notmuch_database_end_atomic (notmuch);\r
+- if (status) {\r
+- ret = status;\r
+- goto DONE;\r
+- }\r
+-\r
+- if (message) {\r
+- notmuch_message_destroy (message);\r
+- message = NULL;\r
+- }\r
+-\r
+- if (do_print_progress) {\r
+- do_print_progress = 0;\r
+- generic_print_progress ("Processed", "files", state->tv_start,\r
+- state->processed_files, state->total_files);\r
+- }\r
++ _report_added_file (state);\r
+ \r
+ talloc_free (next);\r
+ next = NULL;\r
+-- \r
+1.7.5.4\r
+\r