[notmuch] [PATCH 2/2] Preserve folder information when indexing
authorMichal Sojka <sojkam1@fel.cvut.cz>
Thu, 28 Jan 2010 15:25:17 +0000 (16:25 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:36:04 +0000 (09:36 -0800)
71/7e5da09d85555b40b6b24f247a56cdee8ccb49 [new file with mode: 0644]

diff --git a/71/7e5da09d85555b40b6b24f247a56cdee8ccb49 b/71/7e5da09d85555b40b6b24f247a56cdee8ccb49
new file mode 100644 (file)
index 0000000..a560f70
--- /dev/null
@@ -0,0 +1,213 @@
+Return-Path: <sojkam1@fel.cvut.cz>\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 79D44431FD5\r
+       for <notmuch@notmuchmail.org>; Thu, 28 Jan 2010 07:25:47 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.176\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.176 tagged_above=-999 required=5 tests=[AWL=0.423,\r
+       BAYES_00=-2.599] autolearn=ham\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 woab4JcFKE6X for <notmuch@notmuchmail.org>;\r
+       Thu, 28 Jan 2010 07:25:46 -0800 (PST)\r
+Received: from mout.perfora.net (mout.perfora.net [74.208.4.194])\r
+       by olra.theworths.org (Postfix) with ESMTP id B5999431FDE\r
+       for <notmuch@notmuchmail.org>; Thu, 28 Jan 2010 07:25:44 -0800 (PST)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+       by mx.perfora.net (node=mxus2) with ESMTP (Nemesis)\r
+       id 0MFun8-1NXkYB3MGM-00Ex7G for notmuch@notmuchmail.org;\r
+       Thu, 28 Jan 2010 10:25:38 -0500\r
+Received: from localhost (unknown [192.168.200.4])\r
+       by max.feld.cvut.cz (Postfix) with ESMTP id 50E2219F33A0;\r
+       Thu, 28 Jan 2010 16:25:36 +0100 (CET)\r
+X-Virus-Scanned: IMAP AMAVIS\r
+Received: from max.feld.cvut.cz ([192.168.200.1])\r
+       by localhost (styx.feld.cvut.cz [192.168.200.4]) (amavisd-new,\r
+       port 10044)\r
+       with ESMTP id KfaG+Qz8PfD1; Thu, 28 Jan 2010 16:25:32 +0100 (CET)\r
+Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
+       by max.feld.cvut.cz (Postfix) with ESMTP id 166EA19F338C;\r
+       Thu, 28 Jan 2010 16:25:32 +0100 (CET)\r
+Received: from localhost.localdomain (k335-30.felk.cvut.cz [147.32.86.30])\r
+       (Authenticated sender: sojkam1)\r
+       by imap.feld.cvut.cz (Postfix) with ESMTPSA id 105A5FA004;\r
+       Thu, 28 Jan 2010 16:25:32 +0100 (CET)\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Date: Thu, 28 Jan 2010 16:25:17 +0100\r
+Message-Id: <1264692317-9175-2-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 1.6.6\r
+In-Reply-To: <201001281624.01577.sojkam1@fel.cvut.cz>\r
+References: <201001281624.01577.sojkam1@fel.cvut.cz>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+Subject: [notmuch] [PATCH 2/2] Preserve folder information when indexing\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: Thu, 28 Jan 2010 15:25:47 -0000\r
+\r
+This patch was originally sent by Andreas Klöckner. This is a slightly\r
+reworked version (only coding style changes) which was manually rebased\r
+to the current HEAD.\r
+\r
+There are still things to fix (see the previous Carl's email)\r
+- Redundant trailing directory name traversal\r
+- Supporting hierarchical folders\r
+---\r
+ lib/database.cc |   13 +++++++++----\r
+ lib/notmuch.h   |    1 +\r
+ notmuch-new.c   |   38 +++++++++++++++++++++++++++++++++++++-\r
+ 3 files changed, 47 insertions(+), 5 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index cce7847..5f2f35d 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -84,9 +84,9 @@ typedef struct {\r
+  *    MESSAGE_ID:     The unique ID of the mail mess (see "id" above)\r
+  *\r
+  * In addition, terms from the content of the message are added with\r
+- * "from", "to", "attachment", and "subject" prefixes for use by the\r
+- * user in searching. But the database doesn't really care itself\r
+- * about any of these.\r
++ * "from", "to", "attachment", "subject" and "folder" prefixes for use\r
++ * by the user in searching. But the database doesn't really care\r
++ * itself about any of these.\r
+  *\r
+  * The data portion of a mail document is empty.\r
+  *\r
+@@ -154,7 +154,8 @@ prefix_t PROBABILISTIC_PREFIX[]= {\r
+     { "from",                 "XFROM" },\r
+     { "to",                   "XTO" },\r
+     { "attachment",           "XATTACHMENT" },\r
+-    { "subject",              "XSUBJECT"}\r
++    { "subject",              "XSUBJECT"},\r
++    { "folder",               "XFOLDER"}\r
+ };\r
\r
+ int\r
+@@ -1317,6 +1318,7 @@ _notmuch_database_link_message (notmuch_database_t *notmuch,\r
+ notmuch_status_t\r
+ notmuch_database_add_message (notmuch_database_t *notmuch,\r
+                             const char *filename,\r
++                            const char *folder_name,\r
+                             notmuch_message_t **message_ret)\r
+ {\r
+     notmuch_message_file_t *message_file;\r
+@@ -1432,6 +1434,9 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
+           date = notmuch_message_file_get_header (message_file, "date");\r
+           _notmuch_message_set_date (message, date);\r
\r
++          if (folder_name != NULL)\r
++              _notmuch_message_gen_terms (message, "folder", folder_name);\r
++\r
+           _notmuch_message_index_file (message, filename);\r
+       } else {\r
+           ret = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 15c9db4..3a5ab78 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -263,6 +263,7 @@ notmuch_database_get_directory (notmuch_database_t *database,\r
+ notmuch_status_t\r
+ notmuch_database_add_message (notmuch_database_t *database,\r
+                             const char *filename,\r
++                            const char *folder_name,\r
+                             notmuch_message_t **message);\r
\r
+ /* Remove a message from the given notmuch database.\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index f25c71f..b7c65db 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -21,6 +21,7 @@\r
+ #include "notmuch-client.h"\r
\r
+ #include <unistd.h>\r
++#include <glib.h>\r
\r
+ typedef struct _filename_node {\r
+     char *filename;\r
+@@ -169,6 +170,35 @@ _entries_resemble_maildir (struct dirent **entries, int count)\r
+     return 0;\r
+ }\r
\r
++static char*\r
++_get_folder_base_name(const char *path)\r
++{\r
++    gchar *full_folder_name = NULL;\r
++    gchar *folder_base_name = NULL;\r
++\r
++    /* Find name of "folder" containing the email. */\r
++    full_folder_name = g_strdup(path);\r
++    while (1) {\r
++      folder_base_name = g_path_get_basename(full_folder_name);\r
++\r
++      if (strcmp(folder_base_name, "cur") == 0\r
++          || strcmp(folder_base_name, "new") == 0) {\r
++          gchar *parent_name = g_path_get_dirname(full_folder_name);\r
++          g_free(full_folder_name);\r
++          full_folder_name = parent_name;\r
++      } else\r
++          break;\r
++    }\r
++\r
++    g_free(full_folder_name);\r
++\r
++    if (strcmp(folder_base_name, ".") == 0) {\r
++      g_free(folder_base_name);\r
++      folder_base_name = NULL;\r
++    }\r
++    return folder_base_name;\r
++}\r
++\r
+ /* Examine 'path' recursively as follows:\r
+  *\r
+  *   o Ask the filesystem for the mtime of 'path' (fs_mtime)\r
+@@ -222,6 +252,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+     notmuch_filenames_t *db_subdirs = NULL;\r
+     struct stat st;\r
+     notmuch_bool_t is_maildir, new_directory;\r
++    char *folder_base_name = NULL;\r
\r
+     if (stat (path, &st)) {\r
+       fprintf (stderr, "Error reading directory %s: %s\n",\r
+@@ -407,7 +438,10 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+           fflush (stdout);\r
+       }\r
\r
+-      status = notmuch_database_add_message (notmuch, next, &message);\r
++      folder_base_name = _get_folder_base_name(path);\r
++      status = notmuch_database_add_message (notmuch, next,\r
++                                             folder_base_name,\r
++                                             &message);\r
+       switch (status) {\r
+       /* success */\r
+       case NOTMUCH_STATUS_SUCCESS:\r
+@@ -499,6 +533,8 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+       notmuch_filenames_destroy (db_files);\r
+     if (directory)\r
+       notmuch_directory_destroy (directory);\r
++    if (folder_base_name)\r
++      g_free(folder_base_name);\r
\r
+     return ret;\r
+ }\r
+-- \r
+1.6.6\r
+\r