--- /dev/null
+Return-Path: <michiel@michielbuddingh.net>\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 8C865431FBD\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Dec 2009 11:55:25 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\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 Jq4++xPeRDab for <notmuch@notmuchmail.org>;\r
+ Sun, 6 Dec 2009 11:55:24 -0800 (PST)\r
+Received: from aegir.org.uk (aegir.org.uk [87.238.170.13])\r
+ by olra.theworths.org (Postfix) with ESMTP id 29638431FBC\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Dec 2009 11:55:24 -0800 (PST)\r
+Received: from localhost (109-9-ftth.onsnetstudenten.nl [145.120.9.109])\r
+ (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by aegir.org.uk (Postfix) with ESMTPSA id ED35A2E01B;\r
+ Sun, 6 Dec 2009 20:55:22 +0100 (CET)\r
+From: Michiel Buddingh' <michiel@michielbuddingh.net>\r
+To: Carl Worth <cworth@cworth.org>, notmuch@notmuchmail.org\r
+In-Reply-To: <87ws1bjpmm.fsf@yoom.home.cworth.org>\r
+References: <87fx8bygi7.fsf@linux.vnet.ibm.com>\r
+ <87bpiv4t9h.fsf@yoom.home.cworth.org>\r
+ <87y6lz39nd.fsf@yoom.home.cworth.org>\r
+ <20091121221207.GB17268@jukie.net>\r
+ <9cce5525b093b87fe74d427954ffad89@localhost>\r
+ <87d43b2oif.fsf@yoom.home.cworth.org>\r
+ <9bfdedddeab9c58cd45d8d448323d0fc@localhost>\r
+ <87skc23327.fsf@yoom.home.cworth.org>\r
+ <4b0eef22.JwxdgTGElffx149F%michiel@michielbuddingh.net>\r
+ <87ws1bjpmm.fsf@yoom.home.cworth.org>\r
+Date: Sun, 06 Dec 2009 20:55:22 +0100\r
+Message-ID: <878wdfkhcl.fsf@aegir.org.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Subject: Re: [notmuch] [PATCH] notmuch: Add Maildir directory name as tag\r
+ name for messages\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.12\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: Sun, 06 Dec 2009 19:55:25 -0000\r
+\r
+\r
+First of all, apologies for taking so long to get back to this.\r
+\r
+On Fri, 27 Nov 2009, Carl Worth <cworth@cworth.org> wrote:\r
+> The auto-detection is just three additional stats (at most) for each\r
+> directory, right? That seems cheap enough to me.\r
+\r
+If that's cheap enough, then I won't disagree with auto-detection. \r
+Jan Janak's patch seems to take most of the disk access cost out of it,\r
+in any case.\r
+\r
+> That seems orthogonal to me. Would the dovecot index files be easy to\r
+> skip with a pattern-based blacklist?\r
+\r
+Yes, and that's a much more elegant solution.\r
+\r
+> > I'll be happy to implement them, although I'd like for others to\r
+> > chime in on the configure-as-Maildir vs. autodetect-Maildir issue.\r
+> > And thanks for your patience in working through my patch.\r
+\r
+I didn't mean to call a vote--rather to solicit the opinions of others\r
+with possibly even more exotic mail storage configurations.\r
+\r
+A new patch is attached. Apologies for the rather verbose Maildir\r
+handling logic, but I couldn't find a way to minimize the calls to\r
+is_maildir that was both neat and readable.\r
+\r
+-- \r
+Michiel\r
+\r
+---\r
+ notmuch-client.h | 1 +\r
+ notmuch-new.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++--\r
+ 2 files changed, 90 insertions(+), 4 deletions(-)\r
+\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index 50a30fe..7bc84a1 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -77,6 +77,7 @@ typedef struct {\r
+ int saw_read_only_directory;\r
+ int output_is_a_tty;\r
+ int verbose;\r
++ int tag_maildir;\r
+ \r
+ int total_files;\r
+ int processed_files;\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 9d20616..8742ab4 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -109,6 +109,60 @@ is_maildir (struct dirent **entries, int count)\r
+ return 0;\r
+ }\r
+ \r
++/* Tag new mail according to its Maildir attribute flags.\r
++ *\r
++ * Test if the mail file's filename contains any of the\r
++ * standard Maildir attributes, and translate these to\r
++ * the corresponding standard notmuch tags.\r
++ *\r
++ * If the message is not marked as 'seen', or if no\r
++ * flags are present, tag as 'inbox, unread'.\r
++ */\r
++static void\r
++derive_tags_from_maildir_flags (notmuch_message_t *message,\r
++ const char * path)\r
++{\r
++ int seen = FALSE;\r
++ int end_of_flags = FALSE;\r
++ size_t l = strlen(path);\r
++\r
++ /* Non-experimental message flags start with this */\r
++ char * i = strstr(path, ":2,");\r
++ i = (i) ? i : strstr(path, "!2,"); /* This format is used on VFAT */\r
++ if (i != NULL) {\r
++ i += 3;\r
++ for (; i < (path + l) && !end_of_flags; i++) {\r
++ switch (*i) {\r
++ case 'F' :\r
++ notmuch_message_add_tag (message, "flagged");\r
++ break;\r
++ case 'R': /* replied */\r
++ notmuch_message_add_tag (message, "answered");\r
++ break;\r
++ case 'D':\r
++ notmuch_message_add_tag (message, "draft");\r
++ break;\r
++ case 'S': /* seen */\r
++ seen = TRUE;\r
++ break;\r
++ case 'T': /* trashed */\r
++ notmuch_message_add_tag (message, "deleted");\r
++ break;\r
++ case 'P': /* passed */\r
++ notmuch_message_add_tag (message, "forwarded");\r
++ break;\r
++ default:\r
++ end_of_flags = TRUE;\r
++ break;\r
++ }\r
++ }\r
++ }\r
++\r
++ if (i == NULL || !seen) {\r
++ tag_inbox_and_unread (message);\r
++ }\r
++}\r
++\r
+ /* Examine 'path' recursively as follows:\r
+ *\r
+ * o Ask the filesystem for the mtime of 'path' (path_mtime)\r
+@@ -142,6 +196,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;\r
+ notmuch_message_t *message = NULL;\r
+ struct dirent **namelist = NULL;\r
++ int maildir_detected = -1; /* -1 = unset */\r
+ int num_entries;\r
+ \r
+ /* If we're told to, we bail out on encountering a read-only\r
+@@ -189,13 +244,37 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ if (strcmp (entry->d_name, ".") == 0 ||\r
+ strcmp (entry->d_name, "..") == 0 ||\r
+ (entry->d_type == DT_DIR &&\r
+- (strcmp (entry->d_name, "tmp") == 0) &&\r
+- is_maildir (namelist, num_entries)) ||\r
+- strcmp (entry->d_name, ".notmuch") ==0)\r
++ strcmp (entry->d_name, ".notmuch") == 0))\r
+ {\r
+ continue;\r
+ }\r
+ \r
++\r
++ /* If this directory is a Maildir folder, we need to\r
++ * ignore any subdirectories marked tmp/, and scan for\r
++ * Maildir attributes on messages contained in the sub-\r
++ * directories 'new' and 'cur'. */\r
++ if (maildir_detected != 0 &&\r
++ entry->d_type == DT_DIR &&\r
++ ((strcmp (entry->d_name, "tmp") == 0) ||\r
++ (strcmp (entry->d_name, "new") == 0) ||\r
++ (strcmp (entry->d_name, "cur") == 0))) {\r
++\r
++ /* is_maildir scans the entire directory. No need to\r
++ do this more than once, if at all */\r
++ if (maildir_detected == -1) {\r
++ maildir_detected = is_maildir (namelist, num_entries);\r
++ }\r
++\r
++ if (maildir_detected == 1) {\r
++ if (strcmp (entry->d_name, "tmp") == 0) {\r
++ continue;\r
++ } else {\r
++ state->tag_maildir = TRUE;\r
++ }\r
++ }\r
++ }\r
++\r
+ next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
+ \r
+ if (stat (next, st)) {\r
+@@ -240,7 +319,12 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ /* success */\r
+ case NOTMUCH_STATUS_SUCCESS:\r
+ state->added_messages++;\r
+- tag_inbox_and_unread (message);\r
++ if (state->tag_maildir) {\r
++ derive_tags_from_maildir_flags (message,\r
++ entry->d_name);\r
++ } else {\r
++ tag_inbox_and_unread (message);\r
++ }\r
+ break;\r
+ /* Non-fatal issues (go on to next file) */\r
+ case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
+@@ -282,6 +366,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ status = add_files_recursive (notmuch, next, st, state);\r
+ if (status && ret == NOTMUCH_STATUS_SUCCESS)\r
+ ret = status;\r
++ state->tag_maildir = FALSE;\r
+ }\r
+ \r
+ talloc_free (next);\r
+-- \r
+1.6.5.4\r