]> git.notmuchmail.org Git - notmuch/blob - lib/open.cc
0c965d0d70cc900c865c0e52f820a386b6199774
[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 _choose_hook_dir (notmuch_database_t *notmuch,
73                   const char *profile,
74                   char **message)
75 {
76     const char *config;
77     const char *hook_dir;
78     struct stat st;
79     int err;
80
81     hook_dir = notmuch_config_get (notmuch, NOTMUCH_CONFIG_HOOK_DIR);
82
83     if (hook_dir)
84         return NOTMUCH_STATUS_SUCCESS;
85
86     config = _xdg_dir (notmuch, "XDG_CONFIG_HOME", ".config", profile);
87     if (! config)
88         return NOTMUCH_STATUS_PATH_ERROR;
89
90     hook_dir = talloc_asprintf (notmuch, "%s/hooks", config);
91
92     err = stat (hook_dir, &st);
93     if (err) {
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);
97         } else {
98             IGNORE_RESULT (asprintf (message, "Error: Cannot stat %s: %s.\n",
99                                      hook_dir, strerror (errno)));
100             return NOTMUCH_STATUS_FILE_ERROR;
101         }
102     }
103
104     _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_HOOK_DIR, hook_dir);
105
106     return NOTMUCH_STATUS_SUCCESS;
107 }
108
109 static notmuch_status_t
110 _load_key_file (const char *path,
111                 const char *profile,
112                 GKeyFile **key_file)
113 {
114     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
115     void *local = talloc_new (NULL);
116
117     if (path && EMPTY_STRING (path))
118         goto DONE;
119
120     if (! path)
121         path = getenv ("NOTMUCH_CONFIG");
122
123     if (! path) {
124         const char *dir = _xdg_dir (local, "XDG_CONFIG_HOME", ".config", profile);
125
126         if (dir) {
127             path = talloc_asprintf (local, "%s/config", dir);
128             if (access (path, R_OK) != 0)
129                 path = NULL;
130         }
131     }
132
133     if (! path) {
134         const char *home = getenv ("HOME");
135
136         path = talloc_asprintf (local, "%s/.notmuch-config", home);
137
138         if (! profile)
139             profile = getenv ("NOTMUCH_PROFILE");
140
141         if (profile)
142             path = talloc_asprintf (local, "%s.%s", path, profile);
143     }
144
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;
148     }
149
150   DONE:
151     talloc_free (local);
152     return status;
153 }
154
155 static notmuch_status_t
156 _choose_database_path (void *ctx,
157                        const char *config_path,
158                        const char *profile,
159                        GKeyFile **key_file,
160                        const char **database_path,
161                        char **message)
162 {
163     notmuch_status_t status;
164
165     status = _load_key_file (config_path, profile, key_file);
166     if (status) {
167         *message = strdup ("Error: cannot load config file.\n");
168         return status;
169     }
170
171     if (! *database_path && *key_file) {
172         char *path = g_key_file_get_value (*key_file, "database", "path", NULL);
173         if (path) {
174             *database_path = talloc_strdup (ctx, path);
175             g_free (path);
176         }
177     }
178
179     if (*database_path == NULL) {
180         *message = strdup ("Error: Cannot open a database for a NULL path.\n");
181         return NOTMUCH_STATUS_NULL_POINTER;
182     }
183
184     if (*database_path[0] != '/') {
185         *message = strdup ("Error: Database path must be absolute.\n");
186         return NOTMUCH_STATUS_PATH_ERROR;
187     }
188     return NOTMUCH_STATUS_SUCCESS;
189 }
190
191 notmuch_status_t
192 notmuch_database_open_with_config (const char *database_path,
193                                    notmuch_database_mode_t mode,
194                                    const char *config_path,
195                                    const char *profile,
196                                    notmuch_database_t **database,
197                                    char **status_string)
198 {
199     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
200     void *local = talloc_new (NULL);
201     notmuch_database_t *notmuch = NULL;
202     char *notmuch_path, *xapian_path, *incompat_features;
203     char *message = NULL;
204     struct stat st;
205     int err;
206     unsigned int version;
207     GKeyFile *key_file = NULL;
208     static int initialized = 0;
209
210     if ((status = _choose_database_path (local, config_path, profile, &key_file, &database_path,
211                                          &message)))
212         goto DONE;
213
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;
217         goto DONE;
218     }
219
220     err = stat (notmuch_path, &st);
221     if (err) {
222         IGNORE_RESULT (asprintf (&message, "Error opening database at %s: %s\n",
223                                  notmuch_path, strerror (errno)));
224         status = NOTMUCH_STATUS_FILE_ERROR;
225         goto DONE;
226     }
227
228     if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
229         message = strdup ("Out of memory\n");
230         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
231         goto DONE;
232     }
233
234     /* Initialize the GLib type system and threads */
235 #if ! GLIB_CHECK_VERSION (2, 35, 1)
236     g_type_init ();
237 #endif
238
239     /* Initialize gmime */
240     if (! initialized) {
241         g_mime_init ();
242         initialized = 1;
243     }
244
245     notmuch = talloc_zero (NULL, notmuch_database_t);
246     notmuch->exception_reported = false;
247     notmuch->status_string = NULL;
248     notmuch->path = talloc_strdup (notmuch, database_path);
249
250     strip_trailing (notmuch->path, '/');
251
252     notmuch->writable_xapian_db = NULL;
253     notmuch->atomic_nesting = 0;
254     notmuch->view = 1;
255     try {
256         std::string last_thread_id;
257         std::string last_mod;
258
259         if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
260             notmuch->writable_xapian_db = new Xapian::WritableDatabase (xapian_path,
261                                                                         DB_ACTION);
262             notmuch->xapian_db = notmuch->writable_xapian_db;
263         } else {
264             notmuch->xapian_db = new Xapian::Database (xapian_path);
265         }
266
267         /* Check version.  As of database version 3, we represent
268          * changes in terms of features, so assume a version bump
269          * means a dramatically incompatible change. */
270         version = notmuch_database_get_version (notmuch);
271         if (version > NOTMUCH_DATABASE_VERSION) {
272             IGNORE_RESULT (asprintf (&message,
273                                      "Error: Notmuch database at %s\n"
274                                      "       has a newer database format version (%u) than supported by this\n"
275                                      "       version of notmuch (%u).\n",
276                                      notmuch_path, version, NOTMUCH_DATABASE_VERSION));
277             notmuch_database_destroy (notmuch);
278             notmuch = NULL;
279             status = NOTMUCH_STATUS_FILE_ERROR;
280             goto DONE;
281         }
282
283         /* Check features. */
284         incompat_features = NULL;
285         notmuch->features = _notmuch_database_parse_features (
286             local, notmuch->xapian_db->get_metadata ("features").c_str (),
287             version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
288             &incompat_features);
289         if (incompat_features) {
290             IGNORE_RESULT (asprintf (&message,
291                                      "Error: Notmuch database at %s\n"
292                                      "       requires features (%s)\n"
293                                      "       not supported by this version of notmuch.\n",
294                                      notmuch_path, incompat_features));
295             notmuch_database_destroy (notmuch);
296             notmuch = NULL;
297             status = NOTMUCH_STATUS_FILE_ERROR;
298             goto DONE;
299         }
300
301         notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
302         last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
303         if (last_thread_id.empty ()) {
304             notmuch->last_thread_id = 0;
305         } else {
306             const char *str;
307             char *end;
308
309             str = last_thread_id.c_str ();
310             notmuch->last_thread_id = strtoull (str, &end, 16);
311             if (*end != '\0')
312                 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
313         }
314
315         /* Get current highest revision number. */
316         last_mod = notmuch->xapian_db->get_value_upper_bound (
317             NOTMUCH_VALUE_LAST_MOD);
318         if (last_mod.empty ())
319             notmuch->revision = 0;
320         else
321             notmuch->revision = Xapian::sortable_unserialise (last_mod);
322         notmuch->uuid = talloc_strdup (
323             notmuch, notmuch->xapian_db->get_uuid ().c_str ());
324
325         notmuch->query_parser = new Xapian::QueryParser;
326         notmuch->term_gen = new Xapian::TermGenerator;
327         notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
328         notmuch->value_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
329         notmuch->date_range_processor = new ParseTimeRangeProcessor (NOTMUCH_VALUE_TIMESTAMP,
330                                                                      "date:");
331         notmuch->last_mod_range_processor = new Xapian::NumberRangeProcessor (NOTMUCH_VALUE_LAST_MOD,
332                                                                               "lastmod:");
333         notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
334         notmuch->query_parser->set_database (*notmuch->xapian_db);
335         notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
336         notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
337         notmuch->query_parser->add_rangeprocessor (notmuch->value_range_processor);
338         notmuch->query_parser->add_rangeprocessor (notmuch->date_range_processor);
339         notmuch->query_parser->add_rangeprocessor (notmuch->last_mod_range_processor);
340
341         /* Configuration information is needed to set up query parser */
342         status = _notmuch_config_load_from_database (notmuch);
343         if (status)
344             goto DONE;
345
346         if (key_file)
347             status = _notmuch_config_load_from_file (notmuch, key_file);
348         if (status)
349             goto DONE;
350
351         status = _choose_hook_dir (notmuch, profile, &message);
352         if (status)
353             goto DONE;
354
355         status = _notmuch_config_load_defaults (notmuch);
356         if (status)
357             goto DONE;
358
359         status = _notmuch_database_setup_standard_query_fields (notmuch);
360         if (status)
361             goto DONE;
362
363         status = _notmuch_database_setup_user_query_fields (notmuch);
364         if (status)
365             goto DONE;
366
367     } catch (const Xapian::Error &error) {
368         IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
369                                  error.get_msg ().c_str ()));
370         notmuch_database_destroy (notmuch);
371         notmuch = NULL;
372         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
373     }
374
375   DONE:
376     talloc_free (local);
377
378     if (key_file)
379         g_key_file_free (key_file);
380
381     if (message) {
382         if (status_string)
383             *status_string = message;
384         else
385             free (message);
386     }
387
388     if (database)
389         *database = notmuch;
390     else
391         talloc_free (notmuch);
392
393     if (notmuch)
394         notmuch->open = true;
395
396     return status;
397 }
398
399 notmuch_status_t
400 notmuch_database_create (const char *path, notmuch_database_t **database)
401 {
402     char *status_string = NULL;
403     notmuch_status_t status;
404
405     status = notmuch_database_create_verbose (path, database,
406                                               &status_string);
407
408     if (status_string) {
409         fputs (status_string, stderr);
410         free (status_string);
411     }
412
413     return status;
414 }
415
416 notmuch_status_t
417 notmuch_database_create_verbose (const char *path,
418                                  notmuch_database_t **database,
419                                  char **status_string)
420 {
421     return notmuch_database_create_with_config (path, "", NULL, database, status_string);
422 }
423
424 notmuch_status_t
425 notmuch_database_create_with_config (const char *database_path,
426                                      const char *config_path,
427                                      const char *profile,
428                                      notmuch_database_t **database,
429                                      char **status_string)
430 {
431     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
432     notmuch_database_t *notmuch = NULL;
433     char *notmuch_path = NULL;
434     char *message = NULL;
435     GKeyFile *key_file = NULL;
436     struct stat st;
437     int err;
438     void *local = talloc_new (NULL);
439
440     if ((status = _choose_database_path (local, config_path, profile, &key_file, &database_path,
441                                          &message)))
442         goto DONE;
443
444     err = stat (database_path, &st);
445     if (err) {
446         IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: %s.\n",
447                                  database_path, strerror (errno)));
448         status = NOTMUCH_STATUS_FILE_ERROR;
449         goto DONE;
450     }
451
452     if (! S_ISDIR (st.st_mode)) {
453         IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: "
454                                  "Not a directory.\n",
455                                  database_path));
456         status = NOTMUCH_STATUS_FILE_ERROR;
457         goto DONE;
458     }
459
460     notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch");
461
462     err = mkdir (notmuch_path, 0755);
463     if (err) {
464         if (errno == EEXIST) {
465             status = NOTMUCH_STATUS_DATABASE_EXISTS;
466         } else {
467             IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
468                                      notmuch_path, strerror (errno)));
469             status = NOTMUCH_STATUS_FILE_ERROR;
470         }
471         goto DONE;
472     }
473
474     /* XXX this reads the config file twice, which is a bit wasteful */
475     status = notmuch_database_open_with_config (database_path,
476                                                 NOTMUCH_DATABASE_MODE_READ_WRITE,
477                                                 config_path,
478                                                 profile,
479                                                 &notmuch, &message);
480     if (status)
481         goto DONE;
482
483     /* Upgrade doesn't add these feature to existing databases, but
484      * new databases have them. */
485     notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
486     notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
487     notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
488
489     status = notmuch_database_upgrade (notmuch, NULL, NULL);
490     if (status) {
491         notmuch_database_close (notmuch);
492         notmuch = NULL;
493     }
494
495   DONE:
496     talloc_free (local);
497
498     if (key_file)
499         g_key_file_free (key_file);
500
501     if (message) {
502         if (status_string)
503             *status_string = message;
504         else
505             free (message);
506     }
507     if (database)
508         *database = notmuch;
509     else
510         talloc_free (notmuch);
511     return status;
512 }