4 #include "database-private.h"
5 #include "parse-time-vrp.h"
8 #if HAVE_XAPIAN_DB_RETRY_LOCK
9 #define DB_ACTION (Xapian::DB_CREATE_OR_OPEN | Xapian::DB_RETRY_LOCK)
11 #define DB_ACTION Xapian::DB_CREATE_OR_OPEN
15 notmuch_database_open (const char *path,
16 notmuch_database_mode_t mode,
17 notmuch_database_t **database)
19 char *status_string = NULL;
20 notmuch_status_t status;
22 status = notmuch_database_open_verbose (path, mode, database,
26 fputs (status_string, stderr);
34 notmuch_database_open_verbose (const char *path,
35 notmuch_database_mode_t mode,
36 notmuch_database_t **database,
39 return notmuch_database_open_with_config (path, mode, "", NULL,
40 database, status_string);
45 const char *xdg_root_variable,
46 const char *xdg_prefix,
47 const char *profile_name)
49 const char *xdg_root = getenv (xdg_root_variable);
52 const char *home = getenv ("HOME");
54 if (! home) return NULL;
56 xdg_root = talloc_asprintf (ctx,
63 profile_name = getenv ("NOTMUCH_PROFILE");
66 profile_name = "default";
68 return talloc_asprintf (ctx,
74 static notmuch_status_t
75 _choose_dir (notmuch_database_t *notmuch,
77 notmuch_config_key_t key,
79 const char *xdg_subdir,
81 char **message = NULL)
88 dir = notmuch_config_get (notmuch, key);
91 return NOTMUCH_STATUS_SUCCESS;
93 parent = _xdg_dir (notmuch, xdg_var, xdg_subdir, profile);
95 return NOTMUCH_STATUS_PATH_ERROR;
97 dir = talloc_asprintf (notmuch, "%s/%s", parent, subdir);
99 err = stat (dir, &st);
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);
105 IGNORE_RESULT (asprintf (message, "Error: Cannot stat %s: %s.\n",
106 dir, strerror (errno)));
107 return NOTMUCH_STATUS_FILE_ERROR;
111 _notmuch_config_cache (notmuch, key, dir);
113 return NOTMUCH_STATUS_SUCCESS;
116 static notmuch_status_t
117 _load_key_file (notmuch_database_t *notmuch,
122 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
124 if (path && EMPTY_STRING (path))
128 path = getenv ("NOTMUCH_CONFIG");
131 path = talloc_strdup (notmuch, path);
133 const char *dir = _xdg_dir (notmuch, "XDG_CONFIG_HOME", ".config", profile);
136 path = talloc_asprintf (notmuch, "%s/config", dir);
137 if (access (path, R_OK) != 0)
143 const char *home = getenv ("HOME");
145 path = talloc_asprintf (notmuch, "%s/.notmuch-config", home);
148 profile = getenv ("NOTMUCH_PROFILE");
151 path = talloc_asprintf (notmuch, "%s.%s", path, profile);
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;
161 notmuch->config_path = path;
166 static notmuch_status_t
167 _db_dir_exists (const char *database_path, char **message)
172 err = stat (database_path, &st);
174 IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: %s.\n",
175 database_path, strerror (errno)));
176 return NOTMUCH_STATUS_FILE_ERROR;
179 if (! S_ISDIR (st.st_mode)) {
180 IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: "
181 "Not a directory.\n",
183 return NOTMUCH_STATUS_FILE_ERROR;
186 return NOTMUCH_STATUS_SUCCESS;
189 static notmuch_status_t
190 _choose_database_path (void *ctx,
193 const char **database_path,
197 if (! *database_path) {
198 *database_path = getenv ("NOTMUCH_DATABASE");
201 if (! *database_path && key_file) {
202 char *path = g_key_file_get_value (key_file, "database", "path", NULL);
205 *database_path = talloc_strdup (ctx, path);
207 *database_path = talloc_asprintf (ctx, "%s/%s", getenv ("HOME"), path);
211 if (! *database_path) {
212 *database_path = _xdg_dir (ctx, "XDG_DATA_HOME", ".local/share", profile);
216 if (*database_path == NULL) {
217 *message = strdup ("Error: could not locate database.\n");
218 return NOTMUCH_STATUS_NO_DATABASE;
221 if (*database_path[0] != '/') {
222 *message = strdup ("Error: Database path must be absolute.\n");
223 return NOTMUCH_STATUS_PATH_ERROR;
225 return NOTMUCH_STATUS_SUCCESS;
231 notmuch_database_t *notmuch;
233 notmuch = talloc_zero (NULL, notmuch_database_t);
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;
246 static notmuch_status_t
247 _trial_open (const char *xapian_path, char **message_ptr)
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",
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;
263 return NOTMUCH_STATUS_SUCCESS;
267 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
268 const char **xapian_path, char **message_ptr)
270 notmuch_status_t status;
271 const char *trial_path, *notmuch_path;
273 status = _db_dir_exists (database_path, message_ptr);
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)
285 notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
286 status = _db_dir_exists (notmuch_path, message_ptr);
290 trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
291 status = _trial_open (trial_path, message_ptr);
294 if (status == NOTMUCH_STATUS_SUCCESS)
295 *xapian_path = trial_path;
300 _set_database_path (notmuch_database_t *notmuch,
301 const char *database_path)
303 char *path = talloc_strdup (notmuch, database_path);
305 strip_trailing (path, '/');
307 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
314 static int initialized = 0;
316 /* Initialize the GLib type system and threads */
317 #if ! GLIB_CHECK_VERSION (2, 35, 1)
321 /* Initialize gmime */
329 _load_database_state (notmuch_database_t *notmuch)
331 std::string last_thread_id;
332 std::string last_mod;
334 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
335 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
336 if (last_thread_id.empty ()) {
337 notmuch->last_thread_id = 0;
342 str = last_thread_id.c_str ();
343 notmuch->last_thread_id = strtoull (str, &end, 16);
345 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
348 /* Get current highest revision number. */
349 last_mod = notmuch->xapian_db->get_value_upper_bound (
350 NOTMUCH_VALUE_LAST_MOD);
351 if (last_mod.empty ())
352 notmuch->revision = 0;
354 notmuch->revision = Xapian::sortable_unserialise (last_mod);
355 notmuch->uuid = talloc_strdup (
356 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
359 static notmuch_status_t
360 _finish_open (notmuch_database_t *notmuch,
362 notmuch_database_mode_t mode,
366 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
367 char *incompat_features;
368 char *message = NULL;
369 unsigned int version;
370 const char *database_path = notmuch_database_get_path (notmuch);
374 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
375 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
377 notmuch->xapian_db = notmuch->writable_xapian_db;
379 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
382 /* Check version. As of database version 3, we represent
383 * changes in terms of features, so assume a version bump
384 * means a dramatically incompatible change. */
385 version = notmuch_database_get_version (notmuch);
386 if (version > NOTMUCH_DATABASE_VERSION) {
387 IGNORE_RESULT (asprintf (&message,
388 "Error: Notmuch database at %s\n"
389 " has a newer database format version (%u) than supported by this\n"
390 " version of notmuch (%u).\n",
391 database_path, version, NOTMUCH_DATABASE_VERSION));
392 notmuch_database_destroy (notmuch);
394 status = NOTMUCH_STATUS_FILE_ERROR;
398 /* Check features. */
399 incompat_features = NULL;
400 notmuch->features = _notmuch_database_parse_features (
401 notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
402 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
404 if (incompat_features) {
405 IGNORE_RESULT (asprintf (&message,
406 "Error: Notmuch database at %s\n"
407 " requires features (%s)\n"
408 " not supported by this version of notmuch.\n",
409 database_path, incompat_features));
410 notmuch_database_destroy (notmuch);
412 status = NOTMUCH_STATUS_FILE_ERROR;
416 _load_database_state (notmuch);
418 notmuch->query_parser = new Xapian::QueryParser;
419 notmuch->term_gen = new Xapian::TermGenerator;
420 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
421 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
422 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
424 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
426 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
427 notmuch->query_parser->set_database (*notmuch->xapian_db);
428 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
429 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
430 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
431 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
432 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
434 /* Configuration information is needed to set up query parser */
435 status = _notmuch_config_load_from_database (notmuch);
440 status = _notmuch_config_load_from_file (notmuch, key_file);
444 status = _choose_dir (notmuch, profile,
445 NOTMUCH_CONFIG_HOOK_DIR,
453 status = _choose_dir (notmuch, profile,
454 NOTMUCH_CONFIG_BACKUP_DIR,
461 status = _notmuch_config_load_defaults (notmuch);
465 status = _notmuch_database_setup_standard_query_fields (notmuch);
469 status = _notmuch_database_setup_user_query_fields (notmuch);
473 } catch (const Xapian::Error &error) {
474 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
475 error.get_msg ().c_str ()));
476 notmuch_database_destroy (notmuch);
478 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
482 *message_ptr = message;
487 notmuch_database_open_with_config (const char *database_path,
488 notmuch_database_mode_t mode,
489 const char *config_path,
491 notmuch_database_t **database,
492 char **status_string)
494 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
495 void *local = talloc_new (NULL);
496 notmuch_database_t *notmuch = NULL;
497 char *message = NULL;
498 GKeyFile *key_file = NULL;
503 notmuch = _alloc_notmuch ();
505 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
509 status = _load_key_file (notmuch, config_path, profile, &key_file);
511 message = strdup ("Error: cannot load config file.\n");
515 if ((status = _choose_database_path (local, profile, key_file,
516 &database_path, &split,
520 status = _db_dir_exists (database_path, &message);
524 _set_database_path (notmuch, database_path);
526 status = _notmuch_choose_xapian_path (notmuch, database_path,
527 ¬much->xapian_path, &message);
531 status = _finish_open (notmuch, profile, mode, key_file, &message);
537 g_key_file_free (key_file);
541 *status_string = message;
549 talloc_free (notmuch);
552 notmuch->open = true;
558 notmuch_database_create (const char *path, notmuch_database_t **database)
560 char *status_string = NULL;
561 notmuch_status_t status;
563 status = notmuch_database_create_verbose (path, database,
567 fputs (status_string, stderr);
568 free (status_string);
575 notmuch_database_create_verbose (const char *path,
576 notmuch_database_t **database,
577 char **status_string)
579 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
583 notmuch_database_create_with_config (const char *database_path,
584 const char *config_path,
586 notmuch_database_t **database,
587 char **status_string)
589 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
590 notmuch_database_t *notmuch = NULL;
591 const char *notmuch_path = NULL;
592 char *message = NULL;
593 GKeyFile *key_file = NULL;
594 void *local = talloc_new (NULL);
600 notmuch = _alloc_notmuch ();
602 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
606 status = _load_key_file (notmuch, config_path, profile, &key_file);
608 message = strdup ("Error: cannot load config file.\n");
612 if ((status = _choose_database_path (local, profile, key_file,
613 &database_path, &split, &message)))
616 status = _db_dir_exists (database_path, &message);
620 _set_database_path (notmuch, database_path);
622 if (key_file && ! split) {
623 char *mail_root = notmuch_canonicalize_file_name (
624 g_key_file_get_value (key_file, "database", "mail_root", NULL));
625 char *db_path = notmuch_canonicalize_file_name (database_path);
627 split = (mail_root && (0 != strcmp (mail_root, db_path)));
634 notmuch_path = database_path;
636 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
637 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
641 err = mkdir (notmuch_path, 0755);
643 if (errno == EEXIST) {
644 status = NOTMUCH_STATUS_DATABASE_EXISTS;
645 talloc_free (notmuch);
648 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
649 notmuch_path, strerror (errno)));
650 status = NOTMUCH_STATUS_FILE_ERROR;
656 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
657 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
661 status = _trial_open (notmuch->xapian_path, &message);
662 if (status == NOTMUCH_STATUS_SUCCESS) {
663 notmuch_database_destroy (notmuch);
665 status = NOTMUCH_STATUS_DATABASE_EXISTS;
672 status = _finish_open (notmuch,
674 NOTMUCH_DATABASE_MODE_READ_WRITE,
680 /* Upgrade doesn't add these feature to existing databases, but
681 * new databases have them. */
682 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
683 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
684 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
686 status = notmuch_database_upgrade (notmuch, NULL, NULL);
688 notmuch_database_close (notmuch);
696 g_key_file_free (key_file);
700 *status_string = message;
707 talloc_free (notmuch);
712 notmuch_database_reopen (notmuch_database_t *notmuch,
713 notmuch_database_mode_t new_mode)
715 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
717 if (notmuch->xapian_db == NULL) {
718 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
719 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
723 if (cur_mode == new_mode &&
724 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
725 notmuch->xapian_db->reopen ();
727 notmuch->xapian_db->close ();
729 delete notmuch->xapian_db;
730 notmuch->xapian_db = NULL;
731 /* no need to free the same object twice */
732 notmuch->writable_xapian_db = NULL;
734 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
735 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
737 notmuch->xapian_db = notmuch->writable_xapian_db;
739 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
744 _load_database_state (notmuch);
745 } catch (const Xapian::Error &error) {
746 if (! notmuch->exception_reported) {
747 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
748 error.get_msg ().c_str ());
749 notmuch->exception_reported = true;
751 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
755 notmuch->open = true;
756 return NOTMUCH_STATUS_SUCCESS;
760 _maybe_load_config_from_database (notmuch_database_t *notmuch,
762 const char *database_path,
765 char *message; /* ignored */
767 if (_db_dir_exists (database_path, &message))
768 return NOTMUCH_STATUS_NO_DATABASE;
770 _set_database_path (notmuch, database_path);
772 if (_notmuch_choose_xapian_path (notmuch, database_path, ¬much->xapian_path, &message))
773 return NOTMUCH_STATUS_NO_DATABASE;
775 (void) _finish_open (notmuch, profile, NOTMUCH_DATABASE_MODE_READ_ONLY, key_file, &message);
777 return NOTMUCH_STATUS_SUCCESS;
781 notmuch_database_load_config (const char *database_path,
782 const char *config_path,
784 notmuch_database_t **database,
785 char **status_string)
787 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS, warning = NOTMUCH_STATUS_SUCCESS;
788 void *local = talloc_new (NULL);
789 notmuch_database_t *notmuch = NULL;
790 char *message = NULL;
791 GKeyFile *key_file = NULL;
796 notmuch = _alloc_notmuch ();
798 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
802 status = _load_key_file (notmuch, config_path, profile, &key_file);
804 case NOTMUCH_STATUS_SUCCESS:
806 case NOTMUCH_STATUS_NO_CONFIG:
810 message = strdup ("Error: cannot load config file.\n");
814 status = _choose_database_path (local, profile, key_file,
815 &database_path, &split, &message);
817 case NOTMUCH_STATUS_NO_DATABASE:
818 case NOTMUCH_STATUS_SUCCESS:
828 status = _maybe_load_config_from_database (notmuch, key_file, database_path, profile);
830 case NOTMUCH_STATUS_NO_DATABASE:
831 case NOTMUCH_STATUS_SUCCESS:
841 status = _notmuch_config_load_from_file (notmuch, key_file);
845 status = _notmuch_config_load_defaults (notmuch);
853 *status_string = message;