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