Elektra  0.8.10
Functions
Meta Info Manipulation Methods
Key

Methods to do various operations on Key metainfo. More...

Collaboration diagram for Meta Info Manipulation Methods:

Functions

int keyRewindMeta (Key *key)
 
const Key * keyNextMeta (Key *key)
 
const Key * keyCurrentMeta (const Key *key)
 
int keyCopyMeta (Key *dest, const Key *source, const char *metaName)
 
int keyCopyAllMeta (Key *dest, const Key *source)
 
const Key * keyGetMeta (const Key *key, const char *metaName)
 
ssize_t keySetMeta (Key *key, const char *metaName, const char *newMetaString)
 
uid_t keyGetUID (const Key *key)
 
int keySetUID (Key *key, uid_t uid)
 
gid_t keyGetGID (const Key *key)
 
int keySetGID (Key *key, gid_t gid)
 
int keySetDir (Key *key)
 
mode_t keyGetMode (const Key *key)
 
int keySetMode (Key *key, mode_t mode)
 
time_t keyGetATime (const Key *key)
 
int keySetATime (Key *key, time_t atime)
 
time_t keyGetMTime (const Key *key)
 
int keySetMTime (Key *key, time_t mtime)
 
time_t keyGetCTime (const Key *key)
 
int keySetCTime (Key *key, time_t ctime)
 

Detailed Description

Methods to do various operations on Key metainfo.

To use them:

#include <kdb.h>

Next to Name (key and owner) and Value (data and comment) there is the so called meta information inside every key.

Key meta information are an unlimited number of key/value pairs strongly related to a key. It main purpose is to give keys special semantics, so that plugins can treat them differently.

Metakey, as opposed to Key, deliberately has following limitations:

Examples for metadata

File system information (see stat(2) for more information):

The comment can contain userdata which directly belong to that key. The name of the meta information is "comment" for a general purpose comment about the key. Multi-Language comments are also supported by appending [LANG] to the name.

Validators are regular expressions which are tested against the key value. The metakey "validator" can hold a regular expression which will be matched against.

Types can be expressed with the meta information "type".

The relevance of the key can be tagged with a value from -20 to 20. Negative numbers are the more important and must be present in order to start the program.

A version of a key may be stored with "version". Its format is full.major.minor where all of these are integers.

The order inside a persistent storage can be described with the tag "order" which contains a positive number.

The meta key "app" describes to which application a key belongs. It can be used to remove keys from an application no longer installed.

The meta key "path" describes where the key is physically stored.

The "owner" is the user that owns the key. It only works for the user/ hierarchy. It rather says where the key is stored and says nothing about the filesystem properties.

Function Documentation

int keyCopyAllMeta ( Key *  dest,
const Key *  source 
)

Do a shallow copy of all meta data from source to dest.

The key dest will additionally have all meta data the source had. Meta data not present in source will not be changed. Meta data which was present in source and dest will be overwritten.

For example the meta data type is copied into the Key k:

void l(Key *k)
{
// receive c
// the caller will see the changed key k
// with all the metadata from c
}

The main purpose of this function is for plugins or applications which want to add the same meta data to n keys. When you do that with keySetMeta() it will take n times the memory for the key. This can be considerable amount of memory for many keys with some meta data for each.

To avoid that problem you can use keyCopyAllMeta() or keyCopyMeta():

void o(KeySet *ks)
{
Key *current;
Key *shared = keyNew (0);
keySetMeta(shared, "shared1", "this meta data should be shared among many keys");
keySetMeta(shared, "shared2", "this meta data should be shared among many keys also");
keySetMeta(shared, "shared3", "this meta data should be shared among many keys too");
ksRewind(ks);
while ((current = ksNext(ks)) != 0)
{
if (needsSharedData(current)) keyCopyAllMeta(current, shared);
}
keyDel(shared);
}
Postcondition
for every metaName present in source: keyGetMeta(source, metaName) == keyGetMeta(dest, metaName)
Returns
1 if was successfully copied
0 if source did not have any meta data
-1 on null pointer of dest or source
-1 on memory problems
Parameters
destthe destination where the meta data should be copied too
sourcethe key where the meta data should be copied from
int keyCopyMeta ( Key *  dest,
const Key *  source,
const char *  metaName 
)

Do a shallow copy of meta data from source to dest.

The key dest will have the same meta data referred with metaName afterwards then source.

For example the meta data type is copied into the Key k.

void l(Key *k)
{
// receive c
keyCopyMeta(k, c, "type");
// the caller will see the changed key k
// with the metadata "type" from c
}

The main purpose of this function is for plugins or applications which want to add the same meta data to n keys. When you do that with keySetMeta() it will take n times the memory for the key. This can be considerable amount of memory for many keys with some meta data for each.

To avoid that problem you can use keyCopyAllMeta() or keyCopyMeta().

void o(KeySet *ks)
{
Key *current;
Key *shared = keyNew (0);
keySetMeta(shared, "shared", "this meta data should be shared among many keys");
ksRewind(ks);
while ((current = ksNext(ks)) != 0)
{
if (needs_shared_data(current)) keyCopyMeta(current, shared, "shared");
}
}
Postcondition
keyGetMeta(source, metaName) == keyGetMeta(dest, metaName)
Returns
1 if was successfully copied
0 if the meta data in dest was removed too
-1 on null pointers (source or dest)
-1 on memory problems
Parameters
destthe destination where the meta data should be copied too
sourcethe key where the meta data should be copied from
metaNamethe name of the meta data which should be copied
const Key* keyCurrentMeta ( const Key *  key)

Returns the Value of a Meta-Information which is current.

The pointer is NULL if you reached the end or after ksRewind().

Note
You must not delete or change the returned key, use keySetMeta() if you want to delete or change it.
Parameters
keythe key object to work with
Returns
a buffer to the value pointed by key's cursor
0 on NULL pointer
See Also
keyNextMeta(), keyRewindMeta()
ksCurrent() for pedant in iterator interface of KeySet
time_t keyGetATime ( const Key *  key)

Get last time the key data was read from disk.

Deprecated:
This API is obsolete.

Every kdbGet() might update the access time of a key. You get information when the key was read the last time from the database.

You will get 0 when the key was not read already.

Beware that multiple copies of keys with keyDup() might have different atimes because you kdbGet() one, but not the other. You can use this information to decide which key is the latest.

Parameters
keyKey to get information from.
Returns
the time you got the key with kdbGet()
0 on key that was never kdbGet()
(time_t)-1 on NULL pointer
See Also
keySetATime()
kdbGet()
time_t keyGetCTime ( const Key *  key)

Get last time the key metadata was changed from disk.

Deprecated:
This API is obsolete.

You will get 0 when the key was not read already.

Any changed field in metadata will influence the ctime of a key.

This time is not updated if only value or comment are changed.

Not changed keys will not update this time, even after kdbSet().

It is possible that other keys written to disc influence this time if the backend is not grained enough.

Parameters
keyKey to get information from.
See Also
keySetCTime()
Returns
(time_t)-1 on NULL pointer
the metadata change time
gid_t keyGetGID ( const Key *  key)

Get the group ID of a key.

Deprecated:
This API is obsolete.

GID

The group ID is a unique identification for every group present on a system. Keys will belong to root (0) as long as you did not get their real GID with kdbGet().

Unlike UID users might change their group. This makes it possible to share configuration between some users.

A fresh key will have (gid_t)-1 also known as the group nogroup. It means that the key is not related to a group ID at the moment.

Parameters
keythe key object to work with
Returns
the system's GID of the key
(gid_t)-1 on NULL key or currently unknown ID
See Also
keySetGID(), keyGetUID()
const Key* keyGetMeta ( const Key *  key,
const char *  metaName 
)

Returns the Value of a Meta-Information given by name.

This is a much more efficient version of keyGetMeta(). But unlike with keyGetMeta you are not allowed to modify the resulting string.

int f(Key *k)
{
if (!strcmp(keyValue(keyGetMeta(k, "type")), "boolean"))
{
// the type of the key is boolean
}
}
Note
You must not delete or change the returned key, use keySetMeta() if you want to delete or change it.
Parameters
keythe key object to work with
metaNamethe name of the meta information you want the value from
Returns
0 if the key or metaName is 0
0 if no such metaName is found
value of Meta-Information if Meta-Information is found
See Also
keyGetMeta(), keySetMeta()
mode_t keyGetMode ( const Key *  key)

Return the key mode permissions.

Deprecated:
This API is obsolete.

Default is 0664 (octal) for keys and 0775 for directory keys which used keySetDir().

The defaults are defined with the macros KDB_FILE_MODE and KDB_DIR_MODE.

For more information about the mode permissions see Modes.

Parameters
keythe key object to work with
Returns
mode permissions of the key
KDB_FILE_MODE as defaults
(mode_t)-1 on NULL pointer
See Also
keySetMode()
time_t keyGetMTime ( const Key *  key)

Get last modification time of the key on disk.

Deprecated:
This API is obsolete.

You will get 0 when the key was not read already.

Everytime you change value or comment and kdbSet() the key the mtime will be updated. When you kdbGet() the key, the atime is set appropriate.

Not changed keys may not even passed to kdbSet_backend() so it will not update this time, even after kdbSet().

It is possible that other keys written to disc influence this time if the backend is not grained enough.

If you add or remove a key the key thereunder in the hierarchy will update the mtime if written with kdbSet() to disc.

Parameters
keyKey to get information from.
See Also
keySetMTime()
Returns
the last modification time
(time_t)-1 on NULL pointer
uid_t keyGetUID ( const Key *  key)

Get the user ID of a key.

Deprecated:
This API is obsolete.

UID

The user ID is a unique identification for every user present on a system. Keys will belong to root (0) as long as you did not get their real UID with kdbGet().

Although usually the same, the UID of a key is not related to its owner.

A fresh key will have no UID.

Parameters
keythe key object to work with
Returns
the system's UID of the key
(uid_t)-1 on NULL key
See Also
keyGetGID(), keySetUID(), keyGetOwner()
const Key* keyNextMeta ( Key *  key)

Iterate to the next meta information.

Keys have an internal cursor that can be reset with keyRewindMeta(). Every time keyNextMeta() is called the cursor is incremented and the new current Name of Meta Information is returned.

You'll get a NULL pointer if the meta information after the end of the Key was reached. On subsequent calls of keyNextMeta() it will still return the NULL pointer.

The key internal cursor will be changed, so it is not const.

Note
That the resulting key is guaranteed to have a value, because meta information has no binary or null pointer semantics.
You must not delete or change the returned key, use keySetMeta() if you want to delete or change it.
Parameters
keythe key object to work with
Returns
a key representing meta information
0 when the end is reached
0 on NULL pointer
See Also
ksNext() for pedant in iterator interface of KeySet
int keyRewindMeta ( Key *  key)

Rewind the internal iterator to first meta data.

Use it to set the cursor to the beginning of the Key Meta Infos. keyCurrentMeta() will then always return NULL afterwards. So you want to keyNextMeta() first.

Key *key;
const Key *meta;
while ((meta = keyNextMeta (key))!=0)
{
printf ("name: %s, value: %s", keyName(meta), (const char*)keyValue(meta));
}
Parameters
keythe key object to work with
Returns
0 on success
0 if there is no meta information for that key (keyNextMeta() will always return 0 in that case)
-1 on NULL pointer
See Also
keyNextMeta(), keyCurrentMeta()
ksRewind() for pedant in iterator interface of KeySet
int keySetATime ( Key *  key,
time_t  atime 
)

Update the atime information for a key.

Deprecated:
This API is obsolete.

When you do manual sync of keys you might also update the atime to make them indistinguishable.

It can also be useful if you work with keys not using a keydatabase.

Parameters
keyThe Key object to work with
atimeThe new access time for the key
Returns
0 on success
-1 on NULL pointer
See Also
keyGetATime()
int keySetCTime ( Key *  key,
time_t  ctime 
)

Update the ctime information for a key.

Deprecated:
This API is obsolete.
Parameters
keyThe Key object to work with
ctimeThe new change metadata time for the key
Returns
0 on success
-1 on NULL pointer
See Also
keyGetCTime()
int keySetDir ( Key *  key)

Set mode so that key will be recognized as directory.

Deprecated:
This API is obsolete.

The function will add all executable bits.

  • Mode 0200 will be translated to 0311
  • Mode 0400 will be translated to 0711
  • Mode 0664 will be translated to 0775

The macro KDB_DIR_MODE (defined to 0111) will be used for that.

The executable bits show that child keys are allowed and listable. There is no way to have child keys which are not listable for anyone, but it is possible to restrict listing the keys to the owner only.

  • Mode 0000 means that it is a key not read or writable to anyone.
  • Mode 0111 means that it is a directory not read or writable to anyone. But it is recognized as directory to anyone.

For more about mode see keySetMode().

It is not possible to access keys below a not executable key. If a key is not writeable and executable kdbSet() will fail to access the keys below. If a key is not readable and executable kdbGet() will fail to access the keys below.

Parameters
keythe key to set permissions to be recognized as directory.
Returns
0 on success
-1 on NULL pointer
See Also
keySetMode()
int keySetGID ( Key *  key,
gid_t  gid 
)

Set the group ID of a key.

Deprecated:
This API is obsolete.

See GID for more information about group IDs.

Parameters
keythe key object to work with
gidis the group ID
Returns
0 on success
-1 on NULL key
See Also
keyGetGID(), keySetUID()
ssize_t keySetMeta ( Key *  key,
const char *  metaName,
const char *  newMetaString 
)

Set a new Meta-Information.

Will set a new Meta-Information pair consisting of metaName and newMetaString.

Will add a new Pair for Meta-Information if metaName was not added up to now.

It will modify a existing Pair of Meta-Information if the the metaName was inserted already.

It will remove a meta information if newMetaString is 0.

Parameters
keythe key object to work with
metaNamethe name of the meta information where you want to change the value
newMetaStringthe new value for the meta information
Returns
-1 on error if key or metaName is 0, out of memory or names are not valid
0 if the Meta-Information for metaName was removed
size (>0) of newMetaString if Meta-Information was successfully added
See Also
keyGetMeta()
int keySetMode ( Key *  key,
mode_t  mode 
)

Set the key mode permissions.

Deprecated:
This API is obsolete. It is only a mapping to keySetMeta(key, "mode", str) which should be prefered.

The mode consists of 9 individual bits for mode permissions. In the following explanation the octal notation with leading zero will be used.

Default is 0664 (octal) for keys and 0775 for directory keys which used keySetDir().

The defaults are defined with the macros KDB_FILE_MODE and KDB_DIR_MODE.

Note
libelektra 0.7.0 only allows 0775 (directory keys) and 0664 (other keys). More will be added later in a sense of the description below.

Modes

0000 is the most restrictive mode. No user might read, write or execute the key.

Reading the key means to get the value by kdbGet().

Writing the key means to set the value by kdbSet().

Execute the key means to make a step deeper in the hierarchy. But you must be able to read the key to be able to list the keys below. See also keySetDir() in that context. But you must be able to write the key to be able to add or remove keys below.

0777 is the most relaxing mode. Every user is allowed to read, write and execute the key, if he is allowed to execute and read all keys below.

0700 allows every action for the current user, identified by the uid. See keyGetUID() and keySetUID().

To be more specific for the user the single bits can elect the mode for read, write and execute. 0100 only allows executing which gives the information that it is a directory for that user, but not accessable. 0200 only allows reading. This information may be combined to 0300, which allows execute and reading of the directory. Last 0400 decides about the writing permissions.

The same as above is also valid for the 2 other octal digits. 0070 decides about the group permissions, in that case full access. Groups are identified by the gid. See keyGetGID() and keySetGID(). In that example everyone with a different uid, but the gid of the the key, has full access.

0007 decides about the world permissions. This is taken into account when neighter the uid nor the gid matches. So that example would allow everyone with a different uid and gid of that key gains full access.

Parameters
keythe key to set mode permissions
modethe mode permissions
Returns
0 on success
-1 on NULL key
See Also
keyGetMode()
int keySetMTime ( Key *  key,
time_t  mtime 
)

Update the mtime information for a key.

Deprecated:
This API is obsolete.
Parameters
keyThe Key object to work with
mtimeThe new modification time for the key
Returns
0 on success
See Also
keyGetMTime()
int keySetUID ( Key *  key,
uid_t  uid 
)

Set the user ID of a key.

Deprecated:
This API is obsolete.

See UID for more information about user IDs.

Parameters
keythe key object to work with
uidthe user ID to set
Returns
0 on success
-1 on NULL key or conversion error
See Also
keySetGID(), keyGetUID(), keyGetOwner()