]> git.notmuchmail.org Git - notmuch/blob - lib/open.cc
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / lib / open.cc
1 #include <unistd.h>
2 #include <libgen.h>
3
4 #include "database-private.h"
5 #include "parse-time-vrp.h"
6 #include "path-util.h"
7
8 #if HAVE_XAPIAN_DB_RETRY_LOCK
9 #define DB_ACTION (Xapian::DB_CREATE_OR_OPEN | Xapian::DB_RETRY_LOCK)
10 #else
11 #define DB_ACTION Xapian::DB_CREATE_OR_OPEN
12 #endif
13
14 notmuch_status_t
15 notmuch_database_open (const char *path,
16                        notmuch_database_mode_t mode,
17                        notmuch_database_t **database)
18 {
19     char *status_string = NULL;
20     notmuch_status_t status;
21
22     status = notmuch_database_open_verbose (path, mode, database,
23                                             &status_string);
24
25     if (status_string) {
26         fputs (status_string, stderr);
27         free (status_string);
28     }
29
30     return status;
31 }
32
33 notmuch_status_t
34 notmuch_database_open_verbose (const char *path,
35                                notmuch_database_mode_t mode,
36                                notmuch_database_t **database,
37                                char **status_string)
38 {
39     return notmuch_database_open_with_config (path, mode, "", NULL,
40                                               database, status_string);
41 }
42
43 static const char *
44 _xdg_dir (void *ctx,
45           const char *xdg_root_variable,
46           const char *xdg_prefix,
47           const char *profile_name)
48 {
49     const char *xdg_root = getenv (xdg_root_variable);
50
51     if (! xdg_root) {
52         const char *home = getenv ("HOME");
53
54         if (! home) return NULL;
55
56         xdg_root = talloc_asprintf (ctx,
57                                     "%s/%s",
58                                     home,
59                                     xdg_prefix);
60     }
61
62     if (! profile_name)
63         profile_name = getenv ("NOTMUCH_PROFILE");
64
65     if (! profile_name)
66         profile_name = "default";
67
68     return talloc_asprintf (ctx,
69                             "%s/notmuch/%s",
70                             xdg_root,
71                             profile_name);
72 }
73
74 static notmuch_status_t
75 _choose_dir (notmuch_database_t *notmuch,
76              const char *profile,
77              notmuch_config_key_t key,
78              const char *xdg_var,
79              const char *xdg_subdir,
80              const char *subdir,
81              char **message = NULL)
82 {
83     const char *parent;
84     const char *dir;
85     struct stat st;
86     int err;
87
88     dir = notmuch_config_get (notmuch, key);
89
90     if (dir)
91         return NOTMUCH_STATUS_SUCCESS;
92
93     parent = _xdg_dir (notmuch, xdg_var, xdg_subdir, profile);
94     if (! parent)
95         return NOTMUCH_STATUS_PATH_ERROR;
96
97     dir = talloc_asprintf (notmuch, "%s/%s", parent, subdir);
98
99     err = stat (dir, &st);
100     if (err) {
101         if (errno == ENOENT) {
102             char *notmuch_path = dirname (talloc_strdup (notmuch, notmuch->xapian_path));
103             dir = talloc_asprintf (notmuch, "%s/%s", notmuch_path, subdir);
104         } else {
105             IGNORE_RESULT (asprintf (message, "Error: Cannot stat %s: %s.\n",
106                                      dir, strerror (errno)));
107             return NOTMUCH_STATUS_FILE_ERROR;
108         }
109     }
110
111     _notmuch_config_cache (notmuch, key, dir);
112
113     return NOTMUCH_STATUS_SUCCESS;
114 }
115
116 static notmuch_status_t
117 _load_key_file (notmuch_database_t *notmuch,
118                 const char *path,
119                 const char *profile,
120                 GKeyFile **key_file)
121 {
122     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
123
124     if (path && EMPTY_STRING (path))
125         goto DONE;
126
127     if (! path)
128         path = getenv ("NOTMUCH_CONFIG");
129
130     if (path)
131         path = talloc_strdup (notmuch, path);
132     else {
133         const char *dir = _xdg_dir (notmuch, "XDG_CONFIG_HOME", ".config", profile);
134
135         if (dir) {
136             path = talloc_asprintf (notmuch, "%s/config", dir);
137             if (access (path, R_OK) != 0)
138                 path = NULL;
139         }
140     }
141
142     if (! path) {
143         const char *home = getenv ("HOME");
144
145         path = talloc_asprintf (notmuch, "%s/.notmuch-config", home);
146
147         if (! profile)
148             profile = getenv ("NOTMUCH_PROFILE");
149
150         if (profile)
151             path = talloc_asprintf (notmuch, "%s.%s", path, profile);
152     }
153
154     *key_file = g_key_file_new ();
155     if (! g_key_file_load_from_file (*key_file, path, G_KEY_FILE_NONE, NULL)) {
156         status = NOTMUCH_STATUS_NO_CONFIG;
157     }
158
159   DONE:
160     if (path)
161         notmuch->config_path = path;
162
163     return status;
164 }
165
166 static notmuch_status_t
167 _db_dir_exists (const char *database_path, char **message)
168 {
169     struct stat st;
170     int err;
171
172     err = stat (database_path, &st);
173     if (err) {
174         IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: %s.\n",
175                                  database_path, strerror (errno)));
176         return NOTMUCH_STATUS_FILE_ERROR;
177     }
178
179     if (! S_ISDIR (st.st_mode)) {
180         IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: "
181                                  "Not a directory.\n",
182                                  database_path));
183         return NOTMUCH_STATUS_FILE_ERROR;
184     }
185
186     return NOTMUCH_STATUS_SUCCESS;
187 }
188
189 static notmuch_status_t
190 _choose_database_path (void *ctx,
191                        const char *profile,
192                        GKeyFile *key_file,
193                        const char **database_path,
194                        bool *split,
195                        char **message)
196 {
197     if (! *database_path) {
198         *database_path = getenv ("NOTMUCH_DATABASE");
199     }
200
201     if (! *database_path && key_file) {
202         char *path = g_key_file_get_value (key_file, "database", "path", NULL);
203         if (path) {
204             if (path[0] == '/')
205                 *database_path = talloc_strdup (ctx, path);
206             else
207                 *database_path = talloc_asprintf (ctx, "%s/%s", getenv ("HOME"), path);
208             g_free (path);
209         }
210     }
211     if (! *database_path) {
212         notmuch_status_t status;
213
214         *database_path = _xdg_dir (ctx, "XDG_DATA_HOME", ".local/share", profile);
215         status = _db_dir_exists (*database_path, message);
216         if (status) {
217             *database_path = NULL;
218         } else {
219             *split = true;
220         }
221     }
222
223     if (! *database_path) {
224         *database_path = getenv ("MAILDIR");
225     }
226
227     if (! *database_path) {
228         notmuch_status_t status;
229
230         *database_path = talloc_asprintf (ctx, "%s/mail", getenv ("HOME"));
231         status = _db_dir_exists (*database_path, message);
232         if (status) {
233             *database_path = NULL;
234         }
235     }
236
237     if (*database_path == NULL) {
238         *message = strdup ("Error: could not locate database.\n");
239         return NOTMUCH_STATUS_NO_DATABASE;
240     }
241
242     if (*database_path[0] != '/') {
243         *message = strdup ("Error: Database path must be absolute.\n");
244         return NOTMUCH_STATUS_PATH_ERROR;
245     }
246     return NOTMUCH_STATUS_SUCCESS;
247 }
248
249 static notmuch_database_t *
250 _alloc_notmuch ()
251 {
252     notmuch_database_t *notmuch;
253
254     notmuch = talloc_zero (NULL, notmuch_database_t);
255     if (! notmuch)
256         return NULL;
257
258     notmuch->exception_reported = false;
259     notmuch->status_string = NULL;
260     notmuch->writable_xapian_db = NULL;
261     notmuch->config_path = NULL;
262     notmuch->atomic_nesting = 0;
263     notmuch->transaction_count = 0;
264     notmuch->transaction_threshold = 0;
265     notmuch->view = 1;
266     return notmuch;
267 }
268
269 static notmuch_status_t
270 _trial_open (const char *xapian_path, char **message_ptr)
271 {
272     try {
273         Xapian::Database db (xapian_path);
274     } catch (const Xapian::DatabaseOpeningError &error) {
275         IGNORE_RESULT (asprintf (message_ptr,
276                                  "Cannot open Xapian database at %s: %s\n",
277                                  xapian_path,
278                                  error.get_msg ().c_str ()));
279         return NOTMUCH_STATUS_PATH_ERROR;
280     } catch (const Xapian::Error &error) {
281         IGNORE_RESULT (asprintf (message_ptr,
282                                  "A Xapian exception occurred opening database: %s\n",
283                                  error.get_msg ().c_str ()));
284         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
285     }
286     return NOTMUCH_STATUS_SUCCESS;
287 }
288
289 notmuch_status_t
290 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
291                              const char **xapian_path, char **message_ptr)
292 {
293     notmuch_status_t status;
294     const char *trial_path, *notmuch_path;
295
296     status = _db_dir_exists (database_path, message_ptr);
297     if (status)
298         goto DONE;
299
300     trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
301     status = _trial_open (trial_path, message_ptr);
302     if (status != NOTMUCH_STATUS_PATH_ERROR)
303         goto DONE;
304
305     if (*message_ptr)
306         free (*message_ptr);
307
308     notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
309     status = _db_dir_exists (notmuch_path, message_ptr);
310     if (status)
311         goto DONE;
312
313     trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
314     status = _trial_open (trial_path, message_ptr);
315
316   DONE:
317     if (status == NOTMUCH_STATUS_SUCCESS)
318         *xapian_path = trial_path;
319     return status;
320 }
321
322 static void
323 _set_database_path (notmuch_database_t *notmuch,
324                     const char *database_path)
325 {
326     char *path = talloc_strdup (notmuch, database_path);
327
328     strip_trailing (path, '/');
329
330     _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
331 }
332
333 static void
334 _load_database_state (notmuch_database_t *notmuch)
335 {
336     std::string last_thread_id;
337     std::string last_mod;
338
339     notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
340     last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
341     if (last_thread_id.empty ()) {
342         notmuch->last_thread_id = 0;
343     } else {
344         const char *str;
345         char *end;
346
347         str = last_thread_id.c_str ();
348         notmuch->last_thread_id = strtoull (str, &end, 16);
349         if (*end != '\0')
350             INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
351     }
352
353     /* Get current highest revision number. */
354     last_mod = notmuch->xapian_db->get_value_upper_bound (
355         NOTMUCH_VALUE_LAST_MOD);
356     if (last_mod.empty ())
357         notmuch->revision = 0;
358     else
359         notmuch->revision = Xapian::sortable_unserialise (last_mod);
360     notmuch->uuid = talloc_strdup (
361         notmuch, notmuch->xapian_db->get_uuid ().c_str ());
362 }
363
364 static notmuch_status_t
365 _finish_open (notmuch_database_t *notmuch,
366               const char *profile,
367               notmuch_database_mode_t mode,
368               GKeyFile *key_file,
369               char **message_ptr)
370 {
371     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
372     char *incompat_features;
373     char *message = NULL;
374     const char *autocommit_str;
375     char *autocommit_end;
376     unsigned int version;
377     const char *database_path = notmuch_database_get_path (notmuch);
378
379     try {
380
381         if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
382             notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
383                                                                         DB_ACTION);
384             notmuch->xapian_db = notmuch->writable_xapian_db;
385         } else {
386             notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
387         }
388
389         /* Check version.  As of database version 3, we represent
390          * changes in terms of features, so assume a version bump
391          * means a dramatically incompatible change. */
392         version = notmuch_database_get_version (notmuch);
393         if (version > NOTMUCH_DATABASE_VERSION) {
394             IGNORE_RESULT (asprintf (&message,
395                                      "Error: Notmuch database at %s\n"
396                                      "       has a newer database format version (%u) than supported by this\n"
397                                      "       version of notmuch (%u).\n",
398                                      database_path, version, NOTMUCH_DATABASE_VERSION));
399             notmuch_database_destroy (notmuch);
400             notmuch = NULL;
401             status = NOTMUCH_STATUS_FILE_ERROR;
402             goto DONE;
403         }
404
405         /* Check features. */
406         incompat_features = NULL;
407         notmuch->features = _notmuch_database_parse_features (
408             notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
409             version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
410             &incompat_features);
411         if (incompat_features) {
412             IGNORE_RESULT (asprintf (&message,
413                                      "Error: Notmuch database at %s\n"
414                                      "       requires features (%s)\n"
415                                      "       not supported by this version of notmuch.\n",
416                                      database_path, incompat_features));
417             notmuch_database_destroy (notmuch);
418             notmuch = NULL;
419             status = NOTMUCH_STATUS_FILE_ERROR;
420             goto DONE;
421         }
422
423         _load_database_state (notmuch);
424
425         notmuch->query_parser = new Xapian::QueryParser;
426         notmuch->term_gen = new Xapian::TermGenerator;
427         notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
428         notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
429         notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
430                                                                      "date:");
431         notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
432                                                                               "lastmod:");
433         notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
434         notmuch->query_parser->set_database (*notmuch->xapian_db);
435         notmuch->stemmer = new Xapian::Stem ("english");
436         notmuch->query_parser->set_stemmer (*notmuch->stemmer);
437         notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
438         notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
439         notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
440         notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
441
442         /* Configuration information is needed to set up query parser */
443         status = _notmuch_config_load_from_database (notmuch);
444         if (status)
445             goto DONE;
446
447         if (key_file)
448             status = _notmuch_config_load_from_file (notmuch, key_file);
449         if (status)
450             goto DONE;
451
452         status = _choose_dir (notmuch, profile,
453                               NOTMUCH_CONFIG_HOOK_DIR,
454                               "XDG_CONFIG_HOME",
455                               ".config",
456                               "hooks",
457                               &message);
458         if (status)
459             goto DONE;
460
461         status = _choose_dir (notmuch, profile,
462                               NOTMUCH_CONFIG_BACKUP_DIR,
463                               "XDG_DATA_HOME",
464                               ".local/share",
465                               "backups",
466                               &message);
467         if (status)
468             goto DONE;
469         status = _notmuch_config_load_defaults (notmuch);
470         if (status)
471             goto DONE;
472
473         autocommit_str = notmuch_config_get (notmuch, NOTMUCH_CONFIG_AUTOCOMMIT);
474         if (unlikely (! autocommit_str)) {
475             INTERNAL_ERROR ("missing configuration for autocommit");
476         }
477         notmuch->transaction_threshold = strtoul (autocommit_str, &autocommit_end, 10);
478         if (*autocommit_end != '\0')
479             INTERNAL_ERROR ("Malformed database database.autocommit value: %s", autocommit_str);
480
481         status = _notmuch_database_setup_standard_query_fields (notmuch);
482         if (status)
483             goto DONE;
484
485         status = _notmuch_database_setup_user_query_fields (notmuch);
486         if (status)
487             goto DONE;
488
489     } catch (const Xapian::Error &error) {
490         IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
491                                  error.get_msg ().c_str ()));
492         notmuch_database_destroy (notmuch);
493         notmuch = NULL;
494         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
495     }
496   DONE:
497     if (message_ptr)
498         *message_ptr = message;
499     return status;
500 }
501
502 notmuch_status_t
503 notmuch_database_open_with_config (const char *database_path,
504                                    notmuch_database_mode_t mode,
505                                    const char *config_path,
506                                    const char *profile,
507                                    notmuch_database_t **database,
508                                    char **status_string)
509 {
510     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
511     void *local = talloc_new (NULL);
512     notmuch_database_t *notmuch = NULL;
513     char *message = NULL;
514     GKeyFile *key_file = NULL;
515     bool split = false;
516
517     _notmuch_init ();
518
519     notmuch = _alloc_notmuch ();
520     if (! notmuch) {
521         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
522         goto DONE;
523     }
524
525     status = _load_key_file (notmuch, config_path, profile, &key_file);
526     if (status) {
527         message = strdup ("Error: cannot load config file.\n");
528         goto DONE;
529     }
530
531     if ((status = _choose_database_path (local, profile, key_file,
532                                          &database_path, &split,
533                                          &message)))
534         goto DONE;
535
536     status = _db_dir_exists (database_path, &message);
537     if (status)
538         goto DONE;
539
540     _set_database_path (notmuch, database_path);
541
542     status = _notmuch_choose_xapian_path (notmuch, database_path,
543                                           &notmuch->xapian_path, &message);
544     if (status)
545         goto DONE;
546
547     status = _finish_open (notmuch, profile, mode, key_file, &message);
548
549   DONE:
550     talloc_free (local);
551
552     if (key_file)
553         g_key_file_free (key_file);
554
555     if (message) {
556         if (status_string)
557             *status_string = message;
558         else
559             free (message);
560     }
561
562     if (database)
563         *database = notmuch;
564     else
565         talloc_free (notmuch);
566
567     if (notmuch)
568         notmuch->open = true;
569
570     return status;
571 }
572
573 notmuch_status_t
574 notmuch_database_create (const char *path, notmuch_database_t **database)
575 {
576     char *status_string = NULL;
577     notmuch_status_t status;
578
579     status = notmuch_database_create_verbose (path, database,
580                                               &status_string);
581
582     if (status_string) {
583         fputs (status_string, stderr);
584         free (status_string);
585     }
586
587     return status;
588 }
589
590 notmuch_status_t
591 notmuch_database_create_verbose (const char *path,
592                                  notmuch_database_t **database,
593                                  char **status_string)
594 {
595     return notmuch_database_create_with_config (path, "", NULL, database, status_string);
596 }
597
598 notmuch_status_t
599 notmuch_database_create_with_config (const char *database_path,
600                                      const char *config_path,
601                                      const char *profile,
602                                      notmuch_database_t **database,
603                                      char **status_string)
604 {
605     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
606     notmuch_database_t *notmuch = NULL;
607     const char *notmuch_path = NULL;
608     char *message = NULL;
609     GKeyFile *key_file = NULL;
610     void *local = talloc_new (NULL);
611     int err;
612     bool split = false;
613
614     _notmuch_init ();
615
616     notmuch = _alloc_notmuch ();
617     if (! notmuch) {
618         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
619         goto DONE;
620     }
621
622     status = _load_key_file (notmuch, config_path, profile, &key_file);
623     if (status) {
624         message = strdup ("Error: cannot load config file.\n");
625         goto DONE;
626     }
627
628     if ((status = _choose_database_path (local, profile, key_file,
629                                          &database_path, &split, &message)))
630         goto DONE;
631
632     status = _db_dir_exists (database_path, &message);
633     if (status)
634         goto DONE;
635
636     _set_database_path (notmuch, database_path);
637
638     if (key_file && ! split) {
639         char *mail_root = notmuch_canonicalize_file_name (
640             g_key_file_get_value (key_file, "database", "mail_root", NULL));
641         char *db_path = notmuch_canonicalize_file_name (database_path);
642
643         split = (mail_root && (0 != strcmp (mail_root, db_path)));
644
645         free (mail_root);
646         free (db_path);
647     }
648
649     if (split) {
650         notmuch_path = database_path;
651     } else {
652         if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
653             status = NOTMUCH_STATUS_OUT_OF_MEMORY;
654             goto DONE;
655         }
656
657         err = mkdir (notmuch_path, 0755);
658         if (err) {
659             if (errno == EEXIST) {
660                 status = NOTMUCH_STATUS_DATABASE_EXISTS;
661                 talloc_free (notmuch);
662                 notmuch = NULL;
663             } else {
664                 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
665                                          notmuch_path, strerror (errno)));
666                 status = NOTMUCH_STATUS_FILE_ERROR;
667             }
668             goto DONE;
669         }
670     }
671
672     if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
673         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
674         goto DONE;
675     }
676
677     status = _trial_open (notmuch->xapian_path, &message);
678     if (status == NOTMUCH_STATUS_SUCCESS) {
679         notmuch_database_destroy (notmuch);
680         notmuch = NULL;
681         status = NOTMUCH_STATUS_DATABASE_EXISTS;
682         goto DONE;
683     }
684
685     if (message)
686         free (message);
687
688     status = _finish_open (notmuch,
689                            profile,
690                            NOTMUCH_DATABASE_MODE_READ_WRITE,
691                            key_file,
692                            &message);
693     if (status)
694         goto DONE;
695
696     /* Upgrade doesn't add these feature to existing databases, but
697      * new databases have them. */
698     notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
699     notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
700     notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
701
702     status = notmuch_database_upgrade (notmuch, NULL, NULL);
703     if (status) {
704         notmuch_database_close (notmuch);
705         notmuch = NULL;
706     }
707
708   DONE:
709     talloc_free (local);
710
711     if (key_file)
712         g_key_file_free (key_file);
713
714     if (message) {
715         if (status_string)
716             *status_string = message;
717         else
718             free (message);
719     }
720     if (database)
721         *database = notmuch;
722     else
723         talloc_free (notmuch);
724     return status;
725 }
726
727 notmuch_status_t
728 notmuch_database_reopen (notmuch_database_t *notmuch,
729                          notmuch_database_mode_t new_mode)
730 {
731     notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
732
733     if (notmuch->xapian_db == NULL) {
734         _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
735         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
736     }
737
738     try {
739         if (cur_mode == new_mode &&
740             new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
741             notmuch->xapian_db->reopen ();
742         } else {
743             notmuch->xapian_db->close ();
744
745             delete notmuch->xapian_db;
746             notmuch->xapian_db = NULL;
747             /* no need to free the same object twice */
748             notmuch->writable_xapian_db = NULL;
749
750             if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
751                 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
752                                                                             DB_ACTION);
753                 notmuch->xapian_db = notmuch->writable_xapian_db;
754             } else {
755                 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
756                                                            DB_ACTION);
757             }
758         }
759
760         _load_database_state (notmuch);
761     } catch (const Xapian::Error &error) {
762         if (! notmuch->exception_reported) {
763             _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
764                                    error.get_msg ().c_str ());
765             notmuch->exception_reported = true;
766         }
767         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
768     }
769
770     notmuch->view++;
771     notmuch->open = true;
772     return NOTMUCH_STATUS_SUCCESS;
773 }
774
775 static notmuch_status_t
776 _maybe_load_config_from_database (notmuch_database_t *notmuch,
777                                   GKeyFile *key_file,
778                                   const char *database_path,
779                                   const char *profile)
780 {
781     char *message; /* ignored */
782
783     if (_db_dir_exists (database_path, &message))
784         return NOTMUCH_STATUS_NO_DATABASE;
785
786     _set_database_path (notmuch, database_path);
787
788     if (_notmuch_choose_xapian_path (notmuch, database_path, &notmuch->xapian_path, &message))
789         return NOTMUCH_STATUS_NO_DATABASE;
790
791     (void) _finish_open (notmuch, profile, NOTMUCH_DATABASE_MODE_READ_ONLY, key_file, &message);
792
793     return NOTMUCH_STATUS_SUCCESS;
794 }
795
796 notmuch_status_t
797 notmuch_database_load_config (const char *database_path,
798                               const char *config_path,
799                               const char *profile,
800                               notmuch_database_t **database,
801                               char **status_string)
802 {
803     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS, warning = NOTMUCH_STATUS_SUCCESS;
804     void *local = talloc_new (NULL);
805     notmuch_database_t *notmuch = NULL;
806     char *message = NULL;
807     GKeyFile *key_file = NULL;
808     bool split = false;
809
810     _notmuch_init ();
811
812     notmuch = _alloc_notmuch ();
813     if (! notmuch) {
814         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
815         goto DONE;
816     }
817
818     status = _load_key_file (notmuch, config_path, profile, &key_file);
819     switch (status) {
820     case NOTMUCH_STATUS_SUCCESS:
821         break;
822     case NOTMUCH_STATUS_NO_CONFIG:
823         warning = status;
824         break;
825     default:
826         message = strdup ("Error: cannot load config file.\n");
827         goto DONE;
828     }
829
830     status = _choose_database_path (local, profile, key_file,
831                                     &database_path, &split, &message);
832     switch (status) {
833     case NOTMUCH_STATUS_NO_DATABASE:
834     case NOTMUCH_STATUS_SUCCESS:
835         if (! warning)
836             warning = status;
837         break;
838     default:
839         goto DONE;
840     }
841
842
843     if (database_path) {
844         status = _maybe_load_config_from_database (notmuch, key_file, database_path, profile);
845         switch (status) {
846         case NOTMUCH_STATUS_NO_DATABASE:
847         case NOTMUCH_STATUS_SUCCESS:
848             if (! warning)
849                 warning = status;
850             break;
851         default:
852             goto DONE;
853         }
854     }
855
856     if (key_file) {
857         status = _notmuch_config_load_from_file (notmuch, key_file);
858         if (status)
859             goto DONE;
860     }
861     status = _notmuch_config_load_defaults (notmuch);
862     if (status)
863         goto DONE;
864
865   DONE:
866     talloc_free (local);
867
868     if (status_string)
869         *status_string = message;
870
871     if (database)
872         *database = notmuch;
873
874     if (status)
875         return status;
876     else
877         return warning;
878 }