コード例 #1
0
/*
 * Prints to the standard output stream all values in the registry whose path
 * name starts with a given prefix.
 *
 * Arguments:
 *      path            Pointer to a registry pathname to be printed.  Shall
 *                      not be NULL.
 *      quiet           Whether or not to be quiet about a pathname not
 *                      existing.
 * Returns:
 *      0               Success
 *      NO_SUCH_ENTRY   No such value or node.  "log_flush()" called iff "quiet
 *                      == 0".
 *      SYSTEM_ERROR    Failure.  "log_flush()" called.
 */
static Status printPath(
    const char*         path,
    const int           quiet)
{
    Status      status = 0;             /* success */
    RegStatus   regStatus;
    char*       value;

    log_debug("%s printing path \"%s\"", quiet ? "Quietly" : "Non-quietly", path);

    /*
     * The path name is first assumed to reference an existing value;
     * otherwise, the value wouldn't be printed.
     */ 
    if (0 == (regStatus = reg_getString(path, &value))) {
        if (NULL != value) {
            (void)printf("%s\n", value);
            free(value);
        }                               /* "value" allocated */
    }                                   /* got value-string */
    else {
        if (ENOENT != regStatus) {
            log_flush_error();
            status = SYSTEM_ERROR;
        }
        else {
            /*
             * The path must reference a node.
             */
            RegNode*    node;

            log_clear();

            if (0 != (regStatus = reg_getNode(path, &node, 0))) {
                if (ENOENT == regStatus) {
                    if (!quiet) {
                        log_error("No such value or node: \"%s\"", path);
                    }
                    status = NO_SUCH_ENTRY;
                }
                else {
                    log_flush_error();
                    status = SYSTEM_ERROR;
                }
            }                           /* didn't get node */
            else {
                _pathPrefix = path;

                if (0 != reg_visitNodes(node, printNodeValues)) {
                    log_flush_error();
                    status = SYSTEM_ERROR;
                }                       /* error visiting nodes */
            }                           /* got node */
        }                               /* no such value */
    }                                   /* didn't get value-string */

    return status;
}
コード例 #2
0
/*
 * Creates the registry.
 *
 * Returns:
 *      0               Success.
 *      SYSTEM_ERROR    System error.  "log_flush()" called.
 */
static Status createRegistry(void)
{
    RegNode*    rootNode;

    log_debug("Creating registry");

    if (0 != reg_getNode("/", &rootNode, 1)) {
        log_error("Couldn't create registry");
        return SYSTEM_ERROR;
    }

    return 0;
}
コード例 #3
0
/*
 * Removes the values referenced by an absolute registry pathname.
 *
 * Arguments:
 *      path            Pointer to a an absolute registry pathname.  Shall not
 *                      be NULL.  If the pathname refers to a node, then the
 *                      node and all its subnodes are recursively removed.
 *      quiet           Whether or not to be quiet about a pathname not
 *                      existing.
 * Returns:
 *      0               Success.
 *      NO_SUCH_ENTRY   No such entry in the registry.  "log_flush()" called iff
 *                      "quiet == 0".
 *      SYSTEM_ERROR    System error.  "log_flush()" called.
 */
static Status deletePath(
    const char* const   path,
    const int           quiet)
{
    log_debug("%s deleting path \"%s\"", quiet ? "Quietly" : "Non-quietly", path);

    switch (reg_deleteValue(path)) {
        case 0:
            return 0;

        case ENOENT: {
            RegNode*        node;

            switch (reg_getNode(path, &node, 0)) {
                case 0:
                    reg_deleteNode(node);

                    if (reg_flushNode(node)) {
                        log_flush_error();
                        return SYSTEM_ERROR;
                    }

                    return 0;
                case ENOENT:
                    if (!quiet) {
                        log_error("No such value or node: \"%s\"", path);
                    }
                    return NO_SUCH_ENTRY;
                default:
                    log_flush_error();
                    return SYSTEM_ERROR;
            }
        }
        /* FALLTHROUGH */
        /* no break */

        default:
            log_flush_error();
            return SYSTEM_ERROR;
    }
}
コード例 #4
0
static void
test_regNode(void)
{
    RegStatus   status;
    RegNode*    testnode;
    RegNode*    subnode;
    char*       string;
    const char* constString;
    unsigned    uint;
    int         boolean;
    timestampt  time;
    signaturet  defSig1 = {0};
    signaturet  defSig2 = {1};
    signaturet  sig;

    status = reg_getNode("/test_node/subnode", &subnode, 1);
    if (status) {
        log_error("test_regNode(): Couldn't get subnode");
    }
    else {
        CU_ASSERT_EQUAL_FATAL(status, 0);
        CU_ASSERT_PTR_NOT_NULL(subnode);
    }

    constString = reg_getNodeName(subnode);
    CU_ASSERT_STRING_EQUAL(constString, "subnode");

    constString = reg_getNodeAbsPath(subnode);
    CU_ASSERT_STRING_EQUAL(constString, "/test_node/subnode");

    status = reg_getNodeString(subnode, "string_key", &string);
    if (status && ENOENT != status) {
        log_error("test_regNode(): Couldn't verify non-existant subnode string");
    }
    else {
        CU_ASSERT_EQUAL(status, ENOENT);
    }

    status = reg_putNodeString(subnode, "string_key", "string value");
    if (0 != status) {
        log_error("test_regNode(): Couldn't add subnode string");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
    }

    status = reg_getNodeString(subnode, "string_key", &string);
    if (status) {
        log_error("test_regNode(): Couldn't get subnode string");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
        CU_ASSERT_STRING_EQUAL(string, "string value");
        free(string);
    }

    status = reg_getNodeBool(subnode, "bool_key", &boolean);
    if (status && ENOENT != status) {
        log_error("test_regNode(): Couldn't verify non-existant subnode boolean");
    }
    else {
        CU_ASSERT_EQUAL(status, ENOENT);
    }

    status = reg_putNodeBool(subnode, "bool_key", 1);
    if (0 != status) {
        log_error("test_regNode(): Couldn't put subnode boolean");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
    }

    status = reg_getNodeBool(subnode, "bool_key", &boolean);
    if (status) {
        log_error("test_regNode(): Couldn't get subnode boolean");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
        CU_ASSERT_EQUAL(boolean, 1);
    }

    status = reg_getNodeUint(subnode, "uint_key", &uint);
    if (status && ENOENT != status) {
        log_error("test_regNode(): Couldn't verify non-existant subnode int");
    }
    else {
        CU_ASSERT_EQUAL(status, ENOENT);
    }

    status = reg_putNodeUint(subnode, "uint_key", 5);
    if (0 != status) {
        log_error("test_regNode(): Couldn't put subnode int");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
    }

    status = reg_getNodeUint(subnode, "uint_key", &uint);
    if (status) {
        log_error("test_regNode(): Couldn't get subnode int");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
        CU_ASSERT_EQUAL(uint, 5);
    }

    status = reg_getNodeTime(subnode, "time_key", &time);
    if (status && ENOENT != status) {
        log_error("test_regNode(): Couldn't verify non-existant subnode time");
    }
    else {
        CU_ASSERT_EQUAL(status, ENOENT);
    }

    status = reg_putNodeTime(subnode, "time_key", &TS_ZERO);
    if (0 != status) {
        log_error("test_regNode(): Couldn't put subnode time");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
    }

    status = reg_getNodeTime(subnode, "time_key", &time);
    if (status) {
        log_error("test_regNode(): Couldn't get subnode time");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
        CU_ASSERT_EQUAL(d_diff_timestamp(&time, &TS_ZERO), 0.0);
    }

    status = reg_getNodeSignature(subnode, "sig_key", &sig);
    if (status && ENOENT != status) {
        log_error("test_regNode(): Couldn't verify non-existant subnode sig");
    }
    else {
        CU_ASSERT_EQUAL(status, ENOENT);
    }

    status = reg_putNodeSignature(subnode, "sig_key", defSig2);
    if (0 != status) {
        log_error("test_regNode(): Couldn't put subnode sig");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
    }

    status = reg_getNodeSignature(subnode, "sig_key", &sig);
    if (status) {
        log_error("test_regNode(): Couldn't get subnode sig");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
        CU_ASSERT_EQUAL(memcmp(sig, defSig2, sizeof(signaturet)), 0);
    }

    status = reg_deleteNodeValue(subnode, "non-existant_key");
    if (status && ENOENT != status) {
        log_error("test_regNode(): Couldn't verify non-existant subnode value deletion");
    }
    else {
        CU_ASSERT_EQUAL(status, ENOENT);
    }

    status = reg_deleteNodeValue(subnode, "string_key");
    if (status) {
        log_error("test_regNode(): Couldn't delete subnode value");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
    }

    status = reg_getNodeString(subnode, "string_key", &string);
    if (status && ENOENT != status) {
        log_error("test_regNode(): Couldn't verify non-existant subnode string");
    }
    else {
        CU_ASSERT_EQUAL(status, ENOENT);
    }

    status = reg_getNode("/test_node", &testnode, 1);
    if (status) {
        log_error("test_regNode(): Couldn't get subnode");
    }
    else {
        CU_ASSERT_EQUAL_FATAL(status, 0);
        CU_ASSERT_PTR_NOT_NULL(testnode);
    }

    status = reg_flushNode(testnode);
    if (status) {
        log_error("test_regNode(): Couldn't flush node");
    }
    else {
        CU_ASSERT_EQUAL(status, 0);
    }

    rn_free(testnode);

    {
        status = reg_getNode("/test_node2", &testnode, 1);
        if (status) {
            log_error("test_regNode(): Couldn't get temporary node");
        }
        else {
            CU_ASSERT_EQUAL_FATAL(status, 0);
            CU_ASSERT_PTR_NOT_NULL(testnode);
        }

        status = reg_putNodeString(testnode, "string_key", "string value");
        if (0 != status) {
            log_error("test_regNode(): Couldn't add temporary node string");
        }
        else {
            CU_ASSERT_EQUAL(status, 0);
        }

        status = reg_flushNode(testnode);
        if (status) {
            log_error("test_regNode(): Couldn't flush temporary node");
        }
        else {
            CU_ASSERT_EQUAL(status, 0);
        }

        reg_deleteNode(testnode);

        status = reg_flushNode(testnode);
        if (status) {
            log_error("test_regNode(): Couldn't delete temporary node");
        }
        else {
            CU_ASSERT_EQUAL(status, 0);
        }

        rn_free(testnode);

        status = reg_getNode("/test_node2", &testnode, 0);
        if (status && ENOENT != status) {
            log_error("test_regNode(): Couldn't verify temporary node deletion");
        }
        else {
            CU_ASSERT_EQUAL(status, ENOENT);
        }
    }
}