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;
118 notmuch_database_open_with_config (const char *database_path,
119 notmuch_database_mode_t mode,
120 const char *config_path,
121 unused(const char *profile),
122 notmuch_database_t **database,
123 char **status_string)
125 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
126 void *local = talloc_new (NULL);
127 notmuch_database_t *notmuch = NULL;
128 char *notmuch_path, *xapian_path, *incompat_features;
129 char *message = NULL;
132 unsigned int version;
133 GKeyFile *key_file = NULL;
134 static int initialized = 0;
136 status = _load_key_file (config_path, profile, &key_file);
138 message = strdup ("Error: cannot load config file");
142 if (! database_path && key_file)
143 database_path = g_key_file_get_value (key_file, "database", "path", NULL);
145 if (database_path == NULL) {
146 message = strdup ("Error: Cannot open a database for a NULL path.\n");
147 status = NOTMUCH_STATUS_NULL_POINTER;
151 if (database_path[0] != '/') {
152 message = strdup ("Error: Database path must be absolute.\n");
153 status = NOTMUCH_STATUS_PATH_ERROR;
157 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
158 message = strdup ("Out of memory\n");
159 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
163 err = stat (notmuch_path, &st);
165 IGNORE_RESULT (asprintf (&message, "Error opening database at %s: %s\n",
166 notmuch_path, strerror (errno)));
167 status = NOTMUCH_STATUS_FILE_ERROR;
171 if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
172 message = strdup ("Out of memory\n");
173 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
177 /* Initialize the GLib type system and threads */
178 #if ! GLIB_CHECK_VERSION (2, 35, 1)
182 /* Initialize gmime */
188 notmuch = talloc_zero (NULL, notmuch_database_t);
189 notmuch->exception_reported = false;
190 notmuch->status_string = NULL;
191 notmuch->path = talloc_strdup (notmuch, database_path);
193 strip_trailing (notmuch->path, '/');
195 notmuch->writable_xapian_db = NULL;
196 notmuch->atomic_nesting = 0;
199 std::string last_thread_id;
200 std::string last_mod;
202 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
203 notmuch->writable_xapian_db = new Xapian::WritableDatabase (xapian_path,
205 notmuch->xapian_db = notmuch->writable_xapian_db;
207 notmuch->xapian_db = new Xapian::Database (xapian_path);
210 /* Check version. As of database version 3, we represent
211 * changes in terms of features, so assume a version bump
212 * means a dramatically incompatible change. */
213 version = notmuch_database_get_version (notmuch);
214 if (version > NOTMUCH_DATABASE_VERSION) {
215 IGNORE_RESULT (asprintf (&message,
216 "Error: Notmuch database at %s\n"
217 " has a newer database format version (%u) than supported by this\n"
218 " version of notmuch (%u).\n",
219 notmuch_path, version, NOTMUCH_DATABASE_VERSION));
220 notmuch_database_destroy (notmuch);
222 status = NOTMUCH_STATUS_FILE_ERROR;
226 /* Check features. */
227 incompat_features = NULL;
228 notmuch->features = _notmuch_database_parse_features (
229 local, notmuch->xapian_db->get_metadata ("features").c_str (),
230 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
232 if (incompat_features) {
233 IGNORE_RESULT (asprintf (&message,
234 "Error: Notmuch database at %s\n"
235 " requires features (%s)\n"
236 " not supported by this version of notmuch.\n",
237 notmuch_path, incompat_features));
238 notmuch_database_destroy (notmuch);
240 status = NOTMUCH_STATUS_FILE_ERROR;
244 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
245 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
246 if (last_thread_id.empty ()) {
247 notmuch->last_thread_id = 0;
252 str = last_thread_id.c_str ();
253 notmuch->last_thread_id = strtoull (str, &end, 16);
255 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
258 /* Get current highest revision number. */
259 last_mod = notmuch->xapian_db->get_value_upper_bound (
260 NOTMUCH_VALUE_LAST_MOD);
261 if (last_mod.empty ())
262 notmuch->revision = 0;
264 notmuch->revision = Xapian::sortable_unserialise (last_mod);
265 notmuch->uuid = talloc_strdup (
266 notmuch, notmuch->xapian_db->get_uuid ().c_str ());
268 notmuch->query_parser = new Xapian::QueryParser;
269 notmuch->term_gen = new Xapian::TermGenerator;
270 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
271 notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
272 notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP, "date:");
273 notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD, "lastmod:");
274 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
275 notmuch->query_parser->set_database (*notmuch->xapian_db);
276 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
277 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
278 notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
279 notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
280 notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
282 /* Configuration information is needed to set up query parser */
283 status = _notmuch_config_load_from_database (notmuch);
288 status = _notmuch_config_load_from_file (notmuch, key_file);
292 status = _notmuch_database_setup_standard_query_fields (notmuch);
296 status = _notmuch_database_setup_user_query_fields (notmuch);
300 } catch (const Xapian::Error &error) {
301 IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
302 error.get_msg ().c_str ()));
303 notmuch_database_destroy (notmuch);
305 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
313 *status_string = message;
321 talloc_free (notmuch);
324 notmuch->open = true;