-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathcastore.c
409 lines (316 loc) · 11.2 KB
/
castore.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "castore.h"
#include "def.h"
#include "dirent-util.h"
#include "realloc-buffer.h"
#include "rm-rf.h"
#include "util.h"
/* #undef EINVAL */
/* #define EINVAL __LINE__ */
/* #undef EBADMSG */
/* #define EBADMSG __LINE__ */
struct CaStore {
char *root;
bool is_cache:1;
bool mkdir_done:1;
ReallocBuffer buffer;
CaDigestType digest_type;
ReallocBuffer validate_buffer;
CaDigest *validate_digest;
CaChunkCompression compression;
CaCompressionType compression_type;
uint64_t n_requests;
uint64_t n_request_bytes;
};
struct CaStoreIterator {
CaStore *store;
DIR *rootdir;
struct dirent *subdir_de;
DIR *subdir;
};
CaStore* ca_store_new(void) {
CaStore *store;
store = new0(CaStore, 1);
if (!store)
return NULL;
store->digest_type = _CA_DIGEST_TYPE_INVALID;
store->compression = CA_CHUNK_COMPRESSED;
store->compression_type = CA_COMPRESSION_DEFAULT;
return store;
}
CaStore *ca_store_new_cache(void) {
CaStore *s;
s = new0(CaStore, 1);
if (!s)
return NULL;
s->is_cache = true;
s->compression = CA_CHUNK_AS_IS;
s->compression_type = CA_COMPRESSION_DEFAULT;
return s;
}
CaStore* ca_store_unref(CaStore *store) {
if (!store)
return NULL;
if (store->is_cache && store->root)
(void) rm_rf(store->root, REMOVE_ROOT|REMOVE_PHYSICAL);
free(store->root);
realloc_buffer_free(&store->buffer);
ca_digest_free(store->validate_digest);
realloc_buffer_free(&store->validate_buffer);
return mfree(store);
}
int ca_store_set_path(CaStore *store, const char *path) {
if (!store)
return -EINVAL;
if (!path)
return -EINVAL;
if (store->root)
return -EBUSY;
if (endswith(path, "/"))
store->root = strdup(path);
else
store->root = strjoin(path, "/", NULL);
if (!store->root)
return -ENOMEM;
return 0;
}
int ca_store_set_compression(CaStore *store, CaChunkCompression c) {
if (!store)
return -EINVAL;
if (c < 0)
return -EINVAL;
if (c >= _CA_CHUNK_COMPRESSION_MAX)
return -EINVAL;
store->compression = c;
return 0;
}
int ca_store_set_compression_type(CaStore *store, CaCompressionType compression_type) {
if (!store)
return -EINVAL;
if (compression_type < 0)
return -EINVAL;
if (compression_type >= _CA_COMPRESSION_TYPE_MAX)
return -EOPNOTSUPP;
store->compression_type = compression_type;
return 0;
}
int ca_store_get(
CaStore *store,
const CaChunkID *chunk_id,
CaChunkCompression desired_compression,
const void **ret,
uint64_t *ret_size,
CaChunkCompression *ret_effective_compression) {
CaChunkCompression effective;
ReallocBuffer *v;
CaChunkID actual;
int r;
if (!store)
return -EINVAL;
if (!ret)
return -EINVAL;
if (!ret_size)
return -EINVAL;
if (!store->root)
return store->is_cache ? -ENOENT : -EUNATCH;
realloc_buffer_empty(&store->buffer);
r = ca_chunk_file_load(AT_FDCWD, store->root, chunk_id, desired_compression, store->compression_type, &store->buffer, &effective);
if (r < 0)
return r;
if (effective == CA_CHUNK_COMPRESSED) {
realloc_buffer_empty(&store->validate_buffer);
r = ca_decompress(realloc_buffer_data(&store->buffer),
realloc_buffer_size(&store->buffer),
&store->validate_buffer);
if (r < 0)
return r;
v = &store->validate_buffer;
} else
v = &store->buffer;
if (!store->validate_digest) {
r = ca_digest_new(store->digest_type >= 0 ? store->digest_type : CA_DIGEST_DEFAULT, &store->validate_digest);
if (r < 0)
return r;
}
r = ca_chunk_id_make(store->validate_digest, realloc_buffer_data(v), realloc_buffer_size(v), &actual);
if (r < 0)
return r;
if (!ca_chunk_id_equal(chunk_id, &actual)) {
CaDigestType old_type, i;
bool good = false;
/* If a digest is explicitly configured, only accept this digest */
if (store->digest_type >= 0)
return -EBADMSG;
old_type = ca_digest_get_type(store->validate_digest);
if (old_type < 0)
return -EINVAL;
for (i = 0; i < _CA_DIGEST_TYPE_MAX; i++) {
if (i == old_type)
continue;
r = ca_digest_set_type(store->validate_digest, i);
if (r < 0)
return r;
r = ca_chunk_id_make(store->validate_digest, realloc_buffer_data(v), realloc_buffer_size(v), &actual);
if (r < 0)
return r;
if (ca_chunk_id_equal(chunk_id, &actual)) {
good = true;
break;
}
}
if (!good)
return -EBADMSG;
}
*ret = realloc_buffer_data(&store->buffer);
*ret_size = realloc_buffer_size(&store->buffer);
if (ret_effective_compression)
*ret_effective_compression = effective;
store->n_requests++;
store->n_request_bytes += realloc_buffer_size(&store->buffer);
return r;
}
int ca_store_has(CaStore *store, const CaChunkID *chunk_id) {
if (!store)
return -EINVAL;
if (!store->root)
return store->is_cache ? -ENOENT : -EUNATCH;
return ca_chunk_file_test(AT_FDCWD, store->root, chunk_id);
}
int ca_store_put(
CaStore *store,
const CaChunkID *chunk_id,
CaChunkCompression effective_compression,
const void *data,
uint64_t size) {
int r;
if (!store)
return -EINVAL;
if (!store->root) {
const char *d;
if (!store->is_cache)
return -EUNATCH;
r = var_tmp_dir(&d);
if (r < 0)
return r;
if (asprintf(&store->root, "%s/%" PRIx64 ".castr/", d, random_u64()) < 0)
return -ENOMEM;
}
if (!store->mkdir_done) {
if (mkdir(store->root, 0777) < 0 && errno != EEXIST)
return -errno;
store->mkdir_done = true;
}
return ca_chunk_file_save(
AT_FDCWD, store->root,
chunk_id,
effective_compression, store->compression,
store->compression_type,
data, size);
}
int ca_store_get_requests(CaStore *s, uint64_t *ret) {
if (!s)
return -EINVAL;
if (!ret)
return -EINVAL;
*ret = s->n_requests;
return 0;
}
int ca_store_get_request_bytes(CaStore *s, uint64_t *ret) {
if (!s)
return -EINVAL;
if (!ret)
return -EINVAL;
*ret = s->n_request_bytes;
return 0;
}
int ca_store_set_digest_type(CaStore *s, CaDigestType type) {
int r;
if (!s)
return -EINVAL;
if (type >= _CA_DIGEST_TYPE_MAX)
return -EOPNOTSUPP;
if (type < 0)
s->digest_type = _CA_DIGEST_TYPE_INVALID;
else {
if (s->validate_digest) {
r = ca_digest_set_type(s->validate_digest, type);
if (r < 0)
return r;
}
s->digest_type = type;
}
return 0;
}
CaStoreIterator* ca_store_iterator_new(CaStore *store) {
CaStoreIterator *it;
it = new0(CaStoreIterator, 1);
if (!it)
return NULL;
it->store = store;
return it;
}
CaStoreIterator* ca_store_iterator_unref(CaStoreIterator *iter) {
if (!iter)
return NULL;
if (iter->rootdir)
closedir(iter->rootdir);
if (iter->subdir)
closedir(iter->subdir);
return mfree(iter);
}
int ca_store_iterator_next(
CaStoreIterator *iter,
int *rootdir_fd,
const char **subdir,
int *subdir_fd,
const char **chunk) {
struct dirent *de;
if (!iter->rootdir) {
iter->rootdir = opendir(iter->store->root);
if (!iter->rootdir)
return -errno;
}
for (;;) {
if (!iter->subdir) {
int fd;
errno = 0;
iter->subdir_de = readdir(iter->rootdir);
if (!iter->subdir_de) {
if (errno > 0)
return -errno;
return 0; /* done */
}
fd = openat(dirfd(iter->rootdir), iter->subdir_de->d_name,
O_RDONLY|O_CLOEXEC|O_DIRECTORY);
if (fd < 0) {
if (errno == EISDIR)
continue;
return -errno;
}
iter->subdir = fdopendir(fd);
if (!iter->subdir) {
safe_close(fd);
return -errno;
}
}
FOREACH_DIRENT_ALL(de, iter->subdir, return -errno) {
if (!dirent_is_file_with_suffix(de, ".cacnk"))
continue;
if (rootdir_fd)
*rootdir_fd = dirfd(iter->rootdir);
if (subdir)
*subdir = iter->subdir_de->d_name;
if (subdir_fd)
*subdir_fd = dirfd(iter->subdir);
if (chunk)
*chunk = de->d_name;
return 1; /* success */
}
assert_se(closedir(iter->subdir) == 0);
iter->subdir = NULL;
}
}