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 (const char *path,
120 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
121 void *local = talloc_new (NULL);
123 if (path && EMPTY_STRING (path))
127 path = getenv ("NOTMUCH_CONFIG");
130 const char *dir = _xdg_dir (local, "XDG_CONFIG_HOME", ".config", profile);
133 path = talloc_asprintf (local, "%s/config", dir);
134 if (access (path, R_OK) != 0)
140 const char *home = getenv ("HOME");
142 path = talloc_asprintf (local, "%s/.notmuch-config", home);
145 profile = getenv ("NOTMUCH_PROFILE");
148 path = talloc_asprintf (local, "%s.%s", path, profile);
151 *key_file = g_key_file_new ();
152 if (! g_key_file_load_from_file (*key_file, path, G_KEY_FILE_NONE, NULL)) {
153 status = NOTMUCH_STATUS_NO_CONFIG;
161 static notmuch_status_t
162 _db_dir_exists (const char *database_path, char **message)
167 err = stat (database_path, &st);
169 IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: %s.\n",
170 database_path, strerror (errno)));
171 return NOTMUCH_STATUS_FILE_ERROR;
174 if (! S_ISDIR (st.st_mode)) {
175 IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: "
176 "Not a directory.\n",
178 return NOTMUCH_STATUS_FILE_ERROR;
181 return NOTMUCH_STATUS_SUCCESS;
184 static notmuch_status_t
185 _choose_database_path (void *ctx,
186 const char *config_path,
189 const char **database_path,
193 notmuch_status_t status;
195 status = _load_key_file (config_path, profile, key_file);
197 *message = strdup ("Error: cannot load config file.\n");
201 if (! *database_path) {
202 *database_path = getenv ("NOTMUCH_DATABASE");
205 if (! *database_path && *key_file) {
206 char *path = g_key_file_get_value (*key_file, "database", "path", NULL);
208 *database_path = talloc_strdup (ctx, path);
213 if (! *database_path) {
214 *database_path = _xdg_dir (ctx, "XDG_DATA_HOME", ".local/share", profile);
218 if (*database_path == NULL) {
219 *message = strdup ("Error: Cannot open a database for a NULL path.\n");
220 return NOTMUCH_STATUS_NULL_POINTER;
223 if (*database_path[0] != '/') {
224 *message = strdup ("Error: Database path must be absolute.\n");
225 return NOTMUCH_STATUS_PATH_ERROR;
227 return NOTMUCH_STATUS_SUCCESS;
233 notmuch_database_t *notmuch;
235 notmuch = talloc_zero (NULL, notmuch_database_t);
239 notmuch->exception_reported = false;
240 notmuch->status_string = NULL;
241 notmuch->writable_xapian_db = NULL;
242 notmuch->atomic_nesting = 0;
247 static notmuch_status_t
248 _trial_open (const char *xapian_path, char **message_ptr)
251 Xapian::Database db (xapian_path);
252 } catch (const Xapian::DatabaseOpeningError &error) {
253 IGNORE_RESULT (asprintf (message_ptr,
254 "Cannot open Xapian database at %s: %s\n",
256 error.get_msg ().c_str ()));
257 return NOTMUCH_STATUS_PATH_ERROR;
258 } catch (const Xapian::Error &error) {
259 IGNORE_RESULT (asprintf (message_ptr,
260 "A Xapian exception occurred opening database: %s\n",
261 error.get_msg ().c_str ()));
262 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
264 return NOTMUCH_STATUS_SUCCESS;
268 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
269 const char **xapian_path, char **message_ptr)
271 notmuch_status_t status;
272 const char *trial_path, *notmuch_path;
274 status = _db_dir_exists (database_path, message_ptr);
278 trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
279 status = _trial_open (trial_path, message_ptr);
280 if (status != NOTMUCH_STATUS_PATH_ERROR)
283 notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
284 status = _db_dir_exists (notmuch_path, message_ptr);
288 trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
289 status = _trial_open (trial_path, message_ptr);
292 if (status == NOTMUCH_STATUS_SUCCESS)
293 *xapian_path = trial_path;
298 _set_database_path (notmuch_database_t *notmuch,
299 const char *database_path)
301 char *path = talloc_strdup (notmuch, database_path);
303 strip_trailing (path, '/');
305 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
312 static int initialized = 0;
314 /* Initialize the GLib type system and threads */
315 #if ! GLIB_CHECK_VERSION (2, 35, 1)
319 /* Initialize gmime */
326 static notmuch_status_t
327 _finish_open (notmuch_database_t *notmuch,
329 notmuch_database_mode_t mode,
333 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
334 char *incompat_features;
335 char *message = NULL;
336 unsigned int version;
337 const char *database_path = notmuch_database_get_path (notmuch);
340 std::string last_thread_id;
341 std::string last_mod;
343 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
344 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
346 notmuch->xapian_db = notmuch->writable_xapian_db;
348 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
351 /* Check version. As of database version 3, we represent
352 * changes in terms of features, so assume a version bump
353 * means a dramatically incompatible change. */
354 version = notmuch_database_get_version (notmuch);
355 if (version > NOTMUCH_DATABASE_VERSION) {
356 IGNORE_RESULT (asprintf (&message,
357 "Error: Notmuch database at %s\n"
358 " has a newer database format version (%u) than supported by this\n"
359 " version of notmuch (%u).\n",
360 database_path, version, NOTMUCH_DATABASE_VERSION));
361 notmuch_database_destroy (notmuch);
363 status = NOTMUCH_STATUS_FILE_ERROR;
367 /* Check features. */
368 incompat_features = NULL;
369 notmuch->features = _notmuch_database_parse_features (
370 notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
371 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
373 if (incompat_features) {
374 IGNORE_RESULT (asprintf (&message,
375 "Error: Notmuch database at %s\n"
376 " requires features (%s)\n"
377 " not supported by this version of notmuch.\n",
378 database_path, incompat_features));
379 notmuch_database_destroy (notmuch);
381 status = NOTMUCH_STATUS_FILE_ERROR;
385 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
386 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
387 if (last_thread_id.empty ()) {
388 notmuch->last_thread_id = 0;
393 str = last_thread_id.c_str ();
394 notmuch->last_thread_id = strtoull (str, &end, 16);
396 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
399 /* Get current highest revision number. */
400 last_mod = notmuch->xapian_db->get_value_upper_bound (
401 NOTMUCH_VALUE_LAST_MOD);
402 if (last_mod.empty ())
403 notmuch->revision = 0;
405 notmuch->revision = Xapian::sortable_unserialise (last_mod);
406 notmuch->uuid = talloc_strdup (
407 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
409 notmuch->query_parser = new Xapian::QueryParser;
410 notmuch->term_gen = new Xapian::TermGenerator;
411 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
412 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
413 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
415 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
417 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
418 notmuch->query_parser->set_database (*notmuch->xapian_db);
419 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
420 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
421 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
422 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
423 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
425 /* Configuration information is needed to set up query parser */
426 status = _notmuch_config_load_from_database (notmuch);
431 status = _notmuch_config_load_from_file (notmuch, key_file);
435 status = _choose_dir (notmuch, profile,
436 NOTMUCH_CONFIG_HOOK_DIR,
444 status = _choose_dir (notmuch, profile,
445 NOTMUCH_CONFIG_BACKUP_DIR,
452 status = _notmuch_config_load_defaults (notmuch);
456 status = _notmuch_database_setup_standard_query_fields (notmuch);
460 status = _notmuch_database_setup_user_query_fields (notmuch);
464 } catch (const Xapian::Error &error) {
465 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
466 error.get_msg ().c_str ()));
467 notmuch_database_destroy (notmuch);
469 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
473 *message_ptr = message;
478 notmuch_database_open_with_config (const char *database_path,
479 notmuch_database_mode_t mode,
480 const char *config_path,
482 notmuch_database_t **database,
483 char **status_string)
485 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
486 void *local = talloc_new (NULL);
487 notmuch_database_t *notmuch = NULL;
488 char *message = NULL;
489 GKeyFile *key_file = NULL;
494 notmuch = _alloc_notmuch ();
496 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
500 if ((status = _choose_database_path (local, config_path, profile,
501 &key_file, &database_path, &split,
505 status = _db_dir_exists (database_path, &message);
509 _set_database_path (notmuch, database_path);
511 status = _notmuch_choose_xapian_path (notmuch, database_path,
512 ¬much->xapian_path, &message);
516 status = _finish_open (notmuch, profile, mode, key_file, &message);
522 g_key_file_free (key_file);
526 *status_string = message;
534 talloc_free (notmuch);
537 notmuch->open = true;
543 notmuch_database_create (const char *path, notmuch_database_t **database)
545 char *status_string = NULL;
546 notmuch_status_t status;
548 status = notmuch_database_create_verbose (path, database,
552 fputs (status_string, stderr);
553 free (status_string);
560 notmuch_database_create_verbose (const char *path,
561 notmuch_database_t **database,
562 char **status_string)
564 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
568 notmuch_database_create_with_config (const char *database_path,
569 const char *config_path,
571 notmuch_database_t **database,
572 char **status_string)
574 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
575 notmuch_database_t *notmuch = NULL;
576 const char *notmuch_path = NULL;
577 char *message = NULL;
578 GKeyFile *key_file = NULL;
579 void *local = talloc_new (NULL);
585 notmuch = _alloc_notmuch ();
587 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
593 if ((status = _choose_database_path (local, config_path, profile,
594 &key_file, &database_path, &split,
598 status = _db_dir_exists (database_path, &message);
602 _set_database_path (notmuch, database_path);
604 if (key_file && ! split) {
605 char *mail_root = canonicalize_file_name (
606 g_key_file_get_value (key_file, "database", "mail_root", NULL));
607 char *db_path = canonicalize_file_name (database_path);
609 split = (mail_root && (0 != strcmp (mail_root, db_path)));
616 notmuch_path = database_path;
618 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
619 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
623 err = mkdir (notmuch_path, 0755);
625 if (errno == EEXIST) {
626 status = NOTMUCH_STATUS_DATABASE_EXISTS;
627 talloc_free (notmuch);
630 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
631 notmuch_path, strerror (errno)));
632 status = NOTMUCH_STATUS_FILE_ERROR;
638 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
639 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
643 status = _trial_open (notmuch->xapian_path, &message);
644 if (status == NOTMUCH_STATUS_SUCCESS) {
645 notmuch_database_destroy (notmuch);
647 status = NOTMUCH_STATUS_DATABASE_EXISTS;
651 status = _finish_open (notmuch,
653 NOTMUCH_DATABASE_MODE_READ_WRITE,
659 /* Upgrade doesn't add these feature to existing databases, but
660 * new databases have them. */
661 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
662 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
663 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
665 status = notmuch_database_upgrade (notmuch, NULL, NULL);
667 notmuch_database_close (notmuch);
675 g_key_file_free (key_file);
679 *status_string = message;
686 talloc_free (notmuch);
691 notmuch_database_reopen (notmuch_database_t *notmuch,
692 notmuch_database_mode_t new_mode)
694 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
696 if (notmuch->xapian_db == NULL) {
697 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
698 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
702 if (cur_mode == new_mode &&
703 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
704 notmuch->xapian_db->reopen ();
706 notmuch->xapian_db->close ();
708 delete notmuch->xapian_db;
709 notmuch->xapian_db = NULL;
710 /* no need to free the same object twice */
711 notmuch->writable_xapian_db = NULL;
713 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
714 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
716 notmuch->xapian_db = notmuch->writable_xapian_db;
718 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
722 } catch (const Xapian::Error &error) {
723 if (! notmuch->exception_reported) {
724 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
725 error.get_msg ().c_str ());
726 notmuch->exception_reported = true;
728 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
732 notmuch->open = true;
733 return NOTMUCH_STATUS_SUCCESS;