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_with_config (path, mode, "", NULL,
23 database, &status_string);
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_string (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 notmuch_status_t status;
213 *database_path = _xdg_dir (ctx, "XDG_DATA_HOME", ".local/share", profile);
214 status = _db_dir_exists (*database_path, message);
216 *database_path = NULL;
222 if (! *database_path) {
223 *database_path = getenv ("MAILDIR");
226 if (! *database_path) {
227 notmuch_status_t status;
229 *database_path = talloc_asprintf (ctx, "%s/mail", getenv ("HOME"));
230 status = _db_dir_exists (*database_path, message);
232 *database_path = NULL;
236 if (*database_path == NULL) {
237 *message = strdup ("Error: could not locate database.\n");
238 return NOTMUCH_STATUS_NO_DATABASE;
241 if (*database_path[0] != '/') {
242 *message = strdup ("Error: Database path must be absolute.\n");
243 return NOTMUCH_STATUS_PATH_ERROR;
245 return NOTMUCH_STATUS_SUCCESS;
248 static notmuch_database_t *
249 _alloc_notmuch (const char *database_path, const char *config_path, const char *profile)
251 notmuch_database_t *notmuch;
253 notmuch = talloc_zero (NULL, notmuch_database_t);
257 notmuch->exception_reported = false;
258 notmuch->status_string = NULL;
259 notmuch->writable_xapian_db = NULL;
260 notmuch->config_path = NULL;
261 notmuch->atomic_nesting = 0;
262 notmuch->transaction_count = 0;
263 notmuch->transaction_threshold = 0;
266 notmuch->params = NOTMUCH_PARAM_NONE;
268 notmuch->params |= NOTMUCH_PARAM_DATABASE;
270 notmuch->params |= NOTMUCH_PARAM_CONFIG;
272 notmuch->params |= NOTMUCH_PARAM_PROFILE;
277 static notmuch_status_t
278 _trial_open (const char *xapian_path, char **message_ptr)
281 Xapian::Database db (xapian_path);
282 } catch (const Xapian::DatabaseOpeningError &error) {
283 IGNORE_RESULT (asprintf (message_ptr,
284 "Cannot open Xapian database at %s: %s\n",
286 error.get_msg ().c_str ()));
287 return NOTMUCH_STATUS_PATH_ERROR;
288 } catch (const Xapian::Error &error) {
289 IGNORE_RESULT (asprintf (message_ptr,
290 "A Xapian exception occurred opening database: %s\n",
291 error.get_msg ().c_str ()));
292 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
294 return NOTMUCH_STATUS_SUCCESS;
298 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
299 const char **xapian_path, char **message_ptr)
301 notmuch_status_t status;
302 const char *trial_path, *notmuch_path;
304 status = _db_dir_exists (database_path, message_ptr);
308 trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
309 status = _trial_open (trial_path, message_ptr);
310 if (status != NOTMUCH_STATUS_PATH_ERROR)
316 notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
317 status = _db_dir_exists (notmuch_path, message_ptr);
321 trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
322 status = _trial_open (trial_path, message_ptr);
325 if (status == NOTMUCH_STATUS_SUCCESS)
326 *xapian_path = trial_path;
331 _set_database_path (notmuch_database_t *notmuch,
332 const char *database_path)
334 char *path = talloc_strdup (notmuch, database_path);
336 strip_trailing (path, '/');
338 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
342 _load_database_state (notmuch_database_t *notmuch)
344 std::string last_thread_id;
345 std::string last_mod;
347 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
348 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
349 if (last_thread_id.empty ()) {
350 notmuch->last_thread_id = 0;
355 str = last_thread_id.c_str ();
356 notmuch->last_thread_id = strtoull (str, &end, 16);
358 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
361 /* Get current highest revision number. */
362 last_mod = notmuch->xapian_db->get_value_upper_bound (
363 NOTMUCH_VALUE_LAST_MOD);
364 if (last_mod.empty ())
365 notmuch->revision = 0;
367 notmuch->revision = Xapian::sortable_unserialise (last_mod);
368 notmuch->uuid = talloc_strdup (
369 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
372 static notmuch_status_t
373 _finish_open (notmuch_database_t *notmuch,
375 notmuch_database_mode_t mode,
379 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
380 char *incompat_features;
381 char *message = NULL;
382 const char *autocommit_str;
383 char *autocommit_end;
384 unsigned int version;
385 const char *database_path = notmuch_database_get_path (notmuch);
389 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
390 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
392 notmuch->xapian_db = notmuch->writable_xapian_db;
394 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
397 /* Check version. As of database version 3, we represent
398 * changes in terms of features, so assume a version bump
399 * means a dramatically incompatible change. */
400 version = notmuch_database_get_version (notmuch);
401 if (version > NOTMUCH_DATABASE_VERSION) {
402 IGNORE_RESULT (asprintf (&message,
403 "Error: Notmuch database at %s\n"
404 " has a newer database format version (%u) than supported by this\n"
405 " version of notmuch (%u).\n",
406 database_path, version, NOTMUCH_DATABASE_VERSION));
407 status = NOTMUCH_STATUS_FILE_ERROR;
411 /* Check features. */
412 incompat_features = NULL;
413 notmuch->features = _notmuch_database_parse_features (
414 notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
415 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
417 if (incompat_features) {
418 IGNORE_RESULT (asprintf (&message,
419 "Error: Notmuch database at %s\n"
420 " requires features (%s)\n"
421 " not supported by this version of notmuch.\n",
422 database_path, incompat_features));
423 status = NOTMUCH_STATUS_FILE_ERROR;
427 _load_database_state (notmuch);
429 notmuch->query_parser = new Xapian::QueryParser;
430 notmuch->term_gen = new Xapian::TermGenerator;
431 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
432 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
433 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
435 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
437 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
438 notmuch->query_parser->set_database (*notmuch->xapian_db);
439 notmuch->stemmer = new Xapian::Stem ("english");
440 notmuch->query_parser->set_stemmer (*notmuch->stemmer);
441 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
442 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
443 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
444 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
446 /* Configuration information is needed to set up query parser */
447 status = _notmuch_config_load_from_database (notmuch);
452 status = _notmuch_config_load_from_file (notmuch, key_file);
456 status = _choose_dir (notmuch, profile,
457 NOTMUCH_CONFIG_HOOK_DIR,
465 status = _choose_dir (notmuch, profile,
466 NOTMUCH_CONFIG_BACKUP_DIR,
473 status = _notmuch_config_load_defaults (notmuch);
477 autocommit_str = notmuch_config_get (notmuch, NOTMUCH_CONFIG_AUTOCOMMIT);
478 if (unlikely (! autocommit_str)) {
479 INTERNAL_ERROR ("missing configuration for autocommit");
481 notmuch->transaction_threshold = strtoul (autocommit_str, &autocommit_end, 10);
482 if (*autocommit_end != '\0')
483 INTERNAL_ERROR ("Malformed database database.autocommit value: %s", autocommit_str);
485 status = _notmuch_database_setup_standard_query_fields (notmuch);
489 status = _notmuch_database_setup_user_query_fields (notmuch);
493 } catch (const Xapian::Error &error) {
494 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
495 error.get_msg ().c_str ()));
496 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
500 *message_ptr = message;
505 notmuch_database_open_with_config (const char *database_path,
506 notmuch_database_mode_t mode,
507 const char *config_path,
509 notmuch_database_t **database,
510 char **status_string)
512 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
513 void *local = talloc_new (NULL);
514 notmuch_database_t *notmuch = NULL;
515 char *message = NULL;
516 GKeyFile *key_file = NULL;
521 notmuch = _alloc_notmuch (database_path, config_path, profile);
523 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
527 status = _load_key_file (notmuch, config_path, profile, &key_file);
529 message = strdup ("Error: cannot load config file.\n");
533 if ((status = _choose_database_path (local, profile, key_file,
534 &database_path, &split,
538 status = _db_dir_exists (database_path, &message);
542 _set_database_path (notmuch, database_path);
544 status = _notmuch_choose_xapian_path (notmuch, database_path,
545 ¬much->xapian_path, &message);
549 status = _finish_open (notmuch, profile, mode, key_file, &message);
555 g_key_file_free (key_file);
559 *status_string = message;
564 if (status && notmuch) {
565 notmuch_database_destroy (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 (database_path, config_path, profile);
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_string (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;
725 if (status && notmuch) {
726 notmuch_database_destroy (notmuch);
734 notmuch->open = true;
739 notmuch_database_reopen (notmuch_database_t *notmuch,
740 notmuch_database_mode_t new_mode)
742 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
744 if (notmuch->xapian_db == NULL) {
745 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
746 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
750 if (cur_mode == new_mode &&
751 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
752 notmuch->xapian_db->reopen ();
754 notmuch->xapian_db->close ();
756 delete notmuch->xapian_db;
757 notmuch->xapian_db = NULL;
758 /* no need to free the same object twice */
759 notmuch->writable_xapian_db = NULL;
761 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
762 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
764 notmuch->xapian_db = notmuch->writable_xapian_db;
766 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
771 _load_database_state (notmuch);
772 } catch (const Xapian::Error &error) {
773 if (! notmuch->exception_reported) {
774 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
775 error.get_msg ().c_str ());
776 notmuch->exception_reported = true;
778 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
782 notmuch->open = true;
783 return NOTMUCH_STATUS_SUCCESS;
786 static notmuch_status_t
787 _maybe_load_config_from_database (notmuch_database_t *notmuch,
789 const char *database_path,
792 char *message; /* ignored */
794 if (_db_dir_exists (database_path, &message))
795 return NOTMUCH_STATUS_NO_DATABASE;
797 _set_database_path (notmuch, database_path);
799 if (_notmuch_choose_xapian_path (notmuch, database_path, ¬much->xapian_path, &message))
800 return NOTMUCH_STATUS_NO_DATABASE;
802 (void) _finish_open (notmuch, profile, NOTMUCH_DATABASE_MODE_READ_ONLY, key_file, &message);
804 return NOTMUCH_STATUS_SUCCESS;
808 notmuch_database_load_config (const char *database_path,
809 const char *config_path,
811 notmuch_database_t **database,
812 char **status_string)
814 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS, warning = NOTMUCH_STATUS_SUCCESS;
815 void *local = talloc_new (NULL);
816 notmuch_database_t *notmuch = NULL;
817 char *message = NULL;
818 GKeyFile *key_file = NULL;
823 notmuch = _alloc_notmuch (database_path, config_path, profile);
825 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
829 status = _load_key_file (notmuch, config_path, profile, &key_file);
831 case NOTMUCH_STATUS_SUCCESS:
833 case NOTMUCH_STATUS_NO_CONFIG:
837 message = strdup ("Error: cannot load config file.\n");
841 status = _choose_database_path (local, profile, key_file,
842 &database_path, &split, &message);
844 case NOTMUCH_STATUS_NO_DATABASE:
845 case NOTMUCH_STATUS_SUCCESS:
855 status = _maybe_load_config_from_database (notmuch, key_file, database_path, profile);
857 case NOTMUCH_STATUS_NO_DATABASE:
858 case NOTMUCH_STATUS_SUCCESS:
868 status = _notmuch_config_load_from_file (notmuch, key_file);
872 status = _notmuch_config_load_defaults (notmuch);
880 *status_string = message;
883 status != NOTMUCH_STATUS_NO_DATABASE
884 && status != NOTMUCH_STATUS_NO_CONFIG) {
885 notmuch_database_destroy (notmuch);