-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbinary_storage.c
485 lines (412 loc) · 13.3 KB
/
binary_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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
fuse_xattrs - Add xattrs support using sidecar files
Copyright (C) 2016 Felipe Barriga Richards <felipe {at} felipebarriga.cl>
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "binary_storage.h"
#include "utils.h"
#include "fuse_xattrs_config.h"
#if __linux__
#include <sys/xattr.h>
#define ERR_NO_ATTR ENODATA
#elif __APPLE__
#else
#include <attr/xattr.h>
#define ERR_NO_ATTR ENOATTR
#endif
struct on_memory_attr {
u_int16_t name_size;
size_t value_size;
char *name;
char *value;
};
void __print_on_memory_attr(struct on_memory_attr *attr)
{
#ifdef DEBUG
char *sanitized_value = sanitize_value(attr->value, attr->value_size);
debug_print("--------------\n");
debug_print("name size: %hu\n", attr->name_size);
debug_print("name: '%s'\n", attr->name);
debug_print("value size: %zu\n", attr->value_size);
debug_print("sanitized_value: '%s'\n", sanitized_value);
debug_print("--------------\n");
free(sanitized_value);
#endif
}
void __free_on_memory_attr(struct on_memory_attr *attr)
{
if(attr->name != NULL)
free(attr->name);
if(attr->value != NULL)
free(attr->value);
free(attr);
}
char *__read_file(const char *path, int *buffer_size)
{
FILE *file = fopen(path, "r");
char *buffer = NULL;
if (file == NULL) {
debug_print("file not found: %s\n", path);
*buffer_size = -ENOENT;
return NULL;
}
debug_print("file found, reading it: %s\n", path);
fseek(file, 0, SEEK_END);
*buffer_size = (int)ftell(file);
if (*buffer_size == -1) {
error_print("error: path: %s, size: %d, errno=%d\n", path, *buffer_size, errno);
*buffer_size = errno;
fclose(file);
return NULL;
}
if (*buffer_size > MAX_METADATA_SIZE) {
error_print("metadata file too big. path: %s, size: %d\n", path, *buffer_size);
*buffer_size = -ENOSPC;
fclose(file);
return NULL;
}
if (*buffer_size == 0) {
debug_print("empty file.\n");
*buffer_size = -ENOENT;
fclose(file);
return NULL;
}
assert(*buffer_size > 0);
size_t _buffer_size = (size_t) *buffer_size;
fseek(file, 0, SEEK_SET);
buffer = malloc(_buffer_size);
if (buffer == NULL) {
*buffer_size = -ENOMEM;
error_print("cannot allocate memory.\n");
fclose(file);
return NULL;
}
memset(buffer, '\0', _buffer_size);
fread(buffer, 1, _buffer_size, file);
fclose(file);
return buffer;
}
char *__read_file_sidecar(const char *path, int *buffer_size)
{
char *sidecar_path = get_sidecar_path(path);
debug_print("path=%s sidecar_path=%s\n", path, sidecar_path);
char *buffer = __read_file(sidecar_path, buffer_size);
free (sidecar_path);
return buffer;
}
int __cmp_name(const char *name, size_t name_length, struct on_memory_attr *attr)
{
if (attr->name_size == name_length && memcmp(attr->name, name, name_length) == 0) {
debug_print("match: name=%s, name_length=%zu\n", name, name_length);
__print_on_memory_attr(attr);
return 1;
}
debug_print("doesn't match: name=%s, name_length=%zu\n", name, name_length);
__print_on_memory_attr(attr);
return 0;
}
struct on_memory_attr *__read_on_memory_attr(size_t *offset, char *buffer, size_t buffer_size)
{
debug_print("offset=%zu\n", *offset);
struct on_memory_attr *attr = malloc(sizeof(struct on_memory_attr));
attr->name = NULL;
attr->value = NULL;
////////////////////////////////
// Read name size
size_t data_size = sizeof(u_int16_t);
if (*offset + data_size > buffer_size) {
error_print("Error, sizes doesn't match.\n");
__free_on_memory_attr(attr);
return NULL;
}
memcpy(&attr->name_size, buffer + *offset, data_size);
*offset += data_size;
debug_print("attr->name_size=%hu\n", attr->name_size);
////////////////////////////////
// Read name data
data_size = attr->name_size;
if (*offset + data_size > buffer_size) {
error_print("Error, sizes doesn't match.\n");
__free_on_memory_attr(attr);
return NULL;
}
attr->name = malloc(data_size);
memcpy(attr->name, buffer + *offset, data_size);
*offset += data_size;
////////////////////////////////
// Read value size
data_size = sizeof(size_t);
if (*offset + data_size > buffer_size) {
error_print("Error, sizes doesn't match.\n");
__free_on_memory_attr(attr);
return NULL;
}
memcpy(&attr->value_size, buffer + *offset, data_size);
*offset += data_size;
debug_print("attr->value_size=%zu\n", attr->value_size);
////////////////////////////////
// Read value data
data_size = attr->value_size;
if (*offset + data_size > buffer_size) {
error_print("Error, sizes doesn't match. data_size=%zu buffer_size=%zu\n",
data_size, buffer_size);
__free_on_memory_attr(attr);
return NULL;
}
attr->value = malloc(data_size);
memcpy(attr->value, buffer + *offset, data_size);
*offset += data_size;
return attr;
}
int __write_to_file(FILE *file, const char *name, const char *value, const size_t value_size)
{
const u_int16_t name_size = (int) strlen(name) + 1;
#ifdef DEBUG
char *sanitized_value = sanitize_value(value, value_size);
debug_print("name='%s' name_size=%zu sanitized_value='%s' value_size=%zu\n", name, name_size, sanitized_value, value_size);
free(sanitized_value);
#endif
// write name
if (fwrite(&name_size, sizeof(u_int16_t), 1, file) != 1) {
return -1;
}
if (fwrite(name, name_size, 1, file) != 1) {
return -1;
}
// write value
if (fwrite(&value_size, sizeof(size_t), 1, file) != 1) {
return -1;
}
// write value content only if we have something to write.
if (value_size > 0) {
if (fwrite(value, value_size, 1, file) != 1) {
return -1;
}
}
return 0;
}
/**
*
* @param path - path to file.
* @param name - attribute name. string null terminated. size <= XATTR_NAME_MAX bytes
* @param value - attribute value. size < XATTR_SIZE_MAX
* @param size
* @param flags - XATTR_CREATE and/or XATTR_REPLACE
* @return On success, zero is returned. On failure, -errno is returnted.
*/
int binary_storage_write_key(const char *path, const char *name, const char *value, size_t size, int flags)
{
#ifdef DEBUG
char *sanitized_value = sanitize_value(value, size);
debug_print("path=%s name=%s sanitized_value=%s size=%zu flags=%d\n", path, name, sanitized_value, size, flags);
free(sanitized_value);
#endif
int buffer_size;
char *buffer = __read_file_sidecar(path, &buffer_size);
if (buffer == NULL && buffer_size == -ENOENT && flags & XATTR_REPLACE) {
error_print("No xattr. (flag XATTR_REPLACE)");
return -ENODATA;
}
if (buffer == NULL && buffer_size != -ENOENT) {
return buffer_size;
}
int status;
char *sidecar_path = get_sidecar_path(path);
FILE *file = fopen(sidecar_path, "w");
free(sidecar_path);
if (buffer == NULL) {
debug_print("new file, writing directly...\n");
status = __write_to_file(file, name, value, size);
assert(status == 0);
fclose(file);
free(buffer);
return 0;
}
assert(buffer_size >= 0);
size_t _buffer_size = (size_t)buffer_size;
int res = 0;
size_t offset = 0;
size_t name_len = strlen(name) + 1; // null byte
int replaced = 0;
while(offset < _buffer_size)
{
debug_print("replaced=%d offset=%zu buffer_size=%zu\n", replaced, offset, _buffer_size);
struct on_memory_attr *attr = __read_on_memory_attr(&offset, buffer, _buffer_size);
// FIXME: handle attr == NULL
assert(attr != NULL);
if (memcmp(attr->name, name, name_len) == 0) {
assert(replaced == 0);
if (flags & XATTR_CREATE) {
error_print("Key already exists. (flag XATTR_CREATE)");
status = __write_to_file(file, attr->name, attr->value, attr->value_size);
assert(status == 0);
res = -EEXIST;
} else {
status = __write_to_file(file, name, value, size);
assert(status == 0);
replaced = 1;
}
} else {
status = __write_to_file(file, attr->name, attr->value, attr->value_size);
assert(status == 0);
}
__free_on_memory_attr(attr);
}
if (replaced == 0 && res == 0) {
if (flags & XATTR_REPLACE) {
error_print("Key doesn't exists. (flag XATTR_REPLACE)");
res = -ENODATA;
} else {
status = __write_to_file(file, name, value, size);
assert(status == 0);
}
}
fclose(file);
free(buffer);
return res;
}
int binary_storage_read_key(const char *path, const char *name, char *value, size_t size)
{
int buffer_size;
char *buffer = __read_file_sidecar(path, &buffer_size);
if (buffer == NULL) {
if (buffer_size == -ENOENT) {
return -ERR_NO_ATTR;
}
return buffer_size;
}
assert(buffer_size >= 0);
size_t _buffer_size = (size_t)buffer_size;
if (size > 0) {
memset(value, '\0', size);
}
size_t name_len = strlen(name) + 1; // null byte \0
size_t offset = 0;
while(offset < _buffer_size)
{
struct on_memory_attr *attr = __read_on_memory_attr(&offset, buffer, _buffer_size);
if (attr == NULL) {
free(buffer);
return -EILSEQ;
}
if (__cmp_name(name, name_len, attr) == 1) {
int value_size = (int)attr->value_size;
int res;
if (size == 0) {
res = value_size;
} else if (attr->value_size <= size) {
memcpy(value, attr->value, attr->value_size);
res = value_size;
} else {
error_print("error, attr->value_size=%zu > size=%zu\n", attr->value_size, size);
res = -ERANGE;
}
free(buffer);
__free_on_memory_attr(attr);
return res;
}
__free_on_memory_attr(attr);
}
free(buffer);
return -ERR_NO_ATTR;
}
int binary_storage_list_keys(const char *path, char *list, size_t size)
{
int buffer_size;
char *buffer = __read_file_sidecar(path, &buffer_size);
if (buffer == NULL) {
debug_print("buffer == NULL buffer_size=%d\n", buffer_size);
if (buffer_size == -ENOENT) {
return 0;
}
return buffer_size;
}
assert(buffer_size > 0);
size_t _buffer_size = (size_t)buffer_size;
if (size > 0) {
memset(list, '\0', size);
}
size_t res = 0;
size_t offset = 0;
while(offset < _buffer_size)
{
struct on_memory_attr *attr = __read_on_memory_attr(&offset, buffer, _buffer_size);
if (attr == NULL) {
free(buffer);
return -EILSEQ;
}
if (size > 0) {
if (attr->name_size + res > size) {
error_print("Not enough memory allocated. allocated=%zu required=%ld\n",
size, attr->name_size + res);
__free_on_memory_attr(attr);
free(buffer);
return -ERANGE;
} else {
memcpy(list + res, attr->name, attr->name_size);
res += attr->name_size;
}
} else {
res += attr->name_size;
}
__free_on_memory_attr(attr);
}
free(buffer);
if (size == 0 && res > XATTR_LIST_MAX) {
// FIXME: we should return the size or an error ?
return -E2BIG;
}
return (int)res;
}
int binary_storage_remove_key(const char *path, const char *name)
{
debug_print("path=%s name=%s\n", path, name);
int buffer_size;
char *buffer = __read_file_sidecar(path, &buffer_size);
if (buffer == NULL) {
return buffer_size;
}
assert(buffer_size > 0);
size_t _buffer_size = (size_t) buffer_size;
char *sidecar_path = get_sidecar_path(path);
FILE *file = fopen(sidecar_path, "w");
free(sidecar_path);
size_t offset = 0;
size_t name_len = strlen(name) + 1; // null byte \0
int removed = 0;
while(offset < _buffer_size)
{
debug_print("removed=%d offset=%zu buffer_size=%zu\n", removed, offset, _buffer_size);
struct on_memory_attr *attr = __read_on_memory_attr(&offset, buffer, _buffer_size);
if (attr == NULL) {
error_print("error reading file. corrupted ?");
break;
}
if (memcmp(attr->name, name, name_len) == 0) {
removed++;
} else {
int status = __write_to_file(file, attr->name, attr->value, attr->value_size);
assert(status == 0);
}
__free_on_memory_attr(attr);
}
int res = 0;
if (removed == 1) {
debug_print("key removed successfully.\n");
} else if (removed == 0) {
error_print("key not found.\n");
res = -ERR_NO_ATTR;
} else {
debug_print("removed %d keys (was duplicated)\n", removed);
res = -EILSEQ;
}
fclose(file);
free(buffer);
return res;
}