]> git.cworth.org Git - notmuch/blob - lib/open.cc
a942383b84b8cda800ae8006034555e267d11119
[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 (const char *database_path, const char *config_path, const char *profile)
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
267     notmuch->params = NOTMUCH_PARAM_NONE;
268     if (database_path)
269         notmuch->params |= NOTMUCH_PARAM_DATABASE;
270     if (config_path)
271         notmuch->params |= NOTMUCH_PARAM_CONFIG;
272     if (profile)
273         notmuch->params |= NOTMUCH_PARAM_PROFILE;
274
275     return notmuch;
276 }
277
278 static notmuch_status_t
279 _trial_open (const char *xapian_path, char **message_ptr)
280 {
281     try {
282         Xapian::Database db (xapian_path);
283     } catch (const Xapian::DatabaseOpeningError &error) {
284         IGNORE_RESULT (asprintf (message_ptr,
285                                  "Cannot open Xapian database at %s: %s\n",
286                                  xapian_path,
287                                  error.get_msg ().c_str ()));
288         return NOTMUCH_STATUS_PATH_ERROR;
289     } catch (const Xapian::Error &error) {
290         IGNORE_RESULT (asprintf (message_ptr,
291                                  "A Xapian exception occurred opening database: %s\n",
292                                  error.get_msg ().c_str ()));
293         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
294     }
295     return NOTMUCH_STATUS_SUCCESS;
296 }
297
298 notmuch_status_t
299 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
300                              const char **xapian_path, char **message_ptr)
301 {
302     notmuch_status_t status;
303     const char *trial_path, *notmuch_path;
304
305     status = _db_dir_exists (database_path, message_ptr);
306     if (status)
307         goto DONE;
308
309     trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
310     status = _trial_open (trial_path, message_ptr);
311     if (status != NOTMUCH_STATUS_PATH_ERROR)
312         goto DONE;
313
314     if (*message_ptr)
315         free (*message_ptr);
316
317     notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
318     status = _db_dir_exists (notmuch_path, message_ptr);
319     if (status)
320         goto DONE;
321
322     trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
323     status = _trial_open (trial_path, message_ptr);
324
325   DONE:
326     if (status == NOTMUCH_STATUS_SUCCESS)
327         *xapian_path = trial_path;
328     return status;
329 }
330
331 static void
332 _set_database_path (notmuch_database_t *notmuch,
333                     const char *database_path)
334 {
335     char *path = talloc_strdup (notmuch, database_path);
336
337     strip_trailing (path, '/');
338
339     _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
340 }
341
342 static void
343 _load_database_state (notmuch_database_t *notmuch)
344 {
345     std::string last_thread_id;
346     std::string last_mod;
347
348     notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
349     last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
350     if (last_thread_id.empty ()) {
351         notmuch->last_thread_id = 0;
352     } else {
353         const char *str;
354         char *end;
355
356         str = last_thread_id.c_str ();
357         notmuch->last_thread_id = strtoull (str, &end, 16);
358         if (*end != '\0')
359             INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
360     }
361
362     /* Get current highest revision number. */
363     last_mod = notmuch->xapian_db->get_value_upper_bound (
364         NOTMUCH_VALUE_LAST_MOD);
365     if (last_mod.empty ())
366         notmuch->revision = 0;
367     else
368         notmuch->revision = Xapian::sortable_unserialise (last_mod);
369     notmuch->uuid = talloc_strdup (
370         notmuch, notmuch->xapian_db->get_uuid ().c_str ());
371 }
372
373 static notmuch_status_t
374 _finish_open (notmuch_database_t *notmuch,
375               const char *profile,
376               notmuch_database_mode_t mode,
377               GKeyFile *key_file,
378               char **message_ptr)
379 {
380     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
381     char *incompat_features;
382     char *message = NULL;
383     const char *autocommit_str;
384     char *autocommit_end;
385     unsigned int version;
386     const char *database_path = notmuch_database_get_path (notmuch);
387
388     try {
389
390         if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
391             notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
392                                                                         DB_ACTION);
393             notmuch->xapian_db = notmuch->writable_xapian_db;
394         } else {
395             notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
396         }
397
398         /* Check version.  As of database version 3, we represent
399          * changes in terms of features, so assume a version bump
400          * means a dramatically incompatible change. */
401         version = notmuch_database_get_version (notmuch);
402         if (version > NOTMUCH_DATABASE_VERSION) {
403             IGNORE_RESULT (asprintf (&message,
404                                      "Error: Notmuch database at %s\n"
405                                      "       has a newer database format version (%u) than supported by this\n"
406                                      "       version of notmuch (%u).\n",
407                                      database_path, version, NOTMUCH_DATABASE_VERSION));
408             status = NOTMUCH_STATUS_FILE_ERROR;
409             goto DONE;
410         }
411
412         /* Check features. */
413         incompat_features = NULL;
414         notmuch->features = _notmuch_database_parse_features (
415             notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
416             version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
417             &incompat_features);
418         if (incompat_features) {
419             IGNORE_RESULT (asprintf (&message,
420                                      "Error: Notmuch database at %s\n"
421                                      "       requires features (%s)\n"
422                                      "       not supported by this version of notmuch.\n",
423                                      database_path, incompat_features));
424             status = NOTMUCH_STATUS_FILE_ERROR;
425             goto DONE;
426         }
427
428         _load_database_state (notmuch);
429
430         notmuch->query_parser = new Xapian::QueryParser;
431         notmuch->term_gen = new Xapian::TermGenerator;
432         notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
433         notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
434         notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
435                                                                      "date:");
436         notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
437                                                                               "lastmod:");
438         notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
439         notmuch->query_parser->set_database (*notmuch->xapian_db);
440         notmuch->stemmer = new Xapian::Stem ("english");
441         notmuch->query_parser->set_stemmer (*notmuch->stemmer);
442         notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
443         notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
444         notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
445         notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
446
447         /* Configuration information is needed to set up query parser */
448         status = _notmuch_config_load_from_database (notmuch);
449         if (status)
450             goto DONE;
451
452         if (key_file)
453             status = _notmuch_config_load_from_file (notmuch, key_file);
454         if (status)
455             goto DONE;
456
457         status = _choose_dir (notmuch, profile,
458                               NOTMUCH_CONFIG_HOOK_DIR,
459                               "XDG_CONFIG_HOME",
460                               ".config",
461                               "hooks",
462                               &message);
463         if (status)
464             goto DONE;
465
466         status = _choose_dir (notmuch, profile,
467                               NOTMUCH_CONFIG_BACKUP_DIR,
468                               "XDG_DATA_HOME",
469                               ".local/share",
470                               "backups",
471                               &message);
472         if (status)
473             goto DONE;
474         status = _notmuch_config_load_defaults (notmuch);
475         if (status)
476             goto DONE;
477
478         autocommit_str = notmuch_config_get (notmuch, NOTMUCH_CONFIG_AUTOCOMMIT);
479         if (unlikely (! autocommit_str)) {
480             INTERNAL_ERROR ("missing configuration for autocommit");
481         }
482         notmuch->transaction_threshold = strtoul (autocommit_str, &autocommit_end, 10);
483         if (*autocommit_end != '\0')
484             INTERNAL_ERROR ("Malformed database database.autocommit value: %s", autocommit_str);
485
486         status = _notmuch_database_setup_standard_query_fields (notmuch);
487         if (status)
488             goto DONE;
489
490         status = _notmuch_database_setup_user_query_fields (notmuch);
491         if (status)
492             goto DONE;
493
494     } catch (const Xapian::Error &error) {
495         IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
496                                  error.get_msg ().c_str ()));
497         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
498     }
499   DONE:
500     if (message_ptr)
501         *message_ptr = message;
502     return status;
503 }
504
505 notmuch_status_t
506 notmuch_database_open_with_config (const char *database_path,
507                                    notmuch_database_mode_t mode,
508                                    const char *config_path,
509                                    const char *profile,
510                                    notmuch_database_t **database,
511                                    char **status_string)
512 {
513     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
514     void *local = talloc_new (NULL);
515     notmuch_database_t *notmuch = NULL;
516     char *message = NULL;
517     GKeyFile *key_file = NULL;
518     bool split = false;
519
520     _notmuch_init ();
521
522     notmuch = _alloc_notmuch (database_path, config_path, profile);
523     if (! notmuch) {
524         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
525         goto DONE;
526     }
527
528     status = _load_key_file (notmuch, config_path, profile, &key_file);
529     if (status) {
530         message = strdup ("Error: cannot load config file.\n");
531         goto DONE;
532     }
533
534     if ((status = _choose_database_path (local, profile, key_file,
535                                          &database_path, &split,
536                                          &message)))
537         goto DONE;
538
539     status = _db_dir_exists (database_path, &message);
540     if (status)
541         goto DONE;
542
543     _set_database_path (notmuch, database_path);
544
545     status = _notmuch_choose_xapian_path (notmuch, database_path,
546                                           &notmuch->xapian_path, &message);
547     if (status)
548         goto DONE;
549
550     status = _finish_open (notmuch, profile, mode, key_file, &message);
551
552   DONE:
553     talloc_free (local);
554
555     if (key_file)
556         g_key_file_free (key_file);
557
558     if (message) {
559         if (status_string)
560             *status_string = message;
561         else
562             free (message);
563     }
564
565     if (status && notmuch) {
566         notmuch_database_destroy (notmuch);
567         notmuch = NULL;
568     }
569
570     if (database)
571         *database = notmuch;
572
573     if (notmuch)
574         notmuch->open = true;
575
576     return status;
577 }
578
579 notmuch_status_t
580 notmuch_database_create (const char *path, notmuch_database_t **database)
581 {
582     char *status_string = NULL;
583     notmuch_status_t status;
584
585     status = notmuch_database_create_verbose (path, database,
586                                               &status_string);
587
588     if (status_string) {
589         fputs (status_string, stderr);
590         free (status_string);
591     }
592
593     return status;
594 }
595
596 notmuch_status_t
597 notmuch_database_create_verbose (const char *path,
598                                  notmuch_database_t **database,
599                                  char **status_string)
600 {
601     return notmuch_database_create_with_config (path, "", NULL, database, status_string);
602 }
603
604 notmuch_status_t
605 notmuch_database_create_with_config (const char *database_path,
606                                      const char *config_path,
607                                      const char *profile,
608                                      notmuch_database_t **database,
609                                      char **status_string)
610 {
611     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
612     notmuch_database_t *notmuch = NULL;
613     const char *notmuch_path = NULL;
614     char *message = NULL;
615     GKeyFile *key_file = NULL;
616     void *local = talloc_new (NULL);
617     int err;
618     bool split = false;
619
620     _notmuch_init ();
621
622     notmuch = _alloc_notmuch (database_path, config_path, profile);
623     if (! notmuch) {
624         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
625         goto DONE;
626     }
627
628     status = _load_key_file (notmuch, config_path, profile, &key_file);
629     if (status) {
630         message = strdup ("Error: cannot load config file.\n");
631         goto DONE;
632     }
633
634     if ((status = _choose_database_path (local, profile, key_file,
635                                          &database_path, &split, &message)))
636         goto DONE;
637
638     status = _db_dir_exists (database_path, &message);
639     if (status)
640         goto DONE;
641
642     _set_database_path (notmuch, database_path);
643
644     if (key_file && ! split) {
645         char *mail_root = notmuch_canonicalize_file_name (
646             g_key_file_get_value (key_file, "database", "mail_root", NULL));
647         char *db_path = notmuch_canonicalize_file_name (database_path);
648
649         split = (mail_root && (0 != strcmp (mail_root, db_path)));
650
651         free (mail_root);
652         free (db_path);
653     }
654
655     if (split) {
656         notmuch_path = database_path;
657     } else {
658         if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
659             status = NOTMUCH_STATUS_OUT_OF_MEMORY;
660             goto DONE;
661         }
662
663         err = mkdir (notmuch_path, 0755);
664         if (err) {
665             if (errno == EEXIST) {
666                 status = NOTMUCH_STATUS_DATABASE_EXISTS;
667                 talloc_free (notmuch);
668                 notmuch = NULL;
669             } else {
670                 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
671                                          notmuch_path, strerror (errno)));
672                 status = NOTMUCH_STATUS_FILE_ERROR;
673             }
674             goto DONE;
675         }
676     }
677
678     if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
679         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
680         goto DONE;
681     }
682
683     status = _trial_open (notmuch->xapian_path, &message);
684     if (status == NOTMUCH_STATUS_SUCCESS) {
685         notmuch_database_destroy (notmuch);
686         notmuch = NULL;
687         status = NOTMUCH_STATUS_DATABASE_EXISTS;
688         goto DONE;
689     }
690
691     if (message)
692         free (message);
693
694     status = _finish_open (notmuch,
695                            profile,
696                            NOTMUCH_DATABASE_MODE_READ_WRITE,
697                            key_file,
698                            &message);
699     if (status)
700         goto DONE;
701
702     /* Upgrade doesn't add these feature to existing databases, but
703      * new databases have them. */
704     notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
705     notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
706     notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
707
708     status = notmuch_database_upgrade (notmuch, NULL, NULL);
709     if (status) {
710         notmuch_database_close (notmuch);
711         notmuch = NULL;
712     }
713
714   DONE:
715     talloc_free (local);
716
717     if (key_file)
718         g_key_file_free (key_file);
719
720     if (message) {
721         if (status_string)
722             *status_string = message;
723         else
724             free (message);
725     }
726     if (status && notmuch) {
727         notmuch_database_destroy (notmuch);
728         notmuch = NULL;
729     }
730
731     if (database)
732         *database = notmuch;
733
734     if (notmuch)
735         notmuch->open = true;
736     return status;
737 }
738
739 notmuch_status_t
740 notmuch_database_reopen (notmuch_database_t *notmuch,
741                          notmuch_database_mode_t new_mode)
742 {
743     notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
744
745     if (notmuch->xapian_db == NULL) {
746         _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
747         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
748     }
749
750     try {
751         if (cur_mode == new_mode &&
752             new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
753             notmuch->xapian_db->reopen ();
754         } else {
755             notmuch->xapian_db->close ();
756
757             delete notmuch->xapian_db;
758             notmuch->xapian_db = NULL;
759             /* no need to free the same object twice */
760             notmuch->writable_xapian_db = NULL;
761
762             if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
763                 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
764                                                                             DB_ACTION);
765                 notmuch->xapian_db = notmuch->writable_xapian_db;
766             } else {
767                 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
768                                                            DB_ACTION);
769             }
770         }
771
772         _load_database_state (notmuch);
773     } catch (const Xapian::Error &error) {
774         if (! notmuch->exception_reported) {
775             _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
776                                    error.get_msg ().c_str ());
777             notmuch->exception_reported = true;
778         }
779         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
780     }
781
782     notmuch->view++;
783     notmuch->open = true;
784     return NOTMUCH_STATUS_SUCCESS;
785 }
786
787 static notmuch_status_t
788 _maybe_load_config_from_database (notmuch_database_t *notmuch,
789                                   GKeyFile *key_file,
790                                   const char *database_path,
791                                   const char *profile)
792 {
793     char *message; /* ignored */
794
795     if (_db_dir_exists (database_path, &message))
796         return NOTMUCH_STATUS_NO_DATABASE;
797
798     _set_database_path (notmuch, database_path);
799
800     if (_notmuch_choose_xapian_path (notmuch, database_path, &notmuch->xapian_path, &message))
801         return NOTMUCH_STATUS_NO_DATABASE;
802
803     (void) _finish_open (notmuch, profile, NOTMUCH_DATABASE_MODE_READ_ONLY, key_file, &message);
804
805     return NOTMUCH_STATUS_SUCCESS;
806 }
807
808 notmuch_status_t
809 notmuch_database_load_config (const char *database_path,
810                               const char *config_path,
811                               const char *profile,
812                               notmuch_database_t **database,
813                               char **status_string)
814 {
815     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS, warning = NOTMUCH_STATUS_SUCCESS;
816     void *local = talloc_new (NULL);
817     notmuch_database_t *notmuch = NULL;
818     char *message = NULL;
819     GKeyFile *key_file = NULL;
820     bool split = false;
821
822     _notmuch_init ();
823
824     notmuch = _alloc_notmuch (database_path, config_path, profile);
825     if (! notmuch) {
826         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
827         goto DONE;
828     }
829
830     status = _load_key_file (notmuch, config_path, profile, &key_file);
831     switch (status) {
832     case NOTMUCH_STATUS_SUCCESS:
833         break;
834     case NOTMUCH_STATUS_NO_CONFIG:
835         warning = status;
836         break;
837     default:
838         message = strdup ("Error: cannot load config file.\n");
839         goto DONE;
840     }
841
842     status = _choose_database_path (local, profile, key_file,
843                                     &database_path, &split, &message);
844     switch (status) {
845     case NOTMUCH_STATUS_NO_DATABASE:
846     case NOTMUCH_STATUS_SUCCESS:
847         if (! warning)
848             warning = status;
849         break;
850     default:
851         goto DONE;
852     }
853
854
855     if (database_path) {
856         status = _maybe_load_config_from_database (notmuch, key_file, database_path, profile);
857         switch (status) {
858         case NOTMUCH_STATUS_NO_DATABASE:
859         case NOTMUCH_STATUS_SUCCESS:
860             if (! warning)
861                 warning = status;
862             break;
863         default:
864             goto DONE;
865         }
866     }
867
868     if (key_file) {
869         status = _notmuch_config_load_from_file (notmuch, key_file);
870         if (status)
871             goto DONE;
872     }
873     status = _notmuch_config_load_defaults (notmuch);
874     if (status)
875         goto DONE;
876
877   DONE:
878     talloc_free (local);
879
880     if (status_string)
881         *status_string = message;
882
883     if (status &&
884         status != NOTMUCH_STATUS_NO_DATABASE
885         && status != NOTMUCH_STATUS_NO_CONFIG) {
886         notmuch_database_destroy (notmuch);
887         notmuch = NULL;
888     }
889
890     if (database)
891         *database = notmuch;
892
893     if (status)
894         return status;
895     else
896         return warning;
897 }