| 1 | /* |
|---|
| 2 | * Copyright (C) 2006 Brendan Cully <brendan@kublai.com> |
|---|
| 3 | * Copyright (C) 2006 Rocco Rutte <pdmef@gmx.net> |
|---|
| 4 | * |
|---|
| 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. |
|---|
| 9 | * |
|---|
| 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. |
|---|
| 14 | * |
|---|
| 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. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #if HAVE_CONFIG_H |
|---|
| 21 | #include "config.h" |
|---|
| 22 | #endif /* HAVE_CONFIG_H */ |
|---|
| 23 | |
|---|
| 24 | #include <sys/types.h> |
|---|
| 25 | #include <errno.h> |
|---|
| 26 | #include <dirent.h> |
|---|
| 27 | #include <stdio.h> |
|---|
| 28 | |
|---|
| 29 | #include "mutt.h" |
|---|
| 30 | #include "account.h" |
|---|
| 31 | #include "url.h" |
|---|
| 32 | #include "bcache.h" |
|---|
| 33 | |
|---|
| 34 | #include "lib.h" |
|---|
| 35 | |
|---|
| 36 | struct body_cache { |
|---|
| 37 | char path[_POSIX_PATH_MAX]; |
|---|
| 38 | size_t pathlen; |
|---|
| 39 | }; |
|---|
| 40 | |
|---|
| 41 | static int bcache_path(ACCOUNT *account, const char *mailbox, |
|---|
| 42 | char *dst, size_t dstlen) |
|---|
| 43 | { |
|---|
| 44 | char host[STRING]; |
|---|
| 45 | ciss_url_t url; |
|---|
| 46 | size_t len; |
|---|
| 47 | |
|---|
| 48 | if (!account || !MessageCachedir || !*MessageCachedir || !dst || !dstlen) |
|---|
| 49 | return -1; |
|---|
| 50 | |
|---|
| 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); |
|---|
| 54 | /* |
|---|
| 55 | * mutt_account_tourl() just sets up some pointers; |
|---|
| 56 | * if this ever changes, we have a memleak here |
|---|
| 57 | */ |
|---|
| 58 | url.path = NULL; |
|---|
| 59 | if (url_ciss_tostring (&url, host, sizeof (host), U_PATH) < 0) |
|---|
| 60 | { |
|---|
| 61 | dprint (1, (debugfile, "bcache_path: URL to string failed\n")); |
|---|
| 62 | return -1; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | dprint (3, (debugfile, "bcache_path: URL: '%s'\n", host)); |
|---|
| 66 | |
|---|
| 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) |
|---|
| 72 | return -1; |
|---|
| 73 | |
|---|
| 74 | dprint (3, (debugfile, "bcache_path: directory: '%s'\n", dst)); |
|---|
| 75 | |
|---|
| 76 | return 0; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | body_cache_t *mutt_bcache_open (ACCOUNT *account, const char *mailbox) |
|---|
| 80 | { |
|---|
| 81 | struct body_cache *bcache = NULL; |
|---|
| 82 | |
|---|
| 83 | if (!account) |
|---|
| 84 | goto bail; |
|---|
| 85 | |
|---|
| 86 | bcache = safe_calloc (1, sizeof (struct body_cache)); |
|---|
| 87 | if (bcache_path (account, mailbox, bcache->path, |
|---|
| 88 | sizeof (bcache->path)) < 0) |
|---|
| 89 | goto bail; |
|---|
| 90 | bcache->pathlen = mutt_strlen (bcache->path); |
|---|
| 91 | |
|---|
| 92 | return bcache; |
|---|
| 93 | |
|---|
| 94 | bail: |
|---|
| 95 | if (bcache) |
|---|
| 96 | FREE(&bcache); |
|---|
| 97 | return NULL; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | void mutt_bcache_close (body_cache_t **bcache) |
|---|
| 101 | { |
|---|
| 102 | if (!bcache || !*bcache) |
|---|
| 103 | return; |
|---|
| 104 | FREE(bcache); /* __FREE_CHECKED__ */ |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | FILE* mutt_bcache_get(body_cache_t *bcache, const char *id) |
|---|
| 108 | { |
|---|
| 109 | char path[_POSIX_PATH_MAX]; |
|---|
| 110 | FILE* fp = NULL; |
|---|
| 111 | |
|---|
| 112 | if (!id || !*id || !bcache) |
|---|
| 113 | return NULL; |
|---|
| 114 | |
|---|
| 115 | path[0] = '\0'; |
|---|
| 116 | safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen); |
|---|
| 117 | safe_strncat (path, sizeof (path), id, mutt_strlen (id)); |
|---|
| 118 | |
|---|
| 119 | fp = safe_fopen (path, "r"); |
|---|
| 120 | |
|---|
| 121 | dprint (3, (debugfile, "bcache: get: '%s': %s\n", path, fp == NULL ? "no" : "yes")); |
|---|
| 122 | |
|---|
| 123 | return fp; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | FILE* mutt_bcache_put(body_cache_t *bcache, const char *id, int tmp) |
|---|
| 127 | { |
|---|
| 128 | char path[_POSIX_PATH_MAX]; |
|---|
| 129 | FILE* fp; |
|---|
| 130 | char* s; |
|---|
| 131 | struct stat sb; |
|---|
| 132 | |
|---|
| 133 | if (!id || !*id || !bcache) |
|---|
| 134 | return NULL; |
|---|
| 135 | |
|---|
| 136 | snprintf (path, sizeof (path), "%s%s%s", bcache->path, id, |
|---|
| 137 | tmp ? ".tmp" : ""); |
|---|
| 138 | |
|---|
| 139 | s = strchr (path + 1, '/'); |
|---|
| 140 | while (!(fp = safe_fopen (path, "w+")) && errno == ENOENT && s) |
|---|
| 141 | { |
|---|
| 142 | /* create missing path components */ |
|---|
| 143 | *s = '\0'; |
|---|
| 144 | if (stat (path, &sb) < 0 && (errno != ENOENT || mkdir (path, 0777) < 0)) |
|---|
| 145 | return NULL; |
|---|
| 146 | *s = '/'; |
|---|
| 147 | s = strchr (s + 1, '/'); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | dprint (3, (debugfile, "bcache: put: '%s'\n", path)); |
|---|
| 151 | |
|---|
| 152 | return fp; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | int mutt_bcache_commit(body_cache_t* bcache, const char* id) |
|---|
| 156 | { |
|---|
| 157 | char tmpid[_POSIX_PATH_MAX]; |
|---|
| 158 | |
|---|
| 159 | snprintf (tmpid, sizeof (tmpid), "%s.tmp", id); |
|---|
| 160 | |
|---|
| 161 | return mutt_bcache_move (bcache, tmpid, id); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | int mutt_bcache_move(body_cache_t* bcache, const char* id, const char* newid) |
|---|
| 165 | { |
|---|
| 166 | char path[_POSIX_PATH_MAX]; |
|---|
| 167 | char newpath[_POSIX_PATH_MAX]; |
|---|
| 168 | |
|---|
| 169 | if (!bcache || !id || !*id || !newid || !*newid) |
|---|
| 170 | return -1; |
|---|
| 171 | |
|---|
| 172 | snprintf (path, sizeof (path), "%s%s", bcache->path, id); |
|---|
| 173 | snprintf (newpath, sizeof (newpath), "%s%s", bcache->path, newid); |
|---|
| 174 | |
|---|
| 175 | dprint (3, (debugfile, "bcache: mv: '%s' '%s'\n", path, newpath)); |
|---|
| 176 | |
|---|
| 177 | return rename (path, newpath); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | int mutt_bcache_del(body_cache_t *bcache, const char *id) |
|---|
| 181 | { |
|---|
| 182 | char path[_POSIX_PATH_MAX]; |
|---|
| 183 | |
|---|
| 184 | if (!id || !*id || !bcache) |
|---|
| 185 | return -1; |
|---|
| 186 | |
|---|
| 187 | path[0] = '\0'; |
|---|
| 188 | safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen); |
|---|
| 189 | safe_strncat (path, sizeof (path), id, mutt_strlen (id)); |
|---|
| 190 | |
|---|
| 191 | dprint (3, (debugfile, "bcache: del: '%s'\n", path)); |
|---|
| 192 | |
|---|
| 193 | return unlink (path); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | int mutt_bcache_exists(body_cache_t *bcache, const char *id) |
|---|
| 197 | { |
|---|
| 198 | char path[_POSIX_PATH_MAX]; |
|---|
| 199 | struct stat st; |
|---|
| 200 | int rc = 0; |
|---|
| 201 | |
|---|
| 202 | if (!id || !*id || !bcache) |
|---|
| 203 | return -1; |
|---|
| 204 | |
|---|
| 205 | path[0] = '\0'; |
|---|
| 206 | safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen); |
|---|
| 207 | safe_strncat (path, sizeof (path), id, mutt_strlen (id)); |
|---|
| 208 | |
|---|
| 209 | if (stat (path, &st) < 0) |
|---|
| 210 | rc = -1; |
|---|
| 211 | else |
|---|
| 212 | rc = S_ISREG(st.st_mode) && st.st_size != 0 ? 0 : -1; |
|---|
| 213 | |
|---|
| 214 | dprint (3, (debugfile, "bcache: exists: '%s': %s\n", path, rc == 0 ? "yes" : "no")); |
|---|
| 215 | |
|---|
| 216 | return rc; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 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) |
|---|
| 222 | { |
|---|
| 223 | DIR *d = NULL; |
|---|
| 224 | struct dirent *de; |
|---|
| 225 | int rc = -1; |
|---|
| 226 | |
|---|
| 227 | if (!bcache || !(d = opendir (bcache->path))) |
|---|
| 228 | goto out; |
|---|
| 229 | |
|---|
| 230 | rc = 0; |
|---|
| 231 | |
|---|
| 232 | dprint (3, (debugfile, "bcache: list: dir: '%s'\n", bcache->path)); |
|---|
| 233 | |
|---|
| 234 | while ((de = readdir (d))) |
|---|
| 235 | { |
|---|
| 236 | if (mutt_strncmp (de->d_name, ".", 1) == 0 || |
|---|
| 237 | mutt_strncmp (de->d_name, "..", 2) == 0) |
|---|
| 238 | continue; |
|---|
| 239 | |
|---|
| 240 | dprint (3, (debugfile, "bcache: list: dir: '%s', id :'%s'\n", bcache->path, de->d_name)); |
|---|
| 241 | |
|---|
| 242 | if (want_id && want_id (de->d_name, bcache, data) != 0) |
|---|
| 243 | goto out; |
|---|
| 244 | |
|---|
| 245 | rc++; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | out: |
|---|
| 249 | if (d) |
|---|
| 250 | { |
|---|
| 251 | if (closedir (d) < 0) |
|---|
| 252 | rc = -1; |
|---|
| 253 | } |
|---|
| 254 | dprint (3, (debugfile, "bcache: list: did %d entries\n", rc)); |
|---|
| 255 | return rc; |
|---|
| 256 | } |
|---|