NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
lib.h File Reference

Body Caching - local copies of email bodies. More...

#include <stdio.h>
+ Include dependency graph for lib.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef int(* bcache_list_t) (const char *id, struct BodyCache *bcache, void *data)
 Prototype for mutt_bcache_list() callback. More...
 

Functions

void mutt_bcache_close (struct BodyCache **bcache)
 Close an Email-Body Cache. More...
 
int mutt_bcache_commit (struct BodyCache *bcache, const char *id)
 Move a temporary file into the Body Cache. More...
 
int mutt_bcache_del (struct BodyCache *bcache, const char *id)
 Delete a file from the Body Cache. More...
 
int mutt_bcache_exists (struct BodyCache *bcache, const char *id)
 Check if a file exists in the Body Cache. More...
 
FILE * mutt_bcache_get (struct BodyCache *bcache, const char *id)
 Open a file in the Body Cache. More...
 
int mutt_bcache_list (struct BodyCache *bcache, bcache_list_t want_id, void *data)
 Find matching entries in the Body Cache. More...
 
struct BodyCachemutt_bcache_open (struct ConnAccount *account, const char *mailbox)
 Open an Email-Body Cache. More...
 
FILE * mutt_bcache_put (struct BodyCache *bcache, const char *id)
 Create a file in the Body Cache. More...
 

Detailed Description

Body Caching - local copies of email bodies.

Authors
  • Brendan Cully
  • Rocco Rutte

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

Typedef Documentation

◆ bcache_list_t

typedef int(* bcache_list_t) (const char *id, struct BodyCache *bcache, void *data)

Prototype for mutt_bcache_list() callback.

Parameters
idCache id
bcacheBody Cache from mutt_bcache_open()
dataData to pass to the callback function
Return values
-1Failure
>=0count of matching items

mutt_bcache_list() will call this function once for each item in the cache.

Definition at line 52 of file lib.h.

Function Documentation

◆ mutt_bcache_close()

void mutt_bcache_close ( struct BodyCache **  bcache)

Close an Email-Body Cache.

Parameters
[out]bcacheBody cache

Free all memory of bcache and finally FREE() it, too.

Definition at line 164 of file bcache.c.

165 {
166  if (!bcache || !*bcache)
167  return;
168  FREE(&(*bcache)->path);
169  FREE(bcache);
170 }
#define FREE(x)
Definition: memory.h:40
+ Here is the caller graph for this function:

◆ mutt_bcache_commit()

int mutt_bcache_commit ( struct BodyCache bcache,
const char *  id 
)

Move a temporary file into the Body Cache.

Parameters
bcacheBody Cache from mutt_bcache_open()
idPer-mailbox unique identifier for the message
Return values
0Success
-1Failure

Definition at line 247 of file bcache.c.

248 {
249  struct Buffer *tmpid = mutt_buffer_pool_get();
250  mutt_buffer_printf(tmpid, "%s.tmp", id);
251 
252  int rc = mutt_bcache_move(bcache, mutt_buffer_string(tmpid), id);
253  mutt_buffer_pool_release(&tmpid);
254  return rc;
255 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
String manipulation buffer.
Definition: buffer.h:33
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
static int mutt_bcache_move(struct BodyCache *bcache, const char *id, const char *newid)
Change the id of a message in the cache.
Definition: bcache.c:113
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_bcache_del()

int mutt_bcache_del ( struct BodyCache bcache,
const char *  id 
)

Delete a file from the Body Cache.

Parameters
bcacheBody Cache from mutt_bcache_open()
idPer-mailbox unique identifier for the message
Return values
0Success
-1Failure

Definition at line 264 of file bcache.c.

265 {
266  if (!id || (*id == '\0') || !bcache)
267  return -1;
268 
269  struct Buffer *path = mutt_buffer_pool_get();
270  mutt_buffer_addstr(path, bcache->path);
271  mutt_buffer_addstr(path, id);
272 
273  mutt_debug(LL_DEBUG3, "bcache: del: '%s'\n", mutt_buffer_string(path));
274 
275  int rc = unlink(mutt_buffer_string(path));
277  return rc;
278 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
String manipulation buffer.
Definition: buffer.h:33
char * path
Definition: bcache.c:52
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 3.
Definition: logging.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_bcache_exists()

int mutt_bcache_exists ( struct BodyCache bcache,
const char *  id 
)

Check if a file exists in the Body Cache.

Parameters
bcacheBody Cache from mutt_bcache_open()
idPer-mailbox unique identifier for the message
Return values
0Success
-1Failure

Definition at line 287 of file bcache.c.

288 {
289  if (!id || (*id == '\0') || !bcache)
290  return -1;
291 
292  struct Buffer *path = mutt_buffer_pool_get();
293  mutt_buffer_addstr(path, bcache->path);
294  mutt_buffer_addstr(path, id);
295 
296  int rc = 0;
297  struct stat st;
298  if (stat(mutt_buffer_string(path), &st) < 0)
299  rc = -1;
300  else
301  rc = (S_ISREG(st.st_mode) && (st.st_size != 0)) ? 0 : -1;
302 
303  mutt_debug(LL_DEBUG3, "bcache: exists: '%s': %s\n", mutt_buffer_string(path),
304  (rc == 0) ? "yes" : "no");
305 
307  return rc;
308 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
String manipulation buffer.
Definition: buffer.h:33
char * path
Definition: bcache.c:52
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 3.
Definition: logging.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_bcache_get()

FILE* mutt_bcache_get ( struct BodyCache bcache,
const char *  id 
)

Open a file in the Body Cache.

Parameters
bcacheBody Cache from mutt_bcache_open()
idPer-mailbox unique identifier for the message
Return values
ptrSuccess
NULLFailure

Definition at line 179 of file bcache.c.

180 {
181  if (!id || (*id == '\0') || !bcache)
182  return NULL;
183 
184  struct Buffer *path = mutt_buffer_pool_get();
185  mutt_buffer_addstr(path, bcache->path);
186  mutt_buffer_addstr(path, id);
187 
188  FILE *fp = mutt_file_fopen(mutt_buffer_string(path), "r");
189 
190  mutt_debug(LL_DEBUG3, "bcache: get: '%s': %s\n", mutt_buffer_string(path),
191  fp ? "yes" : "no");
192 
194  return fp;
195 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
String manipulation buffer.
Definition: buffer.h:33
char * path
Definition: bcache.c:52
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:589
Log at debug level 3.
Definition: logging.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_bcache_list()

int mutt_bcache_list ( struct BodyCache bcache,
bcache_list_t  want_id,
void *  data 
)

Find matching entries in the Body Cache.

Parameters
bcacheBody Cache from mutt_bcache_open()
want_idCallback function called for each match
dataData to pass to the callback function
Return values
-1Failure
>=0count of matching items

This more or less "examines" the cache and calls a function with each id it finds if given.

The optional callback function gets the id of a message, the very same body cache handle mutt_bcache_list() is called with (to, perhaps, perform further operations on the bcache), and a data cookie which is just passed as-is. If the return value of the callback is non-zero, the listing is aborted and continued otherwise. The callback is optional so that this function can be used to count the items in the cache (see below for return value).

Definition at line 329 of file bcache.c.

330 {
331  DIR *d = NULL;
332  struct dirent *de = NULL;
333  int rc = -1;
334 
335  if (!bcache || !(d = opendir(bcache->path)))
336  goto out;
337 
338  rc = 0;
339 
340  mutt_debug(LL_DEBUG3, "bcache: list: dir: '%s'\n", bcache->path);
341 
342  while ((de = readdir(d)))
343  {
344  if (mutt_str_startswith(de->d_name, ".") || mutt_str_startswith(de->d_name, ".."))
345  {
346  continue;
347  }
348 
349  mutt_debug(LL_DEBUG3, "bcache: list: dir: '%s', id :'%s'\n", bcache->path, de->d_name);
350 
351  if (want_id && (want_id(de->d_name, bcache, data) != 0))
352  goto out;
353 
354  rc++;
355  }
356 
357 out:
358  if (d)
359  {
360  if (closedir(d) < 0)
361  rc = -1;
362  }
363  mutt_debug(LL_DEBUG3, "bcache: list: did %d entries\n", rc);
364  return rc;
365 }
char * path
Definition: bcache.c:52
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:160
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 3.
Definition: logging.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_bcache_open()

struct BodyCache* mutt_bcache_open ( struct ConnAccount account,
const char *  mailbox 
)

Open an Email-Body Cache.

Parameters
accountcurrent mailbox' account (required)
mailboxpath to the mailbox of the account (optional)
Return values
NULLFailure

The driver using it is responsible for ensuring that hierarchies are separated by '/' (if it knows of such a concepts like mailboxes or hierarchies)

Definition at line 143 of file bcache.c.

144 {
145  if (!account)
146  return NULL;
147 
148  struct BodyCache *bcache = mutt_mem_calloc(1, sizeof(struct BodyCache));
149  if (bcache_path(account, mailbox, bcache) < 0)
150  {
151  mutt_bcache_close(&bcache);
152  return NULL;
153  }
154 
155  return bcache;
156 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
Local cache of email bodies.
Definition: bcache.c:50
static int bcache_path(struct ConnAccount *account, const char *mailbox, struct BodyCache *bcache)
Create the cache path for a given account/mailbox.
Definition: bcache.c:63
void mutt_bcache_close(struct BodyCache **bcache)
Close an Email-Body Cache.
Definition: bcache.c:164
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_bcache_put()

FILE* mutt_bcache_put ( struct BodyCache bcache,
const char *  id 
)

Create a file in the Body Cache.

Parameters
bcacheBody Cache from mutt_bcache_open()
idPer-mailbox unique identifier for the message
Return values
ptrSuccess
NULLFailure

The returned FILE* is in a temporary location. Use mutt_bcache_commit to put it into place

Definition at line 207 of file bcache.c.

208 {
209  if (!id || (*id == '\0') || !bcache)
210  return NULL;
211 
212  struct Buffer *path = mutt_buffer_pool_get();
213  mutt_buffer_printf(path, "%s%s%s", bcache->path, id, ".tmp");
214 
215  struct stat sb;
216  if (stat(bcache->path, &sb) == 0)
217  {
218  if (!S_ISDIR(sb.st_mode))
219  {
220  mutt_error(_("Message cache isn't a directory: %s"), bcache->path);
221  return NULL;
222  }
223  }
224  else
225  {
226  if (mutt_file_mkdir(bcache->path, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
227  {
228  mutt_error(_("Can't create %s: %s"), bcache->path, strerror(errno));
229  return NULL;
230  }
231  }
232 
233  mutt_debug(LL_DEBUG3, "bcache: put: '%s'\n", path);
234 
235  FILE *fp = mutt_file_fopen(mutt_buffer_string(path), "w+");
237  return fp;
238 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#define mutt_error(...)
Definition: logging.h:88
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
String manipulation buffer.
Definition: buffer.h:33
char * path
Definition: bcache.c:52
#define _(a)
Definition: message.h:28
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:877
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:589
Log at debug level 3.
Definition: logging.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function: