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