1 Return-Path: <chris@chris-wilson.co.uk>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id F3327431FC0
\r
6 for <notmuch@notmuchmail.org>; Sat, 21 Nov 2009 11:54:31 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
8 Received: from olra.theworths.org ([127.0.0.1])
\r
9 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
10 with ESMTP id JTnaGtNnLRuL for <notmuch@notmuchmail.org>;
\r
11 Sat, 21 Nov 2009 11:54:30 -0800 (PST)
\r
12 Received: from fmsmga101.fm.intel.com (mga05.intel.com [192.55.52.89])
\r
13 by olra.theworths.org (Postfix) with ESMTP id 44ECA431FBF
\r
14 for <notmuch@notmuchmail.org>; Sat, 21 Nov 2009 11:54:30 -0800 (PST)
\r
15 Received: from fmsmga002.fm.intel.com ([10.253.24.26])
\r
16 by fmsmga101.fm.intel.com with ESMTP; 21 Nov 2009 11:49:18 -0800
\r
18 X-IronPort-AV: E=Sophos;i="4.47,264,1257148800"; d="scan'208";a="516162843"
\r
19 Received: from unknown (HELO localhost.localdomain) ([10.255.16.192])
\r
20 by fmsmga002.fm.intel.com with ESMTP; 21 Nov 2009 11:48:14 -0800
\r
21 From: Chris Wilson <chris@chris-wilson.co.uk>
\r
22 To: notmuch@notmuchmail.org
\r
23 Date: Sat, 21 Nov 2009 19:54:25 +0000
\r
24 Message-Id: <1258833265-17012-1-git-send-email-chris@chris-wilson.co.uk>
\r
25 X-Mailer: git-send-email 1.6.5.3
\r
26 Subject: [notmuch] [PATCH] Permit opening the notmuch database in read-only
\r
28 X-BeenThere: notmuch@notmuchmail.org
\r
29 X-Mailman-Version: 2.1.12
\r
31 List-Id: "Use and development of the notmuch mail system."
\r
32 <notmuch.notmuchmail.org>
\r
33 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
34 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
35 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
36 List-Post: <mailto:notmuch@notmuchmail.org>
\r
37 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
38 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
39 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
40 X-List-Received-Date: Sat, 21 Nov 2009 19:54:32 -0000
\r
42 We only rarely need to actually open the database for writing, but we
\r
43 always create a Xapian::WritableDatabase. This has the effect of
\r
44 preventing searches and like whilst updating the index.
\r
46 Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
\r
47 Acked-by: Carl Worth <cworth@cworth.org>
\r
49 lib/database-private.h | 3 ++-
\r
50 lib/database.cc | 31 ++++++++++++++++++++++++-------
\r
51 lib/message.cc | 15 +++++++++++++--
\r
52 lib/notmuch-private.h | 1 +
\r
53 lib/notmuch.h | 13 +++++++++++--
\r
54 notmuch-dump.c | 3 ++-
\r
55 notmuch-new.c | 4 +++-
\r
56 notmuch-reply.c | 3 ++-
\r
57 notmuch-restore.c | 3 ++-
\r
58 notmuch-search.c | 3 ++-
\r
59 notmuch-show.c | 3 ++-
\r
60 notmuch-tag.c | 3 ++-
\r
61 12 files changed, 66 insertions(+), 19 deletions(-)
\r
63 diff --git a/lib/database-private.h b/lib/database-private.h
\r
64 index 76e26ce..79c7916 100644
\r
65 --- a/lib/database-private.h
\r
66 +++ b/lib/database-private.h
\r
69 struct _notmuch_database {
\r
71 - Xapian::WritableDatabase *xapian_db;
\r
72 + notmuch_database_mode_t mode;
\r
73 + Xapian::Database *xapian_db;
\r
74 Xapian::QueryParser *query_parser;
\r
75 Xapian::TermGenerator *term_gen;
\r
77 diff --git a/lib/database.cc b/lib/database.cc
\r
78 index 207246c..fb38664 100644
\r
79 --- a/lib/database.cc
\r
80 +++ b/lib/database.cc
\r
81 @@ -172,6 +172,8 @@ notmuch_status_to_string (notmuch_status_t status)
\r
82 return "No error occurred";
\r
83 case NOTMUCH_STATUS_OUT_OF_MEMORY:
\r
84 return "Out of memory";
\r
85 + case NOTMUCH_STATUS_READONLY_DATABASE:
\r
86 + return "The database is read-only";
\r
87 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
\r
88 return "A Xapian exception occurred";
\r
89 case NOTMUCH_STATUS_FILE_ERROR:
\r
90 @@ -438,7 +440,8 @@ notmuch_database_create (const char *path)
\r
94 - notmuch = notmuch_database_open (path);
\r
95 + notmuch = notmuch_database_open (path,
\r
96 + NOTMUCH_DATABASE_MODE_WRITABLE);
\r
100 @@ -448,7 +451,8 @@ notmuch_database_create (const char *path)
\r
103 notmuch_database_t *
\r
104 -notmuch_database_open (const char *path)
\r
105 +notmuch_database_open (const char *path,
\r
106 + notmuch_database_mode_t mode)
\r
108 notmuch_database_t *notmuch = NULL;
\r
109 char *notmuch_path = NULL, *xapian_path = NULL;
\r
110 @@ -481,9 +485,14 @@ notmuch_database_open (const char *path)
\r
111 if (notmuch->path[strlen (notmuch->path) - 1] == '/')
\r
112 notmuch->path[strlen (notmuch->path) - 1] = '\0';
\r
114 + notmuch->mode = mode;
\r
116 - notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
\r
117 - Xapian::DB_CREATE_OR_OPEN);
\r
118 + if (mode == NOTMUCH_DATABASE_MODE_WRITABLE) {
\r
119 + notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
\r
120 + Xapian::DB_CREATE_OR_OPEN);
\r
122 + notmuch->xapian_db = new Xapian::Database (xapian_path);
\r
124 notmuch->query_parser = new Xapian::QueryParser;
\r
125 notmuch->term_gen = new Xapian::TermGenerator;
\r
126 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
\r
127 @@ -521,7 +530,8 @@ notmuch_database_open (const char *path)
\r
129 notmuch_database_close (notmuch_database_t *notmuch)
\r
131 - notmuch->xapian_db->flush ();
\r
132 + if (notmuch->mode == NOTMUCH_DATABASE_MODE_WRITABLE)
\r
133 + (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();
\r
135 delete notmuch->term_gen;
\r
136 delete notmuch->query_parser;
\r
137 @@ -567,11 +577,18 @@ notmuch_database_set_timestamp (notmuch_database_t *notmuch,
\r
138 const char *key, time_t timestamp)
\r
140 Xapian::Document doc;
\r
141 + Xapian::WritableDatabase *db;
\r
142 unsigned int doc_id;
\r
143 notmuch_private_status_t status;
\r
144 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
\r
145 char *db_key = NULL;
\r
147 + if (notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY) {
\r
148 + fprintf (stderr, "Attempted to update a read-only database.\n");
\r
149 + return NOTMUCH_STATUS_READONLY_DATABASE;
\r
152 + db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
\r
153 db_key = timestamp_db_key (key);
\r
156 @@ -586,9 +603,9 @@ notmuch_database_set_timestamp (notmuch_database_t *notmuch,
\r
157 doc.add_term (term);
\r
158 talloc_free (term);
\r
160 - notmuch->xapian_db->add_document (doc);
\r
161 + db->add_document (doc);
\r
163 - notmuch->xapian_db->replace_document (doc_id, doc);
\r
164 + db->replace_document (doc_id, doc);
\r
167 } catch (Xapian::Error &error) {
\r
168 diff --git a/lib/message.cc b/lib/message.cc
\r
169 index e0b8a8e..7ba06c9 100644
\r
170 --- a/lib/message.cc
\r
171 +++ b/lib/message.cc
\r
172 @@ -168,9 +168,15 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
\r
174 notmuch_message_t *message;
\r
175 Xapian::Document doc;
\r
176 + Xapian::WritableDatabase *db;
\r
177 unsigned int doc_id;
\r
180 + if (notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY) {
\r
181 + *status_ret = NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE;
\r
185 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
\r
187 message = notmuch_database_find_message (notmuch, message_id);
\r
188 @@ -184,13 +190,14 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
\r
192 + db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
\r
194 doc.add_term (term);
\r
195 talloc_free (term);
\r
197 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
\r
199 - doc_id = notmuch->xapian_db->add_document (doc);
\r
200 + doc_id = db->add_document (doc);
\r
201 } catch (const Xapian::Error &error) {
\r
202 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
\r
204 @@ -543,8 +550,12 @@ _notmuch_message_ensure_thread_id (notmuch_message_t *message)
\r
206 _notmuch_message_sync (notmuch_message_t *message)
\r
208 - Xapian::WritableDatabase *db = message->notmuch->xapian_db;
\r
209 + Xapian::WritableDatabase *db;
\r
211 + if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY)
\r
214 + db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
\r
215 db->replace_document (message->doc_id, message->doc);
\r
218 diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
\r
219 index 1583498..2b4bf31 100644
\r
220 --- a/lib/notmuch-private.h
\r
221 +++ b/lib/notmuch-private.h
\r
222 @@ -110,6 +110,7 @@ typedef enum _notmuch_private_status {
\r
223 /* First, copy all the public status values. */
\r
224 NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
\r
225 NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
\r
226 + NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE = NOTMUCH_STATUS_READONLY_DATABASE,
\r
227 NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
\r
228 NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
\r
229 NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
\r
230 diff --git a/lib/notmuch.h b/lib/notmuch.h
\r
231 index cc713a3..89ed7ad 100644
\r
232 --- a/lib/notmuch.h
\r
233 +++ b/lib/notmuch.h
\r
234 @@ -86,6 +86,7 @@ typedef int notmuch_bool_t;
\r
235 typedef enum _notmuch_status {
\r
236 NOTMUCH_STATUS_SUCCESS = 0,
\r
237 NOTMUCH_STATUS_OUT_OF_MEMORY,
\r
238 + NOTMUCH_STATUS_READONLY_DATABASE,
\r
239 NOTMUCH_STATUS_XAPIAN_EXCEPTION,
\r
240 NOTMUCH_STATUS_FILE_ERROR,
\r
241 NOTMUCH_STATUS_FILE_NOT_EMAIL,
\r
242 @@ -139,11 +140,18 @@ notmuch_database_create (const char *path);
\r
243 /* XXX: I think I'd like this to take an extra argument of
\r
244 * notmuch_status_t* for returning a status value on failure. */
\r
247 + NOTMUCH_DATABASE_MODE_READONLY = 0,
\r
248 + NOTMUCH_DATABASE_MODE_WRITABLE
\r
249 +} notmuch_database_mode_t;
\r
251 /* Open an existing notmuch database located at 'path'.
\r
253 * The database should have been created at some time in the past,
\r
254 * (not necessarily by this process), by calling
\r
255 - * notmuch_database_create with 'path'.
\r
256 + * notmuch_database_create with 'path'. By default the database should be
\r
257 + * opened for reading only. In order to write to the database you need to
\r
258 + * pass the NOTMUCH_DATABASE_MODE_WRITABLE mode.
\r
260 * An existing notmuch database can be identified by the presence of a
\r
261 * directory named ".notmuch" below 'path'.
\r
262 @@ -155,7 +163,8 @@ notmuch_database_create (const char *path);
\r
263 * an error message on stderr).
\r
265 notmuch_database_t *
\r
266 -notmuch_database_open (const char *path);
\r
267 +notmuch_database_open (const char *path,
\r
268 + notmuch_database_mode_t mode);
\r
270 /* Close the given notmuch database, freeing all associated
\r
271 * resources. See notmuch_database_open. */
\r
272 diff --git a/notmuch-dump.c b/notmuch-dump.c
\r
273 index 4c6e321..86570d0 100644
\r
274 --- a/notmuch-dump.c
\r
275 +++ b/notmuch-dump.c
\r
276 @@ -35,7 +35,8 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
\r
277 if (config == NULL)
\r
280 - notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
\r
281 + notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
\r
282 + NOTMUCH_DATABASE_MODE_READONLY);
\r
283 if (notmuch == NULL)
\r
286 diff --git a/notmuch-new.c b/notmuch-new.c
\r
287 index 6264628..91c9681 100644
\r
288 --- a/notmuch-new.c
\r
289 +++ b/notmuch-new.c
\r
290 @@ -195,6 +195,7 @@ add_files_recursive (notmuch_database_t *notmuch,
\r
293 /* Fatal issues. Don't process anymore. */
\r
294 + case NOTMUCH_STATUS_READONLY_DATABASE:
\r
295 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
\r
296 case NOTMUCH_STATUS_OUT_OF_MEMORY:
\r
297 fprintf (stderr, "Error: %s. Halting processing.\n",
\r
298 @@ -420,7 +421,8 @@ notmuch_new_command (void *ctx,
\r
299 add_files_state.ignore_read_only_directories = FALSE;
\r
300 add_files_state.total_files = count;
\r
302 - notmuch = notmuch_database_open (db_path);
\r
303 + notmuch = notmuch_database_open (db_path,
\r
304 + NOTMUCH_DATABASE_MODE_READONLY);
\r
305 add_files_state.ignore_read_only_directories = TRUE;
\r
306 add_files_state.total_files = 0;
\r
308 diff --git a/notmuch-reply.c b/notmuch-reply.c
\r
309 index c6122aa..291cd02 100644
\r
310 --- a/notmuch-reply.c
\r
311 +++ b/notmuch-reply.c
\r
312 @@ -223,7 +223,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
\r
316 - notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
\r
317 + notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
\r
318 + NOTMUCH_DATABASE_MODE_READONLY);
\r
319 if (notmuch == NULL)
\r
322 diff --git a/notmuch-restore.c b/notmuch-restore.c
\r
323 index b8f99a3..001f3fc 100644
\r
324 --- a/notmuch-restore.c
\r
325 +++ b/notmuch-restore.c
\r
326 @@ -36,7 +36,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
327 if (config == NULL)
\r
330 - notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
\r
331 + notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
\r
332 + NOTMUCH_DATABASE_MODE_WRITABLE);
\r
333 if (notmuch == NULL)
\r
336 diff --git a/notmuch-search.c b/notmuch-search.c
\r
337 index 2b1c0fe..cde9735 100644
\r
338 --- a/notmuch-search.c
\r
339 +++ b/notmuch-search.c
\r
340 @@ -129,7 +129,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
\r
341 if (config == NULL)
\r
344 - notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
\r
345 + notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
\r
346 + NOTMUCH_DATABASE_MODE_READONLY);
\r
347 if (notmuch == NULL)
\r
350 diff --git a/notmuch-show.c b/notmuch-show.c
\r
351 index a21d223..a764e44 100644
\r
352 --- a/notmuch-show.c
\r
353 +++ b/notmuch-show.c
\r
354 @@ -220,7 +220,8 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
\r
358 - notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
\r
359 + notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
\r
360 + NOTMUCH_DATABASE_MODE_READONLY);
\r
361 if (notmuch == NULL)
\r
364 diff --git a/notmuch-tag.c b/notmuch-tag.c
\r
365 index 12ab86c..75d464f 100644
\r
366 --- a/notmuch-tag.c
\r
367 +++ b/notmuch-tag.c
\r
368 @@ -94,7 +94,8 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
\r
369 if (config == NULL)
\r
372 - notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
\r
373 + notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
\r
374 + NOTMUCH_DATABASE_MODE_WRITABLE);
\r
375 if (notmuch == NULL)
\r