NeoMutt  2018-07-16 +952-a2da0a
Teaching an old dog new tricks
DOXYGEN
bcache.c File Reference

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

#include "config.h"
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt/mutt.h"
#include "email/lib.h"
#include "bcache.h"
#include "mutt_account.h"
#include "muttlib.h"
+ Include dependency graph for bcache.c:

Go to the source code of this file.

Data Structures

struct  BodyCache
 Local cache of email bodies. More...
 

Functions

static int bcache_path (struct ConnAccount *account, const char *mailbox, char *dst, size_t dstlen)
 Create the cache path for a given account/mailbox. More...
 
static int mutt_bcache_move (struct BodyCache *bcache, const char *id, const char *newid)
 Change the id of a message in the cache. More...
 
struct BodyCachemutt_bcache_open (struct ConnAccount *account, const char *mailbox)
 Open an Email-Body Cache. More...
 
void mutt_bcache_close (struct BodyCache **bcache)
 Close an Email-Body Cache. More...
 
FILE * mutt_bcache_get (struct BodyCache *bcache, const char *id)
 Open a file in the Body Cache. More...
 
FILE * mutt_bcache_put (struct BodyCache *bcache, const char *id)
 Create a file in the 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...
 
int mutt_bcache_list (struct BodyCache *bcache, bcache_list_t *want_id, void *data)
 Find matching entries in the Body Cache. More...
 

Variables

char * MessageCachedir
 Config: (imap/pop) Directory for the message 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 bcache.c.

Function Documentation

static int bcache_path ( struct ConnAccount account,
const char *  mailbox,
char *  dst,
size_t  dstlen 
)
static

Create the cache path for a given account/mailbox.

Parameters
accountAccount info
mailboxMailbox name
dstBuffer for the name
dstlenLength of the buffer
Return values
0Success
-1Failure

Definition at line 65 of file bcache.c.

66 {
67  char host[STRING];
68  struct Url url = { U_UNKNOWN };
69  int len;
70 
71  if (!account || !MessageCachedir || !*MessageCachedir || !dst || (dstlen == 0))
72  return -1;
73 
74  /* make up a Url we can turn into a string */
75  mutt_account_tourl(account, &url);
76  /* mutt_account_tourl() just sets up some pointers;
77  * if this ever changes, we have a memleak here
78  */
79  url.path = NULL;
80  if (url_tostring(&url, host, sizeof(host), U_PATH) < 0)
81  {
82  mutt_debug(1, "URL to string failed\n");
83  return -1;
84  }
85 
86  size_t mailboxlen = mutt_str_strlen(mailbox);
87  len = snprintf(dst, dstlen, "%s/%s%s%s", MessageCachedir, host, NONULL(mailbox),
88  (mailboxlen != 0 && mailbox[mailboxlen - 1] == '/') ? "" : "/");
89 
90  mutt_encode_path(dst, dstlen, dst);
91 
92  mutt_debug(3, "rc: %d, path: '%s'\n", len, dst);
93 
94  if (len < 0 || (size_t) len >= dstlen - 1)
95  return -1;
96 
97  mutt_debug(3, "directory: '%s'\n", dst);
98 
99  return 0;
100 }
struct Url url
Definition: url.c:45
#define NONULL(x)
Definition: string2.h:39
A parsed URL proto://user:password@host:port/path?a=1&b=2
Definition: url.h:67
Url wasn&#39;t recognised.
Definition: url.h:34
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:663
char * MessageCachedir
Config: (imap/pop) Directory for the message cache.
Definition: bcache.c:45
void mutt_encode_path(char *buf, size_t buflen, const char *src)
Convert a path into the user&#39;s preferred character set.
Definition: muttlib.c:1507
int url_tostring(struct Url *u, char *buf, size_t buflen, int flags)
Output the URL string for a given Url object.
Definition: url.c:351
char * path
Definition: url.h:74
#define STRING
Definition: string2.h:35
void mutt_account_tourl(struct ConnAccount *account, struct Url *url)
Fill URL with info from account.
Definition: mutt_account.c:144
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
#define U_PATH
Definition: url.h:48

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int mutt_bcache_move ( struct BodyCache bcache,
const char *  id,
const char *  newid 
)
static

Change the id of a message in the cache.

Parameters
bcacheBody cache
idPer-mailbox unique identifier for the message
newidNew id for the message

Definition at line 108 of file bcache.c.

109 {
110  char path[PATH_MAX];
111  char newpath[PATH_MAX];
112 
113  if (!bcache || !id || !*id || !newid || !*newid)
114  return -1;
115 
116  snprintf(path, sizeof(path), "%s%s", bcache->path, id);
117  snprintf(newpath, sizeof(newpath), "%s%s", bcache->path, newid);
118 
119  mutt_debug(3, "bcache: mv: '%s' '%s'\n", path, newpath);
120 
121  return rename(path, newpath);
122 }
char path[PATH_MAX]
Definition: bcache.c:52
#define PATH_MAX
Definition: mutt.h:46
char * path
Definition: url.h:74
#define mutt_debug(LEVEL,...)
Definition: logging.h:85

+ Here is the caller graph for this function:

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 134 of file bcache.c.

135 {
136  struct BodyCache *bcache = NULL;
137 
138  if (!account)
139  goto bail;
140 
141  bcache = mutt_mem_calloc(1, sizeof(struct BodyCache));
142  if (bcache_path(account, mailbox, bcache->path, sizeof(bcache->path)) < 0)
143  goto bail;
144  bcache->pathlen = mutt_str_strlen(bcache->path);
145 
146  return bcache;
147 
148 bail:
149  if (bcache)
150  FREE(&bcache);
151  return NULL;
152 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
static int bcache_path(struct ConnAccount *account, const char *mailbox, char *dst, size_t dstlen)
Create the cache path for a given account/mailbox.
Definition: bcache.c:65
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:663
char path[PATH_MAX]
Definition: bcache.c:52
Local cache of email bodies.
Definition: bcache.c:50
size_t pathlen
Definition: bcache.c:53
#define FREE(x)
Definition: memory.h:46

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void mutt_bcache_close ( struct BodyCache **  bcache)

Close an Email-Body Cache.

Parameters
bcacheBody cache

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

Definition at line 160 of file bcache.c.

161 {
162  if (!bcache || !*bcache)
163  return;
164  FREE(bcache);
165 }
#define FREE(x)
Definition: memory.h:46

+ Here is the caller graph for this function:

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 174 of file bcache.c.

175 {
176  char path[PATH_MAX];
177  FILE *fp = NULL;
178 
179  if (!id || !*id || !bcache)
180  return NULL;
181 
182  path[0] = '\0';
183  mutt_str_strncat(path, sizeof(path), bcache->path, bcache->pathlen);
184  mutt_str_strncat(path, sizeof(path), id, mutt_str_strlen(id));
185 
186  fp = mutt_file_fopen(path, "r");
187 
188  mutt_debug(3, "bcache: get: '%s': %s\n", path, fp ? "yes" : "no");
189 
190  return fp;
191 }
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:663
char * mutt_str_strncat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:431
char path[PATH_MAX]
Definition: bcache.c:52
#define PATH_MAX
Definition: mutt.h:46
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:548
size_t pathlen
Definition: bcache.c:53
#define mutt_debug(LEVEL,...)
Definition: logging.h:85

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 203 of file bcache.c.

204 {
205  char path[PATH_MAX];
206  struct stat sb;
207 
208  if (!id || !*id || !bcache)
209  return NULL;
210 
211  if (snprintf(path, sizeof(path), "%s%s%s", bcache->path, id, ".tmp") >= sizeof(path))
212  {
213  mutt_error(_("Path too long: %s%s%s"), bcache->path, id, ".tmp");
214  return NULL;
215  }
216 
217  if (stat(bcache->path, &sb) == 0)
218  {
219  if (!S_ISDIR(sb.st_mode))
220  {
221  mutt_error(_("Message cache isn't a directory: %s"), bcache->path);
222  return NULL;
223  }
224  }
225  else
226  {
227  if (mutt_file_mkdir(bcache->path, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
228  {
229  mutt_error(_("Can't create %s %s"), bcache->path, strerror(errno));
230  return NULL;
231  }
232  }
233 
234  mutt_debug(3, "bcache: put: '%s'\n", path);
235 
236  return mutt_file_fopen(path, "w+");
237 }
#define _(a)
Definition: message.h:28
char path[PATH_MAX]
Definition: bcache.c:52
#define PATH_MAX
Definition: mutt.h:46
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:796
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:548
#define mutt_error(...)
Definition: logging.h:88
#define mutt_debug(LEVEL,...)
Definition: logging.h:85

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 246 of file bcache.c.

247 {
248  char tmpid[PATH_MAX];
249 
250  snprintf(tmpid, sizeof(tmpid), "%s.tmp", id);
251 
252  return mutt_bcache_move(bcache, tmpid, id);
253 }
#define PATH_MAX
Definition: mutt.h:46
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:108

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 262 of file bcache.c.

263 {
264  char path[PATH_MAX];
265 
266  if (!id || !*id || !bcache)
267  return -1;
268 
269  path[0] = '\0';
270  mutt_str_strncat(path, sizeof(path), bcache->path, bcache->pathlen);
271  mutt_str_strncat(path, sizeof(path), id, mutt_str_strlen(id));
272 
273  mutt_debug(3, "bcache: del: '%s'\n", path);
274 
275  return unlink(path);
276 }
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:663
char * mutt_str_strncat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:431
char path[PATH_MAX]
Definition: bcache.c:52
#define PATH_MAX
Definition: mutt.h:46
size_t pathlen
Definition: bcache.c:53
#define mutt_debug(LEVEL,...)
Definition: logging.h:85

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 285 of file bcache.c.

286 {
287  char path[PATH_MAX];
288  struct stat st;
289  int rc = 0;
290 
291  if (!id || !*id || !bcache)
292  return -1;
293 
294  path[0] = '\0';
295  mutt_str_strncat(path, sizeof(path), bcache->path, bcache->pathlen);
296  mutt_str_strncat(path, sizeof(path), id, mutt_str_strlen(id));
297 
298  if (stat(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(3, "bcache: exists: '%s': %s\n", path, rc == 0 ? "yes" : "no");
304 
305  return rc;
306 }
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:663
char * mutt_str_strncat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:431
char path[PATH_MAX]
Definition: bcache.c:52
#define PATH_MAX
Definition: mutt.h:46
size_t pathlen
Definition: bcache.c:53
#define mutt_debug(LEVEL,...)
Definition: logging.h:85

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 327 of file bcache.c.

328 {
329  DIR *d = NULL;
330  struct dirent *de = NULL;
331  int rc = -1;
332 
333  if (!bcache || !(d = opendir(bcache->path)))
334  goto out;
335 
336  rc = 0;
337 
338  mutt_debug(3, "bcache: list: dir: '%s'\n", bcache->path);
339 
340  while ((de = readdir(d)))
341  {
342  if (mutt_str_startswith(de->d_name, ".", CASE_MATCH) ||
343  mutt_str_startswith(de->d_name, "..", CASE_MATCH))
344  {
345  continue;
346  }
347 
348  mutt_debug(3, "bcache: list: dir: '%s', id :'%s'\n", bcache->path, de->d_name);
349 
350  if (want_id && want_id(de->d_name, bcache, data) != 0)
351  goto out;
352 
353  rc++;
354  }
355 
356 out:
357  if (d)
358  {
359  if (closedir(d) < 0)
360  rc = -1;
361  }
362  mutt_debug(3, "bcache: list: did %d entries\n", rc);
363  return rc;
364 }
Match case when comparing strings.
Definition: string2.h:69
char path[PATH_MAX]
Definition: bcache.c:52
size_t mutt_str_startswith(const char *str, const char *prefix, enum CaseSensitivity cs)
Check whether a string starts with a prefix.
Definition: string.c:166
#define mutt_debug(LEVEL,...)
Definition: logging.h:85

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

char* MessageCachedir

Config: (imap/pop) Directory for the message cache.

Definition at line 45 of file bcache.c.