[notmuch] [PATCH] * notmuch-new.c: refactor and improve dirs-to-ignore a bit

Dirk-Jan C. Binnema djcb.bulk at gmail.com
Sun Jan 10 06:22:20 PST 2010


Below, an updated patch for the latest notmuch; this refractors the dir check
a bit, and adds the useful feature of making 'notmuch new' ignore directories
with a '.noindex'-file in them.

Best wishes,
Dirk.

----
 [PATCH] * notmuch-new.c: refactor and improve dirs-to-ignore a bit
 
  add a new function ignore_dir_entry, which determines whether a dir entry
  should be ignored (not entered).

  dirs to ignore are: '.' and '..', '.notmuch' and 'nnmaildir' (the later from
  gnus). Also, ignore dirs that contain a file called '.noindex'; thus, we can
  tell not much not to consider e.g. dirs with spam messages.
---
 notmuch-new.c |   76 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 56 insertions(+), 20 deletions(-)

diff --git a/notmuch-new.c b/notmuch-new.c
index b740ee2..d1526cd 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -169,6 +169,47 @@ _entries_resemble_maildir (struct dirent **entries, int count)
     return 0;
 }
 
+
+/* Ignore special directories to avoid infinite recursion.
+ * Also ignore the .notmuch directory and any "tmp" directory
+ * that appears within a maildir.
+ */
+static int
+ignore_dir_entry (const char* path, struct dirent *entry)
+{
+    char noindex[4096]; /* any path will fit */
+    
+    /* ignore everything starting with a dot; this covers hidden
+     * files, as well as special dir (. and ..), but also things like
+     * gnus .nnmaildir or .notmuch */
+    
+    /* special handling for dot-dirs */
+    if (entry->d_name[0] == '.') {
+	
+	/* ignore '.' and '..' */
+	if (entry->d_name[1] == '\0' ||
+	    (entry->d_name[1] == '.' && entry->d_name[2] == '\0')) 
+	    return 1;
+	
+	if (entry->d_name[1] == 'n')  { /* optimization */
+	    /* ignore notmuch, gnus special dirs (or such-named files) */
+	    if (strcmp (entry->d_name, ".notmuch") == 0 ||
+		strcmp (entry->d_name, ".nnmaildir") == 0)
+		return 1;
+	}
+    }
+    
+    /* we also check if dir contains a file called '.noindex'; if so,
+     * we ignore this directory; alloca would be suitable here, if not
+     * for the portability. */
+    snprintf (noindex, sizeof(noindex), "%s/%s/.noindex", path, entry->d_name);
+    if (access (noindex, F_OK) == 0)
+	return 1;
+
+    return 0; /* don't ignore */
+}
+
+
 /* Examine 'path' recursively as follows:
  *
  *   o Ask the filesystem for the mtime of 'path' (fs_mtime)
@@ -275,21 +316,18 @@ add_files_recursive (notmuch_database_t *notmuch,
 
 	if (entry->d_type != DT_DIR && entry->d_type != DT_LNK)
 	    continue;
+	
+	/* ignore tmp Maildirs, for obvious reasons */
+	if (is_maildir && strcmp (entry->d_name, "tmp") == 0)
+	    continue;
 
 	/* Ignore special directories to avoid infinite recursion.
-	 * Also ignore the .notmuch directory and any "tmp" directory
-	 * that appears within a maildir.
+	 * Also ignore Maildir tmp-dirs, dirs contain .noindex files, and
+	 * the .notmuch and .nnmaildir directories.
 	 */
-	/* XXX: Eventually we'll want more sophistication to let the
-	 * user specify files to be ignored. */
-	if (strcmp (entry->d_name, ".") == 0 ||
-	    strcmp (entry->d_name, "..") == 0 ||
-	    (is_maildir && strcmp (entry->d_name, "tmp") == 0) ||
-	    strcmp (entry->d_name, ".notmuch") ==0)
-	{
+	if (ignore_dir_entry (path, entry))
 	    continue;
-	}
-
+	
 	next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
 	status = add_files_recursive (notmuch, next, state);
 	if (status && ret == NOTMUCH_STATUS_SUCCESS)
@@ -575,18 +613,16 @@ count_files (const char *path, int *count)
 
         entry = fs_entries[i++];
 
+	/* Note: it seems we're missing the '_entries_resemble_maildir' check
+	 * here */
+	
 	/* Ignore special directories to avoid infinite recursion.
-	 * Also ignore the .notmuch directory.
+	 * Also ignore Maildir tmp-dirs, dirs contain .noindex files, and
+	 * the .notmuch and .nnmaildir directories.
 	 */
-	/* XXX: Eventually we'll want more sophistication to let the
-	 * user specify files to be ignored. */
-	if (strcmp (entry->d_name, ".") == 0 ||
-	    strcmp (entry->d_name, "..") == 0 ||
-	    strcmp (entry->d_name, ".notmuch") == 0)
-	{
+	if (ignore_dir_entry (path, entry))
 	    continue;
-	}
-
+	
 	if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
 	    next = NULL;
 	    fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
-- 
1.6.3.3

-- 
Dirk-Jan C. Binnema                  Helsinki, Finland
e:djcb at djcbsoftware.nl           w:www.djcbsoftware.nl
pgp: D09C E664 897D 7D39 5047 A178 E96A C7A1 017D DA3C


More information about the notmuch mailing list