2 * Copyright (C) 2006 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 s = strchr (path + 1, '/');
140 while (!(fp = safe_fopen (path, "w+")) && errno == ENOENT && s)
142 /* create missing path components */
144 if (stat (path, &sb) < 0 && (errno != ENOENT || mkdir (path, 0777) < 0))
147 s = strchr (s + 1, '/');
150 dprint (3, (debugfile, "bcache: put: '%s'\n", path));
155 int mutt_bcache_commit(body_cache_t* bcache, const char* id)
157 char tmpid[_POSIX_PATH_MAX];
159 snprintf (tmpid, sizeof (tmpid), "%s.tmp", id);
161 return mutt_bcache_move (bcache, tmpid, id);
164 int mutt_bcache_move(body_cache_t* bcache, const char* id, const char* newid)
166 char path[_POSIX_PATH_MAX];
167 char newpath[_POSIX_PATH_MAX];
169 if (!bcache || !id || !*id || !newid || !*newid)
172 snprintf (path, sizeof (path), "%s%s", bcache->path, id);
173 snprintf (newpath, sizeof (newpath), "%s%s", bcache->path, newid);
175 dprint (3, (debugfile, "bcache: mv: '%s' '%s'\n", path, newpath));
177 return rename (path, newpath);
180 int mutt_bcache_del(body_cache_t *bcache, const char *id)
182 char path[_POSIX_PATH_MAX];
184 if (!id || !*id || !bcache)
188 safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen);
189 safe_strncat (path, sizeof (path), id, mutt_strlen (id));
191 dprint (3, (debugfile, "bcache: del: '%s'\n", path));
193 return unlink (path);
196 int mutt_bcache_exists(body_cache_t *bcache, const char *id)
198 char path[_POSIX_PATH_MAX];
202 if (!id || !*id || !bcache)
206 safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen);
207 safe_strncat (path, sizeof (path), id, mutt_strlen (id));
209 if (stat (path, &st) < 0)
212 rc = S_ISREG(st.st_mode) && st.st_size != 0 ? 0 : -1;
214 dprint (3, (debugfile, "bcache: exists: '%s': %s\n", path, rc == 0 ? "yes" : "no"));
219 int mutt_bcache_list(body_cache_t *bcache,
220 int (*want_id)(const char *id, body_cache_t *bcache,
221 void *data), void *data)
227 if (!bcache || !(d = opendir (bcache->path)))
232 dprint (3, (debugfile, "bcache: list: dir: '%s'\n", bcache->path));
234 while ((de = readdir (d)))
236 if (mutt_strncmp (de->d_name, ".", 1) == 0 ||
237 mutt_strncmp (de->d_name, "..", 2) == 0)
240 dprint (3, (debugfile, "bcache: list: dir: '%s', id :'%s'\n", bcache->path, de->d_name));
242 if (want_id && want_id (de->d_name, bcache, data) != 0)
251 if (closedir (d) < 0)
254 dprint (3, (debugfile, "bcache: list: did %d entries\n", rc));