NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
db.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <limits.h>
31#include <notmuch.h>
32#include <stdbool.h>
33#include <stdio.h>
34#include <sys/stat.h>
35#include "private.h"
36#include "mutt/lib.h"
37#include "config/lib.h"
38#include "email/lib.h"
39#include "core/lib.h"
40#include "lib.h"
41#include "adata.h"
42#include "mdata.h"
43#include "mutt_logging.h"
44
54const char *nm_db_get_filename(struct Mailbox *m)
55{
56 struct NmMboxData *mdata = nm_mdata_get(m);
57 const char *db_filename = NULL;
58
59 const char *const c_nm_default_url = cs_subset_string(NeoMutt->sub, "nm_default_url");
60 if (mdata && mdata->db_url && mdata->db_url->path)
61 db_filename = mdata->db_url->path;
62 else
63 db_filename = c_nm_default_url;
64
65 const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
66 if (!db_filename && !c_folder)
67 return NULL;
68
69 if (!db_filename)
70 db_filename = c_folder;
71
72 if (nm_path_probe(db_filename, NULL) == MUTT_NOTMUCH)
73 db_filename += NmUrlProtocolLen;
74
75 mutt_debug(LL_DEBUG2, "nm: db filename '%s'\n", db_filename);
76 return db_filename;
77}
78
79#if LIBNOTMUCH_CHECK_VERSION(5, 4, 0)
85static const char *get_nm_config_file(void)
86{
87 const char *config_to_use = NULL;
88 const char *c_nm_config_file = cs_subset_path(NeoMutt->sub, "nm_config_file");
89
90 // Workaround the configuration system mapping "" to NULL.
91 if (!c_nm_config_file)
92 {
93 config_to_use = "";
94 }
95 else if (!mutt_strn_equal(c_nm_config_file, "auto", 4))
96 {
97 config_to_use = c_nm_config_file;
98 }
99
100 return config_to_use;
101}
102#endif
103
111notmuch_database_t *nm_db_do_open(const char *filename, bool writable, bool verbose)
112{
113 notmuch_database_t *db = NULL;
114 int ct = 0;
115 notmuch_status_t st = NOTMUCH_STATUS_SUCCESS;
116 char *msg = NULL;
117
118 const short c_nm_open_timeout = cs_subset_number(NeoMutt->sub, "nm_open_timeout");
119 mutt_debug(LL_DEBUG1, "nm: db open '%s' %s (timeout %d)\n", filename,
120 writable ? "[WRITE]" : "[READ]", c_nm_open_timeout);
121
122 const notmuch_database_mode_t mode = writable ? NOTMUCH_DATABASE_MODE_READ_WRITE :
123 NOTMUCH_DATABASE_MODE_READ_ONLY;
124
125 do
126 {
127#if LIBNOTMUCH_CHECK_VERSION(5, 4, 0)
128 // notmuch 0.32-0.32.2 didn't bump libnotmuch version to 5.4.
129 const char *config_file = get_nm_config_file();
130 const char *const c_nm_config_profile = cs_subset_string(NeoMutt->sub, "nm_config_profile");
131
132 st = notmuch_database_open_with_config(filename, mode, config_file,
133 c_nm_config_profile, &db, &msg);
134
135 // Attempt opening database without configuration file. Don't if the user specified no config.
136 if (st == NOTMUCH_STATUS_NO_CONFIG && !mutt_str_equal(config_file, ""))
137 {
138 mutt_debug(LL_DEBUG1, "nm: Could not find notmuch configuration file: %s\n", config_file);
139 mutt_debug(LL_DEBUG1, "nm: Attempting to open notmuch db without configuration file.\n");
140
141 FREE(&msg);
142
143 st = notmuch_database_open_with_config(filename, mode, "", NULL, &db, &msg);
144 }
145#elif LIBNOTMUCH_CHECK_VERSION(4, 2, 0)
146 st = notmuch_database_open_verbose(filename, mode, &db, &msg);
147#elif defined(NOTMUCH_API_3)
148 st = notmuch_database_open(filename, mode, &db);
149#else
150 db = notmuch_database_open(filename, mode);
151#endif
152 if ((st == NOTMUCH_STATUS_FILE_ERROR) || db || !c_nm_open_timeout ||
153 ((ct / 2) > c_nm_open_timeout))
154 {
155 break;
156 }
157
158 if (verbose && ct && ((ct % 2) == 0))
159 mutt_error(_("Waiting for notmuch DB... (%d sec)"), ct / 2);
160 mutt_date_sleep_ms(500); /* Half a second */
161 ct++;
162 } while (true);
163
164 if (st != NOTMUCH_STATUS_SUCCESS)
165 {
166 db = NULL;
167 }
168
169 if (verbose)
170 {
171 if (!db)
172 {
173 if (msg)
174 {
175 mutt_error("%s", msg);
176 }
177 else
178 {
179 mutt_error(_("Can't open notmuch database: %s: %s"), filename,
180 st ? notmuch_status_to_string(st) : _("unknown reason"));
181 }
182 }
183 else if (ct > 1)
184 {
186 }
187 }
188
189 FREE(&msg);
190
191 return db;
192}
193
200notmuch_database_t *nm_db_get(struct Mailbox *m, bool writable)
201{
202 struct NmAccountData *adata = nm_adata_get(m);
203
204 if (!adata)
205 return NULL;
206
207 // Use an existing open db if we have one.
208 if (adata->db)
209 return adata->db;
210
211 const char *db_filename = nm_db_get_filename(m);
212 if (db_filename)
213 adata->db = nm_db_do_open(db_filename, writable, true);
214
215 return adata->db;
216}
217
224int nm_db_release(struct Mailbox *m)
225{
226 struct NmAccountData *adata = nm_adata_get(m);
227 if (!adata || !adata->db || nm_db_is_longrun(m))
228 return -1;
229
230 mutt_debug(LL_DEBUG1, "nm: db close\n");
231 nm_db_free(adata->db);
232 adata->db = NULL;
233 adata->longrun = false;
234 return 0;
235}
236
241void nm_db_free(notmuch_database_t *db)
242{
243#ifdef NOTMUCH_API_3
244 notmuch_database_destroy(db);
245#else
246 notmuch_database_close(db);
247#endif
248}
249
258{
259 struct NmAccountData *adata = nm_adata_get(m);
260 if (!adata || !adata->db)
261 return -1;
262
263 if (adata->trans)
264 return 0;
265
266 mutt_debug(LL_DEBUG2, "nm: db trans start\n");
267 if (notmuch_database_begin_atomic(adata->db))
268 return -1;
269 adata->trans = true;
270 return 1;
271}
272
280{
281 struct NmAccountData *adata = nm_adata_get(m);
282 if (!adata || !adata->db)
283 return -1;
284
285 if (!adata->trans)
286 return 0;
287
288 mutt_debug(LL_DEBUG2, "nm: db trans end\n");
289 adata->trans = false;
290 if (notmuch_database_end_atomic(adata->db))
291 return -1;
292
293 return 0;
294}
295
306int nm_db_get_mtime(struct Mailbox *m, time_t *mtime)
307{
308 if (!m || !mtime)
309 return -1;
310
311 struct stat st = { 0 };
312 char path[PATH_MAX] = { 0 };
313 const char *db_filename = nm_db_get_filename(m);
314
315 mutt_debug(LL_DEBUG2, "nm: checking database mtime '%s'\n", db_filename);
316
317 // See if the path we were given has a Xapian directory.
318 // After notmuch 0.32, a .notmuch folder isn't guaranteed.
319 snprintf(path, sizeof(path), "%s/xapian", db_filename);
320 if (stat(path, &st) == 0)
321 {
322 *mtime = st.st_mtime;
323 return 0;
324 }
325
326 // Otherwise, check for a .notmuch directory.
327 snprintf(path, sizeof(path), "%s/.notmuch/xapian", db_filename);
328
329 if (stat(path, &st) != 0)
330 return -1;
331
332 *mtime = st.st_mtime;
333 return 0;
334}
335
342{
343 struct NmAccountData *adata = nm_adata_get(m);
344 if (!adata)
345 return false;
346
347 return adata->longrun;
348}
349
355void nm_db_longrun_init(struct Mailbox *m, bool writable)
356{
357 struct NmAccountData *adata = nm_adata_get(m);
358
359 if (!(adata && nm_db_get(m, writable)))
360 return;
361
362 adata->longrun = true;
363 mutt_debug(LL_DEBUG2, "nm: long run initialized\n");
364}
365
371{
372 struct NmAccountData *adata = nm_adata_get(m);
373
374 if (adata)
375 {
376 adata->longrun = false; /* to force nm_db_release() released DB */
377 if (nm_db_release(m) == 0)
378 mutt_debug(LL_DEBUG2, "nm: long run deinitialized\n");
379 else
380 adata->longrun = true;
381 }
382}
383
389{
390 struct NmAccountData *adata = nm_adata_get(m);
391 if (!adata || !adata->db)
392 return;
393
394 mutt_debug(LL_DEBUG1, "nm: ERROR: db is open, closing\n");
395 nm_db_release(m);
396}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:144
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:169
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
Structs that make up an email.
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
enum MailboxType nm_path_probe(const char *path, const struct stat *st)
Is this a Notmuch Mailbox? - Implements MxOps::path_probe() -.
Definition: notmuch.c:2443
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:51
#define FREE(x)
Definition: memory.h:45
void mutt_date_sleep_ms(size_t ms)
Sleep for milliseconds.
Definition: date.c:956
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition: string.c:497
#define PATH_MAX
Definition: mutt.h:41
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
NeoMutt Logging.
struct NmAccountData * nm_adata_get(struct Mailbox *m)
Get the Notmuch Account data.
Definition: adata.c:71
notmuch_database_t * nm_db_get(struct Mailbox *m, bool writable)
Get the Notmuch database.
Definition: db.c:200
int nm_db_trans_begin(struct Mailbox *m)
Start a Notmuch database transaction.
Definition: db.c:257
notmuch_database_t * nm_db_do_open(const char *filename, bool writable, bool verbose)
Open a Notmuch database.
Definition: db.c:111
void nm_db_longrun_done(struct Mailbox *m)
Finish a long transaction.
Definition: db.c:370
const char * nm_db_get_filename(struct Mailbox *m)
Get the filename of the Notmuch database.
Definition: db.c:54
int nm_db_get_mtime(struct Mailbox *m, time_t *mtime)
Get the database modification time.
Definition: db.c:306
int nm_db_release(struct Mailbox *m)
Close the Notmuch database.
Definition: db.c:224
bool nm_db_is_longrun(struct Mailbox *m)
Is Notmuch in the middle of a long-running transaction.
Definition: db.c:341
void nm_db_longrun_init(struct Mailbox *m, bool writable)
Start a long transaction.
Definition: db.c:355
void nm_db_debug_check(struct Mailbox *m)
Check if the database is open.
Definition: db.c:388
void nm_db_free(notmuch_database_t *db)
Decoupled way to close a Notmuch database.
Definition: db.c:241
int nm_db_trans_end(struct Mailbox *m)
End a database transaction.
Definition: db.c:279
struct NmMboxData * nm_mdata_get(struct Mailbox *m)
Get the Notmuch Mailbox data.
Definition: mdata.c:96
Notmuch-specific Mailbox data.
const int NmUrlProtocolLen
Length of NmUrlProtocol string.
Definition: notmuch.c:95
Pop-specific Account data.
GUI display the mailboxes in a side panel.
Key value store.
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
A mailbox.
Definition: mailbox.h:79
void * mdata
Driver specific data.
Definition: mailbox.h:133
bool verbose
Display status messages?
Definition: mailbox.h:116
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
Notmuch-specific Account data -.
Definition: adata.h:35
notmuch_database_t * db
Connection to Notmuch database.
Definition: adata.h:36
Notmuch-specific Mailbox data -.
Definition: mdata.h:35