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 notmuch_status_t status;
226 *database_path = talloc_asprintf (ctx, "%s/mail", getenv ("HOME"));
227 status = _db_dir_exists (*database_path, message);
229 *database_path = NULL;
233 if (*database_path == NULL) {
234 *message = strdup ("Error: could not locate database.\n");
235 return NOTMUCH_STATUS_NO_DATABASE;
238 if (*database_path[0] != '/') {
239 *message = strdup ("Error: Database path must be absolute.\n");
240 return NOTMUCH_STATUS_PATH_ERROR;
242 return NOTMUCH_STATUS_SUCCESS;
245 static notmuch_database_t *
248 notmuch_database_t *notmuch;
250 notmuch = talloc_zero (NULL, notmuch_database_t);
254 notmuch->exception_reported = false;
255 notmuch->status_string = NULL;
256 notmuch->writable_xapian_db = NULL;
257 notmuch->config_path = NULL;
258 notmuch->atomic_nesting = 0;
259 notmuch->transaction_count = 0;
260 notmuch->transaction_threshold = 0;
265 static notmuch_status_t
266 _trial_open (const char *xapian_path, char **message_ptr)
269 Xapian::Database db (xapian_path);
270 } catch (const Xapian::DatabaseOpeningError &error) {
271 IGNORE_RESULT (asprintf (message_ptr,
272 "Cannot open Xapian database at %s: %s\n",
274 error.get_msg ().c_str ()));
275 return NOTMUCH_STATUS_PATH_ERROR;
276 } catch (const Xapian::Error &error) {
277 IGNORE_RESULT (asprintf (message_ptr,
278 "A Xapian exception occurred opening database: %s\n",
279 error.get_msg ().c_str ()));
280 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
282 return NOTMUCH_STATUS_SUCCESS;
286 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
287 const char **xapian_path, char **message_ptr)
289 notmuch_status_t status;
290 const char *trial_path, *notmuch_path;
292 status = _db_dir_exists (database_path, message_ptr);
296 trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
297 status = _trial_open (trial_path, message_ptr);
298 if (status != NOTMUCH_STATUS_PATH_ERROR)
304 notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
305 status = _db_dir_exists (notmuch_path, message_ptr);
309 trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
310 status = _trial_open (trial_path, message_ptr);
313 if (status == NOTMUCH_STATUS_SUCCESS)
314 *xapian_path = trial_path;
319 _set_database_path (notmuch_database_t *notmuch,
320 const char *database_path)
322 char *path = talloc_strdup (notmuch, database_path);
324 strip_trailing (path, '/');
326 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
330 _load_database_state (notmuch_database_t *notmuch)
332 std::string last_thread_id;
333 std::string last_mod;
335 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
336 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
337 if (last_thread_id.empty ()) {
338 notmuch->last_thread_id = 0;
343 str = last_thread_id.c_str ();
344 notmuch->last_thread_id = strtoull (str, &end, 16);
346 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
349 /* Get current highest revision number. */
350 last_mod = notmuch->xapian_db->get_value_upper_bound (
351 NOTMUCH_VALUE_LAST_MOD);
352 if (last_mod.empty ())
353 notmuch->revision = 0;
355 notmuch->revision = Xapian::sortable_unserialise (last_mod);
356 notmuch->uuid = talloc_strdup (
357 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
360 static notmuch_status_t
361 _finish_open (notmuch_database_t *notmuch,
363 notmuch_database_mode_t mode,
367 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
368 char *incompat_features;
369 char *message = NULL;
370 const char *autocommit_str;
371 char *autocommit_end;
372 unsigned int version;
373 const char *database_path = notmuch_database_get_path (notmuch);
377 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
378 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
380 notmuch->xapian_db = notmuch->writable_xapian_db;
382 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
385 /* Check version. As of database version 3, we represent
386 * changes in terms of features, so assume a version bump
387 * means a dramatically incompatible change. */
388 version = notmuch_database_get_version (notmuch);
389 if (version > NOTMUCH_DATABASE_VERSION) {
390 IGNORE_RESULT (asprintf (&message,
391 "Error: Notmuch database at %s\n"
392 " has a newer database format version (%u) than supported by this\n"
393 " version of notmuch (%u).\n",
394 database_path, version, NOTMUCH_DATABASE_VERSION));
395 notmuch_database_destroy (notmuch);
397 status = NOTMUCH_STATUS_FILE_ERROR;
401 /* Check features. */
402 incompat_features = NULL;
403 notmuch->features = _notmuch_database_parse_features (
404 notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
405 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
407 if (incompat_features) {
408 IGNORE_RESULT (asprintf (&message,
409 "Error: Notmuch database at %s\n"
410 " requires features (%s)\n"
411 " not supported by this version of notmuch.\n",
412 database_path, incompat_features));
413 notmuch_database_destroy (notmuch);
415 status = NOTMUCH_STATUS_FILE_ERROR;
419 _load_database_state (notmuch);
421 notmuch->query_parser = new Xapian::QueryParser;
422 notmuch->term_gen = new Xapian::TermGenerator;
423 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
424 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
425 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
427 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
429 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
430 notmuch->query_parser->set_database (*notmuch->xapian_db);
431 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
432 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
433 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
434 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
435 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
437 /* Configuration information is needed to set up query parser */
438 status = _notmuch_config_load_from_database (notmuch);
443 status = _notmuch_config_load_from_file (notmuch, key_file);
447 status = _choose_dir (notmuch, profile,
448 NOTMUCH_CONFIG_HOOK_DIR,
456 status = _choose_dir (notmuch, profile,
457 NOTMUCH_CONFIG_BACKUP_DIR,
464 status = _notmuch_config_load_defaults (notmuch);
468 autocommit_str = notmuch_config_get (notmuch, NOTMUCH_CONFIG_AUTOCOMMIT);
469 if (unlikely (! autocommit_str)) {
470 INTERNAL_ERROR ("missing configuration for autocommit");
472 notmuch->transaction_threshold = strtoul (autocommit_str, &autocommit_end, 10);
473 if (*autocommit_end != '\0')
474 INTERNAL_ERROR ("Malformed database database.autocommit value: %s", autocommit_str);
476 status = _notmuch_database_setup_standard_query_fields (notmuch);
480 status = _notmuch_database_setup_user_query_fields (notmuch);
484 } catch (const Xapian::Error &error) {
485 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
486 error.get_msg ().c_str ()));
487 notmuch_database_destroy (notmuch);
489 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
493 *message_ptr = message;
498 notmuch_database_open_with_config (const char *database_path,
499 notmuch_database_mode_t mode,
500 const char *config_path,
502 notmuch_database_t **database,
503 char **status_string)
505 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
506 void *local = talloc_new (NULL);
507 notmuch_database_t *notmuch = NULL;
508 char *message = NULL;
509 GKeyFile *key_file = NULL;
514 notmuch = _alloc_notmuch ();
516 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
520 status = _load_key_file (notmuch, config_path, profile, &key_file);
522 message = strdup ("Error: cannot load config file.\n");
526 if ((status = _choose_database_path (local, profile, key_file,
527 &database_path, &split,
531 status = _db_dir_exists (database_path, &message);
535 _set_database_path (notmuch, database_path);
537 status = _notmuch_choose_xapian_path (notmuch, database_path,
538 ¬much->xapian_path, &message);
542 status = _finish_open (notmuch, profile, mode, key_file, &message);
548 g_key_file_free (key_file);
552 *status_string = message;
560 talloc_free (notmuch);
563 notmuch->open = true;
569 notmuch_database_create (const char *path, notmuch_database_t **database)
571 char *status_string = NULL;
572 notmuch_status_t status;
574 status = notmuch_database_create_verbose (path, database,
578 fputs (status_string, stderr);
579 free (status_string);
586 notmuch_database_create_verbose (const char *path,
587 notmuch_database_t **database,
588 char **status_string)
590 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
594 notmuch_database_create_with_config (const char *database_path,
595 const char *config_path,
597 notmuch_database_t **database,
598 char **status_string)
600 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
601 notmuch_database_t *notmuch = NULL;
602 const char *notmuch_path = NULL;
603 char *message = NULL;
604 GKeyFile *key_file = NULL;
605 void *local = talloc_new (NULL);
611 notmuch = _alloc_notmuch ();
613 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
617 status = _load_key_file (notmuch, config_path, profile, &key_file);
619 message = strdup ("Error: cannot load config file.\n");
623 if ((status = _choose_database_path (local, profile, key_file,
624 &database_path, &split, &message)))
627 status = _db_dir_exists (database_path, &message);
631 _set_database_path (notmuch, database_path);
633 if (key_file && ! split) {
634 char *mail_root = notmuch_canonicalize_file_name (
635 g_key_file_get_value (key_file, "database", "mail_root", NULL));
636 char *db_path = notmuch_canonicalize_file_name (database_path);
638 split = (mail_root && (0 != strcmp (mail_root, db_path)));
645 notmuch_path = database_path;
647 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
648 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
652 err = mkdir (notmuch_path, 0755);
654 if (errno == EEXIST) {
655 status = NOTMUCH_STATUS_DATABASE_EXISTS;
656 talloc_free (notmuch);
659 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
660 notmuch_path, strerror (errno)));
661 status = NOTMUCH_STATUS_FILE_ERROR;
667 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
668 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
672 status = _trial_open (notmuch->xapian_path, &message);
673 if (status == NOTMUCH_STATUS_SUCCESS) {
674 notmuch_database_destroy (notmuch);
676 status = NOTMUCH_STATUS_DATABASE_EXISTS;
683 status = _finish_open (notmuch,
685 NOTMUCH_DATABASE_MODE_READ_WRITE,
691 /* Upgrade doesn't add these feature to existing databases, but
692 * new databases have them. */
693 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
694 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
695 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
697 status = notmuch_database_upgrade (notmuch, NULL, NULL);
699 notmuch_database_close (notmuch);
707 g_key_file_free (key_file);
711 *status_string = message;
718 talloc_free (notmuch);
723 notmuch_database_reopen (notmuch_database_t *notmuch,
724 notmuch_database_mode_t new_mode)
726 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
728 if (notmuch->xapian_db == NULL) {
729 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
730 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
734 if (cur_mode == new_mode &&
735 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
736 notmuch->xapian_db->reopen ();
738 notmuch->xapian_db->close ();
740 delete notmuch->xapian_db;
741 notmuch->xapian_db = NULL;
742 /* no need to free the same object twice */
743 notmuch->writable_xapian_db = NULL;
745 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
746 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
748 notmuch->xapian_db = notmuch->writable_xapian_db;
750 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
755 _load_database_state (notmuch);
756 } catch (const Xapian::Error &error) {
757 if (! notmuch->exception_reported) {
758 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
759 error.get_msg ().c_str ());
760 notmuch->exception_reported = true;
762 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
766 notmuch->open = true;
767 return NOTMUCH_STATUS_SUCCESS;
770 static notmuch_status_t
771 _maybe_load_config_from_database (notmuch_database_t *notmuch,
773 const char *database_path,
776 char *message; /* ignored */
778 if (_db_dir_exists (database_path, &message))
779 return NOTMUCH_STATUS_NO_DATABASE;
781 _set_database_path (notmuch, database_path);
783 if (_notmuch_choose_xapian_path (notmuch, database_path, ¬much->xapian_path, &message))
784 return NOTMUCH_STATUS_NO_DATABASE;
786 (void) _finish_open (notmuch, profile, NOTMUCH_DATABASE_MODE_READ_ONLY, key_file, &message);
788 return NOTMUCH_STATUS_SUCCESS;
792 notmuch_database_load_config (const char *database_path,
793 const char *config_path,
795 notmuch_database_t **database,
796 char **status_string)
798 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS, warning = NOTMUCH_STATUS_SUCCESS;
799 void *local = talloc_new (NULL);
800 notmuch_database_t *notmuch = NULL;
801 char *message = NULL;
802 GKeyFile *key_file = NULL;
807 notmuch = _alloc_notmuch ();
809 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
813 status = _load_key_file (notmuch, config_path, profile, &key_file);
815 case NOTMUCH_STATUS_SUCCESS:
817 case NOTMUCH_STATUS_NO_CONFIG:
821 message = strdup ("Error: cannot load config file.\n");
825 status = _choose_database_path (local, profile, key_file,
826 &database_path, &split, &message);
828 case NOTMUCH_STATUS_NO_DATABASE:
829 case NOTMUCH_STATUS_SUCCESS:
839 status = _maybe_load_config_from_database (notmuch, key_file, database_path, profile);
841 case NOTMUCH_STATUS_NO_DATABASE:
842 case NOTMUCH_STATUS_SUCCESS:
852 status = _notmuch_config_load_from_file (notmuch, key_file);
856 status = _notmuch_config_load_defaults (notmuch);
864 *status_string = message;