4 #include "database-private.h"
5 #include "parse-time-vrp.h"
7 #if HAVE_XAPIAN_DB_RETRY_LOCK
8 #define DB_ACTION (Xapian::DB_CREATE_OR_OPEN | Xapian::DB_RETRY_LOCK)
10 #define DB_ACTION Xapian::DB_CREATE_OR_OPEN
14 notmuch_database_open (const char *path,
15 notmuch_database_mode_t mode,
16 notmuch_database_t **database)
18 char *status_string = NULL;
19 notmuch_status_t status;
21 status = notmuch_database_open_verbose (path, mode, database,
25 fputs (status_string, stderr);
33 notmuch_database_open_verbose (const char *path,
34 notmuch_database_mode_t mode,
35 notmuch_database_t **database,
38 return notmuch_database_open_with_config (path, mode, "", NULL,
39 database, status_string);
44 const char *xdg_root_variable,
45 const char *xdg_prefix,
46 const char *profile_name)
48 const char *xdg_root = getenv (xdg_root_variable);
51 const char *home = getenv ("HOME");
53 if (! home) return NULL;
55 xdg_root = talloc_asprintf (ctx,
62 profile_name = getenv ("NOTMUCH_PROFILE");
65 profile_name = "default";
67 return talloc_asprintf (ctx,
73 static notmuch_status_t
74 _choose_dir (notmuch_database_t *notmuch,
76 notmuch_config_key_t key,
78 const char *xdg_subdir,
80 char **message = NULL)
87 dir = notmuch_config_get (notmuch, key);
90 return NOTMUCH_STATUS_SUCCESS;
92 parent = _xdg_dir (notmuch, xdg_var, xdg_subdir, profile);
94 return NOTMUCH_STATUS_PATH_ERROR;
96 dir = talloc_asprintf (notmuch, "%s/%s", parent, subdir);
98 err = stat (dir, &st);
100 if (errno == ENOENT) {
101 char *notmuch_path = dirname (talloc_strdup (notmuch, notmuch->xapian_path));
102 dir = talloc_asprintf (notmuch, "%s/%s", notmuch_path, subdir);
104 IGNORE_RESULT (asprintf (message, "Error: Cannot stat %s: %s.\n",
105 dir, strerror (errno)));
106 return NOTMUCH_STATUS_FILE_ERROR;
110 _notmuch_config_cache (notmuch, key, dir);
112 return NOTMUCH_STATUS_SUCCESS;
115 static notmuch_status_t
116 _load_key_file (notmuch_database_t *notmuch,
121 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
123 if (path && EMPTY_STRING (path))
127 path = getenv ("NOTMUCH_CONFIG");
130 path = talloc_strdup (notmuch, path);
132 const char *dir = _xdg_dir (notmuch, "XDG_CONFIG_HOME", ".config", profile);
135 path = talloc_asprintf (notmuch, "%s/config", dir);
136 if (access (path, R_OK) != 0)
142 const char *home = getenv ("HOME");
144 path = talloc_asprintf (notmuch, "%s/.notmuch-config", home);
147 profile = getenv ("NOTMUCH_PROFILE");
150 path = talloc_asprintf (notmuch, "%s.%s", path, profile);
153 *key_file = g_key_file_new ();
154 if (! g_key_file_load_from_file (*key_file, path, G_KEY_FILE_NONE, NULL)) {
155 status = NOTMUCH_STATUS_NO_CONFIG;
160 notmuch->config_path = path;
165 static notmuch_status_t
166 _db_dir_exists (const char *database_path, char **message)
171 err = stat (database_path, &st);
173 IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: %s.\n",
174 database_path, strerror (errno)));
175 return NOTMUCH_STATUS_FILE_ERROR;
178 if (! S_ISDIR (st.st_mode)) {
179 IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: "
180 "Not a directory.\n",
182 return NOTMUCH_STATUS_FILE_ERROR;
185 return NOTMUCH_STATUS_SUCCESS;
188 static notmuch_status_t
189 _choose_database_path (void *ctx,
192 const char **database_path,
196 if (! *database_path) {
197 *database_path = getenv ("NOTMUCH_DATABASE");
200 if (! *database_path && key_file) {
201 char *path = g_key_file_get_value (key_file, "database", "path", NULL);
204 *database_path = talloc_strdup (ctx, path);
206 *database_path = talloc_asprintf (ctx, "%s/%s", getenv ("HOME"), path);
210 if (! *database_path) {
211 *database_path = _xdg_dir (ctx, "XDG_DATA_HOME", ".local/share", profile);
215 if (*database_path == NULL) {
216 *message = strdup ("Error: could not locate database.\n");
217 return NOTMUCH_STATUS_NO_DATABASE;
220 if (*database_path[0] != '/') {
221 *message = strdup ("Error: Database path must be absolute.\n");
222 return NOTMUCH_STATUS_PATH_ERROR;
224 return NOTMUCH_STATUS_SUCCESS;
230 notmuch_database_t *notmuch;
232 notmuch = talloc_zero (NULL, notmuch_database_t);
236 notmuch->exception_reported = false;
237 notmuch->status_string = NULL;
238 notmuch->writable_xapian_db = NULL;
239 notmuch->config_path = NULL;
240 notmuch->atomic_nesting = 0;
245 static notmuch_status_t
246 _trial_open (const char *xapian_path, char **message_ptr)
249 Xapian::Database db (xapian_path);
250 } catch (const Xapian::DatabaseOpeningError &error) {
251 IGNORE_RESULT (asprintf (message_ptr,
252 "Cannot open Xapian database at %s: %s\n",
254 error.get_msg ().c_str ()));
255 return NOTMUCH_STATUS_PATH_ERROR;
256 } catch (const Xapian::Error &error) {
257 IGNORE_RESULT (asprintf (message_ptr,
258 "A Xapian exception occurred opening database: %s\n",
259 error.get_msg ().c_str ()));
260 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
262 return NOTMUCH_STATUS_SUCCESS;
266 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
267 const char **xapian_path, char **message_ptr)
269 notmuch_status_t status;
270 const char *trial_path, *notmuch_path;
272 status = _db_dir_exists (database_path, message_ptr);
276 trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
277 status = _trial_open (trial_path, message_ptr);
278 if (status != NOTMUCH_STATUS_PATH_ERROR)
284 notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
285 status = _db_dir_exists (notmuch_path, message_ptr);
289 trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
290 status = _trial_open (trial_path, message_ptr);
293 if (status == NOTMUCH_STATUS_SUCCESS)
294 *xapian_path = trial_path;
299 _set_database_path (notmuch_database_t *notmuch,
300 const char *database_path)
302 char *path = talloc_strdup (notmuch, database_path);
304 strip_trailing (path, '/');
306 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
313 static int initialized = 0;
315 /* Initialize the GLib type system and threads */
316 #if ! GLIB_CHECK_VERSION (2, 35, 1)
320 /* Initialize gmime */
327 static notmuch_status_t
328 _finish_open (notmuch_database_t *notmuch,
330 notmuch_database_mode_t mode,
334 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
335 char *incompat_features;
336 char *message = NULL;
337 unsigned int version;
338 const char *database_path = notmuch_database_get_path (notmuch);
341 std::string last_thread_id;
342 std::string last_mod;
344 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
345 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
347 notmuch->xapian_db = notmuch->writable_xapian_db;
349 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
352 /* Check version. As of database version 3, we represent
353 * changes in terms of features, so assume a version bump
354 * means a dramatically incompatible change. */
355 version = notmuch_database_get_version (notmuch);
356 if (version > NOTMUCH_DATABASE_VERSION) {
357 IGNORE_RESULT (asprintf (&message,
358 "Error: Notmuch database at %s\n"
359 " has a newer database format version (%u) than supported by this\n"
360 " version of notmuch (%u).\n",
361 database_path, version, NOTMUCH_DATABASE_VERSION));
362 notmuch_database_destroy (notmuch);
364 status = NOTMUCH_STATUS_FILE_ERROR;
368 /* Check features. */
369 incompat_features = NULL;
370 notmuch->features = _notmuch_database_parse_features (
371 notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
372 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
374 if (incompat_features) {
375 IGNORE_RESULT (asprintf (&message,
376 "Error: Notmuch database at %s\n"
377 " requires features (%s)\n"
378 " not supported by this version of notmuch.\n",
379 database_path, incompat_features));
380 notmuch_database_destroy (notmuch);
382 status = NOTMUCH_STATUS_FILE_ERROR;
386 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
387 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
388 if (last_thread_id.empty ()) {
389 notmuch->last_thread_id = 0;
394 str = last_thread_id.c_str ();
395 notmuch->last_thread_id = strtoull (str, &end, 16);
397 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
400 /* Get current highest revision number. */
401 last_mod = notmuch->xapian_db->get_value_upper_bound (
402 NOTMUCH_VALUE_LAST_MOD);
403 if (last_mod.empty ())
404 notmuch->revision = 0;
406 notmuch->revision = Xapian::sortable_unserialise (last_mod);
407 notmuch->uuid = talloc_strdup (
408 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
410 notmuch->query_parser = new Xapian::QueryParser;
411 notmuch->term_gen = new Xapian::TermGenerator;
412 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
413 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
414 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
416 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
418 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
419 notmuch->query_parser->set_database (*notmuch->xapian_db);
420 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
421 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
422 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
423 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
424 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
426 /* Configuration information is needed to set up query parser */
427 status = _notmuch_config_load_from_database (notmuch);
432 status = _notmuch_config_load_from_file (notmuch, key_file);
436 status = _choose_dir (notmuch, profile,
437 NOTMUCH_CONFIG_HOOK_DIR,
445 status = _choose_dir (notmuch, profile,
446 NOTMUCH_CONFIG_BACKUP_DIR,
453 status = _notmuch_config_load_defaults (notmuch);
457 status = _notmuch_database_setup_standard_query_fields (notmuch);
461 status = _notmuch_database_setup_user_query_fields (notmuch);
465 } catch (const Xapian::Error &error) {
466 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
467 error.get_msg ().c_str ()));
468 notmuch_database_destroy (notmuch);
470 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
474 *message_ptr = message;
479 notmuch_database_open_with_config (const char *database_path,
480 notmuch_database_mode_t mode,
481 const char *config_path,
483 notmuch_database_t **database,
484 char **status_string)
486 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
487 void *local = talloc_new (NULL);
488 notmuch_database_t *notmuch = NULL;
489 char *message = NULL;
490 GKeyFile *key_file = NULL;
495 notmuch = _alloc_notmuch ();
497 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
501 status = _load_key_file (notmuch, config_path, profile, &key_file);
503 message = strdup ("Error: cannot load config file.\n");
507 if ((status = _choose_database_path (local, profile, key_file,
508 &database_path, &split,
512 status = _db_dir_exists (database_path, &message);
516 _set_database_path (notmuch, database_path);
518 status = _notmuch_choose_xapian_path (notmuch, database_path,
519 ¬much->xapian_path, &message);
523 status = _finish_open (notmuch, profile, mode, key_file, &message);
529 g_key_file_free (key_file);
533 *status_string = message;
541 talloc_free (notmuch);
544 notmuch->open = true;
550 notmuch_database_create (const char *path, notmuch_database_t **database)
552 char *status_string = NULL;
553 notmuch_status_t status;
555 status = notmuch_database_create_verbose (path, database,
559 fputs (status_string, stderr);
560 free (status_string);
567 notmuch_database_create_verbose (const char *path,
568 notmuch_database_t **database,
569 char **status_string)
571 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
575 notmuch_database_create_with_config (const char *database_path,
576 const char *config_path,
578 notmuch_database_t **database,
579 char **status_string)
581 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
582 notmuch_database_t *notmuch = NULL;
583 const char *notmuch_path = NULL;
584 char *message = NULL;
585 GKeyFile *key_file = NULL;
586 void *local = talloc_new (NULL);
592 notmuch = _alloc_notmuch ();
594 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
598 status = _load_key_file (notmuch, config_path, profile, &key_file);
600 message = strdup ("Error: cannot load config file.\n");
604 if ((status = _choose_database_path (local, profile, key_file,
605 &database_path, &split, &message)))
608 status = _db_dir_exists (database_path, &message);
612 _set_database_path (notmuch, database_path);
614 if (key_file && ! split) {
615 char *mail_root = canonicalize_file_name (
616 g_key_file_get_value (key_file, "database", "mail_root", NULL));
617 char *db_path = canonicalize_file_name (database_path);
619 split = (mail_root && (0 != strcmp (mail_root, db_path)));
626 notmuch_path = database_path;
628 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
629 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
633 err = mkdir (notmuch_path, 0755);
635 if (errno == EEXIST) {
636 status = NOTMUCH_STATUS_DATABASE_EXISTS;
637 talloc_free (notmuch);
640 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
641 notmuch_path, strerror (errno)));
642 status = NOTMUCH_STATUS_FILE_ERROR;
648 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
649 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
653 status = _trial_open (notmuch->xapian_path, &message);
654 if (status == NOTMUCH_STATUS_SUCCESS) {
655 notmuch_database_destroy (notmuch);
657 status = NOTMUCH_STATUS_DATABASE_EXISTS;
664 status = _finish_open (notmuch,
666 NOTMUCH_DATABASE_MODE_READ_WRITE,
672 /* Upgrade doesn't add these feature to existing databases, but
673 * new databases have them. */
674 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
675 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
676 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
678 status = notmuch_database_upgrade (notmuch, NULL, NULL);
680 notmuch_database_close (notmuch);
688 g_key_file_free (key_file);
692 *status_string = message;
699 talloc_free (notmuch);
704 notmuch_database_reopen (notmuch_database_t *notmuch,
705 notmuch_database_mode_t new_mode)
707 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
709 if (notmuch->xapian_db == NULL) {
710 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
711 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
715 if (cur_mode == new_mode &&
716 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
717 notmuch->xapian_db->reopen ();
719 notmuch->xapian_db->close ();
721 delete notmuch->xapian_db;
722 notmuch->xapian_db = NULL;
723 /* no need to free the same object twice */
724 notmuch->writable_xapian_db = NULL;
726 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
727 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
729 notmuch->xapian_db = notmuch->writable_xapian_db;
731 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
735 } catch (const Xapian::Error &error) {
736 if (! notmuch->exception_reported) {
737 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
738 error.get_msg ().c_str ());
739 notmuch->exception_reported = true;
741 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
745 notmuch->open = true;
746 return NOTMUCH_STATUS_SUCCESS;
750 _maybe_load_config_from_database (notmuch_database_t *notmuch,
752 const char *database_path,
755 char *message; /* ignored */
757 if (_db_dir_exists (database_path, &message))
758 return NOTMUCH_STATUS_NO_DATABASE;
760 _set_database_path (notmuch, database_path);
762 if (_notmuch_choose_xapian_path (notmuch, database_path, ¬much->xapian_path, &message))
763 return NOTMUCH_STATUS_NO_DATABASE;
765 (void) _finish_open (notmuch, profile, NOTMUCH_DATABASE_MODE_READ_ONLY, key_file, &message);
767 return NOTMUCH_STATUS_SUCCESS;
771 notmuch_database_load_config (const char *database_path,
772 const char *config_path,
774 notmuch_database_t **database,
775 char **status_string)
777 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS, warning = NOTMUCH_STATUS_SUCCESS;
778 void *local = talloc_new (NULL);
779 notmuch_database_t *notmuch = NULL;
780 char *message = NULL;
781 GKeyFile *key_file = NULL;
786 notmuch = _alloc_notmuch ();
788 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
792 status = _load_key_file (notmuch, config_path, profile, &key_file);
794 case NOTMUCH_STATUS_SUCCESS:
796 case NOTMUCH_STATUS_NO_CONFIG:
800 message = strdup ("Error: cannot load config file.\n");
804 status = _choose_database_path (local, profile, key_file,
805 &database_path, &split, &message);
807 case NOTMUCH_STATUS_NO_DATABASE:
808 case NOTMUCH_STATUS_SUCCESS:
818 status = _maybe_load_config_from_database (notmuch, key_file, database_path, profile);
820 case NOTMUCH_STATUS_NO_DATABASE:
821 case NOTMUCH_STATUS_SUCCESS:
831 status = _notmuch_config_load_from_file (notmuch, key_file);
835 status = _notmuch_config_load_defaults (notmuch);
843 *status_string = message;