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_hook_dir (notmuch_database_t *notmuch,
83 hook_dir = notmuch_config_get (notmuch, NOTMUCH_CONFIG_HOOK_DIR);
86 return NOTMUCH_STATUS_SUCCESS;
88 config = _xdg_dir (notmuch, "XDG_CONFIG_HOME", ".config", profile);
90 return NOTMUCH_STATUS_PATH_ERROR;
92 hook_dir = talloc_asprintf (notmuch, "%s/hooks", config);
94 err = stat (hook_dir, &st);
96 if (errno == ENOENT) {
97 char *notmuch_path = dirname (talloc_strdup (notmuch, notmuch->xapian_path));
98 hook_dir = talloc_asprintf (notmuch, "%s/hooks", notmuch_path);
100 IGNORE_RESULT (asprintf (message, "Error: Cannot stat %s: %s.\n",
101 hook_dir, strerror (errno)));
102 return NOTMUCH_STATUS_FILE_ERROR;
106 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_HOOK_DIR, hook_dir);
108 return NOTMUCH_STATUS_SUCCESS;
111 static notmuch_status_t
112 _load_key_file (const char *path,
116 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
117 void *local = talloc_new (NULL);
119 if (path && EMPTY_STRING (path))
123 path = getenv ("NOTMUCH_CONFIG");
126 const char *dir = _xdg_dir (local, "XDG_CONFIG_HOME", ".config", profile);
129 path = talloc_asprintf (local, "%s/config", dir);
130 if (access (path, R_OK) != 0)
136 const char *home = getenv ("HOME");
138 path = talloc_asprintf (local, "%s/.notmuch-config", home);
141 profile = getenv ("NOTMUCH_PROFILE");
144 path = talloc_asprintf (local, "%s.%s", path, profile);
147 *key_file = g_key_file_new ();
148 if (! g_key_file_load_from_file (*key_file, path, G_KEY_FILE_NONE, NULL)) {
149 status = NOTMUCH_STATUS_NO_CONFIG;
157 static notmuch_status_t
158 _db_dir_exists (const char *database_path, char **message)
163 err = stat (database_path, &st);
165 IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: %s.\n",
166 database_path, strerror (errno)));
167 return NOTMUCH_STATUS_FILE_ERROR;
170 if (! S_ISDIR (st.st_mode)) {
171 IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: "
172 "Not a directory.\n",
174 return NOTMUCH_STATUS_FILE_ERROR;
177 return NOTMUCH_STATUS_SUCCESS;
180 static notmuch_status_t
181 _choose_database_path (void *ctx,
182 const char *config_path,
185 const char **database_path,
189 notmuch_status_t status;
191 status = _load_key_file (config_path, profile, key_file);
193 *message = strdup ("Error: cannot load config file.\n");
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);
204 *database_path = talloc_strdup (ctx, path);
209 if (! *database_path) {
210 *database_path = _xdg_dir (ctx, "XDG_DATA_HOME", ".local/share", profile);
214 if (*database_path == NULL) {
215 *message = strdup ("Error: Cannot open a database for a NULL path.\n");
216 return NOTMUCH_STATUS_NULL_POINTER;
219 if (*database_path[0] != '/') {
220 *message = strdup ("Error: Database path must be absolute.\n");
221 return NOTMUCH_STATUS_PATH_ERROR;
223 return NOTMUCH_STATUS_SUCCESS;
229 notmuch_database_t *notmuch;
231 notmuch = talloc_zero (NULL, notmuch_database_t);
235 notmuch->exception_reported = false;
236 notmuch->status_string = NULL;
237 notmuch->writable_xapian_db = NULL;
238 notmuch->atomic_nesting = 0;
243 static notmuch_status_t
244 _trial_open (const char *xapian_path, char **message_ptr)
247 Xapian::Database db (xapian_path);
248 } catch (const Xapian::DatabaseOpeningError &error) {
249 IGNORE_RESULT (asprintf (message_ptr,
250 "Cannot open Xapian database at %s: %s\n",
252 error.get_msg ().c_str ()));
253 return NOTMUCH_STATUS_PATH_ERROR;
254 } catch (const Xapian::Error &error) {
255 IGNORE_RESULT (asprintf (message_ptr,
256 "A Xapian exception occurred opening database: %s\n",
257 error.get_msg ().c_str ()));
258 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
260 return NOTMUCH_STATUS_SUCCESS;
264 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
265 const char **xapian_path, char **message_ptr)
267 notmuch_status_t status;
268 const char *trial_path, *notmuch_path;
270 status = _db_dir_exists (database_path, message_ptr);
274 trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
275 status = _trial_open (trial_path, message_ptr);
276 if (status != NOTMUCH_STATUS_PATH_ERROR)
279 notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
280 status = _db_dir_exists (notmuch_path, message_ptr);
284 trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
285 status = _trial_open (trial_path, message_ptr);
288 if (status == NOTMUCH_STATUS_SUCCESS)
289 *xapian_path = trial_path;
294 _set_database_path (notmuch_database_t *notmuch,
295 const char *database_path)
297 char *path = talloc_strdup (notmuch, database_path);
299 strip_trailing (path, '/');
301 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
308 static int initialized = 0;
310 /* Initialize the GLib type system and threads */
311 #if ! GLIB_CHECK_VERSION (2, 35, 1)
315 /* Initialize gmime */
322 static notmuch_status_t
323 _finish_open (notmuch_database_t *notmuch,
325 notmuch_database_mode_t mode,
329 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
330 char *incompat_features;
331 char *message = NULL;
332 unsigned int version;
333 const char *database_path = notmuch_database_get_path (notmuch);
336 std::string last_thread_id;
337 std::string last_mod;
339 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
340 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
342 notmuch->xapian_db = notmuch->writable_xapian_db;
344 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
347 /* Check version. As of database version 3, we represent
348 * changes in terms of features, so assume a version bump
349 * means a dramatically incompatible change. */
350 version = notmuch_database_get_version (notmuch);
351 if (version > NOTMUCH_DATABASE_VERSION) {
352 IGNORE_RESULT (asprintf (&message,
353 "Error: Notmuch database at %s\n"
354 " has a newer database format version (%u) than supported by this\n"
355 " version of notmuch (%u).\n",
356 database_path, version, NOTMUCH_DATABASE_VERSION));
357 notmuch_database_destroy (notmuch);
359 status = NOTMUCH_STATUS_FILE_ERROR;
363 /* Check features. */
364 incompat_features = NULL;
365 notmuch->features = _notmuch_database_parse_features (
366 notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
367 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
369 if (incompat_features) {
370 IGNORE_RESULT (asprintf (&message,
371 "Error: Notmuch database at %s\n"
372 " requires features (%s)\n"
373 " not supported by this version of notmuch.\n",
374 database_path, incompat_features));
375 notmuch_database_destroy (notmuch);
377 status = NOTMUCH_STATUS_FILE_ERROR;
381 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
382 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
383 if (last_thread_id.empty ()) {
384 notmuch->last_thread_id = 0;
389 str = last_thread_id.c_str ();
390 notmuch->last_thread_id = strtoull (str, &end, 16);
392 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
395 /* Get current highest revision number. */
396 last_mod = notmuch->xapian_db->get_value_upper_bound (
397 NOTMUCH_VALUE_LAST_MOD);
398 if (last_mod.empty ())
399 notmuch->revision = 0;
401 notmuch->revision = Xapian::sortable_unserialise (last_mod);
402 notmuch->uuid = talloc_strdup (
403 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
405 notmuch->query_parser = new Xapian::QueryParser;
406 notmuch->term_gen = new Xapian::TermGenerator;
407 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
408 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
409 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
411 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
413 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
414 notmuch->query_parser->set_database (*notmuch->xapian_db);
415 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
416 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
417 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
418 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
419 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
421 /* Configuration information is needed to set up query parser */
422 status = _notmuch_config_load_from_database (notmuch);
427 status = _notmuch_config_load_from_file (notmuch, key_file);
431 status = _choose_hook_dir (notmuch, profile, &message);
435 status = _notmuch_config_load_defaults (notmuch);
439 status = _notmuch_database_setup_standard_query_fields (notmuch);
443 status = _notmuch_database_setup_user_query_fields (notmuch);
447 } catch (const Xapian::Error &error) {
448 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
449 error.get_msg ().c_str ()));
450 notmuch_database_destroy (notmuch);
452 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
456 *message_ptr = message;
461 notmuch_database_open_with_config (const char *database_path,
462 notmuch_database_mode_t mode,
463 const char *config_path,
465 notmuch_database_t **database,
466 char **status_string)
468 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
469 void *local = talloc_new (NULL);
470 notmuch_database_t *notmuch = NULL;
471 char *message = NULL;
472 GKeyFile *key_file = NULL;
477 notmuch = _alloc_notmuch ();
479 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
483 if ((status = _choose_database_path (local, config_path, profile,
484 &key_file, &database_path, &split,
488 status = _db_dir_exists (database_path, &message);
492 _set_database_path (notmuch, database_path);
494 status = _notmuch_choose_xapian_path (notmuch, database_path,
495 ¬much->xapian_path, &message);
499 status = _finish_open (notmuch, profile, mode, key_file, &message);
505 g_key_file_free (key_file);
509 *status_string = message;
517 talloc_free (notmuch);
520 notmuch->open = true;
526 notmuch_database_create (const char *path, notmuch_database_t **database)
528 char *status_string = NULL;
529 notmuch_status_t status;
531 status = notmuch_database_create_verbose (path, database,
535 fputs (status_string, stderr);
536 free (status_string);
543 notmuch_database_create_verbose (const char *path,
544 notmuch_database_t **database,
545 char **status_string)
547 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
551 notmuch_database_create_with_config (const char *database_path,
552 const char *config_path,
554 notmuch_database_t **database,
555 char **status_string)
557 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
558 notmuch_database_t *notmuch = NULL;
559 const char *notmuch_path = NULL;
560 char *message = NULL;
561 GKeyFile *key_file = NULL;
562 void *local = talloc_new (NULL);
568 notmuch = _alloc_notmuch ();
570 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
576 if ((status = _choose_database_path (local, config_path, profile,
577 &key_file, &database_path, &split,
581 status = _db_dir_exists (database_path, &message);
585 _set_database_path (notmuch, database_path);
587 if (key_file && ! split) {
588 char *mail_root = canonicalize_file_name (
589 g_key_file_get_value (key_file, "database", "mail_root", NULL));
590 char *db_path = canonicalize_file_name (database_path);
592 split = (mail_root && (0 != strcmp (mail_root, db_path)));
599 notmuch_path = database_path;
601 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
602 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
606 err = mkdir (notmuch_path, 0755);
608 if (errno == EEXIST) {
609 status = NOTMUCH_STATUS_DATABASE_EXISTS;
610 talloc_free (notmuch);
613 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
614 notmuch_path, strerror (errno)));
615 status = NOTMUCH_STATUS_FILE_ERROR;
621 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
622 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
626 status = _trial_open (notmuch->xapian_path, &message);
627 if (status == NOTMUCH_STATUS_SUCCESS) {
628 notmuch_database_destroy (notmuch);
630 status = NOTMUCH_STATUS_DATABASE_EXISTS;
634 status = _finish_open (notmuch,
636 NOTMUCH_DATABASE_MODE_READ_WRITE,
642 /* Upgrade doesn't add these feature to existing databases, but
643 * new databases have them. */
644 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
645 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
646 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
648 status = notmuch_database_upgrade (notmuch, NULL, NULL);
650 notmuch_database_close (notmuch);
658 g_key_file_free (key_file);
662 *status_string = message;
669 talloc_free (notmuch);
674 notmuch_database_reopen (notmuch_database_t *notmuch,
675 notmuch_database_mode_t new_mode)
677 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
679 if (notmuch->xapian_db == NULL) {
680 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
681 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
685 if (cur_mode == new_mode &&
686 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
687 notmuch->xapian_db->reopen ();
689 notmuch->xapian_db->close ();
691 delete notmuch->xapian_db;
692 notmuch->xapian_db = NULL;
693 /* no need to free the same object twice */
694 notmuch->writable_xapian_db = NULL;
696 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
697 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
699 notmuch->xapian_db = notmuch->writable_xapian_db;
701 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
705 } catch (const Xapian::Error &error) {
706 if (! notmuch->exception_reported) {
707 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
708 error.get_msg ().c_str ());
709 notmuch->exception_reported = true;
711 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
715 notmuch->open = true;
716 return NOTMUCH_STATUS_SUCCESS;