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 _choose_database_path (void *ctx,
157 const char *config_path,
160 const char **database_path,
163 notmuch_status_t status;
165 status = _load_key_file (config_path, profile, key_file);
167 *message = strdup ("Error: cannot load config file.\n");
171 if (! *database_path) {
172 *database_path = getenv ("NOTMUCH_DATABASE");
175 if (! *database_path && *key_file) {
176 char *path = g_key_file_get_value (*key_file, "database", "path", NULL);
178 *database_path = talloc_strdup (ctx, path);
183 if (*database_path == NULL) {
184 *message = strdup ("Error: Cannot open a database for a NULL path.\n");
185 return NOTMUCH_STATUS_NULL_POINTER;
188 if (*database_path[0] != '/') {
189 *message = strdup ("Error: Database path must be absolute.\n");
190 return NOTMUCH_STATUS_PATH_ERROR;
192 return NOTMUCH_STATUS_SUCCESS;
198 notmuch_database_t *notmuch;
200 notmuch = talloc_zero (NULL, notmuch_database_t);
204 notmuch->exception_reported = false;
205 notmuch->status_string = NULL;
206 notmuch->writable_xapian_db = NULL;
207 notmuch->atomic_nesting = 0;
213 _set_database_path (notmuch_database_t *notmuch,
214 const char *database_path)
216 char *path = talloc_strdup (notmuch, database_path);
218 strip_trailing (path, '/');
220 _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path);
227 static int initialized = 0;
229 /* Initialize the GLib type system and threads */
230 #if ! GLIB_CHECK_VERSION (2, 35, 1)
234 /* Initialize gmime */
242 notmuch_database_open_with_config (const char *database_path,
243 notmuch_database_mode_t mode,
244 const char *config_path,
246 notmuch_database_t **database,
247 char **status_string)
249 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
250 void *local = talloc_new (NULL);
251 notmuch_database_t *notmuch = NULL;
252 char *notmuch_path, *incompat_features;
253 char *message = NULL;
256 unsigned int version;
257 GKeyFile *key_file = NULL;
261 notmuch = _alloc_notmuch ();
263 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
267 if ((status = _choose_database_path (local, config_path, profile,
268 &key_file, &database_path, &message)))
271 _set_database_path (notmuch, database_path);
273 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
274 message = strdup ("Out of memory\n");
275 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
279 err = stat (notmuch_path, &st);
281 IGNORE_RESULT (asprintf (&message, "Error opening database at %s: %s\n",
282 notmuch_path, strerror (errno)));
283 status = NOTMUCH_STATUS_FILE_ERROR;
287 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
288 message = strdup ("Out of memory\n");
289 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
294 std::string last_thread_id;
295 std::string last_mod;
297 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
298 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
300 notmuch->xapian_db = notmuch->writable_xapian_db;
302 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
305 /* Check version. As of database version 3, we represent
306 * changes in terms of features, so assume a version bump
307 * means a dramatically incompatible change. */
308 version = notmuch_database_get_version (notmuch);
309 if (version > NOTMUCH_DATABASE_VERSION) {
310 IGNORE_RESULT (asprintf (&message,
311 "Error: Notmuch database at %s\n"
312 " has a newer database format version (%u) than supported by this\n"
313 " version of notmuch (%u).\n",
314 notmuch_path, version, NOTMUCH_DATABASE_VERSION));
315 notmuch_database_destroy (notmuch);
317 status = NOTMUCH_STATUS_FILE_ERROR;
321 /* Check features. */
322 incompat_features = NULL;
323 notmuch->features = _notmuch_database_parse_features (
324 local, notmuch->xapian_db->get_metadata ("features").c_str (),
325 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
327 if (incompat_features) {
328 IGNORE_RESULT (asprintf (&message,
329 "Error: Notmuch database at %s\n"
330 " requires features (%s)\n"
331 " not supported by this version of notmuch.\n",
332 notmuch_path, incompat_features));
333 notmuch_database_destroy (notmuch);
335 status = NOTMUCH_STATUS_FILE_ERROR;
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 ());
363 notmuch->query_parser = new Xapian::QueryParser;
364 notmuch->term_gen = new Xapian::TermGenerator;
365 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
366 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
367 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
369 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
371 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
372 notmuch->query_parser->set_database (*notmuch->xapian_db);
373 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
374 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
375 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
376 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
377 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
379 /* Configuration information is needed to set up query parser */
380 status = _notmuch_config_load_from_database (notmuch);
385 status = _notmuch_config_load_from_file (notmuch, key_file);
389 status = _choose_hook_dir (notmuch, profile, &message);
393 status = _notmuch_config_load_defaults (notmuch);
397 status = _notmuch_database_setup_standard_query_fields (notmuch);
401 status = _notmuch_database_setup_user_query_fields (notmuch);
405 } catch (const Xapian::Error &error) {
406 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
407 error.get_msg ().c_str ()));
408 notmuch_database_destroy (notmuch);
410 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
417 g_key_file_free (key_file);
421 *status_string = message;
429 talloc_free (notmuch);
432 notmuch->open = true;
438 notmuch_database_create (const char *path, notmuch_database_t **database)
440 char *status_string = NULL;
441 notmuch_status_t status;
443 status = notmuch_database_create_verbose (path, database,
447 fputs (status_string, stderr);
448 free (status_string);
455 notmuch_database_create_verbose (const char *path,
456 notmuch_database_t **database,
457 char **status_string)
459 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
463 notmuch_database_create_with_config (const char *database_path,
464 const char *config_path,
466 notmuch_database_t **database,
467 char **status_string)
469 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
470 notmuch_database_t *notmuch = NULL;
471 char *notmuch_path = NULL;
472 char *message = NULL;
473 GKeyFile *key_file = NULL;
476 void *local = talloc_new (NULL);
480 if ((status = _choose_database_path (local, config_path, profile,
481 &key_file, &database_path, &message)))
484 err = stat (database_path, &st);
486 IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: %s.\n",
487 database_path, strerror (errno)));
488 status = NOTMUCH_STATUS_FILE_ERROR;
492 if (! S_ISDIR (st.st_mode)) {
493 IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: "
494 "Not a directory.\n",
496 status = NOTMUCH_STATUS_FILE_ERROR;
500 notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch");
502 err = mkdir (notmuch_path, 0755);
504 if (errno == EEXIST) {
505 status = NOTMUCH_STATUS_DATABASE_EXISTS;
507 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
508 notmuch_path, strerror (errno)));
509 status = NOTMUCH_STATUS_FILE_ERROR;
514 /* XXX this reads the config file twice, which is a bit wasteful */
515 status = notmuch_database_open_with_config (database_path,
516 NOTMUCH_DATABASE_MODE_READ_WRITE,
523 /* Upgrade doesn't add these feature to existing databases, but
524 * new databases have them. */
525 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
526 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
527 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
529 status = notmuch_database_upgrade (notmuch, NULL, NULL);
531 notmuch_database_close (notmuch);
539 g_key_file_free (key_file);
543 *status_string = message;
550 talloc_free (notmuch);
555 notmuch_database_reopen (notmuch_database_t *notmuch,
556 notmuch_database_mode_t new_mode)
558 notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch);
560 if (notmuch->xapian_db == NULL) {
561 _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n");
562 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
566 if (cur_mode == new_mode &&
567 new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
568 notmuch->xapian_db->reopen ();
570 notmuch->xapian_db->close ();
572 delete notmuch->xapian_db;
573 notmuch->xapian_db = NULL;
574 /* no need to free the same object twice */
575 notmuch->writable_xapian_db = NULL;
577 if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
578 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
580 notmuch->xapian_db = notmuch->writable_xapian_db;
582 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path,
586 } catch (const Xapian::Error &error) {
587 if (! notmuch->exception_reported) {
588 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
589 error.get_msg ().c_str ());
590 notmuch->exception_reported = true;
592 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
596 notmuch->open = true;
597 return NOTMUCH_STATUS_SUCCESS;