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 *database_path = getenv ("MAILDIR");
227 if (! *database_path) {
228 notmuch_status_t status;
230 *database_path = talloc_asprintf (ctx, "%s/mail", getenv ("HOME"));
231 status = _db_dir_exists (*database_path, message);
233 *database_path = NULL;
237 if (*database_path == NULL) {
238 *message = strdup ("Error: could not locate database.\n");
239 return NOTMUCH_STATUS_NO_DATABASE;
242 if (*database_path[0] != '/') {
243 *message = strdup ("Error: Database path must be absolute.\n");
244 return NOTMUCH_STATUS_PATH_ERROR;
246 return NOTMUCH_STATUS_SUCCESS;
249 static notmuch_database_t *
252 notmuch_database_t *notmuch;
254 notmuch = talloc_zero (NULL, notmuch_database_t);
258 notmuch->exception_reported = false;
259 notmuch->status_string = NULL;
260 notmuch->writable_xapian_db = NULL;
261 notmuch->config_path = NULL;
262 notmuch->atomic_nesting = 0;
263 notmuch->transaction_count = 0;
264 notmuch->transaction_threshold = 0;
269 static notmuch_status_t
270 _trial_open (const char *xapian_path, char **message_ptr)
273 Xapian::Database db (xapian_path);
274 } catch (const Xapian::DatabaseOpeningError &error) {
275 IGNORE_RESULT (asprintf (message_ptr,
276 "Cannot open Xapian database at %s: %s\n",
278 error.get_msg ().c_str ()));
279 return NOTMUCH_STATUS_PATH_ERROR;
280 } catch (const Xapian::Error &error) {
281 IGNORE_RESULT (asprintf (message_ptr,
282 "A Xapian exception occurred opening database: %s\n",
283 error.get_msg ().c_str ()));
284 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
286 return NOTMUCH_STATUS_SUCCESS;
290 _notmuch_choose_xapian_path (void *ctx, const char *database_path,
291 const char **xapian_path, char **message_ptr)
293 notmuch_status_t status;
294 const char *trial_path, *notmuch_path;
296 status = _db_dir_exists (database_path, message_ptr);
300 trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
301 status = _trial_open (trial_path, message_ptr);
302 if (status != NOTMUCH_STATUS_PATH_ERROR)
308 notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
309 status = _db_dir_exists (notmuch_path, message_ptr);
313 trial_path = talloc_asprintf (ctx, "%s/xapian", notmuch_path);
314 status = _trial_open (trial_path, message_ptr);
317 if (status == NOTMUCH_STATUS_SUCCESS)
318 *xapian_path = trial_path;
323 _set_database_path (notmuch_database_t *notmuch,
324 const char *database_path)
326 char *path = talloc_strdup (notmuch, database_path);
328 strip_trailing (path, '/');
330 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
334 _load_database_state (notmuch_database_t *notmuch)
336 std::string last_thread_id;
337 std::string last_mod;
339 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
340 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
341 if (last_thread_id.empty ()) {
342 notmuch->last_thread_id = 0;
347 str = last_thread_id.c_str ();
348 notmuch->last_thread_id = strtoull (str, &end, 16);
350 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
353 /* Get current highest revision number. */
354 last_mod = notmuch->xapian_db->get_value_upper_bound (
355 NOTMUCH_VALUE_LAST_MOD);
356 if (last_mod.empty ())
357 notmuch->revision = 0;
359 notmuch->revision = Xapian::sortable_unserialise (last_mod);
360 notmuch->uuid = talloc_strdup (
361 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
364 static notmuch_status_t
365 _finish_open (notmuch_database_t *notmuch,
367 notmuch_database_mode_t mode,
371 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
372 char *incompat_features;
373 char *message = NULL;
374 const char *autocommit_str;
375 char *autocommit_end;
376 unsigned int version;
377 const char *database_path = notmuch_database_get_path (notmuch);
381 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
382 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
384 notmuch->xapian_db = notmuch->writable_xapian_db;
386 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
389 /* Check version. As of database version 3, we represent
390 * changes in terms of features, so assume a version bump
391 * means a dramatically incompatible change. */
392 version = notmuch_database_get_version (notmuch);
393 if (version > NOTMUCH_DATABASE_VERSION) {
394 IGNORE_RESULT (asprintf (&message,
395 "Error: Notmuch database at %s\n"
396 " has a newer database format version (%u) than supported by this\n"
397 " version of notmuch (%u).\n",
398 database_path, version, NOTMUCH_DATABASE_VERSION));
399 notmuch_database_destroy (notmuch);
401 status = NOTMUCH_STATUS_FILE_ERROR;
405 /* Check features. */
406 incompat_features = NULL;
407 notmuch->features = _notmuch_database_parse_features (
408 notmuch, notmuch->xapian_db->get_metadata ("features").c_str (),
409 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
411 if (incompat_features) {
412 IGNORE_RESULT (asprintf (&message,
413 "Error: Notmuch database at %s\n"
414 " requires features (%s)\n"
415 " not supported by this version of notmuch.\n",
416 database_path, incompat_features));
417 notmuch_database_destroy (notmuch);
419 status = NOTMUCH_STATUS_FILE_ERROR;
423 _load_database_state (notmuch);
425 notmuch->query_parser = new Xapian::QueryParser;
426 notmuch->term_gen = new Xapian::TermGenerator;
427 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
428 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
429 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
431 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
433 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
434 notmuch->query_parser->set_database (*notmuch->xapian_db);
435 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
436 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
437 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
438 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
439 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
441 /* Configuration information is needed to set up query parser */
442 status = _notmuch_config_load_from_database (notmuch);
447 status = _notmuch_config_load_from_file (notmuch, key_file);
451 status = _choose_dir (notmuch, profile,
452 NOTMUCH_CONFIG_HOOK_DIR,
460 status = _choose_dir (notmuch, profile,
461 NOTMUCH_CONFIG_BACKUP_DIR,
468 status = _notmuch_config_load_defaults (notmuch);
472 autocommit_str = notmuch_config_get (notmuch, NOTMUCH_CONFIG_AUTOCOMMIT);
473 if (unlikely (! autocommit_str)) {
474 INTERNAL_ERROR ("missing configuration for autocommit");
476 notmuch->transaction_threshold = strtoul (autocommit_str, &autocommit_end, 10);
477 if (*autocommit_end != '\0')
478 INTERNAL_ERROR ("Malformed database database.autocommit value: %s", autocommit_str);
480 status = _notmuch_database_setup_standard_query_fields (notmuch);
484 status = _notmuch_database_setup_user_query_fields (notmuch);
488 } catch (const Xapian::Error &error) {
489 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
490 error.get_msg ().c_str ()));
491 notmuch_database_destroy (notmuch);
493 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
497 *message_ptr = message;
502 notmuch_database_open_with_config (const char *database_path,
503 notmuch_database_mode_t mode,
504 const char *config_path,
506 notmuch_database_t **database,
507 char **status_string)
509 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
510 void *local = talloc_new (NULL);
511 notmuch_database_t *notmuch = NULL;
512 char *message = NULL;
513 GKeyFile *key_file = NULL;
518 notmuch = _alloc_notmuch ();
520 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
524 status = _load_key_file (notmuch, config_path, profile, &key_file);
526 message = strdup ("Error: cannot load config file.\n");
530 if ((status = _choose_database_path (local, profile, key_file,
531 &database_path, &split,
535 status = _db_dir_exists (database_path, &message);
539 _set_database_path (notmuch, database_path);
541 status = _notmuch_choose_xapian_path (notmuch, database_path,
542 ¬much->xapian_path, &message);
546 status = _finish_open (notmuch, profile, mode, key_file, &message);
552 g_key_file_free (key_file);
556 *status_string = message;
564 talloc_free (notmuch);
567 notmuch->open = true;
573 notmuch_database_create (const char *path, notmuch_database_t **database)
575 char *status_string = NULL;
576 notmuch_status_t status;
578 status = notmuch_database_create_verbose (path, database,
582 fputs (status_string, stderr);
583 free (status_string);
590 notmuch_database_create_verbose (const char *path,
591 notmuch_database_t **database,
592 char **status_string)
594 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
598 notmuch_database_create_with_config (const char *database_path,
599 const char *config_path,
601 notmuch_database_t **database,
602 char **status_string)
604 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
605 notmuch_database_t *notmuch = NULL;
606 const char *notmuch_path = NULL;
607 char *message = NULL;
608 GKeyFile *key_file = NULL;
609 void *local = talloc_new (NULL);
615 notmuch = _alloc_notmuch ();
617 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
621 status = _load_key_file (notmuch, config_path, profile, &key_file);
623 message = strdup ("Error: cannot load config file.\n");
627 if ((status = _choose_database_path (local, profile, key_file,
628 &database_path, &split, &message)))
631 status = _db_dir_exists (database_path, &message);
635 _set_database_path (notmuch, database_path);
637 if (key_file && ! split) {
638 char *mail_root = notmuch_canonicalize_file_name (
639 g_key_file_get_value (key_file, "database", "mail_root", NULL));
640 char *db_path = notmuch_canonicalize_file_name (database_path);
642 split = (mail_root && (0 != strcmp (mail_root, db_path)));
649 notmuch_path = database_path;
651 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
652 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
656 err = mkdir (notmuch_path, 0755);
658 if (errno == EEXIST) {
659 status = NOTMUCH_STATUS_DATABASE_EXISTS;
660 talloc_free (notmuch);
663 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
664 notmuch_path, strerror (errno)));
665 status = NOTMUCH_STATUS_FILE_ERROR;
671 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
672 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
676 status = _trial_open (notmuch->xapian_path, &message);
677 if (status == NOTMUCH_STATUS_SUCCESS) {
678 notmuch_database_destroy (notmuch);
680 status = NOTMUCH_STATUS_DATABASE_EXISTS;
687 status = _finish_open (notmuch,
689 NOTMUCH_DATABASE_MODE_READ_WRITE,
695 /* Upgrade doesn't add these feature to existing databases, but
696 * new databases have them. */
697 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
698 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
699 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
701 status = notmuch_database_upgrade (notmuch, NULL, NULL);
703 notmuch_database_close (notmuch);
711 g_key_file_free (key_file);
715 *status_string = message;
722 talloc_free (notmuch);
727 notmuch_database_reopen (notmuch_database_t *notmuch,
728 notmuch_database_mode_t new_mode)
730 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
732 if (notmuch->xapian_db == NULL) {
733 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
734 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
738 if (cur_mode == new_mode &&
739 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
740 notmuch->xapian_db->reopen ();
742 notmuch->xapian_db->close ();
744 delete notmuch->xapian_db;
745 notmuch->xapian_db = NULL;
746 /* no need to free the same object twice */
747 notmuch->writable_xapian_db = NULL;
749 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
750 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
752 notmuch->xapian_db = notmuch->writable_xapian_db;
754 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
759 _load_database_state (notmuch);
760 } catch (const Xapian::Error &error) {
761 if (! notmuch->exception_reported) {
762 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
763 error.get_msg ().c_str ());
764 notmuch->exception_reported = true;
766 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
770 notmuch->open = true;
771 return NOTMUCH_STATUS_SUCCESS;
774 static notmuch_status_t
775 _maybe_load_config_from_database (notmuch_database_t *notmuch,
777 const char *database_path,
780 char *message; /* ignored */
782 if (_db_dir_exists (database_path, &message))
783 return NOTMUCH_STATUS_NO_DATABASE;
785 _set_database_path (notmuch, database_path);
787 if (_notmuch_choose_xapian_path (notmuch, database_path, ¬much->xapian_path, &message))
788 return NOTMUCH_STATUS_NO_DATABASE;
790 (void) _finish_open (notmuch, profile, NOTMUCH_DATABASE_MODE_READ_ONLY, key_file, &message);
792 return NOTMUCH_STATUS_SUCCESS;
796 notmuch_database_load_config (const char *database_path,
797 const char *config_path,
799 notmuch_database_t **database,
800 char **status_string)
802 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS, warning = NOTMUCH_STATUS_SUCCESS;
803 void *local = talloc_new (NULL);
804 notmuch_database_t *notmuch = NULL;
805 char *message = NULL;
806 GKeyFile *key_file = NULL;
811 notmuch = _alloc_notmuch ();
813 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
817 status = _load_key_file (notmuch, config_path, profile, &key_file);
819 case NOTMUCH_STATUS_SUCCESS:
821 case NOTMUCH_STATUS_NO_CONFIG:
825 message = strdup ("Error: cannot load config file.\n");
829 status = _choose_database_path (local, profile, key_file,
830 &database_path, &split, &message);
832 case NOTMUCH_STATUS_NO_DATABASE:
833 case NOTMUCH_STATUS_SUCCESS:
843 status = _maybe_load_config_from_database (notmuch, key_file, database_path, profile);
845 case NOTMUCH_STATUS_NO_DATABASE:
846 case NOTMUCH_STATUS_SUCCESS:
856 status = _notmuch_config_load_from_file (notmuch, key_file);
860 status = _notmuch_config_load_defaults (notmuch);
868 *status_string = message;