Re: new "crypto" branch providing full PGP/MIME support
[notmuch-archives.git] / 83 / cbddd7d48a5492c1a5fb3a1a246ea6ffdf42ba
1 Return-Path: <prvs=567195f5a2=davidben@mit.edu>\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 0ACA7431FD4\r
6         for <notmuch@notmuchmail.org>; Tue, 23 Feb 2010 18:28:19 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -1.577\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-1.577 tagged_above=-999 required=5 tests=[AWL=1.022,\r
12         BAYES_00=-2.599] autolearn=ham\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id rZEcWSLUrQ0x for <notmuch@notmuchmail.org>;\r
16         Tue, 23 Feb 2010 18:28:17 -0800 (PST)\r
17 Received: from dmz-mailsec-scanner-3.mit.edu (DMZ-MAILSEC-SCANNER-3.MIT.EDU\r
18         [18.9.25.14])\r
19         by olra.theworths.org (Postfix) with ESMTP id 3B7AD431FAE\r
20         for <notmuch@notmuchmail.org>; Tue, 23 Feb 2010 18:28:17 -0800 (PST)\r
21 X-AuditID: 1209190e-b7ca7ae00000096e-91-4b848d9228a2\r
22 Received: from mailhub-auth-2.mit.edu (MAILHUB-AUTH-2.MIT.EDU [18.7.62.36])\r
23         by dmz-mailsec-scanner-3.mit.edu (Symantec Brightmail Gateway) with\r
24         SMTP id B7.DF.02414.29D848B4; Tue, 23 Feb 2010 21:23:14 -0500 (EST)\r
25 Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
26         by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id o1O2NE5o008874; \r
27         Tue, 23 Feb 2010 21:23:14 -0500\r
28 Received: from localhost.localdomain (ET-NINETY-THREE.MIT.EDU [18.208.1.93])\r
29         (authenticated bits=0)\r
30         (User authenticated as davidben@ATHENA.MIT.EDU)\r
31         by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id o1O2NTit021532\r
32         (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT);\r
33         Tue, 23 Feb 2010 21:23:34 -0500 (EST)\r
34 From: David Benjamin <davidben@MIT.EDU>\r
35 To: notmuch@notmuchmail.org\r
36 Date: Tue, 23 Feb 2010 21:23:01 -0500\r
37 Message-Id: <1266978183-19698-3-git-send-email-davidben@mit.edu>\r
38 X-Mailer: git-send-email 1.7.0.18.g39b3\r
39 In-Reply-To: <1266978183-19698-1-git-send-email-davidben@mit.edu>\r
40 References: <1266978183-19698-1-git-send-email-davidben@mit.edu>\r
41 X-Brightmail-Tracker: AAAAAA==\r
42 Subject: [notmuch] [PATCH 2/4] Pass separate notmuch_path when opening\r
43         database\r
44 X-BeenThere: notmuch@notmuchmail.org\r
45 X-Mailman-Version: 2.1.13\r
46 Precedence: list\r
47 List-Id: "Use and development of the notmuch mail system."\r
48         <notmuch.notmuchmail.org>\r
49 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
50         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
51 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
52 List-Post: <mailto:notmuch@notmuchmail.org>\r
53 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
54 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
55         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
56 X-List-Received-Date: Wed, 24 Feb 2010 02:28:19 -0000\r
57 \r
58 Avoid hard-coding $MAILSTORE/.notmuch in the database layer.\r
59 \r
60 Signed-off-by: David Benjamin <davidben@mit.edu>\r
61 ---\r
62  lib/database-private.h |    1 +\r
63  lib/database.cc        |   28 ++++++++++++++--------------\r
64  lib/notmuch.h          |   32 ++++++++++++++++++++------------\r
65  notmuch-client.h       |    4 ++++\r
66  notmuch-config.c       |   29 +++++++++++++++++++++++++++++\r
67  notmuch-count.c        |    4 ++--\r
68  notmuch-dump.c         |    4 ++--\r
69  notmuch-new.c          |    3 ++-\r
70  notmuch-reply.c        |    4 ++--\r
71  notmuch-restore.c      |    4 ++--\r
72  notmuch-search-tags.c  |    4 ++--\r
73  notmuch-search.c       |    4 ++--\r
74  notmuch-show.c         |    4 ++--\r
75  notmuch-tag.c          |    4 ++--\r
76  14 files changed, 86 insertions(+), 43 deletions(-)\r
77 \r
78 diff --git a/lib/database-private.h b/lib/database-private.h\r
79 index 41918d7..59ed117 100644\r
80 --- a/lib/database-private.h\r
81 +++ b/lib/database-private.h\r
82 @@ -38,6 +38,7 @@ struct _notmuch_database {\r
83      notmuch_bool_t exception_reported;\r
84  \r
85      char *path;\r
86 +    char *notmuch_path;\r
87  \r
88      notmuch_bool_t needs_upgrade;\r
89      notmuch_database_mode_t mode;\r
90 diff --git a/lib/database.cc b/lib/database.cc\r
91 index 1bb24ec..f54fbd1 100644\r
92 --- a/lib/database.cc\r
93 +++ b/lib/database.cc\r
94 @@ -437,14 +437,14 @@ parse_references (void *ctx,\r
95  }\r
96  \r
97  notmuch_database_t *\r
98 -notmuch_database_create (const char *path)\r
99 +notmuch_database_create (const char *path,\r
100 +                        const char *notmuch_path)\r
101  {\r
102      notmuch_database_t *notmuch = NULL;\r
103 -    char *notmuch_path = NULL;\r
104      struct stat st;\r
105      int err;\r
106  \r
107 -    if (path == NULL) {\r
108 +    if (path == NULL || notmuch_path == NULL) {\r
109         fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");\r
110         goto DONE;\r
111      }\r
112 @@ -462,8 +462,6 @@ notmuch_database_create (const char *path)\r
113         goto DONE;\r
114      }\r
115  \r
116 -    notmuch_path = talloc_asprintf (NULL, "%s/%s", path, ".notmuch");\r
117 -\r
118      err = mkdir (notmuch_path, 0755);\r
119  \r
120      if (err) {\r
121 @@ -473,13 +471,11 @@ notmuch_database_create (const char *path)\r
122      }\r
123  \r
124      notmuch = notmuch_database_open (path,\r
125 +                                    notmuch_path,\r
126                                      NOTMUCH_DATABASE_MODE_READ_WRITE);\r
127      notmuch_database_upgrade (notmuch, NULL, NULL);\r
128  \r
129    DONE:\r
130 -    if (notmuch_path)\r
131 -       talloc_free (notmuch_path);\r
132 -\r
133      return notmuch;\r
134  }\r
135  \r
136 @@ -496,17 +492,17 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)\r
137  \r
138  notmuch_database_t *\r
139  notmuch_database_open (const char *path,\r
140 +                      const char *notmuch_path,\r
141                        notmuch_database_mode_t mode)\r
142  {\r
143      notmuch_database_t *notmuch = NULL;\r
144 -    char *notmuch_path = NULL, *xapian_path = NULL;\r
145 +    char *xapian_path = NULL;\r
146      struct stat st;\r
147      int err;\r
148      unsigned int i, version;\r
149  \r
150 -    if (asprintf (&notmuch_path, "%s/%s", path, ".notmuch") == -1) {\r
151 -       notmuch_path = NULL;\r
152 -       fprintf (stderr, "Out of memory\n");\r
153 +    if (path == NULL || notmuch_path == NULL) {\r
154 +       fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");\r
155         goto DONE;\r
156      }\r
157  \r
158 @@ -611,8 +607,6 @@ notmuch_database_open (const char *path,\r
159      }\r
160  \r
161    DONE:\r
162 -    if (notmuch_path)\r
163 -       free (notmuch_path);\r
164      if (xapian_path)\r
165         free (xapian_path);\r
166  \r
167 @@ -645,6 +639,12 @@ notmuch_database_get_path (notmuch_database_t *notmuch)\r
168      return notmuch->path;\r
169  }\r
170  \r
171 +const char *\r
172 +notmuch_database_get_notmuch_path (notmuch_database_t *notmuch)\r
173 +{\r
174 +    return notmuch->notmuch_path;\r
175 +}\r
176 +\r
177  unsigned int\r
178  notmuch_database_get_version (notmuch_database_t *notmuch)\r
179  {\r
180 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
181 index d3e50a7..943c297 100644\r
182 --- a/lib/notmuch.h\r
183 +++ b/lib/notmuch.h\r
184 @@ -120,12 +120,12 @@ typedef struct _notmuch_tags notmuch_tags_t;\r
185  typedef struct _notmuch_directory notmuch_directory_t;\r
186  typedef struct _notmuch_filenames notmuch_filenames_t;\r
187  \r
188 -/* Create a new, empty notmuch database located at 'path'.\r
189 +/* Create a new, empty notmuch database located at 'notmuch_path',\r
190 + * indexing mail store 'path'\r
191   *\r
192   * The path should be a top-level directory to a collection of\r
193   * plain-text email messages (one message per file). This call will\r
194 - * create a new ".notmuch" directory within 'path' where notmuch will\r
195 - * store its data.\r
196 + * create directory 'notmuch_path' where notmuch will store its data.\r
197   *\r
198   * After a successful call to notmuch_database_create, the returned\r
199   * database will be open so the caller should call\r
200 @@ -140,7 +140,8 @@ typedef struct _notmuch_filenames notmuch_filenames_t;\r
201   * an error message on stderr).\r
202   */\r
203  notmuch_database_t *\r
204 -notmuch_database_create (const char *path);\r
205 +notmuch_database_create (const char *path,\r
206 +                        const char *notmuch_path);\r
207  \r
208  typedef enum {\r
209      NOTMUCH_DATABASE_MODE_READ_ONLY = 0,\r
210 @@ -150,16 +151,15 @@ typedef enum {\r
211  /* XXX: I think I'd like this to take an extra argument of\r
212   * notmuch_status_t* for returning a status value on failure. */\r
213  \r
214 -/* Open an existing notmuch database located at 'path'.\r
215 +/* Open an existing notmuch database located at 'notmuch_path' with\r
216 + * mail store rooted at 'path'\r
217   *\r
218   * The database should have been created at some time in the past,\r
219   * (not necessarily by this process), by calling\r
220 - * notmuch_database_create with 'path'. By default the database should be\r
221 - * opened for reading only. In order to write to the database you need to\r
222 - * pass the NOTMUCH_DATABASE_MODE_WRITABLE mode.\r
223 - *\r
224 - * An existing notmuch database can be identified by the presence of a\r
225 - * directory named ".notmuch" below 'path'.\r
226 + * notmuch_database_create with 'path' and 'notmuch_path'. By default\r
227 + * the database should be opened for reading only. In order to write\r
228 + * to the database you need to pass the NOTMUCH_DATABASE_MODE_WRITABLE\r
229 + * mode.\r
230   *\r
231   * The caller should call notmuch_database_close when finished with\r
232   * this database.\r
233 @@ -169,6 +169,7 @@ typedef enum {\r
234   */\r
235  notmuch_database_t *\r
236  notmuch_database_open (const char *path,\r
237 +                      const char *notmuch_path,\r
238                        notmuch_database_mode_t mode);\r
239  \r
240  /* Close the given notmuch database, freeing all associated\r
241 @@ -176,13 +177,20 @@ notmuch_database_open (const char *path,\r
242  void\r
243  notmuch_database_close (notmuch_database_t *database);\r
244  \r
245 -/* Return the database path of the given database.\r
246 +/* Return the mail store path of the given database.\r
247   *\r
248   * The return value is a string owned by notmuch so should not be\r
249   * modified nor freed by the caller. */\r
250  const char *\r
251  notmuch_database_get_path (notmuch_database_t *database);\r
252  \r
253 +/* Return the database path of the given database.\r
254 + *\r
255 + * The return value is a string owned by notmuch so should not be\r
256 + * modified nor freed by the caller. */\r
257 +const char *\r
258 +notmuch_database_get_notmuch_path (notmuch_database_t *database);\r
259 +\r
260  /* Return the database format version of the given database. */\r
261  unsigned int\r
262  notmuch_database_get_version (notmuch_database_t *database);\r
263 diff --git a/notmuch-client.h b/notmuch-client.h\r
264 index c80b39c..1a676d2 100644\r
265 --- a/notmuch-client.h\r
266 +++ b/notmuch-client.h\r
267 @@ -148,6 +148,10 @@ void\r
268  notmuch_config_set_database_path (notmuch_config_t *config,\r
269                                   const char *database_path);\r
270  \r
271 +notmuch_database_t *\r
272 +notmuch_config_open_database (notmuch_config_t *config,\r
273 +                             notmuch_database_mode_t mode);\r
274 +\r
275  const char *\r
276  notmuch_config_get_user_name (notmuch_config_t *config);\r
277  \r
278 diff --git a/notmuch-config.c b/notmuch-config.c\r
279 index 95430db..58a28b1 100644\r
280 --- a/notmuch-config.c\r
281 +++ b/notmuch-config.c\r
282 @@ -353,6 +353,35 @@ notmuch_config_set_database_path (notmuch_config_t *config,\r
283      config->database_path = NULL;\r
284  }\r
285  \r
286 +notmuch_database_t *\r
287 +notmuch_config_open_database (notmuch_config_t *config,\r
288 +                             notmuch_database_mode_t mode)\r
289 +{\r
290 +    const char *path = NULL;\r
291 +    char *db_path = NULL;\r
292 +    notmuch_database_t *notmuch = NULL;\r
293 +\r
294 +    path = notmuch_config_get_database_path (config);\r
295 +    if (path == NULL) {\r
296 +       fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");\r
297 +       goto DONE;\r
298 +    }\r
299 +\r
300 +    if (asprintf (&db_path, "%s/%s", path, ".notmuch") == -1) {\r
301 +       db_path = NULL;\r
302 +       fprintf (stderr, "Out of memory\n");\r
303 +       goto DONE;\r
304 +    }\r
305 +\r
306 +    notmuch = notmuch_database_open (path, db_path, mode);\r
307 +\r
308 +DONE:\r
309 +    if (db_path)\r
310 +       free(db_path);\r
311 +\r
312 +    return notmuch;\r
313 +}\r
314 +\r
315  const char *\r
316  notmuch_config_get_user_name (notmuch_config_t *config)\r
317  {\r
318 diff --git a/notmuch-count.c b/notmuch-count.c\r
319 index 77aa433..8d077eb 100644\r
320 --- a/notmuch-count.c\r
321 +++ b/notmuch-count.c\r
322 @@ -80,8 +80,8 @@ notmuch_count_command (void *ctx, int argc, char *argv[])\r
323      if (config == NULL)\r
324         return 1;\r
325  \r
326 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
327 -                                    NOTMUCH_DATABASE_MODE_READ_ONLY);\r
328 +    notmuch = notmuch_config_open_database (config,\r
329 +                                           NOTMUCH_DATABASE_MODE_READ_ONLY);\r
330      if (notmuch == NULL)\r
331         return 1;\r
332  \r
333 diff --git a/notmuch-dump.c b/notmuch-dump.c\r
334 index ea326bb..a051fd4 100644\r
335 --- a/notmuch-dump.c\r
336 +++ b/notmuch-dump.c\r
337 @@ -35,8 +35,8 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])\r
338      if (config == NULL)\r
339         return 1;\r
340  \r
341 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
342 -                                    NOTMUCH_DATABASE_MODE_READ_ONLY);\r
343 +    notmuch = notmuch_config_open_database (config,\r
344 +                                           NOTMUCH_DATABASE_MODE_READ_ONLY);\r
345      if (notmuch == NULL)\r
346         return 1;\r
347  \r
348 diff --git a/notmuch-new.c b/notmuch-new.c\r
349 index f25c71f..d24dab9 100644\r
350 --- a/notmuch-new.c\r
351 +++ b/notmuch-new.c\r
352 @@ -749,10 +749,11 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
353             return 1;\r
354  \r
355         printf ("Found %d total files (that's not much mail).\n", count);\r
356 -       notmuch = notmuch_database_create (db_path);\r
357 +       notmuch = notmuch_database_create (db_path, dot_notmuch_path);\r
358         add_files_state.total_files = count;\r
359      } else {\r
360         notmuch = notmuch_database_open (db_path,\r
361 +                                        dot_notmuch_path,\r
362                                          NOTMUCH_DATABASE_MODE_READ_WRITE);\r
363         if (notmuch == NULL)\r
364             return 1;\r
365 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
366 index 98f6442..10734bc 100644\r
367 --- a/notmuch-reply.c\r
368 +++ b/notmuch-reply.c\r
369 @@ -467,8 +467,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
370         return 1;\r
371      }\r
372  \r
373 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
374 -                                    NOTMUCH_DATABASE_MODE_READ_ONLY);\r
375 +    notmuch = notmuch_config_open_database (config,\r
376 +                                           NOTMUCH_DATABASE_MODE_READ_ONLY);\r
377      if (notmuch == NULL)\r
378         return 1;\r
379  \r
380 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
381 index 53ce254..d495b4f 100644\r
382 --- a/notmuch-restore.c\r
383 +++ b/notmuch-restore.c\r
384 @@ -36,8 +36,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
385      if (config == NULL)\r
386         return 1;\r
387  \r
388 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
389 -                                    NOTMUCH_DATABASE_MODE_READ_WRITE);\r
390 +    notmuch = notmuch_config_open_database (config,\r
391 +                                           NOTMUCH_DATABASE_MODE_READ_WRITE);\r
392      if (notmuch == NULL)\r
393         return 1;\r
394  \r
395 diff --git a/notmuch-search-tags.c b/notmuch-search-tags.c\r
396 index 7a1305e..1ddbb94 100644\r
397 --- a/notmuch-search-tags.c\r
398 +++ b/notmuch-search-tags.c\r
399 @@ -51,8 +51,8 @@ notmuch_search_tags_command (void *ctx, int argc, char *argv[])\r
400         goto error;\r
401      }\r
402  \r
403 -    db = notmuch_database_open (notmuch_config_get_database_path (config),\r
404 -                               NOTMUCH_DATABASE_MODE_READ_ONLY);\r
405 +    db = notmuch_config_open_database (config,\r
406 +                                      NOTMUCH_DATABASE_MODE_READ_ONLY);\r
407      if (db == NULL) {\r
408         goto error;\r
409      }\r
410 diff --git a/notmuch-search.c b/notmuch-search.c\r
411 index 25dd6eb..2b65e87 100644\r
412 --- a/notmuch-search.c\r
413 +++ b/notmuch-search.c\r
414 @@ -244,8 +244,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
415      if (config == NULL)\r
416         return 1;\r
417  \r
418 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
419 -                                    NOTMUCH_DATABASE_MODE_READ_ONLY);\r
420 +    notmuch = notmuch_config_open_database (config,\r
421 +                                           NOTMUCH_DATABASE_MODE_READ_ONLY);\r
422      if (notmuch == NULL)\r
423         return 1;\r
424  \r
425 diff --git a/notmuch-show.c b/notmuch-show.c\r
426 index 1a1d601..88124dc 100644\r
427 --- a/notmuch-show.c\r
428 +++ b/notmuch-show.c\r
429 @@ -446,8 +446,8 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
430         return 1;\r
431      }\r
432  \r
433 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
434 -                                    NOTMUCH_DATABASE_MODE_READ_ONLY);\r
435 +    notmuch = notmuch_config_open_database (config,\r
436 +                                           NOTMUCH_DATABASE_MODE_READ_ONLY);\r
437      if (notmuch == NULL)\r
438         return 1;\r
439  \r
440 diff --git a/notmuch-tag.c b/notmuch-tag.c\r
441 index 00588a1..1deff1a 100644\r
442 --- a/notmuch-tag.c\r
443 +++ b/notmuch-tag.c\r
444 @@ -96,8 +96,8 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))\r
445      if (config == NULL)\r
446         return 1;\r
447  \r
448 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
449 -                                    NOTMUCH_DATABASE_MODE_READ_WRITE);\r
450 +    notmuch = notmuch_config_open_database (config,\r
451 +                                           NOTMUCH_DATABASE_MODE_READ_WRITE);\r
452      if (notmuch == NULL)\r
453         return 1;\r
454  \r
455 -- \r
456 1.7.0.18.g39b3\r
457 \r