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,
186 notmuch_status_t status;
188 status = _load_key_file (config_path, profile, key_file);
190 *message = strdup ("Error: cannot load config file.\n");
194 if (! *database_path) {
195 *database_path = getenv ("NOTMUCH_DATABASE");
198 if (! *database_path && *key_file) {
199 char *path = g_key_file_get_value (*key_file, "database", "path", NULL);
201 *database_path = talloc_strdup (ctx, path);
206 if (*database_path == NULL) {
207 *message = strdup ("Error: Cannot open a database for a NULL path.\n");
208 return NOTMUCH_STATUS_NULL_POINTER;
211 if (*database_path[0] != '/') {
212 *message = strdup ("Error: Database path must be absolute.\n");
213 return NOTMUCH_STATUS_PATH_ERROR;
215 return NOTMUCH_STATUS_SUCCESS;
221 notmuch_database_t *notmuch;
223 notmuch = talloc_zero (NULL, notmuch_database_t);
227 notmuch->exception_reported = false;
228 notmuch->status_string = NULL;
229 notmuch->writable_xapian_db = NULL;
230 notmuch->atomic_nesting = 0;
236 _set_database_path (notmuch_database_t *notmuch,
237 const char *database_path)
239 char *path = talloc_strdup (notmuch, database_path);
241 strip_trailing (path, '/');
243 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
250 static int initialized = 0;
252 /* Initialize the GLib type system and threads */
253 #if ! GLIB_CHECK_VERSION (2, 35, 1)
257 /* Initialize gmime */
265 notmuch_database_open_with_config (const char *database_path,
266 notmuch_database_mode_t mode,
267 const char *config_path,
269 notmuch_database_t **database,
270 char **status_string)
272 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
273 void *local = talloc_new (NULL);
274 notmuch_database_t *notmuch = NULL;
275 char *notmuch_path, *incompat_features;
276 char *message = NULL;
277 unsigned int version;
278 GKeyFile *key_file = NULL;
282 notmuch = _alloc_notmuch ();
284 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
288 if ((status = _choose_database_path (local, config_path, profile,
289 &key_file, &database_path, &message)))
292 _set_database_path (notmuch, database_path);
294 status = _db_dir_exists (database_path, &message);
298 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
299 message = strdup ("Out of memory\n");
300 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
304 status = _db_dir_exists (notmuch_path, &message);
308 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
309 message = strdup ("Out of memory\n");
310 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
315 std::string last_thread_id;
316 std::string last_mod;
318 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
319 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
321 notmuch->xapian_db = notmuch->writable_xapian_db;
323 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
326 /* Check version. As of database version 3, we represent
327 * changes in terms of features, so assume a version bump
328 * means a dramatically incompatible change. */
329 version = notmuch_database_get_version (notmuch);
330 if (version > NOTMUCH_DATABASE_VERSION) {
331 IGNORE_RESULT (asprintf (&message,
332 "Error: Notmuch database at %s\n"
333 " has a newer database format version (%u) than supported by this\n"
334 " version of notmuch (%u).\n",
335 notmuch_path, version, NOTMUCH_DATABASE_VERSION));
336 notmuch_database_destroy (notmuch);
338 status = NOTMUCH_STATUS_FILE_ERROR;
342 /* Check features. */
343 incompat_features = NULL;
344 notmuch->features = _notmuch_database_parse_features (
345 local, notmuch->xapian_db->get_metadata ("features").c_str (),
346 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
348 if (incompat_features) {
349 IGNORE_RESULT (asprintf (&message,
350 "Error: Notmuch database at %s\n"
351 " requires features (%s)\n"
352 " not supported by this version of notmuch.\n",
353 notmuch_path, incompat_features));
354 notmuch_database_destroy (notmuch);
356 status = NOTMUCH_STATUS_FILE_ERROR;
360 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
361 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
362 if (last_thread_id.empty ()) {
363 notmuch->last_thread_id = 0;
368 str = last_thread_id.c_str ();
369 notmuch->last_thread_id = strtoull (str, &end, 16);
371 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
374 /* Get current highest revision number. */
375 last_mod = notmuch->xapian_db->get_value_upper_bound (
376 NOTMUCH_VALUE_LAST_MOD);
377 if (last_mod.empty ())
378 notmuch->revision = 0;
380 notmuch->revision = Xapian::sortable_unserialise (last_mod);
381 notmuch->uuid = talloc_strdup (
382 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
384 notmuch->query_parser = new Xapian::QueryParser;
385 notmuch->term_gen = new Xapian::TermGenerator;
386 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
387 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
388 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
390 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
392 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
393 notmuch->query_parser->set_database (*notmuch->xapian_db);
394 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
395 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
396 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
397 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
398 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
400 /* Configuration information is needed to set up query parser */
401 status = _notmuch_config_load_from_database (notmuch);
406 status = _notmuch_config_load_from_file (notmuch, key_file);
410 status = _choose_hook_dir (notmuch, profile, &message);
414 status = _notmuch_config_load_defaults (notmuch);
418 status = _notmuch_database_setup_standard_query_fields (notmuch);
422 status = _notmuch_database_setup_user_query_fields (notmuch);
426 } catch (const Xapian::Error &error) {
427 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
428 error.get_msg ().c_str ()));
429 notmuch_database_destroy (notmuch);
431 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
438 g_key_file_free (key_file);
442 *status_string = message;
450 talloc_free (notmuch);
453 notmuch->open = true;
459 notmuch_database_create (const char *path, notmuch_database_t **database)
461 char *status_string = NULL;
462 notmuch_status_t status;
464 status = notmuch_database_create_verbose (path, database,
468 fputs (status_string, stderr);
469 free (status_string);
476 notmuch_database_create_verbose (const char *path,
477 notmuch_database_t **database,
478 char **status_string)
480 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
484 notmuch_database_create_with_config (const char *database_path,
485 const char *config_path,
487 notmuch_database_t **database,
488 char **status_string)
490 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
491 notmuch_database_t *notmuch = NULL;
492 char *notmuch_path = NULL;
493 char *message = NULL;
494 GKeyFile *key_file = NULL;
495 void *local = talloc_new (NULL);
500 notmuch = _alloc_notmuch ();
502 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
508 if ((status = _choose_database_path (local, config_path, profile,
509 &key_file, &database_path, &message)))
512 status = _db_dir_exists (database_path, &message);
516 _set_database_path (notmuch, database_path);
518 notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch");
520 err = mkdir (notmuch_path, 0755);
522 if (errno == EEXIST) {
523 status = NOTMUCH_STATUS_DATABASE_EXISTS;
524 talloc_free (notmuch);
527 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
528 notmuch_path, strerror (errno)));
529 status = NOTMUCH_STATUS_FILE_ERROR;
534 /* XXX this reads the config file twice, which is a bit wasteful */
535 status = notmuch_database_open_with_config (database_path,
536 NOTMUCH_DATABASE_MODE_READ_WRITE,
543 /* Upgrade doesn't add these feature to existing databases, but
544 * new databases have them. */
545 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
546 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
547 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
549 status = notmuch_database_upgrade (notmuch, NULL, NULL);
551 notmuch_database_close (notmuch);
559 g_key_file_free (key_file);
563 *status_string = message;
570 talloc_free (notmuch);
575 notmuch_database_reopen (notmuch_database_t *notmuch,
576 notmuch_database_mode_t new_mode)
578 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
580 if (notmuch->xapian_db == NULL) {
581 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
582 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
586 if (cur_mode == new_mode &&
587 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
588 notmuch->xapian_db->reopen ();
590 notmuch->xapian_db->close ();
592 delete notmuch->xapian_db;
593 notmuch->xapian_db = NULL;
594 /* no need to free the same object twice */
595 notmuch->writable_xapian_db = NULL;
597 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
598 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
600 notmuch->xapian_db = notmuch->writable_xapian_db;
602 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
606 } catch (const Xapian::Error &error) {
607 if (! notmuch->exception_reported) {
608 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
609 error.get_msg ().c_str ());
610 notmuch->exception_reported = true;
612 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
616 notmuch->open = true;
617 return NOTMUCH_STATUS_SUCCESS;