[notmuch] [PATCH] Permit opening the notmuch database in read-only mode.
authorChris Wilson <chris@chris-wilson.co.uk>
Sat, 21 Nov 2009 20:08:31 +0000 (20:08 +0000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:35:38 +0000 (09:35 -0800)
a6/e380d8f05c72dce9d9a273b8d498efb8b14b09 [new file with mode: 0644]

diff --git a/a6/e380d8f05c72dce9d9a273b8d498efb8b14b09 b/a6/e380d8f05c72dce9d9a273b8d498efb8b14b09
new file mode 100644 (file)
index 0000000..0841c6a
--- /dev/null
@@ -0,0 +1,381 @@
+Return-Path: <chris@chris-wilson.co.uk>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 21584431FBF\r
+       for <notmuch@notmuchmail.org>; Sat, 21 Nov 2009 12:08:37 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id VQzzlMdMW8IG for <notmuch@notmuchmail.org>;\r
+       Sat, 21 Nov 2009 12:08:36 -0800 (PST)\r
+Received: from orsmga101.jf.intel.com (mga06.intel.com [134.134.136.21])\r
+       by olra.theworths.org (Postfix) with ESMTP id A64A0431FAE\r
+       for <notmuch@notmuchmail.org>; Sat, 21 Nov 2009 12:08:35 -0800 (PST)\r
+Received: from orsmga002.jf.intel.com ([10.7.209.21])\r
+       by orsmga101.jf.intel.com with ESMTP; 21 Nov 2009 12:08:27 -0800\r
+X-ExtLoop1: 1\r
+X-IronPort-AV: E=Sophos;i="4.47,264,1257148800"; d="scan'208";a="469402951"\r
+Received: from unknown (HELO localhost.localdomain) ([10.255.16.192])\r
+       by orsmga002.jf.intel.com with ESMTP; 21 Nov 2009 12:25:37 -0800\r
+From: Chris Wilson <chris@chris-wilson.co.uk>\r
+To: notmuch@notmuchmail.org\r
+Date: Sat, 21 Nov 2009 20:08:31 +0000\r
+Message-Id: <1258834111-18285-1-git-send-email-chris@chris-wilson.co.uk>\r
+X-Mailer: git-send-email 1.6.5.3\r
+Subject: [notmuch] [PATCH] Permit opening the notmuch database in read-only\r
+       mode.\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.12\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 21 Nov 2009 20:08:37 -0000\r
+\r
+We only rarely need to actually open the database for writing, but we\r
+always create a Xapian::WritableDatabase. This has the effect of\r
+preventing searches and like whilst updating the index.\r
+\r
+Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>\r
+Acked-by: Carl Worth <cworth@cworth.org>\r
+[ickle: Updated to add WRITABLE mode to notmuch-new]\r
+---\r
+ lib/database-private.h |    3 ++-\r
+ lib/database.cc        |   31 ++++++++++++++++++++++++-------\r
+ lib/message.cc         |   15 +++++++++++++--\r
+ lib/notmuch-private.h  |    1 +\r
+ lib/notmuch.h          |   13 +++++++++++--\r
+ notmuch-dump.c         |    3 ++-\r
+ notmuch-new.c          |    4 +++-\r
+ notmuch-reply.c        |    3 ++-\r
+ notmuch-restore.c      |    3 ++-\r
+ notmuch-search.c       |    3 ++-\r
+ notmuch-show.c         |    3 ++-\r
+ notmuch-tag.c          |    3 ++-\r
+ 12 files changed, 66 insertions(+), 19 deletions(-)\r
+\r
+diff --git a/lib/database-private.h b/lib/database-private.h\r
+index 76e26ce..79c7916 100644\r
+--- a/lib/database-private.h\r
++++ b/lib/database-private.h\r
+@@ -27,7 +27,8 @@\r
\r
+ struct _notmuch_database {\r
+     char *path;\r
+-    Xapian::WritableDatabase *xapian_db;\r
++    notmuch_database_mode_t mode;\r
++    Xapian::Database *xapian_db;\r
+     Xapian::QueryParser *query_parser;\r
+     Xapian::TermGenerator *term_gen;\r
+ };\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 207246c..fb38664 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -172,6 +172,8 @@ notmuch_status_to_string (notmuch_status_t status)\r
+       return "No error occurred";\r
+     case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
+       return "Out of memory";\r
++    case NOTMUCH_STATUS_READONLY_DATABASE:\r
++      return "The database is read-only";\r
+     case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
+       return "A Xapian exception occurred";\r
+     case NOTMUCH_STATUS_FILE_ERROR:\r
+@@ -438,7 +440,8 @@ notmuch_database_create (const char *path)\r
+       goto DONE;\r
+     }\r
\r
+-    notmuch = notmuch_database_open (path);\r
++    notmuch = notmuch_database_open (path,\r
++                                   NOTMUCH_DATABASE_MODE_WRITABLE);\r
\r
+   DONE:\r
+     if (notmuch_path)\r
+@@ -448,7 +451,8 @@ notmuch_database_create (const char *path)\r
+ }\r
\r
+ notmuch_database_t *\r
+-notmuch_database_open (const char *path)\r
++notmuch_database_open (const char *path,\r
++                     notmuch_database_mode_t mode)\r
+ {\r
+     notmuch_database_t *notmuch = NULL;\r
+     char *notmuch_path = NULL, *xapian_path = NULL;\r
+@@ -481,9 +485,14 @@ notmuch_database_open (const char *path)\r
+     if (notmuch->path[strlen (notmuch->path) - 1] == '/')\r
+       notmuch->path[strlen (notmuch->path) - 1] = '\0';\r
\r
++    notmuch->mode = mode;\r
+     try {\r
+-      notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,\r
+-                                                         Xapian::DB_CREATE_OR_OPEN);\r
++      if (mode == NOTMUCH_DATABASE_MODE_WRITABLE) {\r
++          notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,\r
++                                                             Xapian::DB_CREATE_OR_OPEN);\r
++      } else {\r
++          notmuch->xapian_db = new Xapian::Database (xapian_path);\r
++      }\r
+       notmuch->query_parser = new Xapian::QueryParser;\r
+       notmuch->term_gen = new Xapian::TermGenerator;\r
+       notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));\r
+@@ -521,7 +530,8 @@ notmuch_database_open (const char *path)\r
+ void\r
+ notmuch_database_close (notmuch_database_t *notmuch)\r
+ {\r
+-    notmuch->xapian_db->flush ();\r
++    if (notmuch->mode == NOTMUCH_DATABASE_MODE_WRITABLE)\r
++      (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();\r
\r
+     delete notmuch->term_gen;\r
+     delete notmuch->query_parser;\r
+@@ -567,11 +577,18 @@ notmuch_database_set_timestamp (notmuch_database_t *notmuch,\r
+                               const char *key, time_t timestamp)\r
+ {\r
+     Xapian::Document doc;\r
++    Xapian::WritableDatabase *db;\r
+     unsigned int doc_id;\r
+     notmuch_private_status_t status;\r
+     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;\r
+     char *db_key = NULL;\r
\r
++    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY) {\r
++      fprintf (stderr, "Attempted to update a read-only database.\n");\r
++      return NOTMUCH_STATUS_READONLY_DATABASE;\r
++    }\r
++\r
++    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
+     db_key = timestamp_db_key (key);\r
\r
+     try {\r
+@@ -586,9 +603,9 @@ notmuch_database_set_timestamp (notmuch_database_t *notmuch,\r
+           doc.add_term (term);\r
+           talloc_free (term);\r
\r
+-          notmuch->xapian_db->add_document (doc);\r
++          db->add_document (doc);\r
+       } else {\r
+-          notmuch->xapian_db->replace_document (doc_id, doc);\r
++          db->replace_document (doc_id, doc);\r
+       }\r
\r
+     } catch (Xapian::Error &error) {\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index e0b8a8e..7ba06c9 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -168,9 +168,15 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,\r
+ {\r
+     notmuch_message_t *message;\r
+     Xapian::Document doc;\r
++    Xapian::WritableDatabase *db;\r
+     unsigned int doc_id;\r
+     char *term;\r
\r
++    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY) {\r
++      *status_ret = NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE;\r
++      return NULL;\r
++    }\r
++\r
+     *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
\r
+     message = notmuch_database_find_message (notmuch, message_id);\r
+@@ -184,13 +190,14 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,\r
+       return NULL;\r
+     }\r
\r
++    db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);\r
+     try {\r
+       doc.add_term (term);\r
+       talloc_free (term);\r
\r
+       doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);\r
\r
+-      doc_id = notmuch->xapian_db->add_document (doc);\r
++      doc_id = db->add_document (doc);\r
+     } catch (const Xapian::Error &error) {\r
+       *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;\r
+       return NULL;\r
+@@ -543,8 +550,12 @@ _notmuch_message_ensure_thread_id (notmuch_message_t *message)\r
+ void\r
+ _notmuch_message_sync (notmuch_message_t *message)\r
+ {\r
+-    Xapian::WritableDatabase *db = message->notmuch->xapian_db;\r
++    Xapian::WritableDatabase *db;\r
++\r
++    if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY)\r
++      return;\r
\r
++    db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);\r
+     db->replace_document (message->doc_id, message->doc);\r
+ }\r
\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index 1583498..2b4bf31 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -110,6 +110,7 @@ typedef enum _notmuch_private_status {\r
+     /* First, copy all the public status values. */\r
+     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,\r
+     NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,\r
++    NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE = NOTMUCH_STATUS_READONLY_DATABASE,\r
+     NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,\r
+     NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,\r
+     NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index cc713a3..89ed7ad 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -86,6 +86,7 @@ typedef int notmuch_bool_t;\r
+ typedef enum _notmuch_status {\r
+     NOTMUCH_STATUS_SUCCESS = 0,\r
+     NOTMUCH_STATUS_OUT_OF_MEMORY,\r
++    NOTMUCH_STATUS_READONLY_DATABASE,\r
+     NOTMUCH_STATUS_XAPIAN_EXCEPTION,\r
+     NOTMUCH_STATUS_FILE_ERROR,\r
+     NOTMUCH_STATUS_FILE_NOT_EMAIL,\r
+@@ -139,11 +140,18 @@ notmuch_database_create (const char *path);\r
+ /* XXX: I think I'd like this to take an extra argument of\r
+  * notmuch_status_t* for returning a status value on failure. */\r
\r
++typedef enum {\r
++    NOTMUCH_DATABASE_MODE_READONLY = 0,\r
++    NOTMUCH_DATABASE_MODE_WRITABLE\r
++} notmuch_database_mode_t;\r
++\r
+ /* Open an existing notmuch database located at 'path'.\r
+  *\r
+  * The database should have been created at some time in the past,\r
+  * (not necessarily by this process), by calling\r
+- * notmuch_database_create with 'path'.\r
++ * notmuch_database_create with 'path'. By default the database should be\r
++ * opened for reading only. In order to write to the database you need to\r
++ * pass the NOTMUCH_DATABASE_MODE_WRITABLE mode.\r
+  *\r
+  * An existing notmuch database can be identified by the presence of a\r
+  * directory named ".notmuch" below 'path'.\r
+@@ -155,7 +163,8 @@ notmuch_database_create (const char *path);\r
+  * an error message on stderr).\r
+  */\r
+ notmuch_database_t *\r
+-notmuch_database_open (const char *path);\r
++notmuch_database_open (const char *path,\r
++                     notmuch_database_mode_t mode);\r
\r
+ /* Close the given notmuch database, freeing all associated\r
+  * resources. See notmuch_database_open. */\r
+diff --git a/notmuch-dump.c b/notmuch-dump.c\r
+index 4c6e321..86570d0 100644\r
+--- a/notmuch-dump.c\r
++++ b/notmuch-dump.c\r
+@@ -35,7 +35,8 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])\r
+     if (config == NULL)\r
+       return 1;\r
\r
+-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
++    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
++                                   NOTMUCH_DATABASE_MODE_READONLY);\r
+     if (notmuch == NULL)\r
+       return 1;\r
\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 6264628..dded885 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -195,6 +195,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+                                next);\r
+                       break;\r
+                   /* Fatal issues. Don't process anymore. */\r
++                  case NOTMUCH_STATUS_READONLY_DATABASE:\r
+                   case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
+                   case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
+                       fprintf (stderr, "Error: %s. Halting processing.\n",\r
+@@ -420,7 +421,8 @@ notmuch_new_command (void *ctx,\r
+       add_files_state.ignore_read_only_directories = FALSE;\r
+       add_files_state.total_files = count;\r
+     } else {\r
+-      notmuch = notmuch_database_open (db_path);\r
++      notmuch = notmuch_database_open (db_path,\r
++                                       NOTMUCH_DATABASE_MODE_WRITABLE);\r
+       add_files_state.ignore_read_only_directories = TRUE;\r
+       add_files_state.total_files = 0;\r
+     }\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index c6122aa..291cd02 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -223,7 +223,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
+       return 1;\r
+     }\r
\r
+-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
++    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
++                                   NOTMUCH_DATABASE_MODE_READONLY);\r
+     if (notmuch == NULL)\r
+       return 1;\r
\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index b8f99a3..001f3fc 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -36,7 +36,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+     if (config == NULL)\r
+       return 1;\r
\r
+-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
++    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
++                                   NOTMUCH_DATABASE_MODE_WRITABLE);\r
+     if (notmuch == NULL)\r
+       return 1;\r
\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 2b1c0fe..cde9735 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -129,7 +129,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+     if (config == NULL)\r
+       return 1;\r
\r
+-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
++    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
++                                   NOTMUCH_DATABASE_MODE_READONLY);\r
+     if (notmuch == NULL)\r
+       return 1;\r
\r
+diff --git a/notmuch-show.c b/notmuch-show.c\r
+index a21d223..a764e44 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -220,7 +220,8 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+       return 1;\r
+     }\r
\r
+-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
++    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
++                                   NOTMUCH_DATABASE_MODE_READONLY);\r
+     if (notmuch == NULL)\r
+       return 1;\r
\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 12ab86c..75d464f 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -94,7 +94,8 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+     if (config == NULL)\r
+       return 1;\r
\r
+-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
++    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
++                                   NOTMUCH_DATABASE_MODE_WRITABLE);\r
+     if (notmuch == NULL)\r
+       return 1;\r
\r
+-- \r
+1.6.5.3\r
+\r