-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.c
323 lines (271 loc) · 7.69 KB
/
storage.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
//
// storage.c
// rwchcd
//
// (C) 2016,2018-2020 Thibaut VARENE
// License: GPLv2 - http://www.gnu.org/licenses/gpl-2.0.html
//
/**
* @file
* Persistent storage implementation.
* Currently a quick hack based on Berkeley DB.
* This implementation is inefficient: among other issues, we keep open()ing/close()ing DB every time.
* @warning no check is performed for @b identifier collisions in any of the output functions.
* @warning in various places where this code is used, no struct marshalling is performed: it is assumed that padding won't change as long as the underlying
* structures are unmodified.
* @note the backend can handle arbitrarily long identifiers and object sizes, within the Berkeley DB limits.
*/
#include <stdlib.h> // free
#include <unistd.h> // chdir
#include <string.h> // memset/strdup
#ifdef HAS_BDB
#include <db.h>
#endif
#include "storage.h"
#include "rwchcd.h"
#ifndef RWCHCD_STORAGE_PATH
#define RWCHCD_STORAGE_PATH "/var/lib/rwchcd/" ///< default filesystem path to permanent storage area. Can be overriden in Makefile or in configuration
#endif
#define STORAGE_VERSION 1UL
#define STORAGE_DB "rwchcd.db" ///< BDB filename
#define STORAGE_DB_VKEY "version" ///< version key
#define STORAGE_DB_OKEY "object" ///< object key
static const storage_version_t Storage_version = STORAGE_VERSION;
bool Storage_configured = false;
const char * Storage_path = NULL;
#ifdef HAS_BDB
static DB_ENV *dbenvp;
#endif
/**
* Generic storage backend write call.
* @param identifier a unique string identifying the object to backup
* @param version a caller-defined version number
* @param object the opaque object to store
* @param size size of the object argument
*/
int storage_dump(const char * restrict const identifier, const storage_version_t * restrict const version, const void * restrict const object, const size_t size)
{
int dbret, ret = -ESTORE;
#ifdef HAS_BDB
DB *dbp;
DBT key, data;
if (!Storage_configured)
return (-ENOTCONFIGURED);
if (!identifier || !version || !object)
return (-EINVALID);
dbret = db_create(&dbp, dbenvp, 0);
if (dbret) {
dbgerr("db_create: %s", db_strerror(dbret));
goto failret;
}
dbret = dbp->open(dbp, NULL, STORAGE_DB, identifier, DB_BTREE, DB_CREATE, 0);
if (dbret) {
dbgerr("db->open \"%s\": %s", identifier, db_strerror(dbret));
goto faildb;
}
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
// store version
key.data = STORAGE_DB_VKEY;
key.size = sizeof(STORAGE_DB_VKEY);
data.data = version;
data.size = sizeof(*version);
dbret = dbp->put(dbp, NULL, &key, &data, 0);
if (dbret) {
dbgerr("db->put version \"%s\": %s", identifier, db_strerror(dbret));
goto faildb;
}
// store object
key.data = STORAGE_DB_OKEY;
key.size = sizeof(STORAGE_DB_OKEY);
data.data = object;
data.size = size;
dbret = dbp->put(dbp, NULL, &key, &data, 0);
if (dbret) {
dbgerr("db->put object \"%s\": %s", identifier, db_strerror(dbret));
goto faildb;
}
dbgmsg(3, 1, "identifier: \"%s\", v: %d, sz: %zu", identifier, *version, size);
ret = ALL_OK;
faildb:
dbp->close(dbp, 0);
failret:
#endif /* HAS_BDB */
return (ret);
}
/**
* Generic storage backend read call.
* @param identifier a unique string identifying the object to recall
* @param version a caller-defined version number
* @param object the opaque object to restore
* @param size size of the object argument
*/
int storage_fetch(const char * restrict const identifier, storage_version_t * restrict const version, void * restrict const object, const size_t size)
{
int dbret, ret = -ESTORE;
#ifdef HAS_BDB
DB *dbp;
DBT key, data;
if (!Storage_configured)
return (-ENOTCONFIGURED);
if (!identifier || !version || !object)
return (-EINVALID);
dbret = db_create(&dbp, dbenvp, 0);
if (dbret) {
dbgerr("db_create: %s", db_strerror(dbret));
goto failret;
}
dbret = dbp->open(dbp, NULL, STORAGE_DB, identifier, DB_BTREE, DB_RDONLY, 0);
if (dbret) {
dbgerr("db->open \"%s\": %s", identifier, db_strerror(dbret));
goto faildb;
}
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
// get version
key.data = STORAGE_DB_VKEY;
key.size = sizeof(STORAGE_DB_VKEY);
data.data = version;
data.ulen = sizeof(*version);
data.flags = DB_DBT_USERMEM;
dbret = dbp->get(dbp, NULL, &key, &data, 0);
if (dbret) {
dbgerr("db->get version \"%s\": %s", identifier, db_strerror(dbret));
goto faildb;
}
// get object
key.data = STORAGE_DB_OKEY;
key.size = sizeof(STORAGE_DB_OKEY);
data.data = object;
data.ulen = size;
data.flags = DB_DBT_USERMEM;
dbret = dbp->get(dbp, NULL, &key, &data, 0);
if (dbret) {
dbgerr("db->get object \"%s\": %s", identifier, db_strerror(dbret));
goto faildb;
}
dbgmsg(3, 1, "identifier: \"%s\", v: %d, sz: %zu", identifier, *version, size);
ret = ALL_OK;
faildb:
dbp->close(dbp, 0);
failret:
#endif /* HAS BDB */
return (ret);
}
/**
* Online storage. Quick hack.
* @warning no other chdir should be performed
* @note if HAS_BDB is undefined, the function will only try to chdir (useful for config dump)
*/
int storage_online(void)
{
#ifdef HAS_BDB
DB *dbp;
DBT key, data;
storage_version_t sv;
int dbret;
#endif
// if we don't have a configured path, fallback to default
if (!Storage_path)
Storage_path = strdup(RWCHCD_STORAGE_PATH); // not the most efficient use of memory
// make sure we're in target wd. XXX This updates wd for all threads
if (chdir(Storage_path)) {
perror(Storage_path);
freeconst(Storage_path);
Storage_path = NULL;
return (-ESTORE);
}
#ifndef HAS_BDB
return (ALL_OK); // we're done
#else
dbret = db_env_create(&dbenvp, 0);
if (dbret) {
dbgerr("db_env_create: %s", db_strerror(dbret));
goto failret;
}
dbret = dbenvp->open(dbenvp, Storage_path, DB_INIT_CDB | DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE | DB_THREAD, 0);
if (dbret) {
dbgerr("dbenvp->open: %s", db_strerror(dbret));
goto failenv;
}
dbret = db_create(&dbp, dbenvp, 0);
if (dbret) {
dbgerr("db_create: %s", db_strerror(dbret));
goto failenv;
}
dbret = dbp->open(dbp, NULL, STORAGE_DB, "storage_version", DB_BTREE, DB_CREATE, 0);
if (dbret) {
/// @todo handle DB_OLD_VERSION -> DB-upgrade()
dbgerr("db->open: %s", db_strerror(dbret));
goto faildb;
}
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
// get version
key.data = STORAGE_DB_VKEY;
key.size = sizeof(STORAGE_DB_VKEY);
data.data = &sv;
data.ulen = sizeof(sv);
data.flags = DB_DBT_USERMEM;
dbret = dbp->get(dbp, NULL, &key, &data, 0);
switch (dbret) {
case 0:
if (Storage_version == sv)
goto end;
// fallthrough
case DB_NOTFOUND:
dbgmsg(1, 1, "will create/truncate");
break; // we'll store the new value and trunc database
default:
dbgerr("db->get: %s", db_strerror(dbret));
goto faildb;
}
// if we reach here, the database is outdated or nonexistent
dbret = dbp->truncate(dbp, NULL, NULL, 0);
if (dbret) {
dbgerr("db->truncate: %s", db_strerror(dbret));
goto faildb;
}
// store version
key.data = STORAGE_DB_VKEY;
key.size = sizeof(STORAGE_DB_VKEY);
data.data = &Storage_version;
data.size = sizeof(Storage_version);
dbret = dbp->put(dbp, NULL, &key, &data, 0);
if (dbret) {
dbgerr("db->put version: %s", db_strerror(dbret));
goto faildb;
}
end:
dbp->close(dbp, 0);
Storage_configured = true;
return (ALL_OK);
faildb:
dbp->close(dbp, 0);
failenv:
dbenvp->close(dbenvp, 0);
failret:
return (-ESTORE);
#endif /* HAS_BDB */
}
bool storage_isconfigured(void)
{
return (Storage_configured);
}
bool storage_haspath(void)
{
return (!!Storage_path);
}
void storage_exit(void)
{
#ifdef HAS_BDB // we have to free Storage_path anyway
if (!Storage_configured)
return;
#endif
Storage_configured = false;
#ifdef HAS_BDB
dbenvp->close(dbenvp, 0);
#endif
freeconst(Storage_path);
Storage_path = NULL;
}