Re: folder: search-term problems
[notmuch-archives.git] / bc / 4bcc06282640f7e2de53c285c0bf1e70f06497
1 Return-Path: <jani@nikula.org>\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 45D0A429E37\r
6         for <notmuch@notmuchmail.org>; Sun,  1 Dec 2013 05:14:21 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id wILZwBwNO8cq for <notmuch@notmuchmail.org>;\r
17         Sun,  1 Dec 2013 05:14:16 -0800 (PST)\r
18 Received: from mail-ea0-f176.google.com (mail-ea0-f176.google.com\r
19         [209.85.215.176]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id B2E91431FDC\r
22         for <notmuch@notmuchmail.org>; Sun,  1 Dec 2013 05:14:14 -0800 (PST)\r
23 Received: by mail-ea0-f176.google.com with SMTP id h14so8145704eaj.35\r
24         for <notmuch@notmuchmail.org>; Sun, 01 Dec 2013 05:14:13 -0800 (PST)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=1e100.net; s=20130820;\r
27         h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
28         :references:in-reply-to:references;\r
29         bh=YAsMLe1afCIk4XzUWa68mP7QmhaTsh/rPVEI/q/voNE=;\r
30         b=HvdnWKAJvijwp9w0HMSnWPUOitHmMWITqOET/4Rq1Mp8CQUhz+2iOfm1w2yexwtSvI\r
31         n+RJoAGbWiV7fuZBQ4w60XUGOt8RPL6FDBTfqUiO2geEIaa1Eu0gZZtwHe2UxAZeRMwn\r
32         NkumMlfhje6nxjG7qFZ2sNNBoBBjceo/qZyekKAGwhgZUO8xG8LCLZx3pwQzofq+LGlp\r
33         IJXknT8/oq5x/2mAcTbXIvN9JtNplrm16maNCfrqRi8rMGeor4y6zO2QzSuLb2Ro/XGS\r
34         irLCivai/AX0Tn1dqYCb9Fztrh+erWHIOAGdIPG1PN1VRUTVGtXUYq9FbcR/4icnceMh\r
35         QVFw==\r
36 X-Gm-Message-State:\r
37  ALoCoQmzcCntAWbCg4RzEfyQY33iR0xvoUvhb6jp1jxG2hPeFRmBGYNEoOuM59QFRO3Em1EPgKxm\r
38 X-Received: by 10.15.86.75 with SMTP id h51mr75434eez.44.1385903653491;\r
39         Sun, 01 Dec 2013 05:14:13 -0800 (PST)\r
40 Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi.\r
41         [88.195.111.91])\r
42         by mx.google.com with ESMTPSA id m1sm54883447eeg.0.2013.12.01.05.14.11\r
43         for <multiple recipients>\r
44         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
45         Sun, 01 Dec 2013 05:14:12 -0800 (PST)\r
46 From: Jani Nikula <jani@nikula.org>\r
47 To: notmuch@notmuchmail.org\r
48 Subject: [PATCH 2/2] lib: introduce notmuch_database_new for initializing a\r
49         database handle\r
50 Date: Sun,  1 Dec 2013 15:14:00 +0200\r
51 Message-Id:\r
52  <fc7ecd990e55fcfba17de4d71e8823c98760f9ce.1385903109.git.jani@nikula.org>\r
53 X-Mailer: git-send-email 1.8.4.2\r
54 In-Reply-To: <cover.1385903109.git.jani@nikula.org>\r
55 References: <cover.1385903109.git.jani@nikula.org>\r
56 In-Reply-To: <cover.1385903109.git.jani@nikula.org>\r
57 References: <cover.1385903109.git.jani@nikula.org>\r
58 X-BeenThere: notmuch@notmuchmail.org\r
59 X-Mailman-Version: 2.1.13\r
60 Precedence: list\r
61 List-Id: "Use and development of the notmuch mail system."\r
62         <notmuch.notmuchmail.org>\r
63 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
64         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
65 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
66 List-Post: <mailto:notmuch@notmuchmail.org>\r
67 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
68 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
69         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
70 X-List-Received-Date: Sun, 01 Dec 2013 13:14:21 -0000\r
71 \r
72 There is a need for setting options before opening a database, such as\r
73 setting a logging function to use instead of writing to stdout or\r
74 stderr. It would be possible to do this by adding new parameters to\r
75 notmuch_database_create and notmuch_database_open, but maintaining a\r
76 backwards compatible API and ABI when new options are added becomes\r
77 burdensome.\r
78 \r
79 Instead, split the opaque database object creation from\r
80 notmuch_database_create and notmuch_database_open into a new\r
81 notmuch_database_new call, to allow operations on the handle before\r
82 create and open. This creates API and ABI breakage now, but allows\r
83 easier future extensions.\r
84 \r
85 The notmuch_database_new call becomes a natural pair to the already\r
86 existing notmuch_database_destroy, and it should be possible to call\r
87 open/close multiple times using an initialized handle.\r
88 ---\r
89  lib/database.cc      | 64 ++++++++++++++++++++++++++++------------------------\r
90  lib/notmuch.h        | 52 ++++++++++++++++++++++++++++++++----------\r
91  notmuch-compact.c    | 11 ++++++++-\r
92  notmuch-count.c      | 10 ++++++--\r
93  notmuch-dump.c       | 10 ++++++--\r
94  notmuch-insert.c     | 10 ++++++--\r
95  notmuch-new.c        | 14 +++++++-----\r
96  notmuch-reply.c      | 10 ++++++--\r
97  notmuch-restore.c    | 10 ++++++--\r
98  notmuch-search.c     | 10 ++++++--\r
99  notmuch-show.c       | 10 ++++++--\r
100  notmuch-tag.c        | 10 ++++++--\r
101  test/random-corpus.c | 10 ++++++--\r
102  test/symbol-test.cc  |  3 ++-\r
103  14 files changed, 166 insertions(+), 68 deletions(-)\r
104 \r
105 diff --git a/lib/database.cc b/lib/database.cc\r
106 index 98e2c31..386b93a 100644\r
107 --- a/lib/database.cc\r
108 +++ b/lib/database.cc\r
109 @@ -539,10 +539,21 @@ parse_references (void *ctx,\r
110  }\r
111  \r
112  notmuch_status_t\r
113 -notmuch_database_create (const char *path, notmuch_database_t **database)\r
114 +notmuch_database_new (notmuch_database_t **notmuch)\r
115 +{\r
116 +    /* Note: try to avoid error conditions! No error printing! */\r
117 +\r
118 +    *notmuch = talloc_zero (NULL, notmuch_database_t);\r
119 +    if (! *notmuch)\r
120 +       return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
121 +\r
122 +    return NOTMUCH_STATUS_SUCCESS;\r
123 +}\r
124 +\r
125 +notmuch_status_t\r
126 +notmuch_database_create (notmuch_database_t *notmuch, const char *path)\r
127  {\r
128      notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
129 -    notmuch_database_t *notmuch = NULL;\r
130      char *notmuch_path = NULL;\r
131      struct stat st;\r
132      int err;\r
133 @@ -579,25 +590,18 @@ notmuch_database_create (const char *path, notmuch_database_t **database)\r
134         goto DONE;\r
135      }\r
136  \r
137 -    status = notmuch_database_open (path,\r
138 -                                   NOTMUCH_DATABASE_MODE_READ_WRITE,\r
139 -                                   &notmuch);\r
140 +    status = notmuch_database_open (notmuch, path,\r
141 +                                   NOTMUCH_DATABASE_MODE_READ_WRITE);\r
142      if (status)\r
143         goto DONE;\r
144      status = notmuch_database_upgrade (notmuch, NULL, NULL);\r
145 -    if (status) {\r
146 +    if (status)\r
147         notmuch_database_close(notmuch);\r
148 -       notmuch = NULL;\r
149 -    }\r
150  \r
151    DONE:\r
152      if (notmuch_path)\r
153         talloc_free (notmuch_path);\r
154  \r
155 -    if (database)\r
156 -       *database = notmuch;\r
157 -    else\r
158 -       talloc_free (notmuch);\r
159      return status;\r
160  }\r
161  \r
162 @@ -612,14 +616,15 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)\r
163      return NOTMUCH_STATUS_SUCCESS;\r
164  }\r
165  \r
166 +/*\r
167 + * XXX: error handling should clean up *all* state created!\r
168 + */\r
169  notmuch_status_t\r
170 -notmuch_database_open (const char *path,\r
171 -                      notmuch_database_mode_t mode,\r
172 -                      notmuch_database_t **database)\r
173 +notmuch_database_open (notmuch_database_t *notmuch, const char *path,\r
174 +                      notmuch_database_mode_t mode)\r
175  {\r
176      notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
177      void *local = talloc_new (NULL);\r
178 -    notmuch_database_t *notmuch = NULL;\r
179      char *notmuch_path, *xapian_path;\r
180      struct stat st;\r
181      int err;\r
182 @@ -663,7 +668,6 @@ notmuch_database_open (const char *path,\r
183         initialized = 1;\r
184      }\r
185  \r
186 -    notmuch = talloc_zero (NULL, notmuch_database_t);\r
187      notmuch->exception_reported = FALSE;\r
188      notmuch->path = talloc_strdup (notmuch, path);\r
189  \r
190 @@ -689,8 +693,7 @@ notmuch_database_open (const char *path,\r
191                          "       read-write mode.\n",\r
192                          notmuch_path, version, NOTMUCH_DATABASE_VERSION);\r
193                 notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;\r
194 -               notmuch_database_destroy (notmuch);\r
195 -               notmuch = NULL;\r
196 +               notmuch_database_close (notmuch);\r
197                 status = NOTMUCH_STATUS_FILE_ERROR;\r
198                 goto DONE;\r
199             }\r
200 @@ -752,21 +755,19 @@ notmuch_database_open (const char *path,\r
201      } catch (const Xapian::Error &error) {\r
202         fprintf (stderr, "A Xapian exception occurred opening database: %s\n",\r
203                  error.get_msg().c_str());\r
204 -       notmuch_database_destroy (notmuch);\r
205 -       notmuch = NULL;\r
206 +       notmuch_database_close (notmuch);\r
207         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
208      }\r
209  \r
210    DONE:\r
211      talloc_free (local);\r
212  \r
213 -    if (database)\r
214 -       *database = notmuch;\r
215 -    else\r
216 -       talloc_free (notmuch);\r
217      return status;\r
218  }\r
219  \r
220 +/*\r
221 + * XXX: close should clean up *all* state created by open/create!\r
222 + */\r
223  notmuch_status_t\r
224  notmuch_database_close (notmuch_database_t *notmuch)\r
225  {\r
226 @@ -869,7 +870,8 @@ public:\r
227   * compaction process to protect data integrity.\r
228   */\r
229  notmuch_status_t\r
230 -notmuch_database_compact (const char *path,\r
231 +notmuch_database_compact (notmuch_database_t *notmuch,\r
232 +                         const char *path,\r
233                           const char *backup_path,\r
234                           notmuch_compact_status_cb_t status_cb,\r
235                           void *closure)\r
236 @@ -877,7 +879,6 @@ notmuch_database_compact (const char *path,\r
237      void *local;\r
238      char *notmuch_path, *xapian_path, *compact_xapian_path;\r
239      notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;\r
240 -    notmuch_database_t *notmuch = NULL;\r
241      struct stat statbuf;\r
242      notmuch_bool_t keep_backup;\r
243  \r
244 @@ -885,7 +886,8 @@ notmuch_database_compact (const char *path,\r
245      if (! local)\r
246         return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
247  \r
248 -    ret = notmuch_database_open (path, NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch);\r
249 +    ret = notmuch_database_open (notmuch, path,\r
250 +                                NOTMUCH_DATABASE_MODE_READ_WRITE);\r
251      if (ret) {\r
252         goto DONE;\r
253      }\r
254 @@ -971,8 +973,9 @@ notmuch_database_compact (const char *path,\r
255      }\r
256  \r
257    DONE:\r
258 +    /* XXX: error handling */\r
259      if (notmuch)\r
260 -       ret = notmuch_database_destroy (notmuch);\r
261 +       ret = notmuch_database_close (notmuch);\r
262  \r
263      talloc_free (local);\r
264  \r
265 @@ -980,7 +983,8 @@ notmuch_database_compact (const char *path,\r
266  }\r
267  #else\r
268  notmuch_status_t\r
269 -notmuch_database_compact (unused (const char *path),\r
270 +notmuch_database_compact (unused (notmuch_database_t *notmuch),\r
271 +                         unused (const char *path),\r
272                           unused (const char *backup_path),\r
273                           unused (notmuch_compact_status_cb_t status_cb),\r
274                           unused (void *closure))\r
275 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
276 index dbdce86..cd58d15 100644\r
277 --- a/lib/notmuch.h\r
278 +++ b/lib/notmuch.h\r
279 @@ -149,6 +149,28 @@ typedef struct _notmuch_tags notmuch_tags_t;\r
280  typedef struct _notmuch_directory notmuch_directory_t;\r
281  typedef struct _notmuch_filenames notmuch_filenames_t;\r
282  \r
283 +/* Initialize a new, empty database handle.\r
284 + *\r
285 + * The database handle is required for creating, opening, and\r
286 + * compacting a database. For further database operations, the\r
287 + * database needs to be created or opened.\r
288 + *\r
289 + * After a successful call to notmuch_database_new, the returned\r
290 + * database handle will remain in memory, so the caller should call\r
291 + * notmuch_database_destroy when finished with the database handle.\r
292 + *\r
293 + * In case of any failure, this function returns an error status and\r
294 + * sets *notmuch to NULL.\r
295 + *\r
296 + * Return value:\r
297 + *\r
298 + * NOTMUCH_STATUS_SUCCESS: Successfully created the database object.\r
299 + *\r
300 + * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.\r
301 + */\r
302 +notmuch_status_t\r
303 +notmuch_database_new (notmuch_database_t **notmuch);\r
304 +\r
305  /* Create a new, empty notmuch database located at 'path'.\r
306   *\r
307   * The path should be a top-level directory to a collection of\r
308 @@ -156,9 +178,9 @@ typedef struct _notmuch_filenames notmuch_filenames_t;\r
309   * create a new ".notmuch" directory within 'path' where notmuch will\r
310   * store its data.\r
311   *\r
312 - * After a successful call to notmuch_database_create, the returned\r
313 - * database will be open so the caller should call\r
314 - * notmuch_database_destroy when finished with it.\r
315 + * After a successful call to notmuch_database_create, the database\r
316 + * will be open so the caller should call notmuch_database_close (or\r
317 + * notmuch_database_destroy) when finished with the database.\r
318   *\r
319   * The database will not yet have any data in it\r
320   * (notmuch_database_create itself is a very cheap function). Messages\r
321 @@ -166,7 +188,8 @@ typedef struct _notmuch_filenames notmuch_filenames_t;\r
322   * notmuch_database_add_message.\r
323   *\r
324   * In case of any failure, this function returns an error status and\r
325 - * sets *database to NULL (after printing an error message on stderr).\r
326 + * the database will be closed (after printing an error message on\r
327 + * stderr).\r
328   *\r
329   * Return value:\r
330   *\r
331 @@ -183,7 +206,7 @@ typedef struct _notmuch_filenames notmuch_filenames_t;\r
332   * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.\r
333   */\r
334  notmuch_status_t\r
335 -notmuch_database_create (const char *path, notmuch_database_t **database);\r
336 +notmuch_database_create (notmuch_database_t *notmuch, const char *path);\r
337  \r
338  typedef enum {\r
339      NOTMUCH_DATABASE_MODE_READ_ONLY = 0,\r
340 @@ -201,11 +224,13 @@ typedef enum {\r
341   * An existing notmuch database can be identified by the presence of a\r
342   * directory named ".notmuch" below 'path'.\r
343   *\r
344 - * The caller should call notmuch_database_destroy when finished with\r
345 - * this database.\r
346 + * After a successful call to notmuch_database_open, the database will\r
347 + * be open so the caller should call notmuch_database_close (or\r
348 + * notmuch_database_destroy) when finished with the database.\r
349   *\r
350   * In case of any failure, this function returns an error status and\r
351 - * sets *database to NULL (after printing an error message on stderr).\r
352 + * the database will be closed (after printing an error message on\r
353 + * stderr).\r
354   *\r
355   * Return value:\r
356   *\r
357 @@ -222,9 +247,8 @@ typedef enum {\r
358   * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.\r
359   */\r
360  notmuch_status_t\r
361 -notmuch_database_open (const char *path,\r
362 -                      notmuch_database_mode_t mode,\r
363 -                      notmuch_database_t **database);\r
364 +notmuch_database_open (notmuch_database_t *notmuch, const char *path,\r
365 +                      notmuch_database_mode_t mode);\r
366  \r
367  /* Close the given notmuch database.\r
368   *\r
369 @@ -264,7 +288,8 @@ typedef void (*notmuch_compact_status_cb_t)(const char *message, void *closure);\r
370   * 'closure' is passed verbatim to any callback invoked.\r
371   */\r
372  notmuch_status_t\r
373 -notmuch_database_compact (const char* path,\r
374 +notmuch_database_compact (notmuch_database_t *notmuch,\r
375 +                         const char* path,\r
376                           const char* backup_path,\r
377                           notmuch_compact_status_cb_t status_cb,\r
378                           void *closure);\r
379 @@ -272,6 +297,9 @@ notmuch_database_compact (const char* path,\r
380  /* Destroy the notmuch database, closing it if necessary and freeing\r
381   * all associated resources.\r
382   *\r
383 + * A database handle initialized with notmuch_database_new should be\r
384 + * destroyed by calling notmuch_database_destroy.\r
385 + *\r
386   * Return value as in notmuch_database_close if the database was open;\r
387   * notmuch_database_destroy itself has no failure modes.\r
388   */\r
389 diff --git a/notmuch-compact.c b/notmuch-compact.c\r
390 index 8b820c0..626685e 100644\r
391 --- a/notmuch-compact.c\r
392 +++ b/notmuch-compact.c\r
393 @@ -29,6 +29,7 @@ status_update_cb (const char *msg, unused (void *closure))\r
394  int\r
395  notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[])\r
396  {\r
397 +    notmuch_database_t *notmuch = NULL;\r
398      const char *path = notmuch_config_get_database_path (config);\r
399      const char *backup_path = NULL;\r
400      notmuch_status_t ret;\r
401 @@ -46,7 +47,13 @@ notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[])\r
402  \r
403      if (! quiet)\r
404         printf ("Compacting database...\n");\r
405 -    ret = notmuch_database_compact (path, backup_path,\r
406 +\r
407 +    if (notmuch_database_new (&notmuch)) {\r
408 +       fprintf (stderr, "Out of memory\n");\r
409 +       return 1;\r
410 +    }\r
411 +\r
412 +    ret = notmuch_database_compact (notmuch, path, backup_path,\r
413                                     quiet ? NULL : status_update_cb, NULL);\r
414      if (ret) {\r
415         fprintf (stderr, "Compaction failed: %s\n", notmuch_status_to_string (ret));\r
416 @@ -60,5 +67,7 @@ notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[])\r
417         printf ("Done.\n");\r
418      }\r
419  \r
420 +    notmuch_database_destroy (notmuch);\r
421 +\r
422      return 0;\r
423  }\r
424 diff --git a/notmuch-count.c b/notmuch-count.c\r
425 index 01e4e30..15c95c7 100644\r
426 --- a/notmuch-count.c\r
427 +++ b/notmuch-count.c\r
428 @@ -170,8 +170,14 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])\r
429         return 1;\r
430      }\r
431  \r
432 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
433 -                              NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
434 +    if (notmuch_database_new (&notmuch)) {\r
435 +       fprintf (stderr, "Out of memory\n");\r
436 +       return 1;\r
437 +    }\r
438 +\r
439 +    if (notmuch_database_open (notmuch,\r
440 +                              notmuch_config_get_database_path (config),\r
441 +                              NOTMUCH_DATABASE_MODE_READ_ONLY))\r
442         return 1;\r
443  \r
444      query_str = query_string_from_args (config, argc-opt_index, argv+opt_index);\r
445 diff --git a/notmuch-dump.c b/notmuch-dump.c\r
446 index 2024e30..73579bc 100644\r
447 --- a/notmuch-dump.c\r
448 +++ b/notmuch-dump.c\r
449 @@ -33,8 +33,14 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
450      notmuch_tags_t *tags;\r
451      const char *query_str = "";\r
452  \r
453 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
454 -                              NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
455 +    if (notmuch_database_new (&notmuch)) {\r
456 +       fprintf (stderr, "Out of memory\n");\r
457 +       return 1;\r
458 +    }\r
459 +\r
460 +    if (notmuch_database_open (notmuch,\r
461 +                              notmuch_config_get_database_path (config),\r
462 +                              NOTMUCH_DATABASE_MODE_READ_ONLY))\r
463         return 1;\r
464  \r
465      char *output_file_name = NULL;\r
466 diff --git a/notmuch-insert.c b/notmuch-insert.c\r
467 index 2207b1e..4a8aad3 100644\r
468 --- a/notmuch-insert.c\r
469 +++ b/notmuch-insert.c\r
470 @@ -467,8 +467,14 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
471      action.sa_flags = 0;\r
472      sigaction (SIGINT, &action, NULL);\r
473  \r
474 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
475 -                              NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
476 +    if (notmuch_database_new (&notmuch)) {\r
477 +       fprintf (stderr, "Out of memory\n");\r
478 +       return 1;\r
479 +    }\r
480 +\r
481 +    if (notmuch_database_open (notmuch,\r
482 +                              notmuch_config_get_database_path (config),\r
483 +                              NOTMUCH_DATABASE_MODE_READ_WRITE))\r
484         return 1;\r
485  \r
486      ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops);\r
487 diff --git a/notmuch-new.c b/notmuch-new.c\r
488 index ba05cb4..f72a4de 100644\r
489 --- a/notmuch-new.c\r
490 +++ b/notmuch-new.c\r
491 @@ -914,6 +914,11 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
492             return ret;\r
493      }\r
494  \r
495 +    if (notmuch_database_new (&notmuch)) {\r
496 +       fprintf (stderr, "Out of memory\n");\r
497 +       return 1;\r
498 +    }\r
499 +\r
500      dot_notmuch_path = talloc_asprintf (config, "%s/%s", db_path, ".notmuch");\r
501  \r
502      if (stat (dot_notmuch_path, &st)) {\r
503 @@ -925,12 +930,12 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
504             return 1;\r
505  \r
506         printf ("Found %d total files (that's not much mail).\n", count);\r
507 -       if (notmuch_database_create (db_path, &notmuch))\r
508 +       if (notmuch_database_create (notmuch, db_path))\r
509             return 1;\r
510         add_files_state.total_files = count;\r
511      } else {\r
512 -       if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,\r
513 -                                  &notmuch))\r
514 +       if (notmuch_database_open (notmuch, db_path,\r
515 +                                  NOTMUCH_DATABASE_MODE_READ_WRITE))\r
516             return 1;\r
517  \r
518         if (notmuch_database_needs_upgrade (notmuch)) {\r
519 @@ -945,9 +950,6 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
520         add_files_state.total_files = 0;\r
521      }\r
522  \r
523 -    if (notmuch == NULL)\r
524 -       return 1;\r
525 -\r
526      /* Setup our handler for SIGINT. We do this after having\r
527       * potentially done a database upgrade we this interrupt handler\r
528       * won't support. */\r
529 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
530 index 9d6f843..7b80841 100644\r
531 --- a/notmuch-reply.c\r
532 +++ b/notmuch-reply.c\r
533 @@ -769,8 +769,14 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
534         return 1;\r
535      }\r
536  \r
537 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
538 -                              NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
539 +    if (notmuch_database_new (&notmuch)) {\r
540 +       fprintf (stderr, "Out of memory\n");\r
541 +       return 1;\r
542 +    }\r
543 +\r
544 +    if (notmuch_database_open (notmuch,\r
545 +                              notmuch_config_get_database_path (config),\r
546 +                              NOTMUCH_DATABASE_MODE_READ_ONLY))\r
547         return 1;\r
548  \r
549      query = notmuch_query_create (notmuch, query_string);\r
550 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
551 index 1419621..fc37838 100644\r
552 --- a/notmuch-restore.c\r
553 +++ b/notmuch-restore.c\r
554 @@ -138,8 +138,14 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
555      int opt_index;\r
556      int input_format = DUMP_FORMAT_AUTO;\r
557  \r
558 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
559 -                              NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
560 +    if (notmuch_database_new (&notmuch)) {\r
561 +       fprintf (stderr, "Out of memory\n");\r
562 +       return 1;\r
563 +    }\r
564 +\r
565 +    if (notmuch_database_open (notmuch,\r
566 +                              notmuch_config_get_database_path (config),\r
567 +                              NOTMUCH_DATABASE_MODE_READ_WRITE))\r
568         return 1;\r
569  \r
570      if (notmuch_config_get_maildir_synchronize_flags (config))\r
571 diff --git a/notmuch-search.c b/notmuch-search.c\r
572 index 7c973b3..50aace9 100644\r
573 --- a/notmuch-search.c\r
574 +++ b/notmuch-search.c\r
575 @@ -430,8 +430,14 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
576  \r
577      notmuch_exit_if_unsupported_format ();\r
578  \r
579 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
580 -                              NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
581 +    if (notmuch_database_new (&notmuch)) {\r
582 +       fprintf (stderr, "Out of memory\n");\r
583 +       return 1;\r
584 +    }\r
585 +\r
586 +    if (notmuch_database_open (notmuch,\r
587 +                              notmuch_config_get_database_path (config),\r
588 +                              NOTMUCH_DATABASE_MODE_READ_ONLY))\r
589         return 1;\r
590  \r
591      query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);\r
592 diff --git a/notmuch-show.c b/notmuch-show.c\r
593 index c07f887..bc44b2c 100644\r
594 --- a/notmuch-show.c\r
595 +++ b/notmuch-show.c\r
596 @@ -1201,8 +1201,14 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])\r
597         return 1;\r
598      }\r
599  \r
600 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
601 -                              NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
602 +    if (notmuch_database_new (&notmuch)) {\r
603 +       fprintf (stderr, "Out of memory\n");\r
604 +       return 1;\r
605 +    }\r
606 +\r
607 +    if (notmuch_database_open (notmuch,\r
608 +                              notmuch_config_get_database_path (config),\r
609 +                              NOTMUCH_DATABASE_MODE_READ_ONLY))\r
610         return 1;\r
611  \r
612      query = notmuch_query_create (notmuch, query_string);\r
613 diff --git a/notmuch-tag.c b/notmuch-tag.c\r
614 index 3b09df9..6e29799 100644\r
615 --- a/notmuch-tag.c\r
616 +++ b/notmuch-tag.c\r
617 @@ -254,8 +254,14 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])\r
618         }\r
619      }\r
620  \r
621 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
622 -                              NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
623 +    if (notmuch_database_new (&notmuch)) {\r
624 +       fprintf (stderr, "Out of memory\n");\r
625 +       return 1;\r
626 +    }\r
627 +\r
628 +    if (notmuch_database_open (notmuch,\r
629 +                              notmuch_config_get_database_path (config),\r
630 +                              NOTMUCH_DATABASE_MODE_READ_WRITE))\r
631         return 1;\r
632  \r
633      if (notmuch_config_get_maildir_synchronize_flags (config))\r
634 diff --git a/test/random-corpus.c b/test/random-corpus.c\r
635 index 790193d..2b205e5 100644\r
636 --- a/test/random-corpus.c\r
637 +++ b/test/random-corpus.c\r
638 @@ -164,8 +164,14 @@ main (int argc, char **argv)\r
639      if (config == NULL)\r
640         return 1;\r
641  \r
642 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
643 -                              NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
644 +    if (notmuch_database_new (&notmuch)) {\r
645 +       fprintf (stderr, "Out of memory\n");\r
646 +       return 1;\r
647 +    }\r
648 +\r
649 +    if (notmuch_database_open (notmuch,\r
650 +                              notmuch_config_get_database_path (config),\r
651 +                              NOTMUCH_DATABASE_MODE_READ_WRITE))\r
652         return 1;\r
653  \r
654      srandom (seed);\r
655 diff --git a/test/symbol-test.cc b/test/symbol-test.cc\r
656 index 3e96c03..47c5351 100644\r
657 --- a/test/symbol-test.cc\r
658 +++ b/test/symbol-test.cc\r
659 @@ -5,7 +5,8 @@\r
660  \r
661  int main() {\r
662    notmuch_database_t *notmuch;\r
663 -  notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch);\r
664 +  notmuch_database_new (&notmuch);\r
665 +  notmuch_database_open (notmuch, "fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY);\r
666  \r
667    try {\r
668      (void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN);\r
669 -- \r
670 1.8.4.2\r
671 \r