]> git.notmuchmail.org Git - notmuch/blob - lib/open.cc
06d079e4c589a9e084b0c1724ce13361722a505c
[notmuch] / lib / open.cc
1 #include <unistd.h>
2 #include "database-private.h"
3 #include "parse-time-vrp.h"
4
5 #if HAVE_XAPIAN_DB_RETRY_LOCK
6 #define DB_ACTION (Xapian::DB_CREATE_OR_OPEN | Xapian::DB_RETRY_LOCK)
7 #else
8 #define DB_ACTION Xapian::DB_CREATE_OR_OPEN
9 #endif
10
11 notmuch_status_t
12 notmuch_database_open (const char *path,
13                        notmuch_database_mode_t mode,
14                        notmuch_database_t **database)
15 {
16     char *status_string = NULL;
17     notmuch_status_t status;
18
19     status = notmuch_database_open_verbose (path, mode, database,
20                                             &status_string);
21
22     if (status_string) {
23         fputs (status_string, stderr);
24         free (status_string);
25     }
26
27     return status;
28 }
29
30 notmuch_status_t
31 notmuch_database_open_verbose (const char *path,
32                                notmuch_database_mode_t mode,
33                                notmuch_database_t **database,
34                                char **status_string)
35 {
36     return notmuch_database_open_with_config (path, mode, "", NULL,
37                                               database, status_string);
38 }
39
40 static const char *
41 _xdg_dir (void *ctx,
42           const char *xdg_root_variable,
43           const char *xdg_prefix,
44           const char *profile_name)
45 {
46     const char *xdg_root = getenv (xdg_root_variable);
47
48     if (! xdg_root) {
49         const char *home = getenv ("HOME");
50
51         if (! home) return NULL;
52
53         xdg_root = talloc_asprintf (ctx,
54                                     "%s/%s",
55                                     home,
56                                     xdg_prefix);
57     }
58
59     if (! profile_name)
60         profile_name = getenv ("NOTMUCH_PROFILE");
61
62     if (! profile_name)
63         profile_name = "default";
64
65     return talloc_asprintf (ctx,
66                             "%s/notmuch/%s",
67                             xdg_root,
68                             profile_name);
69 }
70
71 static notmuch_status_t
72 _load_key_file (const char *path,
73                 const char *profile,
74                 GKeyFile **key_file)
75 {
76     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
77     void *local = talloc_new (NULL);
78
79     if (path && EMPTY_STRING (path))
80         goto DONE;
81
82     if (! path)
83         path = getenv ("NOTMUCH_CONFIG");
84
85     if (! path) {
86         const char *dir = _xdg_dir (local, "XDG_CONFIG_HOME", ".config", profile);
87
88         if (dir) {
89             path = talloc_asprintf (local, "%s/config", dir);
90             if (access (path, R_OK) !=0)
91                 path = NULL;
92         }
93     }
94
95     if (! path) {
96         const char *home = getenv ("HOME");
97
98         path = talloc_asprintf (local, "%s/.notmuch-config", home);
99
100         if (! profile)
101             profile = getenv ("NOTMUCH_PROFILE");
102
103         if (profile)
104             path = talloc_asprintf (local, "%s.%s", path, profile);
105     }
106
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_NO_CONFIG;
110     }
111
112 DONE:
113     talloc_free (local);
114     return status;
115 }
116
117 static notmuch_status_t
118 _choose_database_path (const char *config_path,
119                        const char *profile,
120                        GKeyFile **key_file,
121                        const char **database_path,
122                        char **message)
123 {
124     notmuch_status_t status;
125
126     status =_load_key_file (config_path, profile, key_file);
127     if (status) {
128         *message = strdup ("Error: cannot load config file.\n");
129         return status;
130     }
131
132     if (! *database_path && *key_file)
133         *database_path = g_key_file_get_value (*key_file, "database", "path", NULL);
134
135     if (*database_path == NULL) {
136         *message = strdup ("Error: Cannot open a database for a NULL path.\n");
137         return NOTMUCH_STATUS_NULL_POINTER;
138     }
139
140     if (*database_path[0] != '/') {
141         *message = strdup ("Error: Database path must be absolute.\n");
142         return NOTMUCH_STATUS_PATH_ERROR;
143     }
144     return NOTMUCH_STATUS_SUCCESS;
145 }
146
147 notmuch_status_t
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)
154 {
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;
160     struct stat st;
161     int err;
162     unsigned int version;
163     GKeyFile *key_file = NULL;
164     static int initialized = 0;
165
166     if ((status = _choose_database_path (config_path, profile, &key_file, &database_path, &message)))
167         goto DONE;
168
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;
172         goto DONE;
173     }
174
175     err = stat (notmuch_path, &st);
176     if (err) {
177         IGNORE_RESULT (asprintf (&message, "Error opening database at %s: %s\n",
178                                  notmuch_path, strerror (errno)));
179         status = NOTMUCH_STATUS_FILE_ERROR;
180         goto DONE;
181     }
182
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;
186         goto DONE;
187     }
188
189     /* Initialize the GLib type system and threads */
190 #if ! GLIB_CHECK_VERSION (2, 35, 1)
191     g_type_init ();
192 #endif
193
194     /* Initialize gmime */
195     if (! initialized) {
196         g_mime_init ();
197         initialized = 1;
198     }
199
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);
204
205     strip_trailing (notmuch->path, '/');
206
207     notmuch->writable_xapian_db = NULL;
208     notmuch->atomic_nesting = 0;
209     notmuch->view = 1;
210     try {
211         std::string last_thread_id;
212         std::string last_mod;
213
214         if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
215             notmuch->writable_xapian_db = new Xapian::WritableDatabase (xapian_path,
216                                                                         DB_ACTION);
217             notmuch->xapian_db = notmuch->writable_xapian_db;
218         } else {
219             notmuch->xapian_db = new Xapian::Database (xapian_path);
220         }
221
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);
233             notmuch = NULL;
234             status = NOTMUCH_STATUS_FILE_ERROR;
235             goto DONE;
236         }
237
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',
243             &incompat_features);
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);
251             notmuch = NULL;
252             status = NOTMUCH_STATUS_FILE_ERROR;
253             goto DONE;
254         }
255
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;
260         } else {
261             const char *str;
262             char *end;
263
264             str = last_thread_id.c_str ();
265             notmuch->last_thread_id = strtoull (str, &end, 16);
266             if (*end != '\0')
267                 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
268         }
269
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;
275         else
276             notmuch->revision = Xapian::sortable_unserialise (last_mod);
277         notmuch->uuid = talloc_strdup (
278             notmuch, notmuch->xapian_db->get_uuid ().c_str ());
279
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);
293
294         /* Configuration information is needed to set up query parser */
295         status = _notmuch_config_load_from_database (notmuch);
296         if (status)
297             goto DONE;
298
299         if (key_file)
300             status = _notmuch_config_load_from_file (notmuch, key_file);
301         if (status)
302             goto DONE;
303
304         status = _notmuch_config_load_defaults (notmuch);
305         if (status)
306             goto DONE;
307
308         status = _notmuch_database_setup_standard_query_fields (notmuch);
309         if (status)
310             goto DONE;
311
312         status = _notmuch_database_setup_user_query_fields (notmuch);
313         if (status)
314             goto DONE;
315
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);
320         notmuch = NULL;
321         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
322     }
323
324   DONE:
325     talloc_free (local);
326
327     if (message) {
328         if (status_string)
329             *status_string = message;
330         else
331             free (message);
332     }
333
334     if (database)
335         *database = notmuch;
336     else
337         talloc_free (notmuch);
338
339     if (notmuch)
340         notmuch->open = true;
341
342     return status;
343 }