void test_mode() { Key *key; key = keyNew ("user/mode", KEY_MODE, 0100, KEY_END); succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "100"), "meta value for mode was not set correctly"); succeed_if (keyGetMode(key) == 0100, "mode was not set correctly"); succeed_if (keySetMode(key, 0101) == 0, "could not set mode"); succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "101"), "meta value for mode was not set correctly"); succeed_if (keyGetMode(key) == 0101, "mode was not set correctly"); succeed_if (keySetMode(key, 0) == 0, "could not set mode"); succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "0"), "meta value for mode was not set correctly"); succeed_if (keyGetMode(key) == 0, "mode was not set correctly"); succeed_if (keySetMeta (key, "mode", "102") == sizeof("102"), "could not set meta"); succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "102"), "meta value for mode was not set correctly"); succeed_if (keyGetMode(key) == 0102, "mode was not set correctly"); succeed_if (keySetMeta (key, "mode", "0103") == sizeof("0103"), "could not set meta"); succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "0103"), "meta value for mode was not set correctly"); succeed_if (keyGetMode(key) == 0103, "mode was not set correctly with leading octal 0"); succeed_if (keySetMeta (key, "mode", "x") == sizeof("x"), "could not set meta"); succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "x"), "meta value for mode was not set correctly"); succeed_if (keyGetMode(key) == KDB_FILE_MODE, "mode was not set correctly"); succeed_if (keySetMeta (key, "mode", "x1") == sizeof("x1"), "could not set meta"); succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "x1"), "meta value for mode was not set correctly"); succeed_if (keyGetMode(key) == KDB_FILE_MODE, "mode was not set correctly"); succeed_if (keySetMeta (key, "mode", "2000000") == sizeof("2000000"), "could not set large mode"); succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "2000000"), "meta value for large mode was not set correctly"); succeed_if (keyGetMode(key) == 02000000, "large mode was not set correctly"); succeed_if (keySetMeta (key, "mode", "1x") == sizeof("1x"), "could not set meta"); succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "1x"), "meta value for mode was not set correctly"); succeed_if (keyGetMode(key) == KDB_FILE_MODE, "mode was not set correctly"); succeed_if (keySetMeta (key, "mode", "50x") == sizeof("50x"), "could not set meta"); succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "50x"), "meta value for mode was not set correctly"); succeed_if (keyGetMode(key) == KDB_FILE_MODE, "mode was not set correctly"); keyDel (key); key = keyNew ("user/mode", KEY_END); succeed_if (keyValue (keyGetMeta(key, "mode")) == 0, "got value, but mode was not set up to now"); succeed_if (keyGetMode(key) == KDB_FILE_MODE, "KDB_FILE_MODE not default on new key"); succeed_if (keySetMeta (key, "mode", "") == sizeof(""), "could not set large mode"); succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), ""), "meta value for large mode was not set correctly"); succeed_if (keyGetMode(key) == KDB_FILE_MODE, "empty mode should also yield default"); keyDel (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. * * @param key the key to set permissions to be recognized as directory. * @retval 0 on success * @retval -1 on NULL pointer * @see keySetMode() */ int keySetDir (Key * key) { mode_t mode; if (!key) return -1; mode = keyGetMode (key); mode |= KDB_DIR_MODE; keySetMode (key, mode); return 0; }
/* * Processes the current <key> node from reader, converting from XML * to a Key object, and ksAppendKey() it to ks. * * See keyToStream() for an example of a <key> node. * * This function is completelly dependent on libxml. * * @param ks where to put the resulting reded key * @param context a prent key name, so a full name can be calculated * if the XML node for the current key only provides a basename * @param reader where to read from */ static int consumeKeyNode(KeySet *ks, const char *context, xmlTextReaderPtr reader) { xmlChar *nodeName=0; xmlChar *keyNodeName=0; xmlChar *buffer=0; xmlChar *privateContext=0; Key *newKey=0; int appended=0; /* printf("%s", KDB_SCHEMA_PATH); */ keyNodeName=xmlTextReaderName(reader); if (!strcmp((char *)keyNodeName,"key")) { mode_t isdir=0; int isbin=0; int end=0; newKey=keyNew(0); /* a <key> must have one of the following: - a "name" attribute, used as an absolute name overriding the context - a "basename" attribute, that will be appended to the current context - a "parent" plus "basename" attributes, both appended to current context - only a "parent", appended to current context */ buffer=xmlTextReaderGetAttribute(reader,(const xmlChar *)"name"); if (buffer) { /* set absolute name */ keySetName(newKey,(char *)buffer); xmlFree(buffer); buffer=0; } else { /* logic for relative name calculation */ privateContext=xmlTextReaderGetAttribute(reader, (const xmlChar *)"parent"); buffer=xmlTextReaderGetAttribute(reader, (const xmlChar *)"basename"); if (context) keySetName(newKey,context); if (privateContext) keyAddName(newKey, (char *)privateContext); if (buffer) keyAddName(newKey,(char *)buffer); xmlFree(privateContext); privateContext=0; xmlFree(buffer); buffer=0; } /* test for a short value attribute, instead of <value> bellow */ buffer=xmlTextReaderGetAttribute(reader,(const xmlChar *)"value"); if (buffer) { keySetRaw(newKey,buffer,elektraStrLen((char *)buffer)); xmlFree(buffer); buffer=0; } /* Parse UID */ buffer=xmlTextReaderGetAttribute(reader,(const xmlChar *)"uid"); if (buffer) { int errsave = errno; char * endptr; long int uid = strtol ((const char *)buffer, &endptr, 10); errno = errsave; if (endptr != '\0' && *endptr == '\0') { keySetUID(newKey,uid); } xmlFree(buffer); buffer=0; } /* Parse GID */ buffer=xmlTextReaderGetAttribute(reader,(const xmlChar *)"gid"); if (buffer) { int errsave = errno; char * endptr; long int gid = strtol ((const char *)buffer, &endptr, 10); errno = errsave; if (endptr != '\0' && *endptr == '\0') { keySetGID(newKey,gid); } xmlFree(buffer); buffer=0; } /* Parse mode permissions */ buffer=xmlTextReaderGetAttribute(reader,(const xmlChar *)"mode"); int errsave = errno; if (buffer) keySetMode(newKey,strtol((char *)buffer,0,0)); errno = errsave; xmlFree(buffer); if (xmlTextReaderIsEmptyElement(reader)) { /* we have a <key ..../> element */ if (newKey && !appended) { ksAppendKey(ks,newKey); appended=1; end=1; } } buffer=xmlTextReaderGetAttribute(reader,(const xmlChar *)"type"); if (buffer) { if (!strcmp((char *)buffer,"binary")) isbin = 1; else if (!strcmp((char *)buffer,"bin")) isbin = 1; } xmlFree(buffer); /* If "isdir" appears, everything different from "0", "false" or "no" marks it as a dir key */ buffer=xmlTextReaderGetAttribute(reader,(const xmlChar *)"isdir"); if (!isdir && buffer) { if ( strcmp((char *)buffer,"0") && strcmp((char *)buffer,"false") && strcmp((char *)buffer,"no")) isdir = 1; else isdir = 0; } xmlFree(buffer); if (isdir) keySetDir(newKey); if (isbin) keySetMeta (newKey, "binary", ""); // TODO: should parse arbitrary attributes as metadata /* Parse everything else */ while (!end) { xmlTextReaderRead(reader); nodeName=xmlTextReaderName(reader); if (!strcmp((char *)nodeName,"value")) { if (xmlTextReaderIsEmptyElement(reader) || xmlTextReaderNodeType(reader)==15) { xmlFree (nodeName); continue; } xmlTextReaderRead(reader); buffer=xmlTextReaderValue(reader); if (buffer) { /* Key's value type was already set above */ if (keyIsBinary(newKey)) { /* TODO binary values char *unencoded=0; size_t unencodedSize; unencodedSize=elektraStrLen((char *)buffer)/2; unencoded=elektraMalloc(unencodedSize); unencodedSize=kdbbDecode((char *)buffer,unencoded); if (!unencodedSize) return -1; keySetRaw(newKey,unencoded,unencodedSize); elektraFree (unencoded); */ } else keySetRaw(newKey,buffer,elektraStrLen((char *)buffer)); } xmlFree(buffer); } else if (!strcmp((char *)nodeName,"comment")) { ssize_t commentSize=0; if (xmlTextReaderIsEmptyElement(reader) || xmlTextReaderNodeType(reader)==15) { xmlFree (nodeName); continue; } xmlTextReaderRead(reader); buffer=xmlTextReaderValue(reader); if ((commentSize=keyGetCommentSize(newKey)) > 1) { /*Multiple line comment*/ char *tmpComment=0; tmpComment=elektraMalloc(commentSize+ xmlStrlen(buffer)*sizeof(xmlChar)+1); if (tmpComment) { keyGetComment(newKey,tmpComment,commentSize); strcat(tmpComment,"\n"); strcat(tmpComment,(char *)buffer); keySetComment(newKey,tmpComment); elektraFree (tmpComment); tmpComment=0; } } else keySetComment(newKey,(char *)buffer); xmlFree(buffer); } else if (!strcmp((char *)nodeName,"key")) { /* Here we found </key> or a sub <key>. So include current key in the KeySet. */ if (newKey && !appended) { ksAppendKey(ks,newKey); appended=1; } if (xmlTextReaderNodeType(reader)==15) /* found a </key> */ end=1; else { /* found a sub <key> */ /* prepare the context (parent) */ consumeKeyNode(ks,newKey->key,reader); } } xmlFree (nodeName); } if (privateContext) xmlFree(privateContext); /* seems like we forgot the key, lets delete it */ if (newKey && !appended) { keyDel (newKey); appended=1; } } xmlFree(keyNodeName); return 0; }