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