NeoMutt  2021-10-29-220-g2b1eec
Teaching an old dog new tricks
DOXYGEN
db.c File Reference

Notmuch database handling. More...

#include "config.h"
#include <limits.h>
#include <notmuch.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include "private.h"
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "lib.h"
#include "adata.h"
#include "mdata.h"
#include "mutt_logging.h"
+ Include dependency graph for db.c:

Go to the source code of this file.

Functions

const char * nm_db_get_filename (struct Mailbox *m)
 Get the filename of the Notmuch database. More...
 
notmuch_database_t * nm_db_do_open (const char *filename, bool writable, bool verbose)
 Open a Notmuch database. More...
 
notmuch_database_t * nm_db_get (struct Mailbox *m, bool writable)
 Get the Notmuch database. More...
 
int nm_db_release (struct Mailbox *m)
 Close the Notmuch database. More...
 
void nm_db_free (notmuch_database_t *db)
 Decoupled way to close a Notmuch database. More...
 
int nm_db_trans_begin (struct Mailbox *m)
 Start a Notmuch database transaction. More...
 
int nm_db_trans_end (struct Mailbox *m)
 End a database transaction. More...
 
int nm_db_get_mtime (struct Mailbox *m, time_t *mtime)
 Get the database modification time. More...
 
bool nm_db_is_longrun (struct Mailbox *m)
 Is Notmuch in the middle of a long-running transaction. More...
 
void nm_db_longrun_init (struct Mailbox *m, bool writable)
 Start a long transaction. More...
 
void nm_db_longrun_done (struct Mailbox *m)
 Finish a long transaction. More...
 
void nm_db_debug_check (struct Mailbox *m)
 Check if the database is open. More...
 

Detailed Description

Notmuch database handling.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file db.c.

Function Documentation

◆ nm_db_get_filename()

const char* nm_db_get_filename ( struct Mailbox m)

Get the filename of the Notmuch database.

Parameters
mMailbox
Return values
ptrFilename
Note
The return value is a pointer into the $nm_default_url global variable. If that variable changes, the result will be invalid. It must not be freed.

Definition at line 56 of file db.c.

57 {
58  struct NmMboxData *mdata = nm_mdata_get(m);
59  const char *db_filename = NULL;
60 
61  const char *const c_nm_default_url =
62  cs_subset_string(NeoMutt->sub, "nm_default_url");
63  if (mdata && mdata->db_url && mdata->db_url->path)
64  db_filename = mdata->db_url->path;
65  else
66  db_filename = c_nm_default_url;
67 
68  const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
69  if (!db_filename && !c_folder)
70  return NULL;
71 
72  if (!db_filename)
73  db_filename = c_folder;
74 
75  if (nm_path_probe(db_filename, NULL) == MUTT_NOTMUCH)
76  db_filename += NmUrlProtocolLen;
77 
78  mutt_debug(LL_DEBUG2, "nm: db filename '%s'\n", db_filename);
79  return db_filename;
80 }
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
enum MailboxType nm_path_probe(const char *path, const struct stat *st)
Is this a Notmuch Mailbox? - Implements MxOps::path_probe() -.
Definition: notmuch.c:2453
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:54
struct NmMboxData * nm_mdata_get(struct Mailbox *m)
Get the Notmuch Mailbox data.
Definition: mdata.c:98
const int NmUrlProtocolLen
Definition: notmuch.c:94
void * mdata
Driver specific data.
Definition: mailbox.h:136
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Notmuch-specific Mailbox data -.
Definition: mdata.h:34
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_do_open()

notmuch_database_t* nm_db_do_open ( const char *  filename,
bool  writable,
bool  verbose 
)

Open a Notmuch database.

Parameters
filenameDatabase filename
writableRead/write?
verboseShow errors on failure?
Return values
ptrNotmuch database

Definition at line 89 of file db.c.

90 {
91  notmuch_database_t *db = NULL;
92  int ct = 0;
93  notmuch_status_t st = NOTMUCH_STATUS_SUCCESS;
94 #if LIBNOTMUCH_CHECK_VERSION(4, 2, 0)
95  char *msg = NULL;
96 #endif
97 
98  const short c_nm_open_timeout =
99  cs_subset_number(NeoMutt->sub, "nm_open_timeout");
100  mutt_debug(LL_DEBUG1, "nm: db open '%s' %s (timeout %d)\n", filename,
101  writable ? "[WRITE]" : "[READ]", c_nm_open_timeout);
102 
103  const notmuch_database_mode_t mode =
104  writable ? NOTMUCH_DATABASE_MODE_READ_WRITE : NOTMUCH_DATABASE_MODE_READ_ONLY;
105 
106  do
107  {
108 #if LIBNOTMUCH_CHECK_VERSION(5, 4, 0)
109  // notmuch 0.32-0.32.2 didn't bump libnotmuch version to 5.4.
110  st = notmuch_database_open_with_config(filename, mode, NULL, NULL, &db, &msg);
111  if (st == NOTMUCH_STATUS_NO_CONFIG)
112  {
114  "nm: Could not find a Notmuch configuration file\n");
115  FREE(&msg);
116 
117  st = notmuch_database_open_with_config(filename, mode, "", NULL, &db, &msg);
118  }
119 #elif LIBNOTMUCH_CHECK_VERSION(4, 2, 0)
120  st = notmuch_database_open_verbose(filename, mode, &db, &msg);
121 #elif defined(NOTMUCH_API_3)
122  st = notmuch_database_open(filename, mode, &db);
123 #else
124  db = notmuch_database_open(filename, mode);
125 #endif
126  if ((st == NOTMUCH_STATUS_FILE_ERROR) || db || !c_nm_open_timeout ||
127  ((ct / 2) > c_nm_open_timeout))
128  {
129  break;
130  }
131 
132  if (verbose && ct && ((ct % 2) == 0))
133  mutt_error(_("Waiting for notmuch DB... (%d sec)"), ct / 2);
134  mutt_date_sleep_ms(500); /* Half a second */
135  ct++;
136  } while (true);
137 
138  if (st != NOTMUCH_STATUS_SUCCESS)
139  {
140  db = NULL;
141  }
142 
143  if (verbose)
144  {
145  if (!db)
146  {
147 #if LIBNOTMUCH_CHECK_VERSION(4, 2, 0)
148  if (msg)
149  {
150  mutt_error(msg);
151  FREE(&msg);
152  }
153  else
154 #endif
155  {
156  mutt_error(_("Can't open notmuch database: %s: %s"), filename,
157  st ? notmuch_status_to_string(st) : _("unknown reason"));
158  }
159  }
160  else if (ct > 1)
161  {
163  }
164  }
165  return db;
166 }
void mutt_date_sleep_ms(size_t ms)
Sleep for milliseconds.
Definition: date.c:704
#define mutt_error(...)
Definition: logging.h:87
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
#define FREE(x)
Definition: memory.h:40
#define _(a)
Definition: message.h:28
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:113
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_get()

notmuch_database_t* nm_db_get ( struct Mailbox m,
bool  writable 
)

Get the Notmuch database.

Parameters
mMailbox
writableRead/write?
Return values
ptrNotmuch database

Definition at line 174 of file db.c.

175 {
176  struct NmAccountData *adata = nm_adata_get(m);
177 
178  if (!adata)
179  return NULL;
180 
181  // Use an existing open db if we have one.
182  if (adata->db)
183  return adata->db;
184 
185  const char *db_filename = nm_db_get_filename(m);
186  if (db_filename)
187  adata->db = nm_db_do_open(db_filename, writable, true);
188 
189  return adata->db;
190 }
struct NmAccountData * nm_adata_get(struct Mailbox *m)
Get the Notmuch Account data.
Definition: adata.c:68
const char * nm_db_get_filename(struct Mailbox *m)
Get the filename of the Notmuch database.
Definition: db.c:56
notmuch_database_t * nm_db_do_open(const char *filename, bool writable, bool verbose)
Open a Notmuch database.
Definition: db.c:89
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
Notmuch-specific Account data -.
Definition: adata.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_release()

int nm_db_release ( struct Mailbox m)

Close the Notmuch database.

Parameters
mMailbox
Return values
0Success
-1Failure

Definition at line 198 of file db.c.

199 {
200  struct NmAccountData *adata = nm_adata_get(m);
201  if (!adata || !adata->db || nm_db_is_longrun(m))
202  return -1;
203 
204  mutt_debug(LL_DEBUG1, "nm: db close\n");
205  nm_db_free(adata->db);
206  adata->db = NULL;
207  adata->longrun = false;
208  return 0;
209 }
bool nm_db_is_longrun(struct Mailbox *m)
Is Notmuch in the middle of a long-running transaction.
Definition: db.c:315
void nm_db_free(notmuch_database_t *db)
Decoupled way to close a Notmuch database.
Definition: db.c:215
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_free()

void nm_db_free ( notmuch_database_t *  db)

Decoupled way to close a Notmuch database.

Parameters
dbNotmuch database

Definition at line 215 of file db.c.

216 {
217 #ifdef NOTMUCH_API_3
218  notmuch_database_destroy(db);
219 #else
220  notmuch_database_close(db);
221 #endif
222 }
+ Here is the caller graph for this function:

◆ nm_db_trans_begin()

int nm_db_trans_begin ( struct Mailbox m)

Start a Notmuch database transaction.

Parameters
mMailbox
Return values
<0error
1new transaction started
0already within transaction

Definition at line 231 of file db.c.

232 {
233  struct NmAccountData *adata = nm_adata_get(m);
234  if (!adata || !adata->db)
235  return -1;
236 
237  if (adata->trans)
238  return 0;
239 
240  mutt_debug(LL_DEBUG2, "nm: db trans start\n");
241  if (notmuch_database_begin_atomic(adata->db))
242  return -1;
243  adata->trans = true;
244  return 1;
245 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_trans_end()

int nm_db_trans_end ( struct Mailbox m)

End a database transaction.

Parameters
mMailbox
Return values
0Success
-1Failure

Definition at line 253 of file db.c.

254 {
255  struct NmAccountData *adata = nm_adata_get(m);
256  if (!adata || !adata->db)
257  return -1;
258 
259  if (!adata->trans)
260  return 0;
261 
262  mutt_debug(LL_DEBUG2, "nm: db trans end\n");
263  adata->trans = false;
264  if (notmuch_database_end_atomic(adata->db))
265  return -1;
266 
267  return 0;
268 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_get_mtime()

int nm_db_get_mtime ( struct Mailbox m,
time_t *  mtime 
)

Get the database modification time.

Parameters
[in]mMailbox
[out]mtimeSave the modification time
Return values
0Success (result in mtime)
-1Error

Get the "mtime" (modification time) of the database file. This is the time of the last update.

Definition at line 280 of file db.c.

281 {
282  if (!m || !mtime)
283  return -1;
284 
285  struct stat st = { 0 };
286  char path[PATH_MAX];
287  const char *db_filename = nm_db_get_filename(m);
288 
289  mutt_debug(LL_DEBUG2, "nm: checking database mtime '%s'\n", db_filename);
290 
291  // See if the path we were given has a Xapian directory.
292  // After notmuch 0.32, a .notmuch folder isn't guaranteed.
293  snprintf(path, sizeof(path), "%s/xapian", db_filename);
294  if (stat(path, &st) == 0)
295  {
296  *mtime = st.st_mtime;
297  return 0;
298  }
299 
300  // Otherwise, check for a .notmuch directory.
301  snprintf(path, sizeof(path), "%s/.notmuch/xapian", db_filename);
302 
303  if (stat(path, &st) != 0)
304  return -1;
305 
306  *mtime = st.st_mtime;
307  return 0;
308 }
#define PATH_MAX
Definition: mutt.h:40
+ Here is the call graph for this function:

◆ nm_db_is_longrun()

bool nm_db_is_longrun ( struct Mailbox m)

Is Notmuch in the middle of a long-running transaction.

Parameters
mMailbox
Return values
trueNotmuch is in the middle of a long-running transaction

Definition at line 315 of file db.c.

316 {
317  struct NmAccountData *adata = nm_adata_get(m);
318  if (!adata)
319  return false;
320 
321  return adata->longrun;
322 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_longrun_init()

void nm_db_longrun_init ( struct Mailbox m,
bool  writable 
)

Start a long transaction.

Parameters
mMailbox
writableRead/write?

Definition at line 329 of file db.c.

330 {
331  struct NmAccountData *adata = nm_adata_get(m);
332 
333  if (!(adata && nm_db_get(m, writable)))
334  return;
335 
336  adata->longrun = true;
337  mutt_debug(LL_DEBUG2, "nm: long run initialized\n");
338 }
notmuch_database_t * nm_db_get(struct Mailbox *m, bool writable)
Get the Notmuch database.
Definition: db.c:174
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_longrun_done()

void nm_db_longrun_done ( struct Mailbox m)

Finish a long transaction.

Parameters
mMailbox

Definition at line 344 of file db.c.

345 {
346  struct NmAccountData *adata = nm_adata_get(m);
347 
348  if (adata)
349  {
350  adata->longrun = false; /* to force nm_db_release() released DB */
351  if (nm_db_release(m) == 0)
352  mutt_debug(LL_DEBUG2, "nm: long run deinitialized\n");
353  else
354  adata->longrun = true;
355  }
356 }
int nm_db_release(struct Mailbox *m)
Close the Notmuch database.
Definition: db.c:198
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_debug_check()

void nm_db_debug_check ( struct Mailbox m)

Check if the database is open.

Parameters
mMailbox

Definition at line 362 of file db.c.

363 {
364  struct NmAccountData *adata = nm_adata_get(m);
365  if (!adata || !adata->db)
366  return;
367 
368  mutt_debug(LL_DEBUG1, "nm: ERROR: db is open, closing\n");
369  nm_db_release(m);
370 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function: