4 #include "database-private.h"
5 #include "parse-time-vrp.h"
6 #include "lastmod-fp.h"
9 #if HAVE_XAPIAN_DB_RETRY_LOCK
10 #define DB_ACTION (Xapian::DB_CREATE_OR_OPEN | Xapian::DB_RETRY_LOCK)
12 #define DB_ACTION Xapian::DB_CREATE_OR_OPEN
16 notmuch_database_open (const char *path,
17 notmuch_database_mode_t mode,
18 notmuch_database_t **database)
20 char *status_string = NULL;
21 notmuch_status_t status;
23 status = notmuch_database_open_with_config (path, mode, "", NULL,
24 database, &status_string);
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 (notmuch_database_t *notmuch,
193 const char **database_path,
196 notmuch_status_t status;
198 if (! *database_path) {
199 *database_path = getenv ("NOTMUCH_DATABASE");
202 if (! *database_path && key_file) {
203 char *path = g_key_file_get_string (key_file, "database", "path", NULL);
206 *database_path = talloc_strdup (notmuch, path);
208 *database_path = talloc_asprintf (notmuch, "%s/%s", getenv ("HOME"), path);
212 if (! *database_path) {
213 *database_path = _xdg_dir (notmuch, "XDG_DATA_HOME", ".local/share", profile);
214 status = _db_dir_exists (*database_path, message);
216 *database_path = NULL;
218 notmuch->params |= NOTMUCH_PARAM_SPLIT;
222 if (! *database_path) {
223 *database_path = getenv ("MAILDIR");
226 if (! *database_path) {
227 *database_path = talloc_asprintf (notmuch, "%s/mail", getenv ("HOME"));
228 status = _db_dir_exists (*database_path, message);
230 *database_path = NULL;
234 if (*database_path == NULL) {
235 *message = strdup ("Error: could not locate database.\n");
236 return NOTMUCH_STATUS_NO_DATABASE;
239 if (*database_path[0] != '/') {
240 *message = strdup ("Error: Database path must be absolute.\n");
241 return NOTMUCH_STATUS_PATH_ERROR;
244 status = _db_dir_exists (*database_path, message);
246 IGNORE_RESULT (asprintf (message,
247 "Error: database path '%s' does not exist or is not a directory.\n",
249 return NOTMUCH_STATUS_NO_DATABASE;
252 return NOTMUCH_STATUS_SUCCESS;
255 static notmuch_status_t
256 _mkdir (const char *path, char **message)
258 if (g_mkdir_with_parents (path, 0755)) {
259 IGNORE_RESULT (asprintf (message, "Error: Cannot create directory %s: %s.\n",
260 path, strerror (errno)));
261 return NOTMUCH_STATUS_FILE_ERROR;
263 return NOTMUCH_STATUS_SUCCESS;
266 static notmuch_status_t
267 _create_database_path (notmuch_database_t *notmuch,
270 const char **database_path,
273 notmuch_status_t status;
275 if (! *database_path) {
276 *database_path = getenv ("NOTMUCH_DATABASE");
279 if (! *database_path && key_file) {
280 char *path = g_key_file_get_string (key_file, "database", "path", NULL);
283 *database_path = talloc_strdup (notmuch, path);
285 *database_path = talloc_asprintf (notmuch, "%s/%s", getenv ("HOME"), path);
290 if (! *database_path) {
291 *database_path = _xdg_dir (notmuch, "XDG_DATA_HOME", ".local/share", profile);
292 notmuch->params |= NOTMUCH_PARAM_SPLIT;
295 if (*database_path[0] != '/') {
296 *message = strdup ("Error: Database path must be absolute.\n");
297 return NOTMUCH_STATUS_PATH_ERROR;
300 if ((status = _mkdir (*database_path, message)))
303 return NOTMUCH_STATUS_SUCCESS;
306 static notmuch_database_t *
307 _alloc_notmuch (const char *database_path, const char *config_path, const char *profile)
309 notmuch_database_t *notmuch;
311 notmuch = talloc_zero (NULL, notmuch_database_t);
315 notmuch->exception_reported = false;
316 notmuch->status_string = NULL;
317 notmuch->writable_xapian_db = NULL;
318 notmuch->config_path = NULL;
319 notmuch->atomic_nesting = 0;
320 notmuch->transaction_count = 0;
321 notmuch->transaction_threshold = 0;
324 notmuch->params = NOTMUCH_PARAM_NONE;
326 notmuch->params |= NOTMUCH_PARAM_DATABASE;
328 notmuch->params |= NOTMUCH_PARAM_CONFIG;
330 notmuch->params |= NOTMUCH_PARAM_PROFILE;
335 static notmuch_status_t
336 _trial_open (const char *xapian_path, char **message_ptr)
339 Xapian::Database db (xapian_path);
340 } catch (const Xapian::DatabaseOpeningError &error) {
341 IGNORE_RESULT (asprintf (message_ptr,
342 "Cannot open Xapian database at %s: %s\n",
344 error.get_msg ().c_str ()));
345 return NOTMUCH_STATUS_PATH_ERROR;
346 } catch (const Xapian::Error &error) {
347 IGNORE_RESULT (asprintf (message_ptr,
348 "A Xapian exception occurred opening database: %s\n",
349 error.get_msg ().c_str ()));
350 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
352 return NOTMUCH_STATUS_SUCCESS;
356 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
357 const char **xapian_path, char **message_ptr)
359 notmuch_status_t status;
360 const char *trial_path, *notmuch_path;
362 status = _db_dir_exists (database_path, message_ptr);
366 trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
367 status = _trial_open (trial_path, message_ptr);
368 if (status != NOTMUCH_STATUS_PATH_ERROR)
374 notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
375 status = _db_dir_exists (notmuch_path, message_ptr);
379 trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
380 status = _trial_open (trial_path, message_ptr);
383 if (status == NOTMUCH_STATUS_SUCCESS)
384 *xapian_path = trial_path;
389 _set_database_path (notmuch_database_t *notmuch,
390 const char *database_path)
392 char *path = talloc_strdup (notmuch, database_path);
394 strip_trailing (path, '/');
396 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
400 _load_database_state (notmuch_database_t *notmuch)
402 std::string last_thread_id;
403 std::string last_mod;
405 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
406 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
407 if (last_thread_id.empty ()) {
408 notmuch->last_thread_id = 0;
413 str = last_thread_id.c_str ();
414 notmuch->last_thread_id = strtoull (str, &end, 16);
416 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
419 /* Get current highest revision number. */
420 last_mod = notmuch->xapian_db->get_value_upper_bound (
421 NOTMUCH_VALUE_LAST_MOD);
422 if (last_mod.empty ())
423 notmuch->revision = 0;
425 notmuch->revision = Xapian::sortable_unserialise (last_mod);
426 notmuch->uuid = talloc_strdup (
427 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
430 static notmuch_status_t
431 _finish_open (notmuch_database_t *notmuch,
433 notmuch_database_mode_t mode,
437 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
438 char *incompat_features;
439 char *message = NULL;
440 const char *autocommit_str;
441 char *autocommit_end;
442 unsigned int version;
443 const char *database_path = notmuch_database_get_path (notmuch);
447 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
448 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
450 notmuch->xapian_db = notmuch->writable_xapian_db;
452 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
455 /* Check version. As of database version 3, we represent
456 * changes in terms of features, so assume a version bump
457 * means a dramatically incompatible change. */
458 version = notmuch_database_get_version (notmuch);
459 if (version > NOTMUCH_DATABASE_VERSION) {
460 IGNORE_RESULT (asprintf (&message,
461 "Error: Notmuch database at %s\n"
462 " has a newer database format version (%u) than supported by this\n"
463 " version of notmuch (%u).\n",
464 database_path, version, NOTMUCH_DATABASE_VERSION));
465 status = NOTMUCH_STATUS_FILE_ERROR;
469 /* Check features. */
470 incompat_features = NULL;
471 notmuch->features = _notmuch_database_parse_features (
472 notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
473 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
475 if (incompat_features) {
476 IGNORE_RESULT (asprintf (&message,
477 "Error: Notmuch database at %s\n"
478 " requires features (%s)\n"
479 " not supported by this version of notmuch.\n",
480 database_path, incompat_features));
481 status = NOTMUCH_STATUS_FILE_ERROR;
485 _load_database_state (notmuch);
487 notmuch->query_parser = new Xapian::QueryParser;
488 notmuch->term_gen = new Xapian::TermGenerator;
489 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
490 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
491 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
493 notmuch->last_mod_range_processor = new LastModRangeProcessor (notmuch, "lastmod:");
494 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
495 notmuch->query_parser->set_database (*notmuch->xapian_db);
496 notmuch->stemmer = new Xapian::Stem ("english");
497 notmuch->query_parser->set_stemmer (*notmuch->stemmer);
498 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
499 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
500 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
501 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
503 /* Configuration information is needed to set up query parser */
504 status = _notmuch_config_load_from_database (notmuch);
509 status = _notmuch_config_load_from_file (notmuch, key_file);
513 status = _choose_dir (notmuch, profile,
514 NOTMUCH_CONFIG_HOOK_DIR,
522 status = _choose_dir (notmuch, profile,
523 NOTMUCH_CONFIG_BACKUP_DIR,
530 status = _notmuch_config_load_defaults (notmuch);
534 autocommit_str = notmuch_config_get (notmuch, NOTMUCH_CONFIG_AUTOCOMMIT);
535 if (unlikely (! autocommit_str)) {
536 INTERNAL_ERROR ("missing configuration for autocommit");
538 notmuch->transaction_threshold = strtoul (autocommit_str, &autocommit_end, 10);
539 if (*autocommit_end != '\0')
540 INTERNAL_ERROR ("Malformed database database.autocommit value: %s", autocommit_str);
542 status = _notmuch_database_setup_standard_query_fields (notmuch);
546 status = _notmuch_database_setup_user_query_fields (notmuch);
550 } catch (const Xapian::Error &error) {
551 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
552 error.get_msg ().c_str ()));
553 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
557 *message_ptr = message;
562 notmuch_database_open_with_config (const char *database_path,
563 notmuch_database_mode_t mode,
564 const char *config_path,
566 notmuch_database_t **database,
567 char **status_string)
569 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
570 notmuch_database_t *notmuch = NULL;
571 char *message = NULL;
572 GKeyFile *key_file = NULL;
576 notmuch = _alloc_notmuch (database_path, config_path, profile);
578 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
582 status = _load_key_file (notmuch, config_path, profile, &key_file);
584 message = strdup ("Error: cannot load config file.\n");
588 if ((status = _choose_database_path (notmuch, profile, key_file,
593 status = _db_dir_exists (database_path, &message);
597 _set_database_path (notmuch, database_path);
599 status = _notmuch_choose_xapian_path (notmuch, database_path,
600 ¬much->xapian_path, &message);
604 status = _finish_open (notmuch, profile, mode, key_file, &message);
608 g_key_file_free (key_file);
612 *status_string = message;
617 if (status && notmuch) {
618 notmuch_database_destroy (notmuch);
626 notmuch->open = true;
632 notmuch_database_create (const char *path, notmuch_database_t **database)
634 char *status_string = NULL;
635 notmuch_status_t status;
637 status = notmuch_database_create_verbose (path, database,
641 fputs (status_string, stderr);
642 free (status_string);
649 notmuch_database_create_verbose (const char *path,
650 notmuch_database_t **database,
651 char **status_string)
653 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
657 notmuch_database_create_with_config (const char *database_path,
658 const char *config_path,
660 notmuch_database_t **database,
661 char **status_string)
663 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
664 notmuch_database_t *notmuch = NULL;
665 const char *notmuch_path = NULL;
666 char *message = NULL;
667 GKeyFile *key_file = NULL;
671 notmuch = _alloc_notmuch (database_path, config_path, profile);
673 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
677 status = _load_key_file (notmuch, config_path, profile, &key_file);
679 message = strdup ("Error: cannot load config file.\n");
683 status = _choose_database_path (notmuch, profile, key_file,
684 &database_path, &message);
686 case NOTMUCH_STATUS_SUCCESS:
688 case NOTMUCH_STATUS_NO_DATABASE:
689 if ((status = _create_database_path (notmuch, profile, key_file,
690 &database_path, &message)))
697 _set_database_path (notmuch, database_path);
699 if (key_file && ! (notmuch->params & NOTMUCH_PARAM_SPLIT)) {
700 char *mail_root = notmuch_canonicalize_file_name (
701 g_key_file_get_string (key_file, "database", "mail_root", NULL));
702 char *db_path = notmuch_canonicalize_file_name (database_path);
704 if (mail_root && (0 != strcmp (mail_root, db_path)))
705 notmuch->params |= NOTMUCH_PARAM_SPLIT;
711 if (notmuch->params & NOTMUCH_PARAM_SPLIT) {
712 notmuch_path = database_path;
714 if (! (notmuch_path = talloc_asprintf (notmuch, "%s/%s", database_path, ".notmuch"))) {
715 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
719 status = _mkdir (notmuch_path, &message);
724 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
725 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
729 status = _trial_open (notmuch->xapian_path, &message);
730 if (status == NOTMUCH_STATUS_SUCCESS) {
731 notmuch_database_destroy (notmuch);
733 status = NOTMUCH_STATUS_DATABASE_EXISTS;
740 status = _finish_open (notmuch,
742 NOTMUCH_DATABASE_MODE_READ_WRITE,
748 /* Upgrade doesn't add these feature to existing databases, but
749 * new databases have them. */
750 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
751 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
752 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
754 status = notmuch_database_upgrade (notmuch, NULL, NULL);
756 notmuch_database_close (notmuch);
762 g_key_file_free (key_file);
766 *status_string = message;
770 if (status && notmuch) {
771 notmuch_database_destroy (notmuch);
779 notmuch->open = true;
784 notmuch_database_reopen (notmuch_database_t *notmuch,
785 notmuch_database_mode_t new_mode)
787 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
789 if (notmuch->xapian_db == NULL) {
790 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
791 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
795 if (cur_mode == new_mode &&
796 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
797 notmuch->xapian_db->reopen ();
799 notmuch->xapian_db->close ();
801 delete notmuch->xapian_db;
802 notmuch->xapian_db = NULL;
803 /* no need to free the same object twice */
804 notmuch->writable_xapian_db = NULL;
806 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
807 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
809 notmuch->xapian_db = notmuch->writable_xapian_db;
811 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
816 _load_database_state (notmuch);
817 } catch (const Xapian::Error &error) {
818 if (! notmuch->exception_reported) {
819 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
820 error.get_msg ().c_str ());
821 notmuch->exception_reported = true;
823 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
827 notmuch->open = true;
828 return NOTMUCH_STATUS_SUCCESS;
831 static notmuch_status_t
832 _maybe_load_config_from_database (notmuch_database_t *notmuch,
834 const char *database_path,
837 char *message; /* ignored */
839 if (_db_dir_exists (database_path, &message))
840 return NOTMUCH_STATUS_NO_DATABASE;
842 _set_database_path (notmuch, database_path);
844 if (_notmuch_choose_xapian_path (notmuch, database_path, ¬much->xapian_path, &message))
845 return NOTMUCH_STATUS_NO_DATABASE;
847 (void) _finish_open (notmuch, profile, NOTMUCH_DATABASE_MODE_READ_ONLY, key_file, &message);
849 return NOTMUCH_STATUS_SUCCESS;
853 notmuch_database_load_config (const char *database_path,
854 const char *config_path,
856 notmuch_database_t **database,
857 char **status_string)
859 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS, warning = NOTMUCH_STATUS_SUCCESS;
860 notmuch_database_t *notmuch = NULL;
861 char *message = NULL;
862 GKeyFile *key_file = NULL;
866 notmuch = _alloc_notmuch (database_path, config_path, profile);
868 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
872 status = _load_key_file (notmuch, config_path, profile, &key_file);
874 case NOTMUCH_STATUS_SUCCESS:
876 case NOTMUCH_STATUS_NO_CONFIG:
880 message = strdup ("Error: cannot load config file.\n");
884 status = _choose_database_path (notmuch, profile, key_file,
885 &database_path, &message);
887 case NOTMUCH_STATUS_NO_DATABASE:
888 case NOTMUCH_STATUS_SUCCESS:
898 status = _maybe_load_config_from_database (notmuch, key_file, database_path, profile);
900 case NOTMUCH_STATUS_NO_DATABASE:
901 case NOTMUCH_STATUS_SUCCESS:
911 status = _notmuch_config_load_from_file (notmuch, key_file);
915 status = _notmuch_config_load_defaults (notmuch);
921 *status_string = message;
924 status != NOTMUCH_STATUS_NO_DATABASE
925 && status != NOTMUCH_STATUS_NO_CONFIG) {
926 notmuch_database_destroy (notmuch);