2 * Copyright (C) 2006-7 Brendan Cully <brendan@kublai.com>
3 * Copyright (C) 2006, 2009 Rocco Rutte <pdmef@gmx.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #endif /* HAVE_CONFIG_H */
24 #include <sys/types.h>
37 char path[_POSIX_PATH_MAX];
41 static int bcache_path(ACCOUNT *account, const char *mailbox,
42 char *dst, size_t dstlen)
45 char path[_POSIX_PATH_MAX];
49 if (!account || !MessageCachedir || !*MessageCachedir || !dst || !dstlen)
52 /* make up a ciss_url_t we can turn into a string */
53 memset (&url, 0, sizeof (ciss_url_t));
54 mutt_account_tourl (account, &url);
56 * mutt_account_tourl() just sets up some pointers;
57 * if this ever changes, we have a memleak here
60 if (url_ciss_tostring (&url, host, sizeof (host), U_PATH) < 0)
62 dprint (1, (debugfile, "bcache_path: URL to string failed\n"));
66 mutt_encode_path (path, sizeof (path), NONULL (mailbox));
68 len = snprintf (dst, dstlen-1, "%s/%s%s%s", MessageCachedir,
70 (*path && path[mutt_strlen (path) - 1] == '/') ? "" : "/");
72 dprint (3, (debugfile, "bcache_path: rc: %d, path: '%s'\n", len, dst));
74 if (len < 0 || len >= dstlen-1)
77 dprint (3, (debugfile, "bcache_path: directory: '%s'\n", dst));
82 body_cache_t *mutt_bcache_open (ACCOUNT *account, const char *mailbox)
84 struct body_cache *bcache = NULL;
89 bcache = safe_calloc (1, sizeof (struct body_cache));
90 if (bcache_path (account, mailbox, bcache->path,
91 sizeof (bcache->path)) < 0)
93 bcache->pathlen = mutt_strlen (bcache->path);
103 void mutt_bcache_close (body_cache_t **bcache)
105 if (!bcache || !*bcache)
107 FREE(bcache); /* __FREE_CHECKED__ */
110 FILE* mutt_bcache_get(body_cache_t *bcache, const char *id)
112 char path[_POSIX_PATH_MAX];
115 if (!id || !*id || !bcache)
119 safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen);
120 safe_strncat (path, sizeof (path), id, mutt_strlen (id));
122 fp = safe_fopen (path, "r");
124 dprint (3, (debugfile, "bcache: get: '%s': %s\n", path, fp == NULL ? "no" : "yes"));
129 FILE* mutt_bcache_put(body_cache_t *bcache, const char *id, int tmp)
131 char path[_POSIX_PATH_MAX];
136 if (!id || !*id || !bcache)
139 snprintf (path, sizeof (path), "%s%s%s", bcache->path, id,
142 if ((fp = safe_fopen (path, "w+")))
146 /* clean up leftover tmp file */
149 s = strchr (path + 1, '/');
150 while (!(fp = safe_fopen (path, "w+")) && errno == ENOENT && s)
152 /* create missing path components */
154 if (stat (path, &sb) < 0 && (errno != ENOENT || mkdir (path, 0777) < 0))
157 s = strchr (s + 1, '/');
161 dprint (3, (debugfile, "bcache: put: '%s'\n", path));
166 int mutt_bcache_commit(body_cache_t* bcache, const char* id)
168 char tmpid[_POSIX_PATH_MAX];
170 snprintf (tmpid, sizeof (tmpid), "%s.tmp", id);
172 return mutt_bcache_move (bcache, tmpid, id);
175 int mutt_bcache_move(body_cache_t* bcache, const char* id, const char* newid)
177 char path[_POSIX_PATH_MAX];
178 char newpath[_POSIX_PATH_MAX];
180 if (!bcache || !id || !*id || !newid || !*newid)
183 snprintf (path, sizeof (path), "%s%s", bcache->path, id);
184 snprintf (newpath, sizeof (newpath), "%s%s", bcache->path, newid);
186 dprint (3, (debugfile, "bcache: mv: '%s' '%s'\n", path, newpath));
188 return rename (path, newpath);
191 int mutt_bcache_del(body_cache_t *bcache, const char *id)
193 char path[_POSIX_PATH_MAX];
195 if (!id || !*id || !bcache)
199 safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen);
200 safe_strncat (path, sizeof (path), id, mutt_strlen (id));
202 dprint (3, (debugfile, "bcache: del: '%s'\n", path));
204 return unlink (path);
207 int mutt_bcache_exists(body_cache_t *bcache, const char *id)
209 char path[_POSIX_PATH_MAX];
213 if (!id || !*id || !bcache)
217 safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen);
218 safe_strncat (path, sizeof (path), id, mutt_strlen (id));
220 if (stat (path, &st) < 0)
223 rc = S_ISREG(st.st_mode) && st.st_size != 0 ? 0 : -1;
225 dprint (3, (debugfile, "bcache: exists: '%s': %s\n", path, rc == 0 ? "yes" : "no"));
230 int mutt_bcache_list(body_cache_t *bcache,
231 int (*want_id)(const char *id, body_cache_t *bcache,
232 void *data), void *data)
238 if (!bcache || !(d = opendir (bcache->path)))
243 dprint (3, (debugfile, "bcache: list: dir: '%s'\n", bcache->path));
245 while ((de = readdir (d)))
247 if (mutt_strncmp (de->d_name, ".", 1) == 0 ||
248 mutt_strncmp (de->d_name, "..", 2) == 0)
251 dprint (3, (debugfile, "bcache: list: dir: '%s', id :'%s'\n", bcache->path, de->d_name));
253 if (want_id && want_id (de->d_name, bcache, data) != 0)
262 if (closedir (d) < 0)
265 dprint (3, (debugfile, "bcache: list: did %d entries\n", rc));