forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcache.c
359 lines (302 loc) · 9.5 KB
/
bcache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* @file
* Body Caching - local copies of email bodies
*
* @authors
* Copyright (C) 2006-2007,2009,2017 Brendan Cully <brendan@kublai.com>
* Copyright (C) 2006,2009 Rocco Rutte <pdmef@gmx.net>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
/**
* @page bcache Body Caching - local copies of email bodies
*
* Body Caching - local copies of email bodies
*/
#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"
/* These Config Variables are only used in bcache.c */
char *C_MessageCachedir; ///< Config: (imap/pop) Directory for the message cache
/**
* struct BodyCache - Local cache of email bodies
*/
struct BodyCache
{
char path[PATH_MAX];
size_t pathlen;
};
/**
* bcache_path - Create the cache path for a given account/mailbox
* @param account Account info
* @param mailbox Mailbox name
* @param dst Buffer for the name
* @param dstlen Length of the buffer
* @retval 0 Success
* @retval -1 Failure
*/
static int bcache_path(struct ConnAccount *account, const char *mailbox, char *dst, size_t dstlen)
{
char host[256];
struct Url url = { U_UNKNOWN };
int len;
if (!account || !C_MessageCachedir || !*C_MessageCachedir || !dst || (dstlen == 0))
return -1;
/* make up a Url we can turn into a string */
mutt_account_tourl(account, &url);
/* mutt_account_tourl() just sets up some pointers;
* if this ever changes, we have a memleak here */
url.path = NULL;
if (url_tostring(&url, host, sizeof(host), U_PATH) < 0)
{
mutt_debug(LL_DEBUG1, "URL to string failed\n");
return -1;
}
size_t mailboxlen = mutt_str_strlen(mailbox);
len = snprintf(dst, dstlen, "%s/%s%s%s", C_MessageCachedir, host, NONULL(mailbox),
((mailboxlen != 0) && (mailbox[mailboxlen - 1] == '/')) ? "" : "/");
mutt_encode_path(dst, dstlen, dst);
mutt_debug(LL_DEBUG3, "rc: %d, path: '%s'\n", len, dst);
if ((len < 0) || ((size_t) len >= dstlen - 1))
return -1;
mutt_debug(LL_DEBUG3, "directory: '%s'\n", dst);
return 0;
}
/**
* mutt_bcache_move - Change the id of a message in the cache
* @param bcache Body cache
* @param id Per-mailbox unique identifier for the message
* @param newid New id for the message
*/
static int mutt_bcache_move(struct BodyCache *bcache, const char *id, const char *newid)
{
char path[PATH_MAX];
char newpath[PATH_MAX];
if (!bcache || !id || !*id || !newid || !*newid)
return -1;
snprintf(path, sizeof(path), "%s%s", bcache->path, id);
snprintf(newpath, sizeof(newpath), "%s%s", bcache->path, newid);
mutt_debug(LL_DEBUG3, "bcache: mv: '%s' '%s'\n", path, newpath);
return rename(path, newpath);
}
/**
* mutt_bcache_open - Open an Email-Body Cache
* @param account current mailbox' account (required)
* @param mailbox path to the mailbox of the account (optional)
* @retval NULL Failure
*
* The driver using it is responsible for ensuring that hierarchies are
* separated by '/' (if it knows of such a concepts like mailboxes or
* hierarchies)
*/
struct BodyCache *mutt_bcache_open(struct ConnAccount *account, const char *mailbox)
{
if (!account)
return NULL;
struct BodyCache *bcache = mutt_mem_calloc(1, sizeof(struct BodyCache));
if (bcache_path(account, mailbox, bcache->path, sizeof(bcache->path)) < 0)
{
FREE(&bcache);
return NULL;
}
bcache->pathlen = mutt_str_strlen(bcache->path);
return bcache;
}
/**
* mutt_bcache_close - Close an Email-Body Cache
* @param[out] bcache Body cache
*
* Free all memory of bcache and finally FREE() it, too.
*/
void mutt_bcache_close(struct BodyCache **bcache)
{
if (!bcache || !*bcache)
return;
FREE(bcache);
}
/**
* mutt_bcache_get - Open a file in the Body Cache
* @param bcache Body Cache from mutt_bcache_open()
* @param id Per-mailbox unique identifier for the message
* @retval ptr Success
* @retval NULL Failure
*/
FILE *mutt_bcache_get(struct BodyCache *bcache, const char *id)
{
char path[PATH_MAX];
FILE *fp = NULL;
if (!id || !*id || !bcache)
return NULL;
path[0] = '\0';
mutt_str_strncat(path, sizeof(path), bcache->path, bcache->pathlen);
mutt_str_strncat(path, sizeof(path), id, mutt_str_strlen(id));
fp = mutt_file_fopen(path, "r");
mutt_debug(LL_DEBUG3, "bcache: get: '%s': %s\n", path, fp ? "yes" : "no");
return fp;
}
/**
* mutt_bcache_put - Create a file in the Body Cache
* @param bcache Body Cache from mutt_bcache_open()
* @param id Per-mailbox unique identifier for the message
* @retval ptr Success
* @retval NULL Failure
*
* The returned FILE* is in a temporary location.
* Use mutt_bcache_commit to put it into place
*/
FILE *mutt_bcache_put(struct BodyCache *bcache, const char *id)
{
char path[PATH_MAX];
struct stat sb;
if (!id || !*id || !bcache)
return NULL;
if (snprintf(path, sizeof(path), "%s%s%s", bcache->path, id, ".tmp") >= sizeof(path))
{
mutt_error(_("Path too long: %s%s%s"), bcache->path, id, ".tmp");
return NULL;
}
if (stat(bcache->path, &sb) == 0)
{
if (!S_ISDIR(sb.st_mode))
{
mutt_error(_("Message cache isn't a directory: %s"), bcache->path);
return NULL;
}
}
else
{
if (mutt_file_mkdir(bcache->path, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
{
mutt_error(_("Can't create %s %s"), bcache->path, strerror(errno));
return NULL;
}
}
mutt_debug(LL_DEBUG3, "bcache: put: '%s'\n", path);
return mutt_file_fopen(path, "w+");
}
/**
* mutt_bcache_commit - Move a temporary file into the Body Cache
* @param bcache Body Cache from mutt_bcache_open()
* @param id Per-mailbox unique identifier for the message
* @retval 0 Success
* @retval -1 Failure
*/
int mutt_bcache_commit(struct BodyCache *bcache, const char *id)
{
char tmpid[PATH_MAX];
snprintf(tmpid, sizeof(tmpid), "%s.tmp", id);
return mutt_bcache_move(bcache, tmpid, id);
}
/**
* mutt_bcache_del - Delete a file from the Body Cache
* @param bcache Body Cache from mutt_bcache_open()
* @param id Per-mailbox unique identifier for the message
* @retval 0 Success
* @retval -1 Failure
*/
int mutt_bcache_del(struct BodyCache *bcache, const char *id)
{
char path[PATH_MAX];
if (!id || !*id || !bcache)
return -1;
path[0] = '\0';
mutt_str_strncat(path, sizeof(path), bcache->path, bcache->pathlen);
mutt_str_strncat(path, sizeof(path), id, mutt_str_strlen(id));
mutt_debug(LL_DEBUG3, "bcache: del: '%s'\n", path);
return unlink(path);
}
/**
* mutt_bcache_exists - Check if a file exists in the Body Cache
* @param bcache Body Cache from mutt_bcache_open()
* @param id Per-mailbox unique identifier for the message
* @retval 0 Success
* @retval -1 Failure
*/
int mutt_bcache_exists(struct BodyCache *bcache, const char *id)
{
char path[PATH_MAX];
struct stat st;
int rc = 0;
if (!id || !*id || !bcache)
return -1;
path[0] = '\0';
mutt_str_strncat(path, sizeof(path), bcache->path, bcache->pathlen);
mutt_str_strncat(path, sizeof(path), id, mutt_str_strlen(id));
if (stat(path, &st) < 0)
rc = -1;
else
rc = (S_ISREG(st.st_mode) && (st.st_size != 0)) ? 0 : -1;
mutt_debug(LL_DEBUG3, "bcache: exists: '%s': %s\n", path, (rc == 0) ? "yes" : "no");
return rc;
}
/**
* mutt_bcache_list - Find matching entries in the Body Cache
* @param bcache Body Cache from mutt_bcache_open()
* @param want_id Callback function called for each match
* @param data Data to pass to the callback function
* @retval -1 Failure
* @retval >=0 count 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).
*/
int mutt_bcache_list(struct BodyCache *bcache, bcache_list_t *want_id, void *data)
{
DIR *d = NULL;
struct dirent *de = NULL;
int rc = -1;
if (!bcache || !(d = opendir(bcache->path)))
goto out;
rc = 0;
mutt_debug(LL_DEBUG3, "bcache: list: dir: '%s'\n", bcache->path);
while ((de = readdir(d)))
{
if (mutt_str_startswith(de->d_name, ".", CASE_MATCH) ||
mutt_str_startswith(de->d_name, "..", CASE_MATCH))
{
continue;
}
mutt_debug(LL_DEBUG3, "bcache: list: dir: '%s', id :'%s'\n", bcache->path, de->d_name);
if (want_id && (want_id(de->d_name, bcache, data) != 0))
goto out;
rc++;
}
out:
if (d)
{
if (closedir(d) < 0)
rc = -1;
}
mutt_debug(LL_DEBUG3, "bcache: list: did %d entries\n", rc);
return rc;
}