[notmuch] [PATCH] Permit opening the notmuch database in read-only mode.

Chris Wilson chris at chris-wilson.co.uk
Sat Nov 21 11:54:25 PST 2009


We only rarely need to actually open the database for writing, but we
always create a Xapian::WritableDatabase. This has the effect of
preventing searches and like whilst updating the index.

Signed-off-by: Chris Wilson <chris at chris-wilson.co.uk>
Acked-by: Carl Worth <cworth at cworth.org>
---
 lib/database-private.h |    3 ++-
 lib/database.cc        |   31 ++++++++++++++++++++++++-------
 lib/message.cc         |   15 +++++++++++++--
 lib/notmuch-private.h  |    1 +
 lib/notmuch.h          |   13 +++++++++++--
 notmuch-dump.c         |    3 ++-
 notmuch-new.c          |    4 +++-
 notmuch-reply.c        |    3 ++-
 notmuch-restore.c      |    3 ++-
 notmuch-search.c       |    3 ++-
 notmuch-show.c         |    3 ++-
 notmuch-tag.c          |    3 ++-
 12 files changed, 66 insertions(+), 19 deletions(-)

diff --git a/lib/database-private.h b/lib/database-private.h
index 76e26ce..79c7916 100644
--- a/lib/database-private.h
+++ b/lib/database-private.h
@@ -27,7 +27,8 @@
 
 struct _notmuch_database {
     char *path;
-    Xapian::WritableDatabase *xapian_db;
+    notmuch_database_mode_t mode;
+    Xapian::Database *xapian_db;
     Xapian::QueryParser *query_parser;
     Xapian::TermGenerator *term_gen;
 };
diff --git a/lib/database.cc b/lib/database.cc
index 207246c..fb38664 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -172,6 +172,8 @@ notmuch_status_to_string (notmuch_status_t status)
 	return "No error occurred";
     case NOTMUCH_STATUS_OUT_OF_MEMORY:
 	return "Out of memory";
+    case NOTMUCH_STATUS_READONLY_DATABASE:
+	return "The database is read-only";
     case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
 	return "A Xapian exception occurred";
     case NOTMUCH_STATUS_FILE_ERROR:
@@ -438,7 +440,8 @@ notmuch_database_create (const char *path)
 	goto DONE;
     }
 
-    notmuch = notmuch_database_open (path);
+    notmuch = notmuch_database_open (path,
+				     NOTMUCH_DATABASE_MODE_WRITABLE);
 
   DONE:
     if (notmuch_path)
@@ -448,7 +451,8 @@ notmuch_database_create (const char *path)
 }
 
 notmuch_database_t *
-notmuch_database_open (const char *path)
+notmuch_database_open (const char *path,
+		       notmuch_database_mode_t mode)
 {
     notmuch_database_t *notmuch = NULL;
     char *notmuch_path = NULL, *xapian_path = NULL;
@@ -481,9 +485,14 @@ notmuch_database_open (const char *path)
     if (notmuch->path[strlen (notmuch->path) - 1] == '/')
 	notmuch->path[strlen (notmuch->path) - 1] = '\0';
 
+    notmuch->mode = mode;
     try {
-	notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
-							   Xapian::DB_CREATE_OR_OPEN);
+	if (mode == NOTMUCH_DATABASE_MODE_WRITABLE) {
+	    notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
+							       Xapian::DB_CREATE_OR_OPEN);
+	} else {
+	    notmuch->xapian_db = new Xapian::Database (xapian_path);
+	}
 	notmuch->query_parser = new Xapian::QueryParser;
 	notmuch->term_gen = new Xapian::TermGenerator;
 	notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
@@ -521,7 +530,8 @@ notmuch_database_open (const char *path)
 void
 notmuch_database_close (notmuch_database_t *notmuch)
 {
-    notmuch->xapian_db->flush ();
+    if (notmuch->mode == NOTMUCH_DATABASE_MODE_WRITABLE)
+	(static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();
 
     delete notmuch->term_gen;
     delete notmuch->query_parser;
@@ -567,11 +577,18 @@ notmuch_database_set_timestamp (notmuch_database_t *notmuch,
 				const char *key, time_t timestamp)
 {
     Xapian::Document doc;
+    Xapian::WritableDatabase *db;
     unsigned int doc_id;
     notmuch_private_status_t status;
     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
     char *db_key = NULL;
 
+    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY) {
+	fprintf (stderr, "Attempted to update a read-only database.\n");
+	return NOTMUCH_STATUS_READONLY_DATABASE;
+    }
+
+    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
     db_key = timestamp_db_key (key);
 
     try {
@@ -586,9 +603,9 @@ notmuch_database_set_timestamp (notmuch_database_t *notmuch,
 	    doc.add_term (term);
 	    talloc_free (term);
 
-	    notmuch->xapian_db->add_document (doc);
+	    db->add_document (doc);
 	} else {
-	    notmuch->xapian_db->replace_document (doc_id, doc);
+	    db->replace_document (doc_id, doc);
 	}
 
     } catch (Xapian::Error &error) {
diff --git a/lib/message.cc b/lib/message.cc
index e0b8a8e..7ba06c9 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -168,9 +168,15 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
 {
     notmuch_message_t *message;
     Xapian::Document doc;
+    Xapian::WritableDatabase *db;
     unsigned int doc_id;
     char *term;
 
+    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY) {
+	*status_ret = NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE;
+	return NULL;
+    }
+
     *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
 
     message = notmuch_database_find_message (notmuch, message_id);
@@ -184,13 +190,14 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
 	return NULL;
     }
 
+    db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
     try {
 	doc.add_term (term);
 	talloc_free (term);
 
 	doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
 
-	doc_id = notmuch->xapian_db->add_document (doc);
+	doc_id = db->add_document (doc);
     } catch (const Xapian::Error &error) {
 	*status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
 	return NULL;
@@ -543,8 +550,12 @@ _notmuch_message_ensure_thread_id (notmuch_message_t *message)
 void
 _notmuch_message_sync (notmuch_message_t *message)
 {
-    Xapian::WritableDatabase *db = message->notmuch->xapian_db;
+    Xapian::WritableDatabase *db;
+
+    if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY)
+	return;
 
+    db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
     db->replace_document (message->doc_id, message->doc);
 }
 
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index 1583498..2b4bf31 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -110,6 +110,7 @@ typedef enum _notmuch_private_status {
     /* First, copy all the public status values. */
     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
     NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
+    NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE = NOTMUCH_STATUS_READONLY_DATABASE,
     NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
     NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
     NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
diff --git a/lib/notmuch.h b/lib/notmuch.h
index cc713a3..89ed7ad 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -86,6 +86,7 @@ typedef int notmuch_bool_t;
 typedef enum _notmuch_status {
     NOTMUCH_STATUS_SUCCESS = 0,
     NOTMUCH_STATUS_OUT_OF_MEMORY,
+    NOTMUCH_STATUS_READONLY_DATABASE,
     NOTMUCH_STATUS_XAPIAN_EXCEPTION,
     NOTMUCH_STATUS_FILE_ERROR,
     NOTMUCH_STATUS_FILE_NOT_EMAIL,
@@ -139,11 +140,18 @@ notmuch_database_create (const char *path);
 /* XXX: I think I'd like this to take an extra argument of
  * notmuch_status_t* for returning a status value on failure. */
 
+typedef enum {
+    NOTMUCH_DATABASE_MODE_READONLY = 0,
+    NOTMUCH_DATABASE_MODE_WRITABLE
+} notmuch_database_mode_t;
+
 /* Open an existing notmuch database located 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'.
+ * 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'.
@@ -155,7 +163,8 @@ notmuch_database_create (const char *path);
  * an error message on stderr).
  */
 notmuch_database_t *
-notmuch_database_open (const char *path);
+notmuch_database_open (const char *path,
+		       notmuch_database_mode_t mode);
 
 /* Close the given notmuch database, freeing all associated
  * resources. See notmuch_database_open. */
diff --git a/notmuch-dump.c b/notmuch-dump.c
index 4c6e321..86570d0 100644
--- a/notmuch-dump.c
+++ b/notmuch-dump.c
@@ -35,7 +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 = notmuch_database_open (notmuch_config_get_database_path (config),
+				     NOTMUCH_DATABASE_MODE_READONLY);
     if (notmuch == NULL)
 	return 1;
 
diff --git a/notmuch-new.c b/notmuch-new.c
index 6264628..91c9681 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -195,6 +195,7 @@ add_files_recursive (notmuch_database_t *notmuch,
 				 next);
 			break;
 		    /* Fatal issues. Don't process anymore. */
+		    case NOTMUCH_STATUS_READONLY_DATABASE:
 		    case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
 		    case NOTMUCH_STATUS_OUT_OF_MEMORY:
 			fprintf (stderr, "Error: %s. Halting processing.\n",
@@ -420,7 +421,8 @@ notmuch_new_command (void *ctx,
 	add_files_state.ignore_read_only_directories = FALSE;
 	add_files_state.total_files = count;
     } else {
-	notmuch = notmuch_database_open (db_path);
+	notmuch = notmuch_database_open (db_path,
+					 NOTMUCH_DATABASE_MODE_READONLY);
 	add_files_state.ignore_read_only_directories = TRUE;
 	add_files_state.total_files = 0;
     }
diff --git a/notmuch-reply.c b/notmuch-reply.c
index c6122aa..291cd02 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -223,7 +223,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
 	return 1;
     }
 
-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
+				     NOTMUCH_DATABASE_MODE_READONLY);
     if (notmuch == NULL)
 	return 1;
 
diff --git a/notmuch-restore.c b/notmuch-restore.c
index b8f99a3..001f3fc 100644
--- a/notmuch-restore.c
+++ b/notmuch-restore.c
@@ -36,7 +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 = notmuch_database_open (notmuch_config_get_database_path (config),
+				     NOTMUCH_DATABASE_MODE_WRITABLE);
     if (notmuch == NULL)
 	return 1;
 
diff --git a/notmuch-search.c b/notmuch-search.c
index 2b1c0fe..cde9735 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -129,7 +129,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 = notmuch_database_open (notmuch_config_get_database_path (config),
+				     NOTMUCH_DATABASE_MODE_READONLY);
     if (notmuch == NULL)
 	return 1;
 
diff --git a/notmuch-show.c b/notmuch-show.c
index a21d223..a764e44 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -220,7 +220,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 = notmuch_database_open (notmuch_config_get_database_path (config),
+				     NOTMUCH_DATABASE_MODE_READONLY);
     if (notmuch == NULL)
 	return 1;
 
diff --git a/notmuch-tag.c b/notmuch-tag.c
index 12ab86c..75d464f 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -94,7 +94,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 = notmuch_database_open (notmuch_config_get_database_path (config),
+				     NOTMUCH_DATABASE_MODE_WRITABLE);
     if (notmuch == NULL)
 	return 1;
 
-- 
1.6.5.3



More information about the notmuch mailing list