[notmuch] [PATCH 3/4] Configure the database separately from mail store

David Benjamin davidben at MIT.EDU
Tue Feb 23 18:23:02 PST 2010


Signed-off-by: David Benjamin <davidben at mit.edu>
---
 notmuch-client.h |    7 ++++++
 notmuch-config.c |   62 ++++++++++++++++++++++++++++++++++++++++--------------
 notmuch-new.c    |   14 ++++-------
 3 files changed, 58 insertions(+), 25 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index 1a676d2..010fdc7 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -148,6 +148,13 @@ void
 notmuch_config_set_database_path (notmuch_config_t *config,
 				  const char *database_path);
 
+const char *
+notmuch_config_get_database_notmuch_path (notmuch_config_t *config);
+
+void
+notmuch_config_set_database_notmuch_path (notmuch_config_t *config,
+					  const char *database_notmuch_path);
+
 notmuch_database_t *
 notmuch_config_open_database (notmuch_config_t *config,
 			      notmuch_database_mode_t mode);
diff --git a/notmuch-config.c b/notmuch-config.c
index 58a28b1..b20047c 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -58,6 +58,7 @@ struct _notmuch_config {
     GKeyFile *key_file;
 
     char *database_path;
+    char *database_notmuch_path;
     char *user_name;
     char *user_primary_email;
     char **user_other_email;
@@ -151,6 +152,8 @@ get_username_from_passwd_file (void *ctx)
  *
  *	database_path:		$HOME/mail
  *
+ *	database_notmuch_path:	database_path/.notmuch
+ *
  *	user_name:		From /etc/passwd
  *
  *	user_primary_mail: 	$EMAIL variable if set, otherwise
@@ -195,6 +198,7 @@ notmuch_config_open (void *ctx,
     config->key_file = g_key_file_new ();
 
     config->database_path = NULL;
+    config->database_notmuch_path = NULL;
     config->user_name = NULL;
     config->user_primary_email = NULL;
     config->user_other_email = NULL;
@@ -351,6 +355,45 @@ notmuch_config_set_database_path (notmuch_config_t *config,
 
     talloc_free (config->database_path);
     config->database_path = NULL;
+    /* In case this path is dynamically generated */
+    talloc_free (config->database_notmuch_path);
+    config->database_notmuch_path = NULL
+}
+
+const char *
+notmuch_config_get_database_notmuch_path (notmuch_config_t *config)
+{
+    const char *path;
+    char *notmuch_path;
+
+    if (config->database_notmuch_path == NULL) {
+	notmuch_path = g_key_file_get_string (config->key_file,
+					      "database", "notmuch_path", NULL);
+	if (notmuch_path) {
+	    config->database_notmuch_path = talloc_strdup (config, notmuch_path);
+	    free (notmuch_path);
+	} else {
+	    path = notmuch_config_get_database_path (config);
+	    if (path != NULL) {
+		notmuch_path = talloc_asprintf (config, "%s/%s",
+						path, ".notmuch");
+		config->database_notmuch_path = notmuch_path;
+	    }
+	}
+    }
+
+    return config->database_notmuch_path;
+}
+
+void
+notmuch_config_set_database_notmuch_path (notmuch_config_t *config,
+					  const char *database_notmuch_path)
+{
+    g_key_file_set_string (config->key_file,
+			   "database", "notmuch_path", database_notmuch_path);
+
+    talloc_free (config->database_notmuch_path);
+    config->database_notmuch_path = NULL;
 }
 
 notmuch_database_t *
@@ -358,26 +401,13 @@ notmuch_config_open_database (notmuch_config_t *config,
 			      notmuch_database_mode_t mode)
 {
     const char *path = NULL;
-    char *db_path = NULL;
+    const char *notmuch_path = NULL;
     notmuch_database_t *notmuch = NULL;
 
     path = notmuch_config_get_database_path (config);
-    if (path == NULL) {
-	fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
-	goto DONE;
-    }
-
-    if (asprintf (&db_path, "%s/%s", path, ".notmuch") == -1) {
-	db_path = NULL;
-	fprintf (stderr, "Out of memory\n");
-	goto DONE;
-    }
-
-    notmuch = notmuch_database_open (path, db_path, mode);
+    notmuch_path = notmuch_config_get_database_notmuch_path (config);
 
-DONE:
-    if (db_path)
-	free(db_path);
+    notmuch = notmuch_database_open (path, notmuch_path, mode);
 
     return notmuch;
 }
diff --git a/notmuch-new.c b/notmuch-new.c
index d24dab9..d36edd5 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -713,7 +713,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
     int ret = 0;
     struct stat st;
     const char *db_path;
-    char *dot_notmuch_path;
+    const char *notmuch_path;
     struct sigaction action;
     _filename_node_t *f;
     int renamed_files, removed_files;
@@ -737,10 +737,9 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 	return 1;
 
     db_path = notmuch_config_get_database_path (config);
+    notmuch_path = notmuch_config_get_database_notmuch_path (config);
 
-    dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");
-
-    if (stat (dot_notmuch_path, &st)) {
+    if (stat (notmuch_path, &st)) {
 	int count;
 
 	count = 0;
@@ -749,11 +748,11 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 	    return 1;
 
 	printf ("Found %d total files (that's not much mail).\n", count);
-	notmuch = notmuch_database_create (db_path, dot_notmuch_path);
+	notmuch = notmuch_database_create (db_path, notmuch_path);
 	add_files_state.total_files = count;
     } else {
 	notmuch = notmuch_database_open (db_path,
-					 dot_notmuch_path,
+					 notmuch_path,
 					 NOTMUCH_DATABASE_MODE_READ_WRITE);
 	if (notmuch == NULL)
 	    return 1;
@@ -782,9 +781,6 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
     action.sa_flags = SA_RESTART;
     sigaction (SIGINT, &action, NULL);
 
-    talloc_free (dot_notmuch_path);
-    dot_notmuch_path = NULL;
-
     add_files_state.processed_files = 0;
     add_files_state.added_messages = 0;
     gettimeofday (&add_files_state.tv_start, NULL);
-- 
1.7.0.18.g39b3



More information about the notmuch mailing list