NeoMutt  2025-12-11-911-gd8d604
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
private.h File Reference

Notmuch private types. More...

#include <notmuch.h>
#include <stdbool.h>
#include <time.h>
+ Include dependency graph for private.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define HAVE_NOTMUCH_DATABASE_INDEX_FILE   0
 
#define HAVE_NOTMUCH_DATABASE_OPEN_WITH_CONFIG   0
 
#define LIBNOTMUCH_CHECK_VERSION(major, minor, micro)
 

Functions

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

Variables

const char NmUrlProtocol []
 Protocol string for Notmuch URLs.
 
const int NmUrlProtocolLen
 Length of NmUrlProtocol string.
 

Detailed Description

Notmuch private types.

Authors
  • Richard Russon
  • Austin Ray

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 private.h.

Macro Definition Documentation

◆ HAVE_NOTMUCH_DATABASE_INDEX_FILE

#define HAVE_NOTMUCH_DATABASE_INDEX_FILE   0

Definition at line 38 of file private.h.

◆ HAVE_NOTMUCH_DATABASE_OPEN_WITH_CONFIG

#define HAVE_NOTMUCH_DATABASE_OPEN_WITH_CONFIG   0

Definition at line 42 of file private.h.

◆ LIBNOTMUCH_CHECK_VERSION

#define LIBNOTMUCH_CHECK_VERSION ( major,
minor,
micro )
Value:
(major == 5 && minor == 4 && HAVE_NOTMUCH_DATABASE_OPEN_WITH_CONFIG) || \
(major == 5 && minor == 1 && HAVE_NOTMUCH_DATABASE_INDEX_FILE) || \
((LIBNOTMUCH_MAJOR_VERSION > (major) || \
(LIBNOTMUCH_MAJOR_VERSION == (major) && LIBNOTMUCH_MINOR_VERSION > (minor)) || \
(LIBNOTMUCH_MAJOR_VERSION == (major) && \
LIBNOTMUCH_MINOR_VERSION == (minor) && LIBNOTMUCH_MICRO_VERSION >= (micro))))
#define HAVE_NOTMUCH_DATABASE_INDEX_FILE
Definition private.h:38
#define HAVE_NOTMUCH_DATABASE_OPEN_WITH_CONFIG
Definition private.h:42

Definition at line 52 of file private.h.

52#define LIBNOTMUCH_CHECK_VERSION(major, minor, micro) \
53 (major == 5 && minor == 4 && HAVE_NOTMUCH_DATABASE_OPEN_WITH_CONFIG) || \
54 (major == 5 && minor == 1 && HAVE_NOTMUCH_DATABASE_INDEX_FILE) || \
55 ((LIBNOTMUCH_MAJOR_VERSION > (major) || \
56 (LIBNOTMUCH_MAJOR_VERSION == (major) && LIBNOTMUCH_MINOR_VERSION > (minor)) || \
57 (LIBNOTMUCH_MAJOR_VERSION == (major) && \
58 LIBNOTMUCH_MINOR_VERSION == (minor) && LIBNOTMUCH_MICRO_VERSION >= (micro))))

Function Documentation

◆ 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 116 of file db.c.

117{
118 notmuch_database_t *db = NULL;
119 int ct = 0;
120 notmuch_status_t st = NOTMUCH_STATUS_SUCCESS;
121 char *msg = NULL;
122
123 const short c_nm_open_timeout = cs_subset_number(NeoMutt->sub, "nm_open_timeout");
124 mutt_debug(LL_DEBUG1, "nm: db open '%s' %s (timeout %d)\n", filename ? filename : "(auto)",
125 writable ? "[WRITE]" : "[READ]", c_nm_open_timeout);
126
127 const notmuch_database_mode_t mode = writable ? NOTMUCH_DATABASE_MODE_READ_WRITE :
128 NOTMUCH_DATABASE_MODE_READ_ONLY;
129
130 do
131 {
132#if LIBNOTMUCH_CHECK_VERSION(5, 4, 0)
133 // notmuch 0.32-0.32.2 didn't bump libnotmuch version to 5.4.
134 const char *config_file = get_nm_config_file();
135 const char *const c_nm_config_profile = cs_subset_string(NeoMutt->sub, "nm_config_profile");
136
137 FREE(&msg);
138 st = notmuch_database_open_with_config(filename, mode, config_file,
139 c_nm_config_profile, &db, &msg);
140
141 // Attempt opening database without configuration file. Don't if the user specified no config.
142 if ((st == NOTMUCH_STATUS_NO_CONFIG) && !mutt_str_equal(config_file, ""))
143 {
144 mutt_debug(LL_DEBUG1, "nm: Could not find notmuch configuration file: %s\n", config_file);
145 mutt_debug(LL_DEBUG1, "nm: Attempting to open notmuch db without configuration file\n");
146
147 FREE(&msg);
148
149 st = notmuch_database_open_with_config(filename, mode, "", NULL, &db, &msg);
150 }
151 else if ((st == NOTMUCH_STATUS_NO_CONFIG) && !config_file)
152 {
153 FREE(&msg);
154 }
155#elif LIBNOTMUCH_CHECK_VERSION(4, 2, 0)
156 st = notmuch_database_open_verbose(filename, mode, &db, &msg);
157#elif defined(NOTMUCH_API_3)
158 st = notmuch_database_open(filename, mode, &db);
159#else
160 db = notmuch_database_open(filename, mode);
161#endif
162 if ((st == NOTMUCH_STATUS_FILE_ERROR) || db || !c_nm_open_timeout ||
163 ((ct / 2) > c_nm_open_timeout))
164 {
165 break;
166 }
167
168 if (verbose && ct && ((ct % 2) == 0))
169 mutt_error(_("Waiting for notmuch DB... (%d sec)"), ct / 2);
170 mutt_date_sleep_ms(500); /* Half a second */
171 ct++;
172 } while (true);
173
174 if (st != NOTMUCH_STATUS_SUCCESS)
175 {
176 db = NULL;
177 }
178
179 if (verbose)
180 {
181 if (!db)
182 {
183 if (msg)
184 {
185 mutt_error("%s", msg);
186 }
187 else
188 {
189 mutt_error(_("Can't open notmuch database: %s: %s"), filename,
190 st ? notmuch_status_to_string(st) : _("unknown reason"));
191 }
192 }
193 else if (ct > 1)
194 {
196 }
197 }
198
199 FREE(&msg);
200
201 return db;
202}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition helpers.c:143
#define mutt_error(...)
Definition logging2.h:94
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
void mutt_date_sleep_ms(size_t ms)
Sleep for milliseconds.
Definition date.c:984
#define _(a)
Definition message.h:28
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:665
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
+ 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 262 of file db.c.

263{
264#ifdef NOTMUCH_API_3
265 notmuch_database_destroy(db);
266#else
267 notmuch_database_close(db);
268#endif
269}
+ Here is the caller graph for this function:

◆ 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
NULLNo notmuch-specific database path configured

Returns the database path from the mailbox URL or $nm_default_url. On libnotmuch >= 5.4, a NULL return allows notmuch to resolve the database path from its own configuration file or environment variables.

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 63 of file db.c.

64{
65 struct NmMboxData *mdata = nm_mdata_get(m);
66 const char *db_filename = NULL;
67
68 const char *const c_nm_default_url = cs_subset_string(NeoMutt->sub, "nm_default_url");
69 if (mdata && mdata->db_url && mdata->db_url->path)
70 db_filename = mdata->db_url->path;
71 else
72 db_filename = c_nm_default_url;
73
74 if (!db_filename)
75 return NULL;
76
77 if (nm_path_probe(db_filename, NULL) == MUTT_NOTMUCH)
78 db_filename += NmUrlProtocolLen;
79
80 mutt_debug(LL_DEBUG2, "nm: db filename '%s'\n", db_filename);
81 return db_filename;
82}
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition mailbox.h:50
enum MailboxType nm_path_probe(const char *path, const struct stat *st)
Is this a Notmuch Mailbox?
Definition notmuch.c:2531
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:46
struct NmMboxData * nm_mdata_get(struct Mailbox *m)
Get the Notmuch Mailbox data.
Definition mdata.c:96
const int NmUrlProtocolLen
Length of NmUrlProtocol string.
Definition notmuch.c:104
void * mdata
Driver specific data.
Definition mailbox.h:134
Notmuch-specific Mailbox data -.
Definition mdata.h:35
+ 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 327 of file db.c.

328{
329 if (!m || !mtime)
330 return -1;
331
332 struct stat st = { 0 };
333 char path[PATH_MAX] = { 0 };
334 const char *db_filename = nm_db_get_filename(m);
335 if (!db_filename)
336 return -1;
337
338 mutt_debug(LL_DEBUG2, "nm: checking database mtime '%s'\n", db_filename);
339
340 // See if the path we were given has a Xapian directory.
341 // After notmuch 0.32, a .notmuch folder isn't guaranteed.
342 snprintf(path, sizeof(path), "%s/xapian", db_filename);
343 if (stat(path, &st) == 0)
344 {
345 *mtime = st.st_mtime;
346 return 0;
347 }
348
349 // Otherwise, check for a .notmuch directory.
350 snprintf(path, sizeof(path), "%s/.notmuch/xapian", db_filename);
351
352 if (stat(path, &st) != 0)
353 return -1;
354
355 *mtime = st.st_mtime;
356 return 0;
357}
#define PATH_MAX
Definition mutt.h:49
const char * nm_db_get_filename(struct Mailbox *m)
Get the filename of the Notmuch database.
Definition db.c:63
+ 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 210 of file db.c.

211{
212 struct NmAccountData *adata = nm_adata_get(m);
213
214 if (!adata)
215 return NULL;
216
217 // Use an existing open db if we have one.
218 if (adata->db)
219 return adata->db;
220
221 const char *db_filename = nm_db_get_filename(m);
222#if LIBNOTMUCH_CHECK_VERSION(5, 4, 0)
223 // On libnotmuch >= 5.4, NULL db_filename lets notmuch resolve the
224 // database path from its config file or environment variables.
225 adata->db = nm_db_do_open(db_filename, writable, true);
226#else
227 if (!db_filename)
228 {
229 const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
230 db_filename = c_folder;
231 }
232 if (db_filename)
233 adata->db = nm_db_do_open(db_filename, writable, true);
234#endif
235
236 return adata->db;
237}
struct NmAccountData * nm_adata_get(struct Mailbox *m)
Get the Notmuch Account data.
Definition adata.c:69
notmuch_database_t * nm_db_do_open(const char *filename, bool writable, bool verbose)
Open a Notmuch database.
Definition db.c:116
void * adata
Private data (for Mailbox backends)
Definition account.h:42
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_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 364 of file db.c.

365{
366 struct NmAccountData *adata = nm_adata_get(m);
367 if (!adata)
368 return false;
369
370 return adata->longrun;
371}
+ 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 245 of file db.c.

246{
247 struct NmAccountData *adata = nm_adata_get(m);
248 if (!adata || !adata->db || nm_db_is_longrun(m))
249 return -1;
250
251 mutt_debug(LL_DEBUG1, "nm: db close\n");
252 nm_db_free(adata->db);
253 adata->db = NULL;
254 adata->longrun = false;
255 return 0;
256}
bool nm_db_is_longrun(struct Mailbox *m)
Is Notmuch in the middle of a long-running transaction.
Definition db.c:364
void nm_db_free(notmuch_database_t *db)
Decoupled way to close a Notmuch database.
Definition db.c:262
+ Here is the call graph for this function:
+ 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 278 of file db.c.

279{
280 struct NmAccountData *adata = nm_adata_get(m);
281 if (!adata || !adata->db)
282 return -1;
283
284 if (adata->trans)
285 return 0;
286
287 mutt_debug(LL_DEBUG2, "nm: db trans start\n");
288 if (notmuch_database_begin_atomic(adata->db))
289 return -1;
290 adata->trans = true;
291 return 1;
292}
+ 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 300 of file db.c.

301{
302 struct NmAccountData *adata = nm_adata_get(m);
303 if (!adata || !adata->db)
304 return -1;
305
306 if (!adata->trans)
307 return 0;
308
309 mutt_debug(LL_DEBUG2, "nm: db trans end\n");
310 adata->trans = false;
311 if (notmuch_database_end_atomic(adata->db))
312 return -1;
313
314 return 0;
315}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ NmUrlProtocol

const char NmUrlProtocol[]
extern

Protocol string for Notmuch URLs.

Definition at line 102 of file notmuch.c.

◆ NmUrlProtocolLen

const int NmUrlProtocolLen
extern

Length of NmUrlProtocol string.

Definition at line 104 of file notmuch.c.