General methods to access the Key database. More...
Functions | |
int | kdbMount (KDB *handle, const Key *mountpoint, const KeySet *config) |
int | kdbUnmount (KDB *handle, const Key *mountpoint) |
Key * | kdbGetMountpoint (KDB *handle, const Key *where) |
KDB * | kdbOpen () |
int | kdbClose (KDB *handle) |
ssize_t | kdbGet (KDB *handle, KeySet *returned, Key *parentKey, option_t options) |
ssize_t | kdbSet (KDB *handle, KeySet *ks, Key *parentKey, option_t options) |
General methods to access the Key database.
To use them:
#include <kdb.h>
The kdb*() class of methods are used to access the storage, to get and set Keys or KeySets .
The most important functions are:
The two essential functions for dynamic information about backends are:
They use some backend implementation to know the details about how to access the storage. Currently we have this backends:
berkeleydb:
the keys are stored in a Berkeley DB database, providing very small footprint, speed, and other advantages.filesys:
the key hierarchy and data are saved as plain text files in the filesystem.ini:
the key hierarchy are saved into configuration files. fstab:
a reference backend used to interpret the /etc/fstab
file as a set of keys under system/filesystems
.gconf:
makes Elektra use the GConf daemon to access keys. Only the user/
tree is available since GConf is not system wide.Backends are physically a library named /lib/libelektra-{NAME}
.so.
See writing a new backend for information about how to write a backend.
Language binding writers should follow the same rules:
int kdbClose | ( | KDB * | handle | ) |
Closes the session with the Key database.
You should call this method when you finished your affairs with the key database. You can manipulate Key and KeySet objects also after kdbClose(). You must not use any kdb* call afterwards. You can implement kdbClose() in the atexit() handler.
This is the counterpart of kdbOpen().
The handle
parameter will be finalized and all resources associated to it will be freed. After a kdbClose(), this handle
can't be used anymore, unless it gets initialized again with another call to kdbOpen().
handle | contains internal information of opened key database |
ssize_t kdbGet | ( | KDB * | handle, | |
KeySet * | returned, | |||
Key * | parentKey, | |||
option_t | options | |||
) |
Retrieve keys in an atomic and universal way, all other kdbGet Functions rely on that one.
The returned
KeySet must be initialized or may already contain some keys. The new retrieved keys will be appended using ksAppendKey().
In default behaviour (options
= 0) it will fully retrieve all keys under the parentKey
folder, with all subfolders and their children but not inactive keys or folders.
The keyset will not be sorted at first place, but will be marked dirty and sorted afterwards when needed. That could be a subsequent ksLookup(), ksLookupByName() or kdbSet(). See ksSort() on that issue.
The behaviour can be fine-tuned with options in various ways to make kdbGet() more comfortable.
The option
is an array of the following ORed flags:
option_t::KDB_O_DEL
option_t::KDB_O_POP
parentKey
itself will always be added to returned
. If you only want the children of the parentKey in returned
, but not the parentKey itself, use this flag. This is only valid for the first parentKey, the one you passed. The other recursive parentKeys will stay in the keyset. To get only the leaves of the tree, without any parentKey, see option_t::KDB_O_NODIR below.option_t::KDB_O_NODIR
returned
KeySet, so only keys without subkeys. You can picture it best that you only get the leaves of the tree of keys.option_t::KDB_O_DIRONLY
returned
only the folder keys. The resulting KeySet will be only the skeleton of the tree. This option must not be ORed together with KDB_O_DIR.option_t::KDB_O_NOSTAT
returned
don't have keyNeedStat() set.option_t::KDB_O_STATONLY
returned
have keyNeedStat() set.option_t::KDB_O_INACTIVE
returned
will contain also inactive keys. Inactive keys are those that have names begining with '.' (dot). Please be sure that you know what you are doing, inactive keys must not have any semantics to the application. This flag should only be set in key browsers after explicit user request. You might also get inactive keys when you plan to remove a whole hierarchy.option_t::KDB_O_SORT
returned
to be ksSort()ed. Normally you don't want that the returned
is sorted immediately because you might add other keys or go for another kdbGet(). Sorting will take place automatically when needed by ksLookup() or kdbSet(), also without this option set. But you need to sort the keyset for yourself, when you just iterate over it. If you want to do that, pass this flag at the last kdbGet().option_t::KDB_O_NORECURSIVE
KDB *handle; KeySet *myConfig; Key *key; myConfig=ksNew(0); handle = kdbOpen(); key=keyNew("system/sw/MyApp",KEY_END); rc=kdbGet(handle,key, myConfig, 0); keyDel(key); key=keyNew("user/sw/MyApp",KEY_END); rc=kdbGet(handle,key, myConfig, 0); keyDel(key); // will sort keyset here key=ksLookupByName(myConfig,"/sw/MyApp/key", 0); // check if key is not 0 and work with it... ksDel (myConfig); // delete the in-memory configuration // maybe you want kdbSet() myConfig here kdbClose(handle); // no more affairs with the key database.
When no backend could be found (e.g. no backend mounted) the default backend will be used.
If you pass a NULL pointer as handle and/or returned kdbGet() will return -1 and do nothing but keyDel() the parentKey when requested and not a NULL pointer.
If you pass NULL as parentKey the root keys of all namespaces will be appended to returned.
For every directory key (keyIsDir()) the appropriate backend will be chosen and keys in it will be requested.
If any backend reports an failure the recursive getting of keys will be stopped. Backends only report failure when they are not able to get keys for any problems.
handle | contains internal information of opened key database | |
parentKey | parent key or NULL to get the root keys | |
returned | the (pre-initialized) KeySet returned with all keys found | |
options | ORed options to control approaches |
returned
Key* kdbGetMountpoint | ( | KDB * | handle, | |
const Key * | where | |||
) |
Lookup a mountpoint in a handle for a specific key.
Will return a key representing the mountpoint or null if there is no appropriate mountpoint e.g. its the root mountpoint.
Together with kdbGetCapability() the two essential informations about mounted backends.
handle | is the data structure, where the mounted directories are saved. | |
where | the key, that should be looked up. |
int kdbMount | ( | KDB * | handle, | |
const Key * | mountpoint, | |||
const KeySet * | config | |||
) |
Dynamically mount a single backend.
Maps the mountpoint, defined through its name and value, into the global elektra hierachy. If successfull, under the mountpoint another backend will reside.
This only works for a single KDB, that means a single thread in a single process. You may want statically mounting by editing system/elektra/mountpoints.
If you allocated mountpoint and config first, make sure that you free it! It is ok to free it immediately afterwards.
handle | handle to the kdb data structure | |
mountpoint | the keyName() of this key is the mountpoint, keyValue() the backend | |
config | the configuration passed for that backend |
KDB* kdbOpen | ( | void | ) |
Opens the session with the Key database.
The first step is to open the default backend. With it system/elektra/mountpoints will be loaded and all needed libraries and mountpoints will be determined. These libraries for backends will be loaded and with it the KDB
datastructure will be initialized.
You must always call this method before retrieving or commiting any keys to the database. In the end of the program, after using the key database, you must not forget to kdbClose(). You can use the atexit () handler for it.
The pointer to the KDB
structure returned will be initialized like described above, and it must be passed along on any kdb*() method your application calls.
Get a KDB
handle for every thread using elektra. Don't share the handle across threads, and also not the pointer accessing it:
thread1 { KDB * h; h = kdbOpen(); // fetch keys and work with them kdbClose(h); } thread2 { KDB * h; h = kdbOpen(); // fetch keys and work with them kdbClose(h); }
You don't need to use the kdbOpen() if you only want to manipulate plain in-memory Key or KeySet objects without any affairs with the backend key database,
ssize_t kdbSet | ( | KDB * | handle, | |
KeySet * | ks, | |||
Key * | parentKey, | |||
option_t | options | |||
) |
Set keys in an atomic and universal way, all other kdbSet Functions rely on that one.
The given handle and keyset are the objects to work with.
With parentKey you can only store a part of the given keyset. Otherwise pass a null pointer or a parentKey without a name.
KeySet *ks = ksNew(0); kdbGet (h, ks, keyNew("system/myapp",0), KDB_O_DEL); kdbGet (h, ks, keyNew("user/myapp",0), KDB_O_DEL); //now only set everything below user, because you can't write to system kdbSet (h, ks, keyNew("user/myapp",0), KDB_O_DEL); ksDel (ks);
Each key is checked with keyNeedSync() before being actually committed. So only changed keys are updated. If no key of a backend needs to be synced the kdbSet_backend() will be omitted.
If some error occurs, kdbSet() will stop. In this situation the KeySet internal cursor will be set on the key that generated the error. This specific key and all behind it were not set. To be failsafe jump over it and try to set the rest, but report the error to the user.
int i; KeySet *ks; // the KeySet I want to set // fill ks with some keys for (i=0; i< 10; i++) // limit to 10 tries { ret=kdbSet(handle,ks, 0, 0); if (ret == -1) { // We got an error. Warn user. Key *problem; problem=ksCurrent(ks); if (problem) { char keyname[300]=""; keyGetFullName(problem,keyname,sizeof(keyname)); fprintf(stderr,"kdb import: while importing %s", keyname); } else break; // And try to set keys again starting from the next key, // unless we reached the end of KeySet if (ksNext(ks) == 0) break; } }
There are some options changing the behaviour of kdbSet():
option_t::KDB_O_DEL
option_t::KDB_O_SYNC
option_t::KDB_O_NOREMOVE
returned
don't have keyNeedRemove() set.option_t::KDB_O_REMOVEONLY
returned
will have keyNeedRemove() set, but not keyNeedStat() saying to you that the key was deleted permanently. This option implicit also activates option_t::KDB_O_SYNC
because the sync state will be changed when they are marked remove. You might need option_t::KDB_O_INACTIVE set for the previous call of kdbGet() if there are any. Otherwise the recursive remove will fail, because removing directories is only possible when all subkeys are removed.When you dont have a parentKey or its name empty, then all keys will be set.
You can remove some keys instead of setting them by marking them with keyRemove(). The keyNeedSync() flag will be unset after successful removing. But the keyNeedRemove() flag will stay, but its safe to delete the key.
handle | contains internal information of opened key database | |
ks | a KeySet which should contain changed keys, otherwise nothing is done | |
parentKey | holds the information below which key keys should be set | |
options | see in kdbSet() documentation |
int kdbUnmount | ( | KDB * | handle, | |
const Key * | mountpoint | |||
) |
Dynamically unmount a single backend.
Unmount a backend that was mounted with kdbMount() before.
handle | handle to the kdb data structure | |
mountpoint | directory where backend is mounted to, that should be unmounted |