This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathbackend.c
703 lines (632 loc) · 24.7 KB
/
backend.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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#include "./backend.h"
#include "./backendprivate.h"
#include <kdberrors.h>
#include <kdblogger.h>
#include <kdbprivate.h>
int ELEKTRA_PLUGIN_FUNCTION (open) (Plugin * plugin, Key * errorKey ELEKTRA_UNUSED)
{
BackendHandle * handle = elektraCalloc (sizeof (BackendHandle));
elektraPluginSetData (plugin, handle);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
enum PluginType
{
PLUGIN_TYPE_GET,
PLUGIN_TYPE_SET,
PLUGIN_TYPE_COMMIT,
PLUGIN_TYPE_ERROR,
};
static bool loadPlugin (Plugin ** pluginPtr, Plugin * thisPlugin, Key * pluginRefKey, enum PluginType type, Key * parentKey)
{
if (pluginRefKey == NULL)
{
*pluginPtr = NULL;
return true;
}
const char * pluginRef = keyString (pluginRefKey);
*pluginPtr = elektraPluginFromMountpoint (thisPlugin, pluginRef);
if (*pluginPtr == NULL)
{
ELEKTRA_SET_INSTALLATION_ERRORF (parentKey,
"The plugin referenced in '%s%s' (value: '%s', refering to '%s/plugins/%s') could not be "
"found. (Configuration of mountpoint: '%s')",
keyName (parentKey), keyName (pluginRefKey), pluginRef, keyName (parentKey), pluginRef,
keyBaseName (parentKey));
return false;
}
if (type == PLUGIN_TYPE_GET && (*pluginPtr)->kdbGet == NULL)
{
ELEKTRA_SET_INSTALLATION_ERRORF (parentKey,
"The plugin '%s' was referenced in a kdbGet() position ('%s%s'), but does not implement "
"kdbGet(). (Configuration of mountpoint: '%s')",
(*pluginPtr)->name, keyName (parentKey), keyName (pluginRefKey), keyBaseName (parentKey));
return false;
}
if (type == PLUGIN_TYPE_SET && (*pluginPtr)->kdbSet == NULL)
{
ELEKTRA_SET_INSTALLATION_ERRORF (parentKey,
"The plugin '%s' was referenced in a kdbSet() position ('%s%s'), but does not implement "
"kdbSet(). (Configuration of mountpoint: '%s')",
(*pluginPtr)->name, keyName (parentKey), keyName (pluginRefKey), keyBaseName (parentKey));
return false;
}
if (type == PLUGIN_TYPE_COMMIT && (*pluginPtr)->kdbCommit == NULL)
{
ELEKTRA_SET_INSTALLATION_ERRORF (parentKey,
"The plugin '%s' was referenced in a kdbCommit() position ('%s%s'), but does not "
"implement kdbCommit(). (Configuration of mountpoint: '%s')",
(*pluginPtr)->name, keyName (parentKey), keyName (pluginRefKey), keyBaseName (parentKey));
return false;
}
if (type == PLUGIN_TYPE_ERROR && (*pluginPtr)->kdbError == NULL)
{
ELEKTRA_SET_INSTALLATION_ERRORF (parentKey,
"The plugin '%s' was referenced in a kdbError() position ('%s%s'), but does not implement "
"kdbError(). (Configuration of mountpoint: '%s')",
(*pluginPtr)->name, keyName (parentKey), keyName (pluginRefKey), keyBaseName (parentKey));
return false;
}
return true;
}
static bool loadPluginList (PluginList ** pluginListPtr, Plugin * thisPlugin, KeySet * definition, const char * pluginRefRootName,
enum PluginType type, Key * parentKey)
{
Key * pluginRefRoot = keyNew (pluginRefRootName, KEY_END);
*pluginListPtr = NULL;
PluginList * listEnd = NULL;
for (elektraCursor end, i = ksFindHierarchy (definition, pluginRefRoot, &end); i < end; i++)
{
Key * cur = ksAtCursor (definition, i);
if (keyIsDirectlyBelow (pluginRefRoot, cur) != 1)
{
continue;
}
Plugin * plugin;
if (!loadPlugin (&plugin, thisPlugin, cur, type, parentKey))
{
keyDel (pluginRefRoot);
return false;
}
PluginList * element = elektraMalloc (sizeof (PluginList));
element->plugin = plugin;
element->next = NULL;
if (listEnd == NULL)
{
*pluginListPtr = element;
}
else
{
listEnd->next = element;
}
listEnd = element;
}
keyDel (pluginRefRoot);
return true;
}
int ELEKTRA_PLUGIN_FUNCTION (init) (Plugin * plugin, KeySet * definition, Key * parentKey)
{
if (keyGetNamespace (parentKey) == KEY_NS_PROC)
{
BackendHandle * handle = elektraPluginGetData (plugin);
handle->path = elektraStrDup ("");
if (!loadPluginList (&handle->getPositions.poststorage, plugin, definition, "system:/positions/get/poststorage",
PLUGIN_TYPE_GET, parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
}
Key * pathKey = ksLookupByName (definition, "system:/path", 0);
const char * path = keyString (pathKey);
if (pathKey == NULL || strlen (path) == 0)
{
ELEKTRA_SET_INSTALLATION_ERRORF (
parentKey, "You must set '%s/definition/path' to a non-empty value. (Configuration of mountpoint: %s)",
keyName (parentKey), keyBaseName (parentKey));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
BackendHandle * handle = elektraPluginGetData (plugin);
handle->path = elektraStrDup (path);
// load get plugins
if (!loadPlugin (&handle->getPositions.resolver, plugin, ksLookupByName (definition, "system:/positions/get/resolver", 0),
PLUGIN_TYPE_GET, parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!loadPluginList (&handle->getPositions.prestorage, plugin, definition, "system:/positions/get/prestorage", PLUGIN_TYPE_GET,
parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!loadPlugin (&handle->getPositions.storage, plugin, ksLookupByName (definition, "system:/positions/get/storage", 0),
PLUGIN_TYPE_GET, parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!loadPluginList (&handle->getPositions.poststorage, plugin, definition, "system:/positions/get/poststorage", PLUGIN_TYPE_GET,
parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
// load set plugins
if (!loadPlugin (&handle->setPositions.resolver, plugin, ksLookupByName (definition, "system:/positions/set/resolver", 0),
PLUGIN_TYPE_SET, parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!loadPluginList (&handle->setPositions.prestorage, plugin, definition, "system:/positions/set/prestorage", PLUGIN_TYPE_SET,
parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!loadPlugin (&handle->setPositions.storage, plugin, ksLookupByName (definition, "system:/positions/set/storage", 0),
PLUGIN_TYPE_SET, parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!loadPluginList (&handle->setPositions.poststorage, plugin, definition, "system:/positions/set/poststorage", PLUGIN_TYPE_SET,
parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
// load commit plugins
if (!loadPluginList (&handle->setPositions.precommit, plugin, definition, "system:/positions/set/precommit", PLUGIN_TYPE_COMMIT,
parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!loadPlugin (&handle->setPositions.commit, plugin, ksLookupByName (definition, "system:/positions/set/commit", 0),
PLUGIN_TYPE_COMMIT, parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!loadPluginList (&handle->setPositions.postcommit, plugin, definition, "system:/positions/set/postcommit", PLUGIN_TYPE_COMMIT,
parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
// load error plugins
if (!loadPluginList (&handle->setPositions.prerollback, plugin, definition, "system:/positions/set/prerollback", PLUGIN_TYPE_ERROR,
parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!loadPlugin (&handle->setPositions.rollback, plugin, ksLookupByName (definition, "system:/positions/set/rollback", 0),
PLUGIN_TYPE_ERROR, parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!loadPluginList (&handle->setPositions.postrollback, plugin, definition, "system:/positions/set/postrollback",
PLUGIN_TYPE_ERROR, parentKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (handle->getPositions.resolver != NULL)
{
if (path[0] == '/' && ksLookupByName (definition, "system:/path/absolute", 0) == NULL)
{
ELEKTRA_ADD_INSTALLATION_WARNINGF (
parentKey,
"You configured a resolver. The absolute path in '%s/definition/path' might not be used as-is. If the "
"configuration is intentional, set '%s/definition/path/absolute' to any value to silence this warning. "
"(Configuration of mountpoint: '%s')",
keyName (parentKey), keyName (parentKey), keyBaseName (parentKey));
}
}
else
{
if (handle->setPositions.resolver != NULL)
{
ELEKTRA_SET_INSTALLATION_ERRORF (
parentKey,
"To set '%s/definition/positions/set/resolver', you must also set '%s/definition/positions/get/resolver' "
"to a non-empty value. (Configuration of mountpoint: '%s')",
keyName (parentKey), keyName (parentKey), keyBaseName (parentKey));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (path[0] != '/')
{
ELEKTRA_SET_INSTALLATION_ERRORF (
parentKey,
"If no resolver is configured, you must set '%s/definition/path' to an absolute path. "
"(Configuration of mountpoint: '%s')",
keyName (parentKey), keyBaseName (parentKey));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
bool readOnly = handle->setPositions.resolver == NULL;
if (handle->getPositions.storage == NULL && ksLookupByName (definition, "system:/positions/get/storage/omit", 0) == NULL)
{
ELEKTRA_ADD_INSTALLATION_WARNINGF (
parentKey,
"No storage plugin defined for kdbGet(). You probably forgot to set '%s/definition/positions/get/storage'. If the "
"configuration is intentional, you can silence this warning by setting '%s/definition/positions/get/storage/omit' "
"to any value. (Configuration of mountpoint: '%s')",
keyName (parentKey), keyName (parentKey), keyBaseName (parentKey));
}
if (readOnly)
{
if (handle->setPositions.prestorage != NULL || handle->setPositions.storage != NULL ||
handle->setPositions.poststorage != NULL || handle->setPositions.precommit != NULL ||
handle->setPositions.commit != NULL || handle->setPositions.postcommit != NULL ||
handle->setPositions.prerollback != NULL || handle->setPositions.rollback != NULL ||
handle->setPositions.postrollback != NULL)
{
ELEKTRA_ADD_INSTALLATION_WARNINGF (
parentKey,
"The mountpoint '%s' is configured as read-only (no set-resolver configured), but there are some plugins "
"configured for set-positions below '%s/definition/positions/set'. These plugins will be ignored. Remove "
"them from the configuration to remove this warning. (Configuration of mountpoint: '%s')",
keyBaseName (parentKey), keyName (parentKey), keyBaseName (parentKey));
}
}
else
{
if (handle->setPositions.storage == NULL)
{
ELEKTRA_SET_INSTALLATION_ERRORF (
parentKey,
"You defined a set-resolver plugin, but no storage plugin for kdbSet(). You probably forgot to set "
"'%s/definition/positions/set/storage'. (Configuration of mountpoint: '%s')",
keyName (parentKey), keyBaseName (parentKey));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (handle->setPositions.commit == NULL)
{
ELEKTRA_SET_INSTALLATION_ERRORF (
parentKey,
"You defined a set-resolver plugin, but no commit plugin for kdbSet(). In most cases the same plugin is "
"used as resolver and commit. To enable this configuration, set '%s/definition/positions/set/commit' to "
"'%s', i.e. to the same value as '%s/definition/positions/set/resolver'. (Configuration of mountpoint: "
"'%s')",
keyName (parentKey), handle->setPositions.resolver->name, keyName (parentKey), keyBaseName (parentKey));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (handle->setPositions.rollback == NULL)
{
ELEKTRA_SET_INSTALLATION_ERRORF (
parentKey,
"You defined a set-resolver and commit plugin, but no rollback plugin for kdbSet(). In most cases the same "
"plugin is used as commit and rollback. To enable this configuration, set "
"'%s/definition/positions/set/rollback' to '%s', i.e. to the same value as "
"'%s/definition/positions/set/commit'. (Configuration of mountpoint: '%s')",
keyName (parentKey), handle->setPositions.resolver->name, keyName (parentKey), keyBaseName (parentKey));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (handle->getPositions.resolver != handle->setPositions.resolver &&
ksLookupByName (definition, "system:/positions/set/resolver/differs", 0) == NULL)
{
ELEKTRA_ADD_INSTALLATION_WARNINGF (
parentKey,
"The resolver plugin for kdbSet() ('%s') differs from the resolver plugin for kdbGet() ('%s'). This is a "
"non-standard configuration. Normally, '%s/definition/positions/get/resolver' and "
"'%s/definition/positions/set/resolver' should have the same value. If the configuration is intentional, "
"you can "
"silence this warning by setting '%s/definition/positions/set/resolver/differs' to any value. "
"(Configuration of "
"mountpoint: '%s')",
handle->getPositions.resolver->name, handle->setPositions.resolver->name, keyName (parentKey),
keyName (parentKey), keyName (parentKey), keyBaseName (parentKey));
}
if (handle->setPositions.resolver != handle->setPositions.commit &&
ksLookupByName (definition, "system:/positions/set/commit/differs", 0) == NULL)
{
ELEKTRA_ADD_INSTALLATION_WARNINGF (
parentKey,
"The resolver plugin ('%s') differs from the commit plugin ('%s'). This is a non-standard configuration. "
"Normally, "
"'%s/definition/positions/set/resolver' and '%s/definition/positions/set/commit' should have the same "
"value. If "
"the configuration is intentional, you can silence this warning by setting "
"'%s/definition/positions/set/commit/differs' to any value. (Configuration of mountpoint: '%s')",
handle->setPositions.resolver->name, handle->setPositions.commit->name, keyName (parentKey),
keyName (parentKey), keyName (parentKey), keyBaseName (parentKey));
}
if (handle->setPositions.resolver != handle->setPositions.rollback &&
ksLookupByName (definition, "system:/positions/set/rollback/differs", 0) == NULL)
{
ELEKTRA_ADD_INSTALLATION_WARNINGF (
parentKey,
"The resolver plugin ('%s') differs from the rollback plugin ('%s'). This is a non-standard configuration. "
"Normally, '%s/definition/positions/set/resolver' and '%s/definition/positions/set/rollback' should have "
"the same "
"value. If the configuration is intentional, you can silence this warning by setting "
"'%s/definition/positions/set/rollback/differs' to any value. (Configuration of mountpoint: '%s')",
handle->setPositions.resolver->name, handle->setPositions.rollback->name, keyName (parentKey),
keyName (parentKey), keyName (parentKey), keyBaseName (parentKey));
}
}
return readOnly ? ELEKTRA_PLUGIN_STATUS_NO_UPDATE : ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
static inline void addGenericError (Key * key, const char * function, const char * plugin)
{
ELEKTRA_SET_INTERFACE_ERRORF (key,
"The %s() function of the plugin '%s' returned ELEKTRA_PLUGIN_STATUS_ERROR, but did not actually set "
"an error. If you are the author of this plugin, please add a proper error to the parentKey.",
function, plugin);
}
static int runPluginGet (Plugin * plugin, KeySet * ks, Key * parentKey)
{
// TODO: provide way to access kdbGet and name without kdbprivate.h
ksRewind (ks);
int ret = plugin->kdbGet (plugin, ks, parentKey);
if (ret == ELEKTRA_PLUGIN_STATUS_ERROR)
{
if (keyGetMeta (parentKey, "error") == NULL)
{
addGenericError (parentKey, "kdbGet", plugin->name);
}
}
return ret;
}
static int runPluginListGet (PluginList * plugins, KeySet * ks, Key * parentKey)
{
for (PluginList * cur = plugins; cur != NULL; cur = cur->next)
{
if (runPluginGet (cur->plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR)
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
int ELEKTRA_PLUGIN_FUNCTION (get) (Plugin * plugin, KeySet * ks, Key * parentKey)
{
if (!elektraStrCmp (keyName (parentKey), "system:/elektra/modules/backend"))
{
KeySet * contract = ksNew (
30, keyNew ("system:/elektra/modules/backend", KEY_VALUE, "backend plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/backend/exports", KEY_END),
keyNew ("system:/elektra/modules/backend/exports/open", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (open), KEY_END),
keyNew ("system:/elektra/modules/backend/exports/init", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (init), KEY_END),
keyNew ("system:/elektra/modules/backend/exports/get", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (get), KEY_END),
keyNew ("system:/elektra/modules/backend/exports/set", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (set), KEY_END),
keyNew ("system:/elektra/modules/backend/exports/commit", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (commit), KEY_END),
keyNew ("system:/elektra/modules/backend/exports/error", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (error), KEY_END),
keyNew ("system:/elektra/modules/backend/exports/close", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (close), KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/backend/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (ks, contract);
ksDel (contract);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
BackendHandle * handle = elektraPluginGetData (plugin);
if (handle == NULL)
{
ELEKTRA_SET_INTERNAL_ERROR (parentKey,
"Internal plugin data was NULL. Please report this bug at https://issues.libelektra.org.");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
ElektraKdbPhase phase = elektraPluginGetPhase (plugin);
switch (phase)
{
case ELEKTRA_KDB_GET_PHASE_RESOLVER:
keySetString (parentKey, handle->path);
if (handle->getPositions.resolver == NULL)
{
// no resolver configured -> path is absolute
// TODO [new_backend]: check mtime to determine up date needed?
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
return runPluginGet (handle->getPositions.resolver, ks, parentKey);
case ELEKTRA_KDB_GET_PHASE_CACHECHECK:
// TODO [new_backend]: implement cache
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
case ELEKTRA_KDB_GET_PHASE_PRE_STORAGE:
return runPluginListGet (handle->getPositions.prestorage, ks, parentKey);
case ELEKTRA_KDB_GET_PHASE_STORAGE:
return runPluginGet (handle->getPositions.storage, ks, parentKey);
case ELEKTRA_KDB_GET_PHASE_POST_STORAGE:
return runPluginListGet (handle->getPositions.poststorage, ks, parentKey);
default:
ELEKTRA_SET_INTERNAL_ERRORF (
parentKey, "Unknown phase of kdbGet(): %02x\n Please report this bug at https://issues.libelektra.org.", phase);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
static int runPluginSet (Plugin * plugin, KeySet * ks, Key * parentKey)
{
// TODO: provide way to access kdbSet and name without kdbprivate.h
ksRewind (ks);
int ret = plugin->kdbSet (plugin, ks, parentKey);
if (ret == ELEKTRA_PLUGIN_STATUS_ERROR)
{
if (keyGetMeta (parentKey, "error") == NULL)
{
addGenericError (parentKey, "kdbSet", plugin->name);
}
}
return ret;
}
static int runPluginListSet (PluginList * plugins, KeySet * ks, Key * parentKey)
{
for (PluginList * cur = plugins; cur != NULL; cur = cur->next)
{
if (runPluginSet (cur->plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR)
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
int ELEKTRA_PLUGIN_FUNCTION (set) (Plugin * plugin, KeySet * ks, Key * parentKey)
{
BackendHandle * handle = elektraPluginGetData (plugin);
if (handle == NULL)
{
ELEKTRA_SET_INTERNAL_ERROR (parentKey,
"Internal plugin data was NULL. Please report this bug at https://issues.libelektra.org.");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
ElektraKdbPhase phase = elektraPluginGetPhase (plugin);
switch (phase)
{
case ELEKTRA_KDB_SET_PHASE_RESOLVER:
keySetString (parentKey, handle->path);
if (handle->setPositions.resolver == NULL)
{
// no resolver configured -> path is absolute
ELEKTRA_SET_INTERNAL_ERROR (
parentKey,
"No resolver, but initialized as read-write. Please report this bug at https://issues.libelektra.org.");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
return runPluginSet (handle->setPositions.resolver, ks, parentKey);
case ELEKTRA_KDB_SET_PHASE_PRE_STORAGE:
return runPluginListSet (handle->setPositions.prestorage, ks, parentKey);
case ELEKTRA_KDB_SET_PHASE_STORAGE:
return runPluginSet (handle->setPositions.storage, ks, parentKey);
case ELEKTRA_KDB_SET_PHASE_POST_STORAGE:
return runPluginListSet (handle->setPositions.poststorage, ks, parentKey);
default:
ELEKTRA_SET_INTERNAL_ERRORF (
parentKey, "Unknown phase of kdbSet(): %02x\n Please report this bug at https://issues.libelektra.org.", phase);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
static int runPluginCommit (Plugin * plugin, KeySet * ks, Key * parentKey)
{
// TODO: provide way to access kdbCommit and name without kdbprivate.h
ksRewind (ks);
int ret = plugin->kdbCommit (plugin, ks, parentKey);
if (ret == ELEKTRA_PLUGIN_STATUS_ERROR)
{
if (keyGetMeta (parentKey, "error") == NULL)
{
addGenericError (parentKey, "kdbCommit", plugin->name);
}
}
return ret;
}
static int runPluginListCommit (PluginList * plugins, KeySet * ks, Key * parentKey)
{
for (PluginList * cur = plugins; cur != NULL; cur = cur->next)
{
if (runPluginCommit (cur->plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR)
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
int ELEKTRA_PLUGIN_FUNCTION (commit) (Plugin * plugin, KeySet * ks, Key * parentKey)
{
BackendHandle * handle = elektraPluginGetData (plugin);
if (handle == NULL)
{
ELEKTRA_SET_INTERNAL_ERROR (parentKey,
"Internal plugin data was NULL. Please report this bug at https://issues.libelektra.org.");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
ElektraKdbPhase phase = elektraPluginGetPhase (plugin);
switch (phase)
{
case ELEKTRA_KDB_SET_PHASE_PRE_COMMIT:
return runPluginListCommit (handle->setPositions.precommit, ks, parentKey);
case ELEKTRA_KDB_SET_PHASE_COMMIT:
return runPluginCommit (handle->setPositions.commit, ks, parentKey);
case ELEKTRA_KDB_SET_PHASE_POST_COMMIT:
return runPluginListCommit (handle->setPositions.postcommit, ks, parentKey);
default:
ELEKTRA_SET_INTERNAL_ERRORF (
parentKey, "Unknown phase of kdbSet(): %02x\n Please report this bug at https://issues.libelektra.org.", phase);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
static int runPluginError (Plugin * plugin, KeySet * ks, Key * parentKey)
{
// TODO: provide way to access kdbError and name without kdbprivate.h
ksRewind (ks);
int ret = plugin->kdbError (plugin, ks, parentKey);
if (ret == ELEKTRA_PLUGIN_STATUS_ERROR)
{
if (keyGetMeta (parentKey, "error") == NULL)
{
addGenericError (parentKey, "kdbError", plugin->name);
}
}
return ret;
}
static int runPluginListError (PluginList * plugins, KeySet * ks, Key * parentKey)
{
for (PluginList * cur = plugins; cur != NULL; cur = cur->next)
{
if (runPluginError (cur->plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR)
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
int ELEKTRA_PLUGIN_FUNCTION (error) (Plugin * plugin, KeySet * ks, Key * parentKey)
{
BackendHandle * handle = elektraPluginGetData (plugin);
if (handle == NULL)
{
ELEKTRA_SET_INTERNAL_ERROR (parentKey,
"Internal plugin data was NULL. Please report this bug at https://issues.libelektra.org.");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
ElektraKdbPhase phase = elektraPluginGetPhase (plugin);
switch (phase)
{
case ELEKTRA_KDB_SET_PHASE_PRE_ROLLBACK:
return runPluginListError (handle->setPositions.prerollback, ks, parentKey);
case ELEKTRA_KDB_SET_PHASE_ROLLBACK:
return runPluginError (handle->setPositions.rollback, ks, parentKey);
case ELEKTRA_KDB_SET_PHASE_POST_ROLLBACK:
return runPluginListError (handle->setPositions.postrollback, ks, parentKey);
default:
ELEKTRA_SET_INTERNAL_ERRORF (
parentKey, "Unknown phase of kdbSet(): %02x\n Please report this bug at https://issues.libelektra.org.", phase);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
static void freePluginList (PluginList ** pluginsPtr)
{
PluginList * cur = *pluginsPtr;
while (cur != NULL)
{
PluginList * next = cur->next;
elektraFree (cur);
cur = next;
}
*pluginsPtr = NULL;
}
int ELEKTRA_PLUGIN_FUNCTION (close) (Plugin * plugin, Key * errorKey ELEKTRA_UNUSED)
{
BackendHandle * handle = elektraPluginGetData (plugin);
if (handle == NULL)
{
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
if (handle->path != NULL)
{
elektraFree (handle->path);
}
freePluginList (&handle->getPositions.prestorage);
freePluginList (&handle->getPositions.poststorage);
freePluginList (&handle->setPositions.prestorage);
freePluginList (&handle->setPositions.poststorage);
freePluginList (&handle->setPositions.precommit);
freePluginList (&handle->setPositions.postcommit);
freePluginList (&handle->setPositions.prerollback);
freePluginList (&handle->setPositions.postrollback);
elektraFree (handle);
elektraPluginSetData (plugin, NULL);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("backend",
ELEKTRA_PLUGIN_OPEN, &ELEKTRA_PLUGIN_FUNCTION (open),
ELEKTRA_PLUGIN_INIT, &ELEKTRA_PLUGIN_FUNCTION (init),
ELEKTRA_PLUGIN_GET, &ELEKTRA_PLUGIN_FUNCTION (get),
ELEKTRA_PLUGIN_SET, &ELEKTRA_PLUGIN_FUNCTION (set),
ELEKTRA_PLUGIN_COMMIT, &ELEKTRA_PLUGIN_FUNCTION (commit),
ELEKTRA_PLUGIN_ERROR, &ELEKTRA_PLUGIN_FUNCTION (error),
ELEKTRA_PLUGIN_CLOSE, &ELEKTRA_PLUGIN_FUNCTION (close),
ELEKTRA_PLUGIN_END);
// clang-format on
}