NeoMutt  2018-07-16 +1360-3df4a2
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 * C_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[256];
68  struct Url url = { U_UNKNOWN };
69  int len;
70 
71  if (!account || !C_MessageCachedir || !*C_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  url.path = NULL;
79  if (url_tostring(&url, host, sizeof(host), U_PATH) < 0)
80  {
81  mutt_debug(LL_DEBUG1, "URL to string failed\n");
82  return -1;
83  }
84 
85  size_t mailboxlen = mutt_str_strlen(mailbox);
86  len = snprintf(dst, dstlen, "%s/%s%s%s", C_MessageCachedir, host, NONULL(mailbox),
87  (mailboxlen != 0 && mailbox[mailboxlen - 1] == '/') ? "" : "/");
88 
89  mutt_encode_path(dst, dstlen, dst);
90 
91  mutt_debug(LL_DEBUG3, "rc: %d, path: '%s'\n", len, dst);
92 
93  if ((len < 0) || ((size_t) len >= dstlen - 1))
94  return -1;
95 
96  mutt_debug(LL_DEBUG3, "directory: '%s'\n", dst);
97 
98  return 0;
99 }
struct Url url
Definition: url.c:45
#define NONULL(x)
Definition: string2.h:36
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:662
char * C_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:1500
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:350
char * path
Definition: url.h:74
Log at debug level 1.
Definition: logging.h:56
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
Log at debug level 3.
Definition: logging.h:58
#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 107 of file bcache.c.

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

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

134 {
135  if (!account)
136  return NULL;
137 
138  struct BodyCache *bcache = mutt_mem_calloc(1, sizeof(struct BodyCache));
139  if (bcache_path(account, mailbox, bcache->path, sizeof(bcache->path)) < 0)
140  {
141  FREE(&bcache);
142  return NULL;
143  }
144 
145  bcache->pathlen = mutt_str_strlen(bcache->path);
146  return bcache;
147 }
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:662
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:40

+ 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
[out]bcacheBody cache

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

Definition at line 155 of file bcache.c.

156 {
157  if (!bcache || !*bcache)
158  return;
159  FREE(bcache);
160 }
#define FREE(x)
Definition: memory.h:40

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

170 {
171  char path[PATH_MAX];
172  FILE *fp = NULL;
173 
174  if (!id || !*id || !bcache)
175  return NULL;
176 
177  path[0] = '\0';
178  mutt_str_strncat(path, sizeof(path), bcache->path, bcache->pathlen);
179  mutt_str_strncat(path, sizeof(path), id, mutt_str_strlen(id));
180 
181  fp = mutt_file_fopen(path, "r");
182 
183  mutt_debug(LL_DEBUG3, "bcache: get: '%s': %s\n", path, fp ? "yes" : "no");
184 
185  return fp;
186 }
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:662
char * mutt_str_strncat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:430
char path[PATH_MAX]
Definition: bcache.c:52
#define PATH_MAX
Definition: mutt.h:48
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:546
size_t pathlen
Definition: bcache.c:53
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 3.
Definition: logging.h:58

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

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

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

242 {
243  char tmpid[PATH_MAX];
244 
245  snprintf(tmpid, sizeof(tmpid), "%s.tmp", id);
246 
247  return mutt_bcache_move(bcache, tmpid, id);
248 }
#define PATH_MAX
Definition: mutt.h:48
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:107

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

258 {
259  char path[PATH_MAX];
260 
261  if (!id || !*id || !bcache)
262  return -1;
263 
264  path[0] = '\0';
265  mutt_str_strncat(path, sizeof(path), bcache->path, bcache->pathlen);
266  mutt_str_strncat(path, sizeof(path), id, mutt_str_strlen(id));
267 
268  mutt_debug(LL_DEBUG3, "bcache: del: '%s'\n", path);
269 
270  return unlink(path);
271 }
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:662
char * mutt_str_strncat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:430
char path[PATH_MAX]
Definition: bcache.c:52
#define PATH_MAX
Definition: mutt.h:48
size_t pathlen
Definition: bcache.c:53
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 3.
Definition: logging.h:58

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

281 {
282  char path[PATH_MAX];
283  struct stat st;
284  int rc = 0;
285 
286  if (!id || !*id || !bcache)
287  return -1;
288 
289  path[0] = '\0';
290  mutt_str_strncat(path, sizeof(path), bcache->path, bcache->pathlen);
291  mutt_str_strncat(path, sizeof(path), id, mutt_str_strlen(id));
292 
293  if (stat(path, &st) < 0)
294  rc = -1;
295  else
296  rc = S_ISREG(st.st_mode) && st.st_size != 0 ? 0 : -1;
297 
298  mutt_debug(LL_DEBUG3, "bcache: exists: '%s': %s\n", path, rc == 0 ? "yes" : "no");
299 
300  return rc;
301 }
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:662
char * mutt_str_strncat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:430
char path[PATH_MAX]
Definition: bcache.c:52
#define PATH_MAX
Definition: mutt.h:48
size_t pathlen
Definition: bcache.c:53
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 3.
Definition: logging.h:58

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

323 {
324  DIR *d = NULL;
325  struct dirent *de = NULL;
326  int rc = -1;
327 
328  if (!bcache || !(d = opendir(bcache->path)))
329  goto out;
330 
331  rc = 0;
332 
333  mutt_debug(LL_DEBUG3, "bcache: list: dir: '%s'\n", bcache->path);
334 
335  while ((de = readdir(d)))
336  {
337  if (mutt_str_startswith(de->d_name, ".", CASE_MATCH) ||
338  mutt_str_startswith(de->d_name, "..", CASE_MATCH))
339  {
340  continue;
341  }
342 
343  mutt_debug(LL_DEBUG3, "bcache: list: dir: '%s', id :'%s'\n", bcache->path, de->d_name);
344 
345  if (want_id && (want_id(de->d_name, bcache, data) != 0))
346  goto out;
347 
348  rc++;
349  }
350 
351 out:
352  if (d)
353  {
354  if (closedir(d) < 0)
355  rc = -1;
356  }
357  mutt_debug(LL_DEBUG3, "bcache: list: did %d entries\n", rc);
358  return rc;
359 }
Match case when comparing strings.
Definition: string2.h:66
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:165
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 3.
Definition: logging.h:58

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

char* C_MessageCachedir

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

Definition at line 45 of file bcache.c.