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