[notmuch] [PATCH 2/4] Pass separate notmuch_path when opening database

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


Avoid hard-coding $MAILSTORE/.notmuch in the database layer.

Signed-off-by: David Benjamin <davidben at mit.edu>
---
 lib/database-private.h |    1 +
 lib/database.cc        |   28 ++++++++++++++--------------
 lib/notmuch.h          |   32 ++++++++++++++++++++------------
 notmuch-client.h       |    4 ++++
 notmuch-config.c       |   29 +++++++++++++++++++++++++++++
 notmuch-count.c        |    4 ++--
 notmuch-dump.c         |    4 ++--
 notmuch-new.c          |    3 ++-
 notmuch-reply.c        |    4 ++--
 notmuch-restore.c      |    4 ++--
 notmuch-search-tags.c  |    4 ++--
 notmuch-search.c       |    4 ++--
 notmuch-show.c         |    4 ++--
 notmuch-tag.c          |    4 ++--
 14 files changed, 86 insertions(+), 43 deletions(-)

diff --git a/lib/database-private.h b/lib/database-private.h
index 41918d7..59ed117 100644
--- a/lib/database-private.h
+++ b/lib/database-private.h
@@ -38,6 +38,7 @@ struct _notmuch_database {
     notmuch_bool_t exception_reported;
 
     char *path;
+    char *notmuch_path;
 
     notmuch_bool_t needs_upgrade;
     notmuch_database_mode_t mode;
diff --git a/lib/database.cc b/lib/database.cc
index 1bb24ec..f54fbd1 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -437,14 +437,14 @@ parse_references (void *ctx,
 }
 
 notmuch_database_t *
-notmuch_database_create (const char *path)
+notmuch_database_create (const char *path,
+			 const char *notmuch_path)
 {
     notmuch_database_t *notmuch = NULL;
-    char *notmuch_path = NULL;
     struct stat st;
     int err;
 
-    if (path == NULL) {
+    if (path == NULL || notmuch_path == NULL) {
 	fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
 	goto DONE;
     }
@@ -462,8 +462,6 @@ notmuch_database_create (const char *path)
 	goto DONE;
     }
 
-    notmuch_path = talloc_asprintf (NULL, "%s/%s", path, ".notmuch");
-
     err = mkdir (notmuch_path, 0755);
 
     if (err) {
@@ -473,13 +471,11 @@ notmuch_database_create (const char *path)
     }
 
     notmuch = notmuch_database_open (path,
+				     notmuch_path,
 				     NOTMUCH_DATABASE_MODE_READ_WRITE);
     notmuch_database_upgrade (notmuch, NULL, NULL);
 
   DONE:
-    if (notmuch_path)
-	talloc_free (notmuch_path);
-
     return notmuch;
 }
 
@@ -496,17 +492,17 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)
 
 notmuch_database_t *
 notmuch_database_open (const char *path,
+		       const char *notmuch_path,
 		       notmuch_database_mode_t mode)
 {
     notmuch_database_t *notmuch = NULL;
-    char *notmuch_path = NULL, *xapian_path = NULL;
+    char *xapian_path = NULL;
     struct stat st;
     int err;
     unsigned int i, version;
 
-    if (asprintf (&notmuch_path, "%s/%s", path, ".notmuch") == -1) {
-	notmuch_path = NULL;
-	fprintf (stderr, "Out of memory\n");
+    if (path == NULL || notmuch_path == NULL) {
+	fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
 	goto DONE;
     }
 
@@ -611,8 +607,6 @@ notmuch_database_open (const char *path,
     }
 
   DONE:
-    if (notmuch_path)
-	free (notmuch_path);
     if (xapian_path)
 	free (xapian_path);
 
@@ -645,6 +639,12 @@ notmuch_database_get_path (notmuch_database_t *notmuch)
     return notmuch->path;
 }
 
+const char *
+notmuch_database_get_notmuch_path (notmuch_database_t *notmuch)
+{
+    return notmuch->notmuch_path;
+}
+
 unsigned int
 notmuch_database_get_version (notmuch_database_t *notmuch)
 {
diff --git a/lib/notmuch.h b/lib/notmuch.h
index d3e50a7..943c297 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -120,12 +120,12 @@ typedef struct _notmuch_tags notmuch_tags_t;
 typedef struct _notmuch_directory notmuch_directory_t;
 typedef struct _notmuch_filenames notmuch_filenames_t;
 
-/* Create a new, empty notmuch database located at 'path'.
+/* Create a new, empty notmuch database located at 'notmuch_path',
+ * indexing mail store 'path'
  *
  * The path should be a top-level directory to a collection of
  * plain-text email messages (one message per file). This call will
- * create a new ".notmuch" directory within 'path' where notmuch will
- * store its data.
+ * create directory 'notmuch_path' where notmuch will store its data.
  *
  * After a successful call to notmuch_database_create, the returned
  * database will be open so the caller should call
@@ -140,7 +140,8 @@ typedef struct _notmuch_filenames notmuch_filenames_t;
  * an error message on stderr).
  */
 notmuch_database_t *
-notmuch_database_create (const char *path);
+notmuch_database_create (const char *path,
+			 const char *notmuch_path);
 
 typedef enum {
     NOTMUCH_DATABASE_MODE_READ_ONLY = 0,
@@ -150,16 +151,15 @@ typedef enum {
 /* XXX: I think I'd like this to take an extra argument of
  * notmuch_status_t* for returning a status value on failure. */
 
-/* Open an existing notmuch database located at 'path'.
+/* Open an existing notmuch database located at 'notmuch_path' with
+ * mail store rooted at 'path'
  *
  * The database should have been created at some time in the past,
  * (not necessarily by this process), by calling
- * notmuch_database_create with 'path'. By default the database should be
- * opened for reading only. In order to write to the database you need to
- * pass the NOTMUCH_DATABASE_MODE_WRITABLE mode.
- *
- * An existing notmuch database can be identified by the presence of a
- * directory named ".notmuch" below 'path'.
+ * notmuch_database_create with 'path' and 'notmuch_path'. By default
+ * the database should be opened for reading only. In order to write
+ * to the database you need to pass the NOTMUCH_DATABASE_MODE_WRITABLE
+ * mode.
  *
  * The caller should call notmuch_database_close when finished with
  * this database.
@@ -169,6 +169,7 @@ typedef enum {
  */
 notmuch_database_t *
 notmuch_database_open (const char *path,
+		       const char *notmuch_path,
 		       notmuch_database_mode_t mode);
 
 /* Close the given notmuch database, freeing all associated
@@ -176,13 +177,20 @@ notmuch_database_open (const char *path,
 void
 notmuch_database_close (notmuch_database_t *database);
 
-/* Return the database path of the given database.
+/* Return the mail store path of the given database.
  *
  * The return value is a string owned by notmuch so should not be
  * modified nor freed by the caller. */
 const char *
 notmuch_database_get_path (notmuch_database_t *database);
 
+/* Return the database path of the given database.
+ *
+ * The return value is a string owned by notmuch so should not be
+ * modified nor freed by the caller. */
+const char *
+notmuch_database_get_notmuch_path (notmuch_database_t *database);
+
 /* Return the database format version of the given database. */
 unsigned int
 notmuch_database_get_version (notmuch_database_t *database);
diff --git a/notmuch-client.h b/notmuch-client.h
index c80b39c..1a676d2 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -148,6 +148,10 @@ void
 notmuch_config_set_database_path (notmuch_config_t *config,
 				  const char *database_path);
 
+notmuch_database_t *
+notmuch_config_open_database (notmuch_config_t *config,
+			      notmuch_database_mode_t mode);
+
 const char *
 notmuch_config_get_user_name (notmuch_config_t *config);
 
diff --git a/notmuch-config.c b/notmuch-config.c
index 95430db..58a28b1 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -353,6 +353,35 @@ notmuch_config_set_database_path (notmuch_config_t *config,
     config->database_path = NULL;
 }
 
+notmuch_database_t *
+notmuch_config_open_database (notmuch_config_t *config,
+			      notmuch_database_mode_t mode)
+{
+    const char *path = NULL;
+    char *db_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);
+
+DONE:
+    if (db_path)
+	free(db_path);
+
+    return notmuch;
+}
+
 const char *
 notmuch_config_get_user_name (notmuch_config_t *config)
 {
diff --git a/notmuch-count.c b/notmuch-count.c
index 77aa433..8d077eb 100644
--- a/notmuch-count.c
+++ b/notmuch-count.c
@@ -80,8 +80,8 @@ notmuch_count_command (void *ctx, int argc, char *argv[])
     if (config == NULL)
 	return 1;
 
-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
-				     NOTMUCH_DATABASE_MODE_READ_ONLY);
+    notmuch = notmuch_config_open_database (config,
+					    NOTMUCH_DATABASE_MODE_READ_ONLY);
     if (notmuch == NULL)
 	return 1;
 
diff --git a/notmuch-dump.c b/notmuch-dump.c
index ea326bb..a051fd4 100644
--- a/notmuch-dump.c
+++ b/notmuch-dump.c
@@ -35,8 +35,8 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
     if (config == NULL)
 	return 1;
 
-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
-				     NOTMUCH_DATABASE_MODE_READ_ONLY);
+    notmuch = notmuch_config_open_database (config,
+					    NOTMUCH_DATABASE_MODE_READ_ONLY);
     if (notmuch == NULL)
 	return 1;
 
diff --git a/notmuch-new.c b/notmuch-new.c
index f25c71f..d24dab9 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -749,10 +749,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);
+	notmuch = notmuch_database_create (db_path, dot_notmuch_path);
 	add_files_state.total_files = count;
     } else {
 	notmuch = notmuch_database_open (db_path,
+					 dot_notmuch_path,
 					 NOTMUCH_DATABASE_MODE_READ_WRITE);
 	if (notmuch == NULL)
 	    return 1;
diff --git a/notmuch-reply.c b/notmuch-reply.c
index 98f6442..10734bc 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -467,8 +467,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
 	return 1;
     }
 
-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
-				     NOTMUCH_DATABASE_MODE_READ_ONLY);
+    notmuch = notmuch_config_open_database (config,
+					    NOTMUCH_DATABASE_MODE_READ_ONLY);
     if (notmuch == NULL)
 	return 1;
 
diff --git a/notmuch-restore.c b/notmuch-restore.c
index 53ce254..d495b4f 100644
--- a/notmuch-restore.c
+++ b/notmuch-restore.c
@@ -36,8 +36,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
     if (config == NULL)
 	return 1;
 
-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
-				     NOTMUCH_DATABASE_MODE_READ_WRITE);
+    notmuch = notmuch_config_open_database (config,
+					    NOTMUCH_DATABASE_MODE_READ_WRITE);
     if (notmuch == NULL)
 	return 1;
 
diff --git a/notmuch-search-tags.c b/notmuch-search-tags.c
index 7a1305e..1ddbb94 100644
--- a/notmuch-search-tags.c
+++ b/notmuch-search-tags.c
@@ -51,8 +51,8 @@ notmuch_search_tags_command (void *ctx, int argc, char *argv[])
 	goto error;
     }
 
-    db = notmuch_database_open (notmuch_config_get_database_path (config),
-				NOTMUCH_DATABASE_MODE_READ_ONLY);
+    db = notmuch_config_open_database (config,
+				       NOTMUCH_DATABASE_MODE_READ_ONLY);
     if (db == NULL) {
 	goto error;
     }
diff --git a/notmuch-search.c b/notmuch-search.c
index 25dd6eb..2b65e87 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -244,8 +244,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
     if (config == NULL)
 	return 1;
 
-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
-				     NOTMUCH_DATABASE_MODE_READ_ONLY);
+    notmuch = notmuch_config_open_database (config,
+					    NOTMUCH_DATABASE_MODE_READ_ONLY);
     if (notmuch == NULL)
 	return 1;
 
diff --git a/notmuch-show.c b/notmuch-show.c
index 1a1d601..88124dc 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -446,8 +446,8 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
 	return 1;
     }
 
-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
-				     NOTMUCH_DATABASE_MODE_READ_ONLY);
+    notmuch = notmuch_config_open_database (config,
+					    NOTMUCH_DATABASE_MODE_READ_ONLY);
     if (notmuch == NULL)
 	return 1;
 
diff --git a/notmuch-tag.c b/notmuch-tag.c
index 00588a1..1deff1a 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -96,8 +96,8 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
     if (config == NULL)
 	return 1;
 
-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
-				     NOTMUCH_DATABASE_MODE_READ_WRITE);
+    notmuch = notmuch_config_open_database (config,
+					    NOTMUCH_DATABASE_MODE_READ_WRITE);
     if (notmuch == NULL)
 	return 1;
 
-- 
1.7.0.18.g39b3



More information about the notmuch mailing list