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;
261 static notmuch_status_t
262 _choose_xapian_path (void *ctx, const char *database_path, const char **xapian_path,
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 = _choose_xapian_path (notmuch, database_path, ¬much->xapian_path, &message);
496 status = _finish_open (notmuch, profile, mode, key_file, &message);
502 g_key_file_free (key_file);
506 *status_string = message;
514 talloc_free (notmuch);
517 notmuch->open = true;
523 notmuch_database_create (const char *path, notmuch_database_t **database)
525 char *status_string = NULL;
526 notmuch_status_t status;
528 status = notmuch_database_create_verbose (path, database,
532 fputs (status_string, stderr);
533 free (status_string);
540 notmuch_database_create_verbose (const char *path,
541 notmuch_database_t **database,
542 char **status_string)
544 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
548 notmuch_database_create_with_config (const char *database_path,
549 const char *config_path,
551 notmuch_database_t **database,
552 char **status_string)
554 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
555 notmuch_database_t *notmuch = NULL;
556 const char *notmuch_path = NULL;
557 char *message = NULL;
558 GKeyFile *key_file = NULL;
559 void *local = talloc_new (NULL);
565 notmuch = _alloc_notmuch ();
567 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
573 if ((status = _choose_database_path (local, config_path, profile,
574 &key_file, &database_path, &split,
578 status = _db_dir_exists (database_path, &message);
582 _set_database_path (notmuch, database_path);
584 if (key_file && ! split) {
585 char *mail_root = canonicalize_file_name (
586 g_key_file_get_value (key_file, "database", "mail_root", NULL));
587 char *db_path = canonicalize_file_name (database_path);
589 split = (mail_root && (0 != strcmp (mail_root, db_path)));
596 notmuch_path = database_path;
598 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
599 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
603 err = mkdir (notmuch_path, 0755);
605 if (errno == EEXIST) {
606 status = NOTMUCH_STATUS_DATABASE_EXISTS;
607 talloc_free (notmuch);
610 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
611 notmuch_path, strerror (errno)));
612 status = NOTMUCH_STATUS_FILE_ERROR;
618 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
619 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
623 status = _trial_open (notmuch->xapian_path, &message);
624 if (status == NOTMUCH_STATUS_SUCCESS) {
625 notmuch_database_destroy (notmuch);
627 status = NOTMUCH_STATUS_DATABASE_EXISTS;
631 status = _finish_open (notmuch,
633 NOTMUCH_DATABASE_MODE_READ_WRITE,
639 /* Upgrade doesn't add these feature to existing databases, but
640 * new databases have them. */
641 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
642 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
643 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
645 status = notmuch_database_upgrade (notmuch, NULL, NULL);
647 notmuch_database_close (notmuch);
655 g_key_file_free (key_file);
659 *status_string = message;
666 talloc_free (notmuch);
671 notmuch_database_reopen (notmuch_database_t *notmuch,
672 notmuch_database_mode_t new_mode)
674 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
676 if (notmuch->xapian_db == NULL) {
677 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
678 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
682 if (cur_mode == new_mode &&
683 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
684 notmuch->xapian_db->reopen ();
686 notmuch->xapian_db->close ();
688 delete notmuch->xapian_db;
689 notmuch->xapian_db = NULL;
690 /* no need to free the same object twice */
691 notmuch->writable_xapian_db = NULL;
693 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
694 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
696 notmuch->xapian_db = notmuch->writable_xapian_db;
698 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
702 } catch (const Xapian::Error &error) {
703 if (! notmuch->exception_reported) {
704 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
705 error.get_msg ().c_str ());
706 notmuch->exception_reported = true;
708 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
712 notmuch->open = true;
713 return NOTMUCH_STATUS_SUCCESS;