2 #include "database-private.h"
3 #include "parse-time-vrp.h"
5 #if HAVE_XAPIAN_DB_RETRY_LOCK
6 #define DB_ACTION (Xapian::DB_CREATE_OR_OPEN | Xapian::DB_RETRY_LOCK)
8 #define DB_ACTION Xapian::DB_CREATE_OR_OPEN
12 notmuch_database_open (const char *path,
13 notmuch_database_mode_t mode,
14 notmuch_database_t **database)
16 char *status_string = NULL;
17 notmuch_status_t status;
19 status = notmuch_database_open_verbose (path, mode, database,
23 fputs (status_string, stderr);
31 notmuch_database_open_verbose (const char *path,
32 notmuch_database_mode_t mode,
33 notmuch_database_t **database,
36 return notmuch_database_open_with_config (path, mode, "", NULL,
37 database, status_string);
42 const char *xdg_root_variable,
43 const char *xdg_prefix,
44 const char *profile_name)
46 const char *xdg_root = getenv (xdg_root_variable);
49 const char *home = getenv ("HOME");
51 if (! home) return NULL;
53 xdg_root = talloc_asprintf (ctx,
60 profile_name = getenv ("NOTMUCH_PROFILE");
63 profile_name = "default";
65 return talloc_asprintf (ctx,
71 static notmuch_status_t
72 _choose_hook_dir (notmuch_database_t *notmuch,
81 hook_dir = notmuch_config_get (notmuch, NOTMUCH_CONFIG_HOOK_DIR);
84 return NOTMUCH_STATUS_SUCCESS;
86 config = _xdg_dir (notmuch, "XDG_CONFIG_HOME", ".config", profile);
88 return NOTMUCH_STATUS_PATH_ERROR;
90 hook_dir = talloc_asprintf (notmuch, "%s/hooks", config);
92 err = stat (hook_dir, &st);
94 if (errno == ENOENT) {
95 const char *database_path = notmuch_database_get_path (notmuch);
96 hook_dir = talloc_asprintf (notmuch, "%s/.notmuch/hooks", database_path);
98 IGNORE_RESULT (asprintf (message, "Error: Cannot stat %s: %s.\n",
99 hook_dir, strerror (errno)));
100 return NOTMUCH_STATUS_FILE_ERROR;
104 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_HOOK_DIR, hook_dir);
106 return NOTMUCH_STATUS_SUCCESS;
109 static notmuch_status_t
110 _load_key_file (const char *path,
114 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
115 void *local = talloc_new (NULL);
117 if (path && EMPTY_STRING (path))
121 path = getenv ("NOTMUCH_CONFIG");
124 const char *dir = _xdg_dir (local, "XDG_CONFIG_HOME", ".config", profile);
127 path = talloc_asprintf (local, "%s/config", dir);
128 if (access (path, R_OK) != 0)
134 const char *home = getenv ("HOME");
136 path = talloc_asprintf (local, "%s/.notmuch-config", home);
139 profile = getenv ("NOTMUCH_PROFILE");
142 path = talloc_asprintf (local, "%s.%s", path, profile);
145 *key_file = g_key_file_new ();
146 if (! g_key_file_load_from_file (*key_file, path, G_KEY_FILE_NONE, NULL)) {
147 status = NOTMUCH_STATUS_NO_CONFIG;
155 static notmuch_status_t
156 _db_dir_exists (const char *database_path, char **message)
161 err = stat (database_path, &st);
163 IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: %s.\n",
164 database_path, strerror (errno)));
165 return NOTMUCH_STATUS_FILE_ERROR;
168 if (! S_ISDIR (st.st_mode)) {
169 IGNORE_RESULT (asprintf (message, "Error: Cannot open database at %s: "
170 "Not a directory.\n",
172 return NOTMUCH_STATUS_FILE_ERROR;
175 return NOTMUCH_STATUS_SUCCESS;
178 static notmuch_status_t
179 _choose_database_path (void *ctx,
180 const char *config_path,
183 const char **database_path,
187 notmuch_status_t status;
189 status = _load_key_file (config_path, profile, key_file);
191 *message = strdup ("Error: cannot load config file.\n");
195 if (! *database_path) {
196 *database_path = getenv ("NOTMUCH_DATABASE");
199 if (! *database_path && *key_file) {
200 char *path = g_key_file_get_value (*key_file, "database", "path", NULL);
202 *database_path = talloc_strdup (ctx, path);
207 if (! *database_path) {
208 *database_path = _xdg_dir (ctx, "XDG_DATA_HOME", ".local/share", profile);
212 if (*database_path == NULL) {
213 *message = strdup ("Error: Cannot open a database for a NULL path.\n");
214 return NOTMUCH_STATUS_NULL_POINTER;
217 if (*database_path[0] != '/') {
218 *message = strdup ("Error: Database path must be absolute.\n");
219 return NOTMUCH_STATUS_PATH_ERROR;
221 return NOTMUCH_STATUS_SUCCESS;
227 notmuch_database_t *notmuch;
229 notmuch = talloc_zero (NULL, notmuch_database_t);
233 notmuch->exception_reported = false;
234 notmuch->status_string = NULL;
235 notmuch->writable_xapian_db = NULL;
236 notmuch->atomic_nesting = 0;
241 static notmuch_status_t
242 _trial_open (const char *xapian_path, char **message_ptr)
245 Xapian::Database db (xapian_path);
246 } catch (const Xapian::DatabaseOpeningError &error) {
247 IGNORE_RESULT (asprintf (message_ptr,
248 "Cannot open Xapian database at %s: %s\n",
250 error.get_msg ().c_str ()));
251 return NOTMUCH_STATUS_PATH_ERROR;
252 } catch (const Xapian::Error &error) {
253 IGNORE_RESULT (asprintf (message_ptr,
254 "A Xapian exception occurred opening database: %s\n",
255 error.get_msg ().c_str ()));
256 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
258 return NOTMUCH_STATUS_SUCCESS;
262 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
263 const char **xapian_path, char **message_ptr)
265 notmuch_status_t status;
266 const char *trial_path, *notmuch_path;
268 status = _db_dir_exists (database_path, message_ptr);
272 trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
273 status = _trial_open (trial_path, message_ptr);
274 if (status != NOTMUCH_STATUS_PATH_ERROR)
277 notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
278 status = _db_dir_exists (notmuch_path, message_ptr);
282 trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
283 status = _trial_open (trial_path, message_ptr);
286 if (status == NOTMUCH_STATUS_SUCCESS)
287 *xapian_path = trial_path;
292 _set_database_path (notmuch_database_t *notmuch,
293 const char *database_path)
295 char *path = talloc_strdup (notmuch, database_path);
297 strip_trailing (path, '/');
299 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
306 static int initialized = 0;
308 /* Initialize the GLib type system and threads */
309 #if ! GLIB_CHECK_VERSION (2, 35, 1)
313 /* Initialize gmime */
320 static notmuch_status_t
321 _finish_open (notmuch_database_t *notmuch,
323 notmuch_database_mode_t mode,
327 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
328 char *incompat_features;
329 char *message = NULL;
330 unsigned int version;
331 const char *database_path = notmuch_database_get_path (notmuch);
334 std::string last_thread_id;
335 std::string last_mod;
337 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
338 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
340 notmuch->xapian_db = notmuch->writable_xapian_db;
342 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
345 /* Check version. As of database version 3, we represent
346 * changes in terms of features, so assume a version bump
347 * means a dramatically incompatible change. */
348 version = notmuch_database_get_version (notmuch);
349 if (version > NOTMUCH_DATABASE_VERSION) {
350 IGNORE_RESULT (asprintf (&message,
351 "Error: Notmuch database at %s\n"
352 " has a newer database format version (%u) than supported by this\n"
353 " version of notmuch (%u).\n",
354 database_path, version, NOTMUCH_DATABASE_VERSION));
355 notmuch_database_destroy (notmuch);
357 status = NOTMUCH_STATUS_FILE_ERROR;
361 /* Check features. */
362 incompat_features = NULL;
363 notmuch->features = _notmuch_database_parse_features (
364 notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
365 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
367 if (incompat_features) {
368 IGNORE_RESULT (asprintf (&message,
369 "Error: Notmuch database at %s\n"
370 " requires features (%s)\n"
371 " not supported by this version of notmuch.\n",
372 database_path, incompat_features));
373 notmuch_database_destroy (notmuch);
375 status = NOTMUCH_STATUS_FILE_ERROR;
379 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
380 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
381 if (last_thread_id.empty ()) {
382 notmuch->last_thread_id = 0;
387 str = last_thread_id.c_str ();
388 notmuch->last_thread_id = strtoull (str, &end, 16);
390 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
393 /* Get current highest revision number. */
394 last_mod = notmuch->xapian_db->get_value_upper_bound (
395 NOTMUCH_VALUE_LAST_MOD);
396 if (last_mod.empty ())
397 notmuch->revision = 0;
399 notmuch->revision = Xapian::sortable_unserialise (last_mod);
400 notmuch->uuid = talloc_strdup (
401 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
403 notmuch->query_parser = new Xapian::QueryParser;
404 notmuch->term_gen = new Xapian::TermGenerator;
405 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
406 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
407 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
409 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
411 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
412 notmuch->query_parser->set_database (*notmuch->xapian_db);
413 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
414 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
415 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
416 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
417 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
419 /* Configuration information is needed to set up query parser */
420 status = _notmuch_config_load_from_database (notmuch);
425 status = _notmuch_config_load_from_file (notmuch, key_file);
429 status = _choose_hook_dir (notmuch, profile, &message);
433 status = _notmuch_config_load_defaults (notmuch);
437 status = _notmuch_database_setup_standard_query_fields (notmuch);
441 status = _notmuch_database_setup_user_query_fields (notmuch);
445 } catch (const Xapian::Error &error) {
446 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
447 error.get_msg ().c_str ()));
448 notmuch_database_destroy (notmuch);
450 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
454 *message_ptr = message;
459 notmuch_database_open_with_config (const char *database_path,
460 notmuch_database_mode_t mode,
461 const char *config_path,
463 notmuch_database_t **database,
464 char **status_string)
466 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
467 void *local = talloc_new (NULL);
468 notmuch_database_t *notmuch = NULL;
469 char *message = NULL;
470 GKeyFile *key_file = NULL;
475 notmuch = _alloc_notmuch ();
477 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
481 if ((status = _choose_database_path (local, config_path, profile,
482 &key_file, &database_path, &split,
486 status = _db_dir_exists (database_path, &message);
490 _set_database_path (notmuch, database_path);
492 status = _notmuch_choose_xapian_path (notmuch, database_path,
493 ¬much->xapian_path, &message);
497 status = _finish_open (notmuch, profile, mode, key_file, &message);
503 g_key_file_free (key_file);
507 *status_string = message;
515 talloc_free (notmuch);
518 notmuch->open = true;
524 notmuch_database_create (const char *path, notmuch_database_t **database)
526 char *status_string = NULL;
527 notmuch_status_t status;
529 status = notmuch_database_create_verbose (path, database,
533 fputs (status_string, stderr);
534 free (status_string);
541 notmuch_database_create_verbose (const char *path,
542 notmuch_database_t **database,
543 char **status_string)
545 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
549 notmuch_database_create_with_config (const char *database_path,
550 const char *config_path,
552 notmuch_database_t **database,
553 char **status_string)
555 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
556 notmuch_database_t *notmuch = NULL;
557 const char *notmuch_path = NULL;
558 char *message = NULL;
559 GKeyFile *key_file = NULL;
560 void *local = talloc_new (NULL);
566 notmuch = _alloc_notmuch ();
568 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
574 if ((status = _choose_database_path (local, config_path, profile,
575 &key_file, &database_path, &split,
579 status = _db_dir_exists (database_path, &message);
583 _set_database_path (notmuch, database_path);
585 if (key_file && ! split) {
586 char *mail_root = canonicalize_file_name (
587 g_key_file_get_value (key_file, "database", "mail_root", NULL));
588 char *db_path = canonicalize_file_name (database_path);
590 split = (mail_root && (0 != strcmp (mail_root, db_path)));
597 notmuch_path = database_path;
599 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
600 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
604 err = mkdir (notmuch_path, 0755);
606 if (errno == EEXIST) {
607 status = NOTMUCH_STATUS_DATABASE_EXISTS;
608 talloc_free (notmuch);
611 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
612 notmuch_path, strerror (errno)));
613 status = NOTMUCH_STATUS_FILE_ERROR;
619 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
620 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
624 status = _trial_open (notmuch->xapian_path, &message);
625 if (status == NOTMUCH_STATUS_SUCCESS) {
626 notmuch_database_destroy (notmuch);
628 status = NOTMUCH_STATUS_DATABASE_EXISTS;
632 status = _finish_open (notmuch,
634 NOTMUCH_DATABASE_MODE_READ_WRITE,
640 /* Upgrade doesn't add these feature to existing databases, but
641 * new databases have them. */
642 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
643 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
644 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
646 status = notmuch_database_upgrade (notmuch, NULL, NULL);
648 notmuch_database_close (notmuch);
656 g_key_file_free (key_file);
660 *status_string = message;
667 talloc_free (notmuch);
672 notmuch_database_reopen (notmuch_database_t *notmuch,
673 notmuch_database_mode_t new_mode)
675 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
677 if (notmuch->xapian_db == NULL) {
678 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
679 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
683 if (cur_mode == new_mode &&
684 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
685 notmuch->xapian_db->reopen ();
687 notmuch->xapian_db->close ();
689 delete notmuch->xapian_db;
690 notmuch->xapian_db = NULL;
691 /* no need to free the same object twice */
692 notmuch->writable_xapian_db = NULL;
694 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
695 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
697 notmuch->xapian_db = notmuch->writable_xapian_db;
699 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
703 } catch (const Xapian::Error &error) {
704 if (! notmuch->exception_reported) {
705 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
706 error.get_msg ().c_str ());
707 notmuch->exception_reported = true;
709 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
713 notmuch->open = true;
714 return NOTMUCH_STATUS_SUCCESS;