.TH "KDB :: Low Level Methods" 3 "Fri Aug 26 2011" "Elektra Projekt" \" -*- nroff -*- .ad l .nh .SH NAME KDB :: Low Level Methods \- .PP General methods to access the Key database. .SS "Functions" .in +1c .ti -1c .RI "int \fBkdbMount\fP (KDB *handle, const Key *mountpoint, const KeySet *config)" .br .ti -1c .RI "int \fBkdbUnmount\fP (KDB *handle, const Key *mountpoint)" .br .ti -1c .RI "Key * \fBkdbGetMountpoint\fP (KDB *handle, const Key *where)" .br .ti -1c .RI "KDB * \fBkdbOpen\fP ()" .br .ti -1c .RI "int \fBkdbClose\fP (KDB *handle)" .br .ti -1c .RI "ssize_t \fBkdbGet\fP (KDB *handle, KeySet *returned, Key *parentKey, option_t options)" .br .ti -1c .RI "ssize_t \fBkdbSet\fP (KDB *handle, KeySet *ks, Key *parentKey, option_t options)" .br .in -1c .SH "Detailed Description" .PP General methods to access the Key database. To use them: .PP .nf #include .fi .PP .PP The kdb*() class of methods are used to access the storage, to get and set \fBKeys \fP or \fBKeySets \fP. .PP The most important functions are: .IP "\(bu" 2 \fBkdbOpen()\fP .IP "\(bu" 2 \fBkdbClose()\fP .IP "\(bu" 2 \fBkdbGet()\fP .IP "\(bu" 2 \fBkdbSet()\fP .PP .PP The two essential functions for dynamic information about backends are: .IP "\(bu" 2 \fBkdbGetMountpoint()\fP .IP "\(bu" 2 kdbGetCapability() .PP .PP They use some backend implementation to know the details about how to access the storage. Currently we have this backends: .IP "\(bu" 2 \fCberkeleydb:\fP the keys are stored in a Berkeley DB database, providing very small footprint, speed, and other advantages. .IP "\(bu" 2 \fCfilesys:\fP the key hierarchy and data are saved as plain text files in the filesystem. .IP "\(bu" 2 \fCini:\fP the key hierarchy are saved into configuration files. .PP \fBSee also:\fP .RS 4 http://www.libelektra.org/Ini .RE .PP .IP "\(bu" 2 \fCfstab:\fP a reference backend used to interpret the \fC/etc/fstab\fP file as a set of keys under \fCsystem/filesystems\fP . .IP "\(bu" 2 \fCgconf:\fP makes Elektra use the GConf daemon to access keys. Only the \fCuser/\fP tree is available since GConf is not system wide. .PP .PP Backends are physically a library named \fC/lib/libelektra-{NAME}\fP.so. .PP See \fBwriting a new backend \fP for information about how to write a backend. .PP Language binding writers should follow the same rules: .IP "\(bu" 2 You must relay completely on the backend-dependent methods. .IP "\(bu" 2 You may use or reimplement the second set of methods. .IP "\(bu" 2 You should completely reimplement in your language the higher lever methods. .IP "\(bu" 2 Many methods are just for comfort in C. These methods are marked and need not to be implemented if the binding language has e.g. string operators which can do the operation easily. .PP .SH "Function Documentation" .PP .SS "int kdbClose (KDB *handle)"Closes the session with the Key database. .PP You should call this method when you finished your affairs with the key database. You can manipulate Key and KeySet objects also after \fBkdbClose()\fP. You must not use any kdb* call afterwards. You can implement \fBkdbClose()\fP in the atexit() handler. .PP This is the counterpart of \fBkdbOpen()\fP. .PP The \fChandle\fP parameter will be finalized and all resources associated to it will be freed. After a \fBkdbClose()\fP, this \fChandle\fP can't be used anymore, unless it gets initialized again with another call to \fBkdbOpen()\fP. .PP \fBSee also:\fP .RS 4 \fBkdbOpen()\fP .RE .PP \fBParameters:\fP .RS 4 \fIhandle\fP contains internal information of \fBopened \fP key database .RE .PP \fBReturns:\fP .RS 4 0 on success .PP -1 on NULL pointer .RE .PP .SS "ssize_t kdbGet (KDB *handle, KeySet *returned, Key *parentKey, option_toptions)"Retrieve keys in an atomic and universal way, all other kdbGet Functions rely on that one. .PP The \fCreturned\fP KeySet must be initialized or may already contain some keys. The new retrieved keys will be appended using \fBksAppendKey()\fP. .PP In default behaviour (\fCoptions\fP = 0) it will fully retrieve all keys under the \fCparentKey\fP folder, with all subfolders and their children but not inactive keys or folders. .PP The keyset will not be sorted at first place, but will be marked dirty and sorted afterwards when needed. That could be a subsequent \fBksLookup()\fP, \fBksLookupByName()\fP or \fBkdbSet()\fP. See \fBksSort()\fP on that issue. .PP The behaviour can be fine-tuned with options in various ways to make \fBkdbGet()\fP more comfortable. .SH "Options" .PP The \fCoption\fP is an array of the following ORed flags: .PP .IP "\(bu" 2 \fCoption_t::KDB_O_DEL\fP .br Its often useful to \fBkeyDel()\fP the parentKey in the line after \fBkdbGet()\fP. Using this flag, you can just pass a key allocated with \fBkeyNew()\fP, \fBkdbGet()\fP will free it for you in the end. .IP "\(bu" 2 \fCoption_t::KDB_O_POP\fP .br The \fCparentKey\fP itself will always be added to \fCreturned\fP. If you only want the children of the parentKey in \fCreturned\fP, 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 \fBoption_t::KDB_O_NODIR\fP below. .IP "\(bu" 2 \fCoption_t::KDB_O_NODIR\fP .br Don't include folders in the \fCreturned\fP KeySet, so only keys without subkeys. You can picture it best that you only get the leaves of the tree of keys. .IP "\(bu" 2 \fCoption_t::KDB_O_DIRONLY\fP .br Put in \fCreturned\fP 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. .IP "\(bu" 2 \fCoption_t::KDB_O_NOSTAT\fP .br Don't stat they keys, whatever \fBkeyNeedStat()\fP says. That means that also the key value and comment will be retrieved. The flag will result in that all keys in \fCreturned\fP don't have \fBkeyNeedStat()\fP set. .IP "\(bu" 2 \fCoption_t::KDB_O_STATONLY\fP .br Only stat the keys. It means that key value and comment will not be retrieved. The resulting keys will contain only meta info such as user and group IDs, owner, mode permissions and modification times. You don't need that flag if the keys already have \fBkeyNeedStat()\fP set. The flag will result in that all keys in \fCreturned\fP have \fBkeyNeedStat()\fP set. .IP "\(bu" 2 \fCoption_t::KDB_O_INACTIVE\fP .br Will make it not ignore inactive keys, so \fCreturned\fP will contain also inactive keys. Inactive keys are those that have names beginning 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. .IP "\(bu" 2 \fCoption_t::KDB_O_SORT\fP .br Force \fCreturned\fP to be \fBksSort()\fPed. Normally you don't want that the \fCreturned\fP is sorted immediately because you might add other keys or go for another \fBkdbGet()\fP. Sorting will take place automatically when needed by \fBksLookup()\fP or \fBkdbSet()\fP, 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 \fBkdbGet()\fP. .IP "\(bu" 2 \fCoption_t::KDB_O_NORECURSIVE\fP .br Don't get the keys recursive. Only receive keys from one folder. This might not work if the backend does not support it. Be prepared for more keys and use \fBksLookup()\fP and avoid static assumptions on how many keys you get. .PP .PP \fBExample:\fP .RS 4 .PP .nf 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. .fi .PP .RE .PP .SH "Details" .PP When no backend could be found (e.g. no backend mounted) the default backend will be used. .PP If you pass a NULL pointer as handle and/or returned \fBkdbGet()\fP will return -1 and do nothing but \fBkeyDel()\fP the parentKey when requested and not a NULL pointer. .PP If you pass NULL as parentKey the root keys of all namespaces will be appended to returned. .PP For every directory key (\fBkeyIsDir()\fP) the appropriate backend will be chosen and keys in it will be requested. .PP 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. .PP \fBParameters:\fP .RS 4 \fIhandle\fP contains internal information of \fBopened \fP key database .br \fIparentKey\fP parent key or NULL to get the root keys .br \fIreturned\fP the (pre-initialized) KeySet returned with all keys found .br \fIoptions\fP ORed options to control approaches .RE .PP \fBSee also:\fP .RS 4 #option_t .PP \fBkdb higher level Methods \fP that rely on \fBkdbGet()\fP .PP \fBksLookupByName()\fP, ksLookupByString() for powerful lookups after the KeySet was retrieved .PP commandList() code in \fBKDB :: Low Level Methods\fP command for usage example .PP commandEdit() code in \fBKDB :: Low Level Methods\fP command for usage example .PP commandExport() code in \fBKDB :: Low Level Methods\fP command for usage example .RE .PP \fBReturns:\fP .RS 4 number of keys contained by \fCreturned\fP .PP -1 on failure .RE .PP .SS "Key* kdbGetMountpoint (KDB *handle, const Key *where)"Lookup a mountpoint in a handle for a specific key. .PP Will return a key representing the mountpoint or null if there is no appropriate mountpoint e.g. its the root mountpoint. .PP Together with kdbGetCapability() the two essential information about mounted backends. .PP \fBExample:\fP .RS 4 .PP .nf Key * key = keyNew ('system/template'); KDB * handle = kdbOpen(); Key *mountpoint=0; mountpoint=kdbGetMountpoint(handle, key); printf('The library I am using is %s mounted in %s\n', keyValue(mountpoint), keyName(mountpoint)); kdbClose (handle); keyDel (key); .fi .PP .RE .PP \fBParameters:\fP .RS 4 \fIhandle\fP is the data structure, where the mounted directories are saved. .br \fIwhere\fP the key, that should be looked up. .RE .PP \fBReturns:\fP .RS 4 the mountpoint associated with the key .RE .PP .SS "int kdbMount (KDB *handle, const Key *mountpoint, const KeySet *config)"Dynamically mount a single backend. .PP Maps the mountpoint, defined through its name and value, into the global elektra hierarchy. If successfull, under the mountpoint another backend will reside. .PP 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. .PP If you allocated mountpoint and config first, make sure that you free it! It is ok to free it immediately afterwards. .PP \fBParameters:\fP .RS 4 \fIhandle\fP handle to the kdb data structure .br \fImountpoint\fP the \fBkeyName()\fP of this key is the mountpoint, \fBkeyValue()\fP the backend .br \fIconfig\fP the configuration passed for that backend .RE .PP \fBReturns:\fP .RS 4 0 on success, -1 if an error occurred .RE .PP .SS "KDB* kdbOpen (void)"Opens the session with the Key database. .PP 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 \fCKDB\fP datastructure will be initialized. .PP You must always call this method before retrieving or committing any keys to the database. In the end of the program, after using the key database, you must not forget to \fBkdbClose()\fP. You can use the atexit () handler for it. .PP The pointer to the \fCKDB\fP structure returned will be initialized like described above, and it must be passed along on any kdb*() method your application calls. .PP Get a \fCKDB\fP handle for every thread using elektra. Don't share the handle across threads, and also not the pointer accessing it: .PP .nf 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); } .fi .PP .PP You don't need to use the \fBkdbOpen()\fP if you only want to manipulate plain in-memory Key or KeySet objects without any affairs with the backend key database, .PP \fBSee also:\fP .RS 4 \fBkdbClose()\fP to end all affairs to the \fBKey :: Basic Methods\fP database. .RE .PP \fBReturns:\fP .RS 4 a KDB pointer on success .PP NULL on failure .RE .PP .SS "ssize_t kdbSet (KDB *handle, KeySet *ks, Key *parentKey, option_toptions)"Set keys in an atomic and universal way, all other kdbSet Functions rely on that one. .PP The given handle and keyset are the objects to work with. .PP With parentKey you can only store a part of the given keyset. Otherwise pass a null pointer or a parentKey without a name. .PP .PP .nf 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); .fi .PP .PP Each key is checked with \fBkeyNeedSync()\fP before being actually committed. So only changed keys are updated. If no key of a backend needs to be synced the \fBkdbSet_backend()\fP will be omitted. .PP If some error occurs, \fBkdbSet()\fP 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. .PP \fBExample of how this method can be used:\fP .RS 4 .PP .nf 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; } } .fi .PP .RE .PP .SH "Options" .PP There are some options changing the behaviour of \fBkdbSet()\fP: .PP .IP "\(bu" 2 \fCoption_t::KDB_O_DEL\fP .br Its often useful to \fBkeyDel()\fP the parentKey in the line after \fBkdbGet()\fP. Using this flag, you can just pass a key allocated with \fBkeyNew()\fP, \fBkdbGet()\fP will free it for you in the end. .IP "\(bu" 2 \fCoption_t::KDB_O_SYNC\fP .br Will force to save all keys, independent of their sync state. .IP "\(bu" 2 \fCoption_t::KDB_O_NOREMOVE\fP .br Don't remove any key from disk, even if \fBkeyRemove()\fP was set. With that flag removing keys can't happen unintentional. The flag will result in that all keys in \fCreturned\fP don't have \fBkeyNeedRemove()\fP set. .IP "\(bu" 2 \fCoption_t::KDB_O_REMOVEONLY\fP .br Remove all keys instead of setting them. All keys in \fCreturned\fP will have \fBkeyNeedRemove()\fP set, but not \fBkeyNeedStat()\fP saying to you that the key was deleted permanently. This option implicit also activates \fCoption_t::KDB_O_SYNC\fP because the sync state will be changed when they are marked remove. You might need \fBoption_t::KDB_O_INACTIVE\fP set for the previous call of \fBkdbGet()\fP if there are any. Otherwise the recursive remove will fail, because removing directories is only possible when all subkeys are removed. .PP .SH "Details" .PP When you don't have a parentKey or its name empty, then all keys will be set. .PP You can remove some keys instead of setting them by marking them with \fBkeyRemove()\fP. The \fBkeyNeedSync()\fP flag will be unset after successful removing. But the \fBkeyNeedRemove()\fP flag will stay, but its safe to delete the key. .PP \fBParameters:\fP .RS 4 \fIhandle\fP contains internal information of \fBopened \fP key database .br \fIks\fP a KeySet which should contain changed keys, otherwise nothing is done .br \fIparentKey\fP holds the information below which key keys should be set .br \fIoptions\fP see in \fBkdbSet()\fP documentation .RE .PP \fBReturns:\fP .RS 4 0 on success .PP -1 on failure .RE .PP \fBSee also:\fP .RS 4 \fBkeyNeedSync()\fP, \fBksNext()\fP, \fBksCurrent()\fP .PP \fBkeyRemove()\fP, \fBkeyNeedRemove()\fP .PP commandEdit(), commandImport() code in \fBKDB :: Low Level Methods\fP command for usage and error handling example .RE .PP .SS "int kdbUnmount (KDB *handle, const Key *mountpoint)"Dynamically unmount a single backend. .PP Unmount a backend that was mounted with \fBkdbMount()\fP before. .PP \fBParameters:\fP .RS 4 \fIhandle\fP handle to the kdb data structure .br \fImountpoint\fP directory where backend is mounted to, that should be unmounted .RE .PP \fBReturns:\fP .RS 4 0 on success, -1 if an error ocurred. .RE .PP .SH "Author" .PP Generated automatically by Doxygen for Elektra Projekt from the source code.