2 * Copyright (C) 2006-7 Brendan Cully <brendan@kublai.com>
3 * Copyright (C) 2006 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)
48 if (!account || !MessageCachedir || !*MessageCachedir || !dst || !dstlen)
51 /* make up a ciss_url_t we can turn into a string */
52 memset (&url, 0, sizeof (ciss_url_t));
53 mutt_account_tourl (account, &url);
55 * mutt_account_tourl() just sets up some pointers;
56 * if this ever changes, we have a memleak here
59 if (url_ciss_tostring (&url, host, sizeof (host), U_PATH) < 0)
61 dprint (1, (debugfile, "bcache_path: URL to string failed\n"));
65 dprint (3, (debugfile, "bcache_path: URL: '%s'\n", host));
67 len = snprintf (dst, dstlen-1, "%s/%s%s%s", MessageCachedir,
68 host, NONULL(mailbox),
69 (mailbox && *mailbox &&
70 mailbox[mutt_strlen(mailbox) - 1] == '/') ? "" : "/");
71 if (len < 0 || len >= dstlen-1)
74 dprint (3, (debugfile, "bcache_path: directory: '%s'\n", dst));
79 body_cache_t *mutt_bcache_open (ACCOUNT *account, const char *mailbox)
81 struct body_cache *bcache = NULL;
86 bcache = safe_calloc (1, sizeof (struct body_cache));
87 if (bcache_path (account, mailbox, bcache->path,
88 sizeof (bcache->path)) < 0)
90 bcache->pathlen = mutt_strlen (bcache->path);
100 void mutt_bcache_close (body_cache_t **bcache)
102 if (!bcache || !*bcache)
104 FREE(bcache); /* __FREE_CHECKED__ */
107 FILE* mutt_bcache_get(body_cache_t *bcache, const char *id)
109 char path[_POSIX_PATH_MAX];
112 if (!id || !*id || !bcache)
116 safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen);
117 safe_strncat (path, sizeof (path), id, mutt_strlen (id));
119 fp = safe_fopen (path, "r");
121 dprint (3, (debugfile, "bcache: get: '%s': %s\n", path, fp == NULL ? "no" : "yes"));
126 FILE* mutt_bcache_put(body_cache_t *bcache, const char *id, int tmp)
128 char path[_POSIX_PATH_MAX];
133 if (!id || !*id || !bcache)
136 snprintf (path, sizeof (path), "%s%s%s", bcache->path, id,
139 if ((fp = safe_fopen (path, "w+")))
143 /* clean up leftover tmp file */
146 s = strchr (path + 1, '/');
147 while (!(fp = safe_fopen (path, "w+")) && errno == ENOENT && s)
149 /* create missing path components */
151 if (stat (path, &sb) < 0 && (errno != ENOENT || mkdir (path, 0777) < 0))
154 s = strchr (s + 1, '/');
158 dprint (3, (debugfile, "bcache: put: '%s'\n", path));
163 int mutt_bcache_commit(body_cache_t* bcache, const char* id)
165 char tmpid[_POSIX_PATH_MAX];
167 snprintf (tmpid, sizeof (tmpid), "%s.tmp", id);
169 return mutt_bcache_move (bcache, tmpid, id);
172 int mutt_bcache_move(body_cache_t* bcache, const char* id, const char* newid)
174 char path[_POSIX_PATH_MAX];
175 char newpath[_POSIX_PATH_MAX];
177 if (!bcache || !id || !*id || !newid || !*newid)
180 snprintf (path, sizeof (path), "%s%s", bcache->path, id);
181 snprintf (newpath, sizeof (newpath), "%s%s", bcache->path, newid);
183 dprint (3, (debugfile, "bcache: mv: '%s' '%s'\n", path, newpath));
185 return rename (path, newpath);
188 int mutt_bcache_del(body_cache_t *bcache, const char *id)
190 char path[_POSIX_PATH_MAX];
192 if (!id || !*id || !bcache)
196 safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen);
197 safe_strncat (path, sizeof (path), id, mutt_strlen (id));
199 dprint (3, (debugfile, "bcache: del: '%s'\n", path));
201 return unlink (path);
204 int mutt_bcache_exists(body_cache_t *bcache, const char *id)
206 char path[_POSIX_PATH_MAX];
210 if (!id || !*id || !bcache)
214 safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen);
215 safe_strncat (path, sizeof (path), id, mutt_strlen (id));
217 if (stat (path, &st) < 0)
220 rc = S_ISREG(st.st_mode) && st.st_size != 0 ? 0 : -1;
222 dprint (3, (debugfile, "bcache: exists: '%s': %s\n", path, rc == 0 ? "yes" : "no"));
227 int mutt_bcache_list(body_cache_t *bcache,
228 int (*want_id)(const char *id, body_cache_t *bcache,
229 void *data), void *data)
235 if (!bcache || !(d = opendir (bcache->path)))
240 dprint (3, (debugfile, "bcache: list: dir: '%s'\n", bcache->path));
242 while ((de = readdir (d)))
244 if (mutt_strncmp (de->d_name, ".", 1) == 0 ||
245 mutt_strncmp (de->d_name, "..", 2) == 0)
248 dprint (3, (debugfile, "bcache: list: dir: '%s', id :'%s'\n", bcache->path, de->d_name));
250 if (want_id && want_id (de->d_name, bcache, data) != 0)
259 if (closedir (d) < 0)
262 dprint (3, (debugfile, "bcache: list: did %d entries\n", rc));