/
kdbprivate.h
569 lines (467 loc) · 16.9 KB
/
kdbprivate.h
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
/**
* @file
*
* @brief Private declarations.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifndef KDBPRIVATE_H
#define KDBPRIVATE_H
#include <kdb.h>
#include <kdbconfig.h>
#include <kdbextension.h>
#include <kdbhelper.h>
#include <kdbio.h>
#include <kdbmacros.h>
#include <kdbnotificationinternal.h>
#include <kdbplugin.h>
#include <kdbproposal.h>
#include <kdbtypes.h>
#ifdef ELEKTRA_ENABLE_OPTIMIZATIONS
#include <kdbopmphm.h>
#endif
#include <kdbglobal.h>
#include <limits.h>
/** The minimal allocation size of a keyset inclusive
NULL byte. ksGetAlloc() will return one less because
it says how much can actually be stored.*/
#define KEYSET_SIZE 16
/** How many plugins can exist in an backend. */
#define NR_OF_PLUGINS 10
/** The index of the commit plugin */
#define COMMIT_PLUGIN 7
/** The index of the storage plugin */
#define STORAGE_PLUGIN 5
/** The index of the resolver plugin */
#define RESOLVER_PLUGIN 0
/** Trie optimization */
#define APPROXIMATE_NR_OF_BACKENDS 16
/**The maximum of how many characters an integer
needs as decimal number.*/
#define MAX_LEN_INT 31
/**Backend mounting information.
*
* This key directory tells you where each backend is mounted
* to which mountpoint. */
#define KDB_SYSTEM_ELEKTRA "system/elektra"
#ifdef __cplusplus
namespace ckdb
{
extern "C" {
#endif
typedef struct _Trie Trie;
typedef struct _Split Split;
typedef struct _Backend Backend;
/* These define the type for pointers to all the kdb functions */
typedef int (*kdbOpenPtr) (Plugin *, Key * errorKey);
typedef int (*kdbClosePtr) (Plugin *, Key * errorKey);
typedef int (*kdbGetPtr) (Plugin * handle, KeySet * returned, Key * parentKey);
typedef int (*kdbSetPtr) (Plugin * handle, KeySet * returned, Key * parentKey);
typedef int (*kdbErrorPtr) (Plugin * handle, KeySet * returned, Key * parentKey);
typedef Backend * (*OpenMapper) (const char *, const char *, KeySet *);
typedef int (*CloseMapper) (Backend *);
/*****************
* Key Flags
*****************/
enum { KEY_EMPTY_NAME = 1 << 22 };
// clang-format off
/**
* Key Flags.
*
* Store a synchronizer state so that the Elektra knows if something
* has changed or not.
*
* @ingroup backend
*/
typedef enum {
KEY_FLAG_SYNC = 1, /*!<
Key need sync.
If name, value or metadata
are changed this flag will be set, so that the backend will sync
the key to database.*/
KEY_FLAG_RO_NAME = 1 << 1, /*!<
Read only flag for name.
Key name is read only and not allowed
to be changed. All attempts to change the name
will lead to an error.
Needed for metakeys and keys that are in a data
structure that depends on name ordering.*/
KEY_FLAG_RO_VALUE = 1 << 2, /*!<
Read only flag for value.
Key value is read only and not allowed
to be changed. All attempts to change the value
will lead to an error.
Needed for metakeys*/
KEY_FLAG_RO_META = 1 << 3 /*!<
Read only flag for meta.
Key meta is read only and not allowed
to be changed. All attempts to change the value
will lead to an error.
Needed for metakeys.*/
} keyflag_t;
/**
* Ks Flags.
*
* Store a synchronizer state so that the Elektra knows if something
* has changed or not.
*
* @ingroup backend
*/
typedef enum {
KS_FLAG_SYNC = 1, /*!<
KeySet need sync.
If keys were popped from the Keyset
this flag will be set, so that the backend will sync
the keys to database.*/
KS_FLAG_REBUILD_OPMPHM = 1 << 1 /*!<
The OPMPHM needs to be rebuild.
Every Key add, Key removal or Key name change operation
sets this flag.*/
} ksflag_t;
/**
* The private Key struct.
*
* Its internal private attributes should not be accessed directly by regular
* programs. Use the @ref key "Key access methods" instead.
* Only a backend writer needs to have access to the private attributes of the
* Key object which is defined as:
* @code
typedef struct _Key Key;
* @endcode
*
* @ingroup backend
*/
struct _Key
{
/**
* The value, which is a NULL terminated string or binary.
* @see keyString(), keyBinary(),
* @see keyGetString(), keyGetBinary(),
* @see keySetString(), keySetBinary()
*/
union {
char * c;
void * v;
} data;
/**
* Size of the value, in bytes, including ending NULL.
* @see keyGetValueSize()
*/
size_t dataSize;
/**
* The name of the key.
* @see keySetName(), keySetName()
*/
char * key;
/**
* Size of the name, in bytes, including ending NULL.
* @see keyGetName(), keyGetNameSize(), keySetName()
*/
size_t keySize;
/**
* Size of the unescaped key name in bytes, including all NULL.
* @see keyBaseName(), keyUnescapedName()
*/
size_t keyUSize;
/**
* Some control and internal flags.
*/
keyflag_t flags;
/**
* In how many keysets the key resists.
* keySetName() is only allowed if ksReference is 0.
* @see ksPop(), ksAppendKey(), ksAppend()
*/
size_t ksReference;
/**
* All the key's meta information.
*/
KeySet * meta;
};
/**
* The private KeySet structure.
*
* Its internal private attributes should not be accessed directly by regular
* programs. Use the @ref keyset "KeySet access methods" instead.
* Only a backend writer needs to have access to the private attributes of the
* KeySet object which is defined as:
* @code
typedef struct _KeySet KeySet;
* @endcode
*
* @ingroup backend
*/
struct _KeySet
{
struct _Key ** array; /**<Array which holds the keys */
size_t size; /**< Number of keys contained in the KeySet */
size_t alloc; /**< Allocated size of array */
struct _Key * cursor; /**< Internal cursor */
size_t current; /**< Current position of cursor */
/**
* Some control and internal flags.
*/
ksflag_t flags;
#ifdef ELEKTRA_ENABLE_OPTIMIZATIONS
/**
* The Order Preserving Minimal Perfect Hash Map.
*/
Opmphm * opmphm;
#endif
};
/**
* The access point to the key database.
*
* The structure which holds all information about loaded backends.
*
* Its internal private attributes should not be accessed directly.
*
* See kdb mount tool to mount new backends.
*
* KDB object is defined as:
* @code
typedef struct _KDB KDB;
* @endcode
*
* @see kdbOpen() and kdbClose() for external use
* @ingroup backend
*/
struct _KDB
{
Trie * trie; /*!< The pointer to the trie holding backends.*/
Split * split; /*!< A list of all mountpoints. It basically has the
same information than in the trie, but it is not trivial
to convert from one to the other.*/
KeySet * modules; /*!< A list of all modules loaded at the moment.*/
Backend * defaultBackend; /*!< The default backend as fallback when nothing else is found.*/
Backend * initBackend; /*!< The init backend for bootstrapping.*/
Plugin * globalPlugins[NR_GLOBAL_POSITIONS][NR_GLOBAL_SUBPOSITIONS];
ElektraIoInterface * ioBinding; /*!< binding for asynchronous I/O operations.*/
Plugin * notificationPlugin; /*!< reference to global plugin for notifications.*/
ElektraNotificationCallbackContext * notificationCallbackContext; /*!< reference to context for notification callbacks.*/
};
/**
* Holds all information related to a backend.
*
* Since Elektra 0.8 a Backend consists of many plugins.
* A backend is responsible for everything related to the process
* of writing out or reading in configuration.
*
* So this holds a list of set and get plugins.
*
* Backends are put together through the configuration
* in system/elektra/mountpoints
*
* See kdb mount tool to mount new backends.
*
* To develop a backend you have first to develop plugins and describe
* through dependencies how they belong together.
*
* @ingroup backend
*/
struct _Backend
{
Key * mountpoint; /*!< The mountpoint where the backend resides.
The keyName() is the point where the backend was mounted.
The keyValue() is the name of the backend without pre/postfix, e.g.
filesys. */
Plugin * setplugins[NR_OF_PLUGINS];
Plugin * getplugins[NR_OF_PLUGINS];
Plugin * errorplugins[NR_OF_PLUGINS];
ssize_t specsize; /*!< The size of the spec key from the previous get.
-1 if still uninitialized.
Needed to know if a key was removed from a keyset. */
ssize_t dirsize; /*!< The size of the dir key from the previous get.
-1 if still uninitialized.
Needed to know if a key was removed from a keyset. */
ssize_t usersize; /*!< The size of the users key from the previous get.
-1 if still uninitialized.
Needed to know if a key was removed from a keyset. */
ssize_t systemsize; /*!< The size of the systems key from the previous get.
-1 if still uninitialized.
Needed to know if a key was removed from a keyset. */
size_t refcounter; /*!< This refcounter shows how often the backend
is used. Not cascading or default backends have 1 in it.
More than three is not possible, because a backend
can be only mounted in dir, system and user each once
OR only in spec.*/
};
/**
* Holds all information related to a plugin.
*
* Since Elektra 0.8 a Backend consists of many plugins.
*
* A plugin should be reusable and only implement a single concern.
* Plugins which are supplied with Elektra are located below src/plugins.
* It is no problem that plugins are developed external too.
*
* @ingroup backend
*/
struct _Plugin
{
KeySet * config; /*!< This keyset contains configuration for the plugin.
Direct below system/ there is the configuration supplied for the backend.
Direct below user/ there is the configuration supplied just for the
plugin, which should be of course preferred to the backend configuration.
The keys inside contain information like /path which path should be used
to write configuration to or /host to which host packets should be send.
@see elektraPluginGetConfig() */
kdbOpenPtr kdbOpen; /*!< The pointer to kdbOpen_template() of the backend. */
kdbClosePtr kdbClose; /*!< The pointer to kdbClose_template() of the backend. */
kdbGetPtr kdbGet; /*!< The pointer to kdbGet_template() of the backend. */
kdbSetPtr kdbSet; /*!< The pointer to kdbSet_template() of the backend. */
kdbErrorPtr kdbError; /*!< The pointer to kdbError_template() of the backend. */
const char * name; /*!< The name of the module responsible for that plugin. */
size_t refcounter; /*!< This refcounter shows how often the plugin
is used. Not shared plugins have 1 in it */
void * data; /*!< This handle can be used for a plugin to store
any data its want to. */
};
/**
*
* The private trie structure.
*
* A trie is a data structure which can handle the longest prefix matching very
* fast. This is exactly what needs to be done when using kdbGet() and kdbSet()
* in a hierarchy where backends are mounted - you need the backend mounted
* closest to the parentKey.
*/
struct _Trie
{
struct _Trie * children[KDB_MAX_UCHAR]; /*!< The children building up the trie recursively */
char * text[KDB_MAX_UCHAR]; /*!< Text identifying this node */
size_t textlen[KDB_MAX_UCHAR]; /*!< Length of the text */
Backend * value[KDB_MAX_UCHAR]; /*!< Pointer to a backend */
Backend * empty_value; /*!< Pointer to a backend for the empty string "" */
};
typedef enum {
SPLIT_FLAG_SYNC = 1, /*!< KeySet in Split need sync.
Is there any key in there which need to be synced?
If keys were popped from the Keyset
this flag will be set, so that the backend will sync
the keys to database.
*/
SPLIT_FLAG_CASCADING = 1 << 1 /*!< Do we need relative checks?
Is this a cascading backend?
*/
} splitflag_t;
/** The private split structure.
*
* kdbGet() and kdbSet() split keysets. This structure contains arrays for
* various information needed to process the keysets afterwards.
*/
struct _Split
{
size_t size; /*!< Number of keysets */
size_t alloc; /*!< How large the arrays are allocated */
KeySet ** keysets; /*!< The keysets */
Backend ** handles; /*!< The KDB for the keyset */
Key ** parents; /*!< The parentkey for the keyset.
Is either the mountpoint of the backend
or "user", "system", "spec" for the split root/cascading backends */
splitflag_t * syncbits; /*!< Bits for various options, see #splitflag_t for documentation */
};
// clang-format on
/***************************************
*
* Not exported functions, for internal use only
*
**************************************/
ssize_t keySetRaw (Key * key, const void * newBinary, size_t dataSize);
/*Methods for split keysets */
Split * splitNew (void);
void splitDel (Split * keysets);
void splitRemove (Split * split, size_t where);
ssize_t splitAppend (Split * split, Backend * backend, Key * parentKey, int syncbits);
int splitBuildup (Split * split, KDB * handle, Key * parentKey);
void splitUpdateFileName (Split * split, KDB * handle, Key * key);
/* for kdbGet() algorithm */
int splitAppoint (Split * split, KDB * handle, KeySet * ks);
int splitGet (Split * split, Key * warningKey, KDB * handle);
int splitMerge (Split * split, KeySet * dest);
/* for kdbSet() algorithm */
int splitDivide (Split * split, KDB * handle, KeySet * ks);
int splitSync (Split * split);
void splitPrepare (Split * split);
int splitUpdateSize (Split * split);
/*Backend handling*/
Backend * backendOpen (KeySet * elektra_config, KeySet * modules, Key * errorKey);
Backend * backendOpenDefault (KeySet * modules, const char * file, Key * errorKey);
Backend * backendOpenModules (KeySet * modules, Key * errorKey);
Backend * backendOpenVersion (Key * errorKey);
int backendClose (Backend * backend, Key * errorKey);
int backendUpdateSize (Backend * backend, Key * parent, int size);
/*Plugin handling*/
Plugin * elektraPluginOpen (const char * backendname, KeySet * modules, KeySet * config, Key * errorKey);
int elektraPluginClose (Plugin * handle, Key * errorKey);
int elektraProcessPlugin (Key * cur, int * pluginNumber, char ** pluginName, char ** referenceName, Key * errorKey);
int elektraProcessPlugins (Plugin ** plugins, KeySet * modules, KeySet * referencePlugins, KeySet * config, KeySet * systemConfig,
Key * errorKey);
size_t elektraPluginGetFunction (Plugin * plugin, const char * name);
Plugin * elektraPluginMissing (void);
Plugin * elektraPluginVersion (void);
/*Trie handling*/
int trieClose (Trie * trie, Key * errorKey);
Backend * trieLookup (Trie * trie, const Key * key);
Trie * trieInsert (Trie * trie, const char * name, Backend * value);
/*Mounting handling */
int mountOpen (KDB * kdb, KeySet * config, KeySet * modules, Key * errorKey);
int mountDefault (KDB * kdb, KeySet * modules, int inFallback, Key * errorKey);
int mountModules (KDB * kdb, KeySet * modules, Key * errorKey);
int mountVersion (KDB * kdb, Key * errorKey);
int mountGlobals (KDB * kdb, KeySet * keys, KeySet * modules, Key * errorKey);
int mountBackend (KDB * kdb, Backend * backend, Key * errorKey);
Key * mountGetMountpoint (KDB * handle, const Key * where);
Backend * mountGetBackend (KDB * handle, const Key * key);
int keyInit (Key * key);
void keyVInit (Key * key, const char * keyname, va_list ap);
int keyClearSync (Key * key);
/*Private helper for keyset*/
int ksInit (KeySet * ks);
int ksClose (KeySet * ks);
int ksResize (KeySet * ks, size_t size);
size_t ksGetAlloc (const KeySet * ks);
KeySet * ksDeepDup (const KeySet * source);
Key * elektraKsPrev (KeySet * ks);
Key * elektraKsPopAtCursor (KeySet * ks, cursor_t pos);
int elektraKeyLock (Key * key, enum elektraLockOptions what);
ssize_t ksSearchInternal (const KeySet * ks, const Key * toAppend);
/*Used for internal memcpy/memmove*/
ssize_t elektraMemcpy (Key ** array1, Key ** array2, size_t size);
ssize_t elektraMemmove (Key ** array1, Key ** array2, size_t size);
ssize_t elektraFinalizeName (Key * key);
ssize_t elektraFinalizeEmptyName (Key * key);
char * elektraEscapeKeyNamePart (const char * source, char * dest);
size_t elektraUnescapeKeyName (const char * source, char * dest);
int elektraUnescapeKeyNamePartBegin (const char * source, size_t size, char ** dest);
char * elektraUnescapeKeyNamePart (const char * source, size_t size, char * dest);
/*Internally used for array handling*/
int elektraValidateKeyName (const char * name, size_t size);
int elektraReadArrayNumber (const char * baseName, kdb_long_long_t * oldIndex);
KeySet * elektraRenameKeys (KeySet * config, const char * name);
/* Conveniences Methods for Making Tests */
int keyIsSpec (const Key * key);
int keyIsProc (const Key * key);
int keyIsDir (const Key * key);
int keyIsSystem (const Key * key);
int keyIsUser (const Key * key);
int keyNameIsSpec (const char * keyname);
int keyNameIsProc (const char * keyname);
int keyNameIsDir (const char * keyname);
int keyNameIsSystem (const char * keyname);
int keyNameIsUser (const char * keyname);
/* global plugin calls */
void elektraGlobalGet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition);
void elektraGlobalSet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition);
void elektraGlobalError (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition);
/** Test a bit. @see set_bit(), clear_bit() */
#define test_bit(var, bit) ((var) & (bit))
/** Set a bit. @see clear_bit() */
#define set_bit(var, bit) ((var) |= (bit))
/** Clear a bit. @see set_bit() */
#define clear_bit(var, bit) ((var) &= ~(bit))
#ifdef __cplusplus
}
}
#endif
#endif /* KDBPRIVATE_H */