NeoMutt  2024-04-16-36-g75b6fb
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.

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

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

250{
251#ifdef NOTMUCH_API_3
252 notmuch_database_destroy(db);
253#else
254 notmuch_database_close(db);
255#endif
256}
+ 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
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 57 of file db.c.

58{
59 struct NmMboxData *mdata = nm_mdata_get(m);
60 const char *db_filename = NULL;
61
62 const char *const c_nm_default_url = 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}
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:51
enum MailboxType nm_path_probe(const char *path, const struct stat *st)
Is this a Notmuch Mailbox? - Implements MxOps::path_probe() -.
Definition: notmuch.c:2457
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
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:103
void * mdata
Driver specific data.
Definition: mailbox.h:132
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 314 of file db.c.

315{
316 if (!m || !mtime)
317 return -1;
318
319 struct stat st = { 0 };
320 char path[PATH_MAX] = { 0 };
321 const char *db_filename = nm_db_get_filename(m);
322
323 mutt_debug(LL_DEBUG2, "nm: checking database mtime '%s'\n", db_filename);
324
325 // See if the path we were given has a Xapian directory.
326 // After notmuch 0.32, a .notmuch folder isn't guaranteed.
327 snprintf(path, sizeof(path), "%s/xapian", db_filename);
328 if (stat(path, &st) == 0)
329 {
330 *mtime = st.st_mtime;
331 return 0;
332 }
333
334 // Otherwise, check for a .notmuch directory.
335 snprintf(path, sizeof(path), "%s/.notmuch/xapian", db_filename);
336
337 if (stat(path, &st) != 0)
338 return -1;
339
340 *mtime = st.st_mtime;
341 return 0;
342}
#define PATH_MAX
Definition: mutt.h:42
const char * nm_db_get_filename(struct Mailbox *m)
Get the filename of the Notmuch database.
Definition: db.c:57
+ 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 208 of file db.c.

209{
210 struct NmAccountData *adata = nm_adata_get(m);
211
212 if (!adata)
213 return NULL;
214
215 // Use an existing open db if we have one.
216 if (adata->db)
217 return adata->db;
218
219 const char *db_filename = nm_db_get_filename(m);
220 if (db_filename)
221 adata->db = nm_db_do_open(db_filename, writable, true);
222
223 return adata->db;
224}
struct NmAccountData * nm_adata_get(struct Mailbox *m)
Get the Notmuch Account data.
Definition: adata.c:71
notmuch_database_t * nm_db_do_open(const char *filename, bool writable, bool verbose)
Open a Notmuch database.
Definition: db.c:114
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 349 of file db.c.

350{
351 struct NmAccountData *adata = nm_adata_get(m);
352 if (!adata)
353 return false;
354
355 return adata->longrun;
356}
+ 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 232 of file db.c.

233{
234 struct NmAccountData *adata = nm_adata_get(m);
235 if (!adata || !adata->db || nm_db_is_longrun(m))
236 return -1;
237
238 mutt_debug(LL_DEBUG1, "nm: db close\n");
239 nm_db_free(adata->db);
240 adata->db = NULL;
241 adata->longrun = false;
242 return 0;
243}
bool nm_db_is_longrun(struct Mailbox *m)
Is Notmuch in the middle of a long-running transaction.
Definition: db.c:349
void nm_db_free(notmuch_database_t *db)
Decoupled way to close a Notmuch database.
Definition: db.c:249
+ 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 265 of file db.c.

266{
267 struct NmAccountData *adata = nm_adata_get(m);
268 if (!adata || !adata->db)
269 return -1;
270
271 if (adata->trans)
272 return 0;
273
274 mutt_debug(LL_DEBUG2, "nm: db trans start\n");
275 if (notmuch_database_begin_atomic(adata->db))
276 return -1;
277 adata->trans = true;
278 return 1;
279}
+ 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 287 of file db.c.

288{
289 struct NmAccountData *adata = nm_adata_get(m);
290 if (!adata || !adata->db)
291 return -1;
292
293 if (!adata->trans)
294 return 0;
295
296 mutt_debug(LL_DEBUG2, "nm: db trans end\n");
297 adata->trans = false;
298 if (notmuch_database_end_atomic(adata->db))
299 return -1;
300
301 return 0;
302}
+ 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 101 of file notmuch.c.

◆ NmUrlProtocolLen

const int NmUrlProtocolLen
extern

Length of NmUrlProtocol string.

Definition at line 103 of file notmuch.c.