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 && *key_file) {
172 char *path = g_key_file_get_value (*key_file, "database", "path", NULL);
174 *database_path = talloc_strdup (ctx, path);
179 if (*database_path == NULL) {
180 *message = strdup ("Error: Cannot open a database for a NULL path.\n");
181 return NOTMUCH_STATUS_NULL_POINTER;
184 if (*database_path[0] != '/') {
185 *message = strdup ("Error: Database path must be absolute.\n");
186 return NOTMUCH_STATUS_PATH_ERROR;
188 return NOTMUCH_STATUS_SUCCESS;
192 notmuch_database_open_with_config (const char *database_path,
193 notmuch_database_mode_t mode,
194 const char *config_path,
196 notmuch_database_t **database,
197 char **status_string)
199 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
200 void *local = talloc_new (NULL);
201 notmuch_database_t *notmuch = NULL;
202 char *notmuch_path, *incompat_features;
203 char *message = NULL;
206 unsigned int version;
207 GKeyFile *key_file = NULL;
208 static int initialized = 0;
210 if ((status = _choose_database_path (local, config_path, profile, &key_file, &database_path,
214 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
215 message = strdup ("Out of memory\n");
216 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
220 err = stat (notmuch_path, &st);
222 IGNORE_RESULT (asprintf (&message, "Error opening database at %s: %s\n",
223 notmuch_path, strerror (errno)));
224 status = NOTMUCH_STATUS_FILE_ERROR;
228 /* Initialize the GLib type system and threads */
229 #if ! GLIB_CHECK_VERSION (2, 35, 1)
233 /* Initialize gmime */
239 notmuch = talloc_zero (NULL, notmuch_database_t);
240 notmuch->exception_reported = false;
241 notmuch->status_string = NULL;
242 notmuch->path = talloc_strdup (notmuch, database_path);
244 strip_trailing (notmuch->path, '/');
246 notmuch->writable_xapian_db = NULL;
247 notmuch->atomic_nesting = 0;
250 if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
251 message = strdup ("Out of memory\n");
252 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
257 std::string last_thread_id;
258 std::string last_mod;
260 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
261 notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path,
263 notmuch->xapian_db = notmuch->writable_xapian_db;
265 notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path);
268 /* Check version. As of database version 3, we represent
269 * changes in terms of features, so assume a version bump
270 * means a dramatically incompatible change. */
271 version = notmuch_database_get_version (notmuch);
272 if (version > NOTMUCH_DATABASE_VERSION) {
273 IGNORE_RESULT (asprintf (&message,
274 "Error: Notmuch database at %s\n"
275 " has a newer database format version (%u) than supported by this\n"
276 " version of notmuch (%u).\n",
277 notmuch_path, version, NOTMUCH_DATABASE_VERSION));
278 notmuch_database_destroy (notmuch);
280 status = NOTMUCH_STATUS_FILE_ERROR;
284 /* Check features. */
285 incompat_features = NULL;
286 notmuch->features = _notmuch_database_parse_features (
287 local, notmuch->xapian_db->get_metadata ("features").c_str (),
288 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
290 if (incompat_features) {
291 IGNORE_RESULT (asprintf (&message,
292 "Error: Notmuch database at %s\n"
293 " requires features (%s)\n"
294 " not supported by this version of notmuch.\n",
295 notmuch_path, incompat_features));
296 notmuch_database_destroy (notmuch);
298 status = NOTMUCH_STATUS_FILE_ERROR;
302 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
303 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
304 if (last_thread_id.empty ()) {
305 notmuch->last_thread_id = 0;
310 str = last_thread_id.c_str ();
311 notmuch->last_thread_id = strtoull (str, &end, 16);
313 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
316 /* Get current highest revision number. */
317 last_mod = notmuch->xapian_db->get_value_upper_bound (
318 NOTMUCH_VALUE_LAST_MOD);
319 if (last_mod.empty ())
320 notmuch->revision = 0;
322 notmuch->revision = Xapian::sortable_unserialise (last_mod);
323 notmuch->uuid = talloc_strdup (
324 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
326 notmuch->query_parser = new Xapian::QueryParser;
327 notmuch->term_gen = new Xapian::TermGenerator;
328 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
329 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
330 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
332 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
334 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
335 notmuch->query_parser->set_database (*notmuch->xapian_db);
336 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
337 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
338 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
339 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
340 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
342 /* Configuration information is needed to set up query parser */
343 status = _notmuch_config_load_from_database (notmuch);
348 status = _notmuch_config_load_from_file (notmuch, key_file);
352 status = _choose_hook_dir (notmuch, profile, &message);
356 status = _notmuch_config_load_defaults (notmuch);
360 status = _notmuch_database_setup_standard_query_fields (notmuch);
364 status = _notmuch_database_setup_user_query_fields (notmuch);
368 } catch (const Xapian::Error &error) {
369 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
370 error.get_msg ().c_str ()));
371 notmuch_database_destroy (notmuch);
373 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
380 g_key_file_free (key_file);
384 *status_string = message;
392 talloc_free (notmuch);
395 notmuch->open = true;
401 notmuch_database_create (const char *path, notmuch_database_t **database)
403 char *status_string = NULL;
404 notmuch_status_t status;
406 status = notmuch_database_create_verbose (path, database,
410 fputs (status_string, stderr);
411 free (status_string);
418 notmuch_database_create_verbose (const char *path,
419 notmuch_database_t **database,
420 char **status_string)
422 return notmuch_database_create_with_config (path, "", NULL, database, status_string);
426 notmuch_database_create_with_config (const char *database_path,
427 const char *config_path,
429 notmuch_database_t **database,
430 char **status_string)
432 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
433 notmuch_database_t *notmuch = NULL;
434 char *notmuch_path = NULL;
435 char *message = NULL;
436 GKeyFile *key_file = NULL;
439 void *local = talloc_new (NULL);
441 if ((status = _choose_database_path (local, config_path, profile, &key_file, &database_path,
445 err = stat (database_path, &st);
447 IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: %s.\n",
448 database_path, strerror (errno)));
449 status = NOTMUCH_STATUS_FILE_ERROR;
453 if (! S_ISDIR (st.st_mode)) {
454 IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: "
455 "Not a directory.\n",
457 status = NOTMUCH_STATUS_FILE_ERROR;
461 notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch");
463 err = mkdir (notmuch_path, 0755);
465 if (errno == EEXIST) {
466 status = NOTMUCH_STATUS_DATABASE_EXISTS;
468 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
469 notmuch_path, strerror (errno)));
470 status = NOTMUCH_STATUS_FILE_ERROR;
475 /* XXX this reads the config file twice, which is a bit wasteful */
476 status = notmuch_database_open_with_config (database_path,
477 NOTMUCH_DATABASE_MODE_READ_WRITE,
484 /* Upgrade doesn't add these feature to existing databases, but
485 * new databases have them. */
486 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
487 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
488 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
490 status = notmuch_database_upgrade (notmuch, NULL, NULL);
492 notmuch_database_close (notmuch);
500 g_key_file_free (key_file);
504 *status_string = message;
511 talloc_free (notmuch);