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 notmuch_status_t status;
214 *database_path = _xdg_dir (ctx, "XDG_DATA_HOME", ".local/share", profile);
215 status = _db_dir_exists (*database_path, message);
217 *database_path = NULL;
223 if (! *database_path) {
224 *database_path = getenv ("MAILDIR");
227 if (! *database_path) {
228 notmuch_status_t status;
230 *database_path = talloc_asprintf (ctx, "%s/mail", getenv ("HOME"));
231 status = _db_dir_exists (*database_path, message);
233 *database_path = NULL;
237 if (*database_path == NULL) {
238 *message = strdup ("Error: could not locate database.\n");
239 return NOTMUCH_STATUS_NO_DATABASE;
242 if (*database_path[0] != '/') {
243 *message = strdup ("Error: Database path must be absolute.\n");
244 return NOTMUCH_STATUS_PATH_ERROR;
246 return NOTMUCH_STATUS_SUCCESS;
252 notmuch_database_t *notmuch;
254 notmuch = talloc_zero (NULL, notmuch_database_t);
258 notmuch->exception_reported = false;
259 notmuch->status_string = NULL;
260 notmuch->writable_xapian_db = NULL;
261 notmuch->config_path = NULL;
262 notmuch->atomic_nesting = 0;
267 static notmuch_status_t
268 _trial_open (const char *xapian_path, char **message_ptr)
271 Xapian::Database db (xapian_path);
272 } catch (const Xapian::DatabaseOpeningError &error) {
273 IGNORE_RESULT (asprintf (message_ptr,
274 "Cannot open Xapian database at %s: %s\n",
276 error.get_msg ().c_str ()));
277 return NOTMUCH_STATUS_PATH_ERROR;
278 } catch (const Xapian::Error &error) {
279 IGNORE_RESULT (asprintf (message_ptr,
280 "A Xapian exception occurred opening database: %s\n",
281 error.get_msg ().c_str ()));
282 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
284 return NOTMUCH_STATUS_SUCCESS;
288 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
289 const char **xapian_path, char **message_ptr)
291 notmuch_status_t status;
292 const char *trial_path, *notmuch_path;
294 status = _db_dir_exists (database_path, message_ptr);
298 trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
299 status = _trial_open (trial_path, message_ptr);
300 if (status != NOTMUCH_STATUS_PATH_ERROR)
306 notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
307 status = _db_dir_exists (notmuch_path, message_ptr);
311 trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
312 status = _trial_open (trial_path, message_ptr);
315 if (status == NOTMUCH_STATUS_SUCCESS)
316 *xapian_path = trial_path;
321 _set_database_path (notmuch_database_t *notmuch,
322 const char *database_path)
324 char *path = talloc_strdup (notmuch, database_path);
326 strip_trailing (path, '/');
328 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
335 static int initialized = 0;
337 /* Initialize the GLib type system and threads */
338 #if ! GLIB_CHECK_VERSION (2, 35, 1)
342 /* Initialize gmime */
350 _load_database_state (notmuch_database_t *notmuch)
352 std::string last_thread_id;
353 std::string last_mod;
355 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
356 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
357 if (last_thread_id.empty ()) {
358 notmuch->last_thread_id = 0;
363 str = last_thread_id.c_str ();
364 notmuch->last_thread_id = strtoull (str, &end, 16);
366 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
369 /* Get current highest revision number. */
370 last_mod = notmuch->xapian_db->get_value_upper_bound (
371 NOTMUCH_VALUE_LAST_MOD);
372 if (last_mod.empty ())
373 notmuch->revision = 0;
375 notmuch->revision = Xapian::sortable_unserialise (last_mod);
376 notmuch->uuid = talloc_strdup (
377 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
380 static notmuch_status_t
381 _finish_open (notmuch_database_t *notmuch,
383 notmuch_database_mode_t mode,
387 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
388 char *incompat_features;
389 char *message = NULL;
390 unsigned int version;
391 const char *database_path = notmuch_database_get_path (notmuch);
395 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
396 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
398 notmuch->xapian_db = notmuch->writable_xapian_db;
400 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
403 /* Check version. As of database version 3, we represent
404 * changes in terms of features, so assume a version bump
405 * means a dramatically incompatible change. */
406 version = notmuch_database_get_version (notmuch);
407 if (version > NOTMUCH_DATABASE_VERSION) {
408 IGNORE_RESULT (asprintf (&message,
409 "Error: Notmuch database at %s\n"
410 " has a newer database format version (%u) than supported by this\n"
411 " version of notmuch (%u).\n",
412 database_path, version, NOTMUCH_DATABASE_VERSION));
413 notmuch_database_destroy (notmuch);
415 status = NOTMUCH_STATUS_FILE_ERROR;
419 /* Check features. */
420 incompat_features = NULL;
421 notmuch->features = _notmuch_database_parse_features (
422 notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
423 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
425 if (incompat_features) {
426 IGNORE_RESULT (asprintf (&message,
427 "Error: Notmuch database at %s\n"
428 " requires features (%s)\n"
429 " not supported by this version of notmuch.\n",
430 database_path, incompat_features));
431 notmuch_database_destroy (notmuch);
433 status = NOTMUCH_STATUS_FILE_ERROR;
437 _load_database_state (notmuch);
439 notmuch->query_parser = new Xapian::QueryParser;
440 notmuch->term_gen = new Xapian::TermGenerator;
441 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
442 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
443 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
445 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
447 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
448 notmuch->query_parser->set_database (*notmuch->xapian_db);
449 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
450 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
451 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
452 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
453 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
455 /* Configuration information is needed to set up query parser */
456 status = _notmuch_config_load_from_database (notmuch);
461 status = _notmuch_config_load_from_file (notmuch, key_file);
465 status = _choose_dir (notmuch, profile,
466 NOTMUCH_CONFIG_HOOK_DIR,
474 status = _choose_dir (notmuch, profile,
475 NOTMUCH_CONFIG_BACKUP_DIR,
482 status = _notmuch_config_load_defaults (notmuch);
486 status = _notmuch_database_setup_standard_query_fields (notmuch);
490 status = _notmuch_database_setup_user_query_fields (notmuch);
494 } catch (const Xapian::Error &error) {
495 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
496 error.get_msg ().c_str ()));
497 notmuch_database_destroy (notmuch);
499 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
503 *message_ptr = message;
508 notmuch_database_open_with_config (const char *database_path,
509 notmuch_database_mode_t mode,
510 const char *config_path,
512 notmuch_database_t **database,
513 char **status_string)
515 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
516 void *local = talloc_new (NULL);
517 notmuch_database_t *notmuch = NULL;
518 char *message = NULL;
519 GKeyFile *key_file = NULL;
524 notmuch = _alloc_notmuch ();
526 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
530 status = _load_key_file (notmuch, config_path, profile, &key_file);
532 message = strdup ("Error: cannot load config file.\n");
536 if ((status = _choose_database_path (local, profile, key_file,
537 &database_path, &split,
541 status = _db_dir_exists (database_path, &message);
545 _set_database_path (notmuch, database_path);
547 status = _notmuch_choose_xapian_path (notmuch, database_path,
548 ¬much->xapian_path, &message);
552 status = _finish_open (notmuch, profile, mode, key_file, &message);
558 g_key_file_free (key_file);
562 *status_string = message;
570 talloc_free (notmuch);
573 notmuch->open = true;
579 notmuch_database_create (const char *path, notmuch_database_t **database)
581 char *status_string = NULL;
582 notmuch_status_t status;
584 status = notmuch_database_create_verbose (path, database,
588 fputs (status_string, stderr);
589 free (status_string);
596 notmuch_database_create_verbose (const char *path,
597 notmuch_database_t **database,
598 char **status_string)
600 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
604 notmuch_database_create_with_config (const char *database_path,
605 const char *config_path,
607 notmuch_database_t **database,
608 char **status_string)
610 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
611 notmuch_database_t *notmuch = NULL;
612 const char *notmuch_path = NULL;
613 char *message = NULL;
614 GKeyFile *key_file = NULL;
615 void *local = talloc_new (NULL);
621 notmuch = _alloc_notmuch ();
623 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
627 status = _load_key_file (notmuch, config_path, profile, &key_file);
629 message = strdup ("Error: cannot load config file.\n");
633 if ((status = _choose_database_path (local, profile, key_file,
634 &database_path, &split, &message)))
637 status = _db_dir_exists (database_path, &message);
641 _set_database_path (notmuch, database_path);
643 if (key_file && ! split) {
644 char *mail_root = notmuch_canonicalize_file_name (
645 g_key_file_get_value (key_file, "database", "mail_root", NULL));
646 char *db_path = notmuch_canonicalize_file_name (database_path);
648 split = (mail_root && (0 != strcmp (mail_root, db_path)));
655 notmuch_path = database_path;
657 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
658 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
662 err = mkdir (notmuch_path, 0755);
664 if (errno == EEXIST) {
665 status = NOTMUCH_STATUS_DATABASE_EXISTS;
666 talloc_free (notmuch);
669 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
670 notmuch_path, strerror (errno)));
671 status = NOTMUCH_STATUS_FILE_ERROR;
677 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
678 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
682 status = _trial_open (notmuch->xapian_path, &message);
683 if (status == NOTMUCH_STATUS_SUCCESS) {
684 notmuch_database_destroy (notmuch);
686 status = NOTMUCH_STATUS_DATABASE_EXISTS;
693 status = _finish_open (notmuch,
695 NOTMUCH_DATABASE_MODE_READ_WRITE,
701 /* Upgrade doesn't add these feature to existing databases, but
702 * new databases have them. */
703 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
704 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
705 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
707 status = notmuch_database_upgrade (notmuch, NULL, NULL);
709 notmuch_database_close (notmuch);
717 g_key_file_free (key_file);
721 *status_string = message;
728 talloc_free (notmuch);
733 notmuch_database_reopen (notmuch_database_t *notmuch,
734 notmuch_database_mode_t new_mode)
736 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
738 if (notmuch->xapian_db == NULL) {
739 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
740 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
744 if (cur_mode == new_mode &&
745 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
746 notmuch->xapian_db->reopen ();
748 notmuch->xapian_db->close ();
750 delete notmuch->xapian_db;
751 notmuch->xapian_db = NULL;
752 /* no need to free the same object twice */
753 notmuch->writable_xapian_db = NULL;
755 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
756 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
758 notmuch->xapian_db = notmuch->writable_xapian_db;
760 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
765 _load_database_state (notmuch);
766 } catch (const Xapian::Error &error) {
767 if (! notmuch->exception_reported) {
768 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
769 error.get_msg ().c_str ());
770 notmuch->exception_reported = true;
772 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
776 notmuch->open = true;
777 return NOTMUCH_STATUS_SUCCESS;
781 _maybe_load_config_from_database (notmuch_database_t *notmuch,
783 const char *database_path,
786 char *message; /* ignored */
788 if (_db_dir_exists (database_path, &message))
789 return NOTMUCH_STATUS_NO_DATABASE;
791 _set_database_path (notmuch, database_path);
793 if (_notmuch_choose_xapian_path (notmuch, database_path, ¬much->xapian_path, &message))
794 return NOTMUCH_STATUS_NO_DATABASE;
796 (void) _finish_open (notmuch, profile, NOTMUCH_DATABASE_MODE_READ_ONLY, key_file, &message);
798 return NOTMUCH_STATUS_SUCCESS;
802 notmuch_database_load_config (const char *database_path,
803 const char *config_path,
805 notmuch_database_t **database,
806 char **status_string)
808 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS, warning = NOTMUCH_STATUS_SUCCESS;
809 void *local = talloc_new (NULL);
810 notmuch_database_t *notmuch = NULL;
811 char *message = NULL;
812 GKeyFile *key_file = NULL;
817 notmuch = _alloc_notmuch ();
819 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
823 status = _load_key_file (notmuch, config_path, profile, &key_file);
825 case NOTMUCH_STATUS_SUCCESS:
827 case NOTMUCH_STATUS_NO_CONFIG:
831 message = strdup ("Error: cannot load config file.\n");
835 status = _choose_database_path (local, profile, key_file,
836 &database_path, &split, &message);
838 case NOTMUCH_STATUS_NO_DATABASE:
839 case NOTMUCH_STATUS_SUCCESS:
849 status = _maybe_load_config_from_database (notmuch, key_file, database_path, profile);
851 case NOTMUCH_STATUS_NO_DATABASE:
852 case NOTMUCH_STATUS_SUCCESS:
862 status = _notmuch_config_load_from_file (notmuch, key_file);
866 status = _notmuch_config_load_defaults (notmuch);
874 *status_string = message;