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 _load_key_file (const char *path,
76 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
77 void *local = talloc_new (NULL);
79 if (path && EMPTY_STRING (path))
83 path = getenv ("NOTMUCH_CONFIG");
86 const char *dir = _xdg_dir (local, "XDG_CONFIG_HOME", ".config", profile);
89 path = talloc_asprintf (local, "%s/config", dir);
90 if (access (path, R_OK) !=0)
96 const char *home = getenv ("HOME");
98 path = talloc_asprintf (local, "%s/.notmuch-config", home);
101 profile = getenv ("NOTMUCH_PROFILE");
104 path = talloc_asprintf (local, "%s.%s", path, profile);
107 *key_file = g_key_file_new ();
108 if (! g_key_file_load_from_file (*key_file, path, G_KEY_FILE_NONE, NULL)) {
109 status = NOTMUCH_STATUS_FILE_ERROR;
117 static notmuch_status_t
118 _choose_database_path (const char *config_path,
121 const char **database_path,
124 notmuch_status_t status;
126 status =_load_key_file (config_path, profile, key_file);
128 *message = strdup ("Error: cannot load config file.\n");
132 if (! *database_path && *key_file)
133 *database_path = g_key_file_get_value (*key_file, "database", "path", NULL);
135 if (*database_path == NULL) {
136 *message = strdup ("Error: Cannot open a database for a NULL path.\n");
137 return NOTMUCH_STATUS_NULL_POINTER;
140 if (*database_path[0] != '/') {
141 *message = strdup ("Error: Database path must be absolute.\n");
142 return NOTMUCH_STATUS_PATH_ERROR;
144 return NOTMUCH_STATUS_SUCCESS;
148 notmuch_database_open_with_config (const char *database_path,
149 notmuch_database_mode_t mode,
150 const char *config_path,
151 unused(const char *profile),
152 notmuch_database_t **database,
153 char **status_string)
155 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
156 void *local = talloc_new (NULL);
157 notmuch_database_t *notmuch = NULL;
158 char *notmuch_path, *xapian_path, *incompat_features;
159 char *message = NULL;
162 unsigned int version;
163 GKeyFile *key_file = NULL;
164 static int initialized = 0;
166 if ((status = _choose_database_path (config_path, profile, &key_file, &database_path, &message)))
169 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
170 message = strdup ("Out of memory\n");
171 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
175 err = stat (notmuch_path, &st);
177 IGNORE_RESULT (asprintf (&message, "Error opening database at %s: %s\n",
178 notmuch_path, strerror (errno)));
179 status = NOTMUCH_STATUS_FILE_ERROR;
183 if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
184 message = strdup ("Out of memory\n");
185 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
189 /* Initialize the GLib type system and threads */
190 #if ! GLIB_CHECK_VERSION (2, 35, 1)
194 /* Initialize gmime */
200 notmuch = talloc_zero (NULL, notmuch_database_t);
201 notmuch->exception_reported = false;
202 notmuch->status_string = NULL;
203 notmuch->path = talloc_strdup (notmuch, database_path);
205 strip_trailing (notmuch->path, '/');
207 notmuch->writable_xapian_db = NULL;
208 notmuch->atomic_nesting = 0;
211 std::string last_thread_id;
212 std::string last_mod;
214 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
215 notmuch->writable_xapian_db = new Xapian::WritableDatabase (xapian_path,
217 notmuch->xapian_db = notmuch->writable_xapian_db;
219 notmuch->xapian_db = new Xapian::Database (xapian_path);
222 /* Check version. As of database version 3, we represent
223 * changes in terms of features, so assume a version bump
224 * means a dramatically incompatible change. */
225 version = notmuch_database_get_version (notmuch);
226 if (version > NOTMUCH_DATABASE_VERSION) {
227 IGNORE_RESULT (asprintf (&message,
228 "Error: Notmuch database at %s\n"
229 " has a newer database format version (%u) than supported by this\n"
230 " version of notmuch (%u).\n",
231 notmuch_path, version, NOTMUCH_DATABASE_VERSION));
232 notmuch_database_destroy (notmuch);
234 status = NOTMUCH_STATUS_FILE_ERROR;
238 /* Check features. */
239 incompat_features = NULL;
240 notmuch->features = _notmuch_database_parse_features (
241 local, notmuch->xapian_db->get_metadata ("features").c_str (),
242 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
244 if (incompat_features) {
245 IGNORE_RESULT (asprintf (&message,
246 "Error: Notmuch database at %s\n"
247 " requires features (%s)\n"
248 " not supported by this version of notmuch.\n",
249 notmuch_path, incompat_features));
250 notmuch_database_destroy (notmuch);
252 status = NOTMUCH_STATUS_FILE_ERROR;
256 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
257 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
258 if (last_thread_id.empty ()) {
259 notmuch->last_thread_id = 0;
264 str = last_thread_id.c_str ();
265 notmuch->last_thread_id = strtoull (str, &end, 16);
267 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
270 /* Get current highest revision number. */
271 last_mod = notmuch->xapian_db->get_value_upper_bound (
272 NOTMUCH_VALUE_LAST_MOD);
273 if (last_mod.empty ())
274 notmuch->revision = 0;
276 notmuch->revision = Xapian::sortable_unserialise (last_mod);
277 notmuch->uuid = talloc_strdup (
278 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
280 notmuch->query_parser = new Xapian::QueryParser;
281 notmuch->term_gen = new Xapian::TermGenerator;
282 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
283 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
284 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP, "date:");
285 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD, "lastmod:");
286 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
287 notmuch->query_parser->set_database (*notmuch->xapian_db);
288 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
289 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
290 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
291 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
292 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
294 /* Configuration information is needed to set up query parser */
295 status = _notmuch_config_load_from_database (notmuch);
300 status = _notmuch_config_load_from_file (notmuch, key_file);
304 status = _notmuch_config_load_defaults (notmuch);
308 status = _notmuch_database_setup_standard_query_fields (notmuch);
312 status = _notmuch_database_setup_user_query_fields (notmuch);
316 } catch (const Xapian::Error &error) {
317 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
318 error.get_msg ().c_str ()));
319 notmuch_database_destroy (notmuch);
321 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
329 *status_string = message;
337 talloc_free (notmuch);
340 notmuch->open = true;