forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickdump.c
653 lines (566 loc) · 16.4 KB
/
quickdump.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/**
* @file
*
* @brief Source for quickdump plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "quickdump.h"
#include <kdbendian.h>
#include <kdbhelper.h>
#include <kdberrors.h>
#include <stdio.h>
#define MAGIC_NUMBER_BASE (0x454b444200000000UL) // EKDB (in ASCII) + Version placeholder
#define MAGIC_NUMBER_V1 ((kdb_unsigned_long_long_t) (MAGIC_NUMBER_BASE + 1))
#define MAGIC_NUMBER_V2 ((kdb_unsigned_long_long_t) (MAGIC_NUMBER_BASE + 2))
#define MAGIC_NUMBER_V3 ((kdb_unsigned_long_long_t) (MAGIC_NUMBER_BASE + 3))
struct metaLink
{
const void * meta;
size_t keyNameSize;
const char * keyName;
};
struct list
{
size_t alloc;
size_t size;
struct metaLink ** array;
};
struct stringbuffer
{
size_t alloc;
size_t offset;
char * string;
};
static ssize_t findMetaLink (struct list * list, const Key * meta);
static void insertMetaLink (struct list * list, size_t index, const Key * meta, Key * key, size_t parentOffset);
static void setupBuffer (struct stringbuffer * buffer, size_t initialAlloc);
static void ensureBufferSize (struct stringbuffer * buffer, size_t minSize);
// keep #ifdef in sync with kdb export
#ifdef _WIN32
#define STDOUT_FILENAME ("CON")
#else
#define STDOUT_FILENAME ("/dev/stdout")
#endif
#include "varint.c"
static inline bool writeData (FILE * file, const char * data, kdb_unsigned_long_long_t size, Key * errorKey)
{
if (!varintWrite (file, size))
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (errorKey, feof (file) ? "Premature end of file" : "Unknown error");
return false;
}
if (size > 0)
{
if (fwrite (data, sizeof (char), size, file) < size)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (errorKey, feof (file) ? "Premature end of file" : "Unknown error");
return false;
}
}
return true;
}
static inline bool readStringIntoBuffer (FILE * file, struct stringbuffer * buffer, Key * errorKey)
{
kdb_unsigned_long_long_t size = 0;
if (!varintRead (file, &size))
{
ELEKTRA_SET_RESOURCE_ERROR (errorKey, feof (file) ? "Premature end of file" : "Unknown error");
return false;
}
size_t newSize = buffer->offset + size + 1;
ensureBufferSize (buffer, newSize);
if (fread (&buffer->string[buffer->offset], sizeof (char), size, file) < size)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (errorKey, feof (file) ? "Premature end of file" : "Unknown error");
return false;
}
buffer->string[newSize - 1] = '\0';
return true;
}
int elektraQuickdumpGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
if (!elektraStrCmp (keyName (parentKey), "system:/elektra/modules/quickdump"))
{
KeySet * contract = ksNew (
30, keyNew ("system:/elektra/modules/quickdump", KEY_VALUE, "quickdump plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/quickdump/exports", KEY_END),
keyNew ("system:/elektra/modules/quickdump/exports/get", KEY_FUNC, elektraQuickdumpGet, KEY_END),
keyNew ("system:/elektra/modules/quickdump/exports/set", KEY_FUNC, elektraQuickdumpSet, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/quickdump/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
// get all keys
FILE * file = fopen (keyString (parentKey), "rb");
if (file == NULL)
{
ELEKTRA_SET_ERROR_GET (parentKey);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
kdb_unsigned_long_long_t magic;
if (fread (&magic, sizeof (kdb_unsigned_long_long_t), 1, file) < 1)
{
if (feof (file) && ftell (file) == 0)
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
else
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
magic = be64toh (magic); // magic number is written big endian so EKDB magic string is readable
switch (magic)
{
case MAGIC_NUMBER_V1:
ELEKTRA_SET_RESOURCE_ERROR (parentKey, "Quickdump v1 no longer supported");
return ELEKTRA_PLUGIN_STATUS_ERROR;
case MAGIC_NUMBER_V2:
ELEKTRA_SET_RESOURCE_ERROR (parentKey, "Quickdump v2 no longer supported");
return ELEKTRA_PLUGIN_STATUS_ERROR;
case MAGIC_NUMBER_V3:
// break, current version implemented below
break;
default:
fclose (file);
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey, "Unknown magic number " ELEKTRA_UNSIGNED_LONG_LONG_F, magic);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
// setup buffers
struct stringbuffer valueBuffer;
setupBuffer (&valueBuffer, 4);
struct stringbuffer metaNameBuffer;
setupBuffer (&metaNameBuffer, 4);
// setup name buffer with parent key
struct stringbuffer nameBuffer;
size_t parentSize = keyGetNameSize (parentKey); // includes null terminator
setupBuffer (&nameBuffer, parentSize + 4);
keyGetName (parentKey, nameBuffer.string, parentSize);
nameBuffer.string[parentSize - 1] = '/'; // replaces null terminator
nameBuffer.string[parentSize] = '\0'; // set new null terminator
nameBuffer.offset = parentSize; // set offset to null terminator
int fc;
while ((fc = fgetc (file)) != EOF)
{
char c = fc;
ungetc (c, file);
if (!readStringIntoBuffer (file, &nameBuffer, parentKey))
{
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
int ftype = fgetc (file);
if (ftype == EOF)
{
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
ELEKTRA_SET_VALIDATION_SEMANTIC_ERROR (parentKey, "Missing key type");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
char type = ftype;
Key * k;
switch (type)
{
case 'b': {
// binary key value
kdb_unsigned_long_long_t valueSize = 0;
if (!varintRead (file, &valueSize))
{
ELEKTRA_SET_RESOURCE_ERROR (parentKey, feof (file) ? "Premature end of file" : "Unknown error");
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (valueSize == 0)
{
k = keyNew (nameBuffer.string, KEY_BINARY, KEY_SIZE, valueSize, KEY_END);
}
else
{
void * value = elektraMalloc (valueSize);
if (fread (value, sizeof (char), valueSize, file) < valueSize)
{
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
fclose (file);
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parentKey, "Error while reading file");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
k = keyNew (nameBuffer.string, KEY_BINARY, KEY_SIZE, (size_t) valueSize, KEY_VALUE, value, KEY_END);
elektraFree (value);
}
break;
}
case 's': {
// string key value
if (!readStringIntoBuffer (file, &valueBuffer, parentKey))
{
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
k = keyNew (nameBuffer.string, KEY_VALUE, valueBuffer.string, KEY_END);
break;
}
default:
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (parentKey, "Unknown key type %c", type);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
while ((fc = fgetc (file)) != 0)
{
if (fc == EOF)
{
keyDel (k);
fclose (file);
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parentKey, "Missing key end");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
c = fc;
switch (c)
{
case 'm': {
// meta key
if (!readStringIntoBuffer (file, &metaNameBuffer, parentKey))
{
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!readStringIntoBuffer (file, &valueBuffer, parentKey))
{
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
const char * metaValue = valueBuffer.string;
keySetMeta (k, metaNameBuffer.string, metaValue);
break;
}
case 'c': {
// copy meta
if (!readStringIntoBuffer (file, &nameBuffer, parentKey))
{
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!readStringIntoBuffer (file, &metaNameBuffer, parentKey))
{
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
const Key * sourceKey = ksLookupByName (returned, nameBuffer.string, 0);
if (sourceKey == NULL)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Could not copy meta data from key '%s': Key not found",
nameBuffer.string);
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (keyCopyMeta (k, sourceKey, metaNameBuffer.string) != 1)
{
ELEKTRA_SET_INTERNAL_ERRORF (parentKey, "Could not copy meta data from key '%s': Error during copy",
&nameBuffer.string[nameBuffer.offset]);
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
break;
}
default:
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey, "Unknown meta type %c", type);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
ksAppendKey (returned, k);
}
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
int elektraQuickdumpSet (Plugin * handle, KeySet * returned, Key * parentKey)
{
elektraCursor cursor = ksGetCursor (returned);
ksRewind (returned);
FILE * file;
// cannot open stdout for writing, because its already open
if (elektraStrCmp (keyString (parentKey), STDOUT_FILENAME) == 0)
{
file = stdout;
}
else
{
file = fopen (keyString (parentKey), "wb");
}
if (file == NULL)
{
ELEKTRA_SET_ERROR_SET (parentKey);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
// magic number is written big endian so EKDB magic string is readable
kdb_unsigned_long_long_t magic = htobe64 (MAGIC_NUMBER_V3);
if (fwrite (&magic, sizeof (kdb_unsigned_long_long_t), 1, file) < 1)
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
struct list metaKeys;
metaKeys.alloc = 16;
metaKeys.size = 0;
metaKeys.array = elektraMalloc (metaKeys.alloc * sizeof (struct metaLink *));
// we assume all keys in returned are below parentKey
size_t parentOffset = keyGetNameSize (parentKey);
// ... unless /noparent is in config, then we just take the full
// (cascading) keynames as relative to the parentKey
KeySet * config = elektraPluginGetConfig (handle);
if (ksLookupByName (config, "/noparent", 0) != NULL)
{
parentOffset = 1;
}
Key * cur;
while ((cur = ksNext (returned)) != NULL)
{
size_t fullNameSize = keyGetNameSize (cur);
if (fullNameSize < parentOffset)
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
kdb_unsigned_long_long_t nameSize = fullNameSize == parentOffset ? 0 : fullNameSize - 1 - parentOffset;
if (!writeData (file, keyName (cur) + parentOffset, nameSize, parentKey))
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (keyIsBinary (cur))
{
if (fputc ('b', file) == EOF)
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
kdb_unsigned_long_long_t valueSize = keyGetValueSize (cur);
char * value = NULL;
if (valueSize > 0)
{
value = elektraMalloc (valueSize);
if (keyGetBinary (cur, value, valueSize) == -1)
{
fclose (file);
elektraFree (value);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
if (!writeData (file, value, valueSize, parentKey))
{
fclose (file);
elektraFree (value);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
elektraFree (value);
}
else
{
if (fputc ('s', file) == EOF)
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
kdb_unsigned_long_long_t valueSize = keyGetValueSize (cur) - 1;
if (!writeData (file, keyString (cur), valueSize, parentKey))
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
keyRewindMeta (cur);
const Key * meta;
while ((meta = keyNextMeta (cur)) != NULL)
{
ssize_t result = findMetaLink (&metaKeys, meta);
if (result < 0)
{
if (fputc ('m', file) == EOF)
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
// ignore meta namespace when writing to file
kdb_unsigned_long_long_t metaNameSize = keyGetNameSize (meta) - 1 - (sizeof ("meta:/") - 1);
if (!writeData (file, keyName (meta) + sizeof ("meta:/") - 1, metaNameSize, parentKey))
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
kdb_unsigned_long_long_t metaValueSize = keyGetValueSize (meta) - 1;
if (!writeData (file, keyString (meta), metaValueSize, parentKey))
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
insertMetaLink (&metaKeys, -result - 1, meta, cur, parentOffset);
}
else
{
if (fputc ('c', file) == EOF)
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
kdb_unsigned_long_long_t keyNameSize = metaKeys.array[result]->keyNameSize;
if (!writeData (file, metaKeys.array[result]->keyName, keyNameSize, parentKey))
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
// ignore meta namespace when writing to file
kdb_unsigned_long_long_t metaNameSize = keyGetNameSize (meta) - 1 - (sizeof ("meta:/") - 1);
if (!writeData (file, keyName (meta) + sizeof ("meta:/") - 1, metaNameSize, parentKey))
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
}
if (fputc (0, file) == EOF)
{
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
for (size_t i = 0; i < metaKeys.size; ++i)
{
elektraFree (metaKeys.array[i]);
}
elektraFree (metaKeys.array);
fclose (file);
ksSetCursor (returned, cursor);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
ssize_t findMetaLink (struct list * list, const Key * meta)
{
const void * search = meta;
if (list->size == 0)
{
return -1;
}
if (search > list->array[list->size - 1]->meta)
{
return -(ssize_t) list->size - 1;
}
ssize_t left = 0;
ssize_t right = list->size;
--right;
ssize_t insertpos = 0;
while (left <= right)
{
size_t middle = left + ((right - left) / 2);
if (list->array[middle]->meta < search)
{
insertpos = left = middle + 1;
}
else if (list->array[middle]->meta == search)
{
return middle;
}
else
{
insertpos = middle;
right = middle - 1;
}
}
return -insertpos - 1;
}
void insertMetaLink (struct list * list, size_t index, const Key * meta, Key * key, size_t parentOffset)
{
if (list->size + 1 >= list->alloc)
{
list->alloc *= 2;
elektraRealloc ((void **) &list->array, sizeof (struct metaLink *) * list->alloc);
}
struct metaLink * link = elektraMalloc (sizeof (struct metaLink));
link->meta = meta;
size_t fullNameSize = keyGetNameSize (key);
link->keyNameSize = fullNameSize <= parentOffset ? 0 : fullNameSize - 1 - parentOffset;
link->keyName = keyName (key) + parentOffset;
if (index < list->size)
{
memmove (&list->array[index + 1], &list->array[index], (list->size - index) * sizeof (struct metaLink *));
}
list->array[index] = link;
++list->size;
}
void setupBuffer (struct stringbuffer * buffer, size_t initialAlloc)
{
buffer->offset = 0;
buffer->alloc = initialAlloc;
buffer->string = elektraMalloc (initialAlloc * sizeof (char));
}
void ensureBufferSize (struct stringbuffer * buffer, size_t minSize)
{
size_t alloc = buffer->alloc;
while (alloc < minSize)
{
alloc *= 2;
}
if (alloc != buffer->alloc)
{
elektraRealloc ((void **) &buffer->string, alloc);
buffer->alloc = alloc;
}
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("quickdump",
ELEKTRA_PLUGIN_GET, &elektraQuickdumpGet,
ELEKTRA_PLUGIN_SET, &elektraQuickdumpSet,
ELEKTRA_PLUGIN_END);
// clang-format on
}