[notmuch] [RFC PATCH -V2] notmuch: Add support for multiple maildirs

Aneesh Kumar K.V aneesh.kumar at linux.vnet.ibm.com
Sun Nov 22 10:28:46 PST 2009


This patch separate database path and maildir paths.
It also adds support for multiple maildir paths which
is represented by comma separated values. You need
to have in ~/.notmuch-config

[maildirs]
path=path1,path2

Signed-off-by: Aneesh Kumar K.V <aneesh.kumar at linux.vnet.ibm.com>
---
 notmuch-client.h |   12 ++++++++++++
 notmuch-config.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 notmuch-new.c    |   24 +++++++++++++++++++++---
 notmuch-setup.c  |   37 +++++++++++++++++++++++++++++++++++--
 4 files changed, 122 insertions(+), 5 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index ea77686..f2fc19f 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -83,6 +83,11 @@ typedef struct {
     add_files_callback_t callback;
 } add_files_state_t;
 
+struct count_ele {
+	int count;
+	char ele[0];
+};
+
 static inline void
 chomp_newline (char *str)
 {
@@ -182,4 +187,11 @@ notmuch_config_set_user_other_email (notmuch_config_t *config,
 notmuch_bool_t
 debugger_is_active (void);
 
+const struct count_ele *
+notmuch_config_get_maildirs (notmuch_config_t *config);
+
+void
+notmuch_config_set_maildirs (notmuch_config_t *config,
+				const char *maildirs);
+
 #endif
diff --git a/notmuch-config.c b/notmuch-config.c
index aaa0372..330da48 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -62,6 +62,7 @@ struct _notmuch_config {
     char *user_primary_email;
     char **user_other_email;
     size_t user_other_email_length;
+    struct count_ele *maildirs;
 };
 
 static int
@@ -345,6 +346,59 @@ notmuch_config_set_database_path (notmuch_config_t *config,
     config->database_path = NULL;
 }
 
+const struct count_ele *
+notmuch_config_get_maildirs (notmuch_config_t *config)
+{
+	int size;
+	char *cur_ptr;
+	char *dirs, *token, *saveptr;
+	struct count_ele *maildirs = NULL;
+	if (config->maildirs == NULL) {
+	     dirs = g_key_file_get_string (config->key_file,
+				     "maildirs", "path", NULL);
+		if (dirs) {
+			size = sizeof(struct count_ele) + strlen(dirs) + 1;
+			/* comma separated paths */
+			maildirs = (struct count_ele *)malloc(size);
+			maildirs->count = 0;
+			cur_ptr = maildirs->ele;
+			token = strtok_r(dirs, ",", &saveptr);
+			if (token == NULL) {
+				/* only one element */
+				strcpy(maildirs->ele, dirs);
+				maildirs->count = 1;
+				free(dirs);
+				config->maildirs = maildirs;
+				return maildirs;
+			}
+			strcpy(maildirs->ele, token);
+			maildirs->count++;
+			cur_ptr += strlen(token) + 1;
+			while ((token = strtok_r(NULL, ",", &saveptr))) {
+				strcpy(cur_ptr, token);
+				maildirs->count++;
+				cur_ptr += strlen(token) + 1;
+			}
+			free (dirs);
+		}
+		config->maildirs = maildirs;
+	}
+	return config->maildirs;
+
+
+}
+
+void
+notmuch_config_set_maildirs (notmuch_config_t *config,
+				  const char *maildirs)
+{
+    g_key_file_set_string (config->key_file,
+			   "maildirs", "path", maildirs);
+
+    free (config->maildirs);
+    config->maildirs = NULL;
+}
+
 const char *
 notmuch_config_get_user_name (notmuch_config_t *config)
 {
diff --git a/notmuch-new.c b/notmuch-new.c
index 0dd2784..1a9406b 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -385,14 +385,16 @@ notmuch_new_command (void *ctx,
 {
     notmuch_config_t *config;
     notmuch_database_t *notmuch;
+    const struct count_ele *maildirs;
     add_files_state_t add_files_state;
     double elapsed;
     struct timeval tv_now;
-    int ret = 0;
+    int ret = 0, maildirs_count;
     struct stat st;
     const char *db_path;
     char *dot_notmuch_path;
     struct sigaction action;
+    const char *maildir_path;
 
     /* Setup our handler for SIGINT */
     memset (&action, 0, sizeof (struct sigaction));
@@ -406,6 +408,9 @@ notmuch_new_command (void *ctx,
 	return 1;
 
     db_path = notmuch_config_get_database_path (config);
+    maildirs = notmuch_config_get_maildirs (config);
+    if (maildirs == NULL)
+	    return 1;
 
     dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
 
@@ -413,7 +418,13 @@ notmuch_new_command (void *ctx,
 	int count;
 
 	count = 0;
-	count_files (db_path, &count);
+	maildirs_count = maildirs->count;
+	maildir_path   = maildirs->ele;
+	while (maildirs_count) {
+		count_files (maildir_path, &count);
+		maildir_path += strlen(maildir_path) + 1;
+		maildirs_count--;
+	}
 	if (interrupted)
 	    return 1;
 
@@ -439,7 +450,14 @@ notmuch_new_command (void *ctx,
     add_files_state.added_messages = 0;
     gettimeofday (&add_files_state.tv_start, NULL);
 
-    ret = add_files (notmuch, db_path, &add_files_state);
+    maildirs_count = maildirs->count;
+    maildir_path   = maildirs->ele;
+    while (maildirs_count) {
+	    printf ("Processing maildir %s\n", maildir_path);
+	    ret = add_files (notmuch, maildir_path, &add_files_state);
+	    maildir_path += strlen(maildir_path) + 1;
+	    maildirs_count--;
+    }
 
     gettimeofday (&tv_now, NULL);
     elapsed = notmuch_time_elapsed (add_files_state.tv_start,
diff --git a/notmuch-setup.c b/notmuch-setup.c
index 482efd2..e358c68 100644
--- a/notmuch-setup.c
+++ b/notmuch-setup.c
@@ -97,7 +97,10 @@ notmuch_setup_command (unused (void *ctx),
     size_t old_other_emails_len;
     GPtrArray *other_emails;
     unsigned int i;
-    int is_new;
+    int is_new, maildirs_count, size = 0;
+    const struct count_ele *maildirs;
+    const char *maildir_path;
+    char *cmaildirs = NULL;;
 
 #define prompt(format, ...)				\
     do {						\
@@ -146,7 +149,7 @@ notmuch_setup_command (unused (void *ctx),
 					     other_emails->len);
     g_ptr_array_free (other_emails, TRUE);
 
-    prompt ("Top-level directory of your email archive [%s]: ",
+    prompt ("Directory for notmuch database [%s]: ",
 	    notmuch_config_get_database_path (config));
     if (strlen (response)) {
 	const char *absolute_path;
@@ -155,6 +158,36 @@ notmuch_setup_command (unused (void *ctx),
 	notmuch_config_set_database_path (config, absolute_path);
     }
 
+    maildirs = notmuch_config_get_maildirs (config);
+    if (maildirs) {
+	    /* build the comma separated value */
+	    maildirs_count = maildirs->count;
+	    maildir_path   = maildirs->ele;
+	    while (maildirs_count) {
+		    size += strlen(maildir_path) + 1;
+		    maildir_path += strlen(maildir_path) + 1;
+		    maildirs_count--;
+	    }
+	    maildirs_count = maildirs->count;
+	    cmaildirs      = malloc(size);
+	    maildir_path   = maildirs->ele;
+	    memset(cmaildirs, 0, size);
+	    while(maildirs_count) {
+		    strncat(cmaildirs, maildir_path, size);
+		    maildirs_count--;
+		    if (maildirs_count == 0)
+			    break;
+		    strncat(cmaildirs, ",", size);
+		    maildir_path += strlen(maildir_path) + 1;
+
+	    }
+    }
+
+    prompt ("Comma separated maildirs [%s]: ", cmaildirs);
+
+    if (strlen (response))
+	notmuch_config_set_maildirs (config, response);
+
     notmuch_config_save (config);
 
     if (is_new)
-- 
1.6.5.2.74.g610f9



More information about the notmuch mailing list