NeoMutt  2022-04-29-145-g9b6a0e
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 <sys/stat.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 54 of file db.c.

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 }
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
#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:2432
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:51
struct NmMboxData * nm_mdata_get(struct Mailbox *m)
Get the Notmuch Mailbox data.
Definition: mdata.c:97
const int NmUrlProtocolLen
Definition: notmuch.c:94
void * mdata
Driver specific data.
Definition: mailbox.h:133
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 86 of file db.c.

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

170 {
171  struct NmAccountData *adata = nm_adata_get(m);
172 
173  if (!adata)
174  return NULL;
175 
176  // Use an existing open db if we have one.
177  if (adata->db)
178  return adata->db;
179 
180  const char *db_filename = nm_db_get_filename(m);
181  if (db_filename)
182  adata->db = nm_db_do_open(db_filename, writable, true);
183 
184  return adata->db;
185 }
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:54
notmuch_database_t * nm_db_do_open(const char *filename, bool writable, bool verbose)
Open a Notmuch database.
Definition: db.c:86
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 193 of file db.c.

194 {
195  struct NmAccountData *adata = nm_adata_get(m);
196  if (!adata || !adata->db || nm_db_is_longrun(m))
197  return -1;
198 
199  mutt_debug(LL_DEBUG1, "nm: db close\n");
200  nm_db_free(adata->db);
201  adata->db = NULL;
202  adata->longrun = false;
203  return 0;
204 }
bool nm_db_is_longrun(struct Mailbox *m)
Is Notmuch in the middle of a long-running transaction.
Definition: db.c:310
void nm_db_free(notmuch_database_t *db)
Decoupled way to close a Notmuch database.
Definition: db.c:210
+ 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 210 of file db.c.

211 {
212 #ifdef NOTMUCH_API_3
213  notmuch_database_destroy(db);
214 #else
215  notmuch_database_close(db);
216 #endif
217 }
+ 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 226 of file db.c.

227 {
228  struct NmAccountData *adata = nm_adata_get(m);
229  if (!adata || !adata->db)
230  return -1;
231 
232  if (adata->trans)
233  return 0;
234 
235  mutt_debug(LL_DEBUG2, "nm: db trans start\n");
236  if (notmuch_database_begin_atomic(adata->db))
237  return -1;
238  adata->trans = true;
239  return 1;
240 }
+ 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 248 of file db.c.

249 {
250  struct NmAccountData *adata = nm_adata_get(m);
251  if (!adata || !adata->db)
252  return -1;
253 
254  if (!adata->trans)
255  return 0;
256 
257  mutt_debug(LL_DEBUG2, "nm: db trans end\n");
258  adata->trans = false;
259  if (notmuch_database_end_atomic(adata->db))
260  return -1;
261 
262  return 0;
263 }
+ 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 275 of file db.c.

276 {
277  if (!m || !mtime)
278  return -1;
279 
280  struct stat st = { 0 };
281  char path[PATH_MAX];
282  const char *db_filename = nm_db_get_filename(m);
283 
284  mutt_debug(LL_DEBUG2, "nm: checking database mtime '%s'\n", db_filename);
285 
286  // See if the path we were given has a Xapian directory.
287  // After notmuch 0.32, a .notmuch folder isn't guaranteed.
288  snprintf(path, sizeof(path), "%s/xapian", db_filename);
289  if (stat(path, &st) == 0)
290  {
291  *mtime = st.st_mtime;
292  return 0;
293  }
294 
295  // Otherwise, check for a .notmuch directory.
296  snprintf(path, sizeof(path), "%s/.notmuch/xapian", db_filename);
297 
298  if (stat(path, &st) != 0)
299  return -1;
300 
301  *mtime = st.st_mtime;
302  return 0;
303 }
#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 310 of file db.c.

311 {
312  struct NmAccountData *adata = nm_adata_get(m);
313  if (!adata)
314  return false;
315 
316  return adata->longrun;
317 }
+ 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 324 of file db.c.

325 {
326  struct NmAccountData *adata = nm_adata_get(m);
327 
328  if (!(adata && nm_db_get(m, writable)))
329  return;
330 
331  adata->longrun = true;
332  mutt_debug(LL_DEBUG2, "nm: long run initialized\n");
333 }
notmuch_database_t * nm_db_get(struct Mailbox *m, bool writable)
Get the Notmuch database.
Definition: db.c:169
+ 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 339 of file db.c.

340 {
341  struct NmAccountData *adata = nm_adata_get(m);
342 
343  if (adata)
344  {
345  adata->longrun = false; /* to force nm_db_release() released DB */
346  if (nm_db_release(m) == 0)
347  mutt_debug(LL_DEBUG2, "nm: long run deinitialized\n");
348  else
349  adata->longrun = true;
350  }
351 }
int nm_db_release(struct Mailbox *m)
Close the Notmuch database.
Definition: db.c:193
+ 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 357 of file db.c.

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