forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhcache_gdbm.c
110 lines (85 loc) · 2.42 KB
/
hcache_gdbm.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
/**
* Copyright (C) 2004 Thomas Glanzmann <[email protected]>
* Copyright (C) 2004 Tobias Werth <[email protected]>
* Copyright (C) 2004 Brian Fundakowski Feldman <[email protected]>
* Copyright (C) 2016 Pietro Cerutti <[email protected]>
*
* 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/>.
*/
#include "config.h"
#include <gdbm.h>
#include "mutt.h"
#include "hcache_backend.h"
static void *hcache_gdbm_open(const char *path)
{
int pagesize;
if (mutt_atoi(HeaderCachePageSize, &pagesize) < 0 || pagesize <= 0)
pagesize = 16384;
GDBM_FILE db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
if (db)
return db;
/* if rw failed try ro */
return gdbm_open((char *) path, pagesize, GDBM_READER, 00600, NULL);
}
static void *hcache_gdbm_fetch(void *ctx, const char *key, size_t keylen)
{
datum dkey;
datum data;
if (!ctx)
return NULL;
GDBM_FILE db = ctx;
dkey.dptr = (char *) key;
dkey.dsize = keylen;
data = gdbm_fetch(db, dkey);
return data.dptr;
}
static void hcache_gdbm_free(void *vctx, void **data)
{
FREE(data);
}
static int hcache_gdbm_store(void *ctx, const char *key, size_t keylen, void *data, size_t dlen)
{
datum dkey;
datum databuf;
if (!ctx)
return -1;
GDBM_FILE db = ctx;
dkey.dptr = (char *) key;
dkey.dsize = keylen;
databuf.dsize = dlen;
databuf.dptr = data;
return gdbm_store(db, dkey, databuf, GDBM_REPLACE);
}
static int hcache_gdbm_delete(void *ctx, const char *key, size_t keylen)
{
datum dkey;
if (!ctx)
return -1;
GDBM_FILE db = ctx;
dkey.dptr = (char *) key;
dkey.dsize = keylen;
return gdbm_delete(db, dkey);
}
static void hcache_gdbm_close(void **ctx)
{
if (!ctx)
return;
GDBM_FILE db = *ctx;
gdbm_close(db);
}
static const char *hcache_gdbm_backend(void)
{
return gdbm_version;
}
HCACHE_BACKEND_OPS(gdbm)