TEST(PersistentStorageHandlerTest, PersistentStorageValidHandlers)
{
    OCPersistentStorage *psi = SRMGetPersistentStorageHandler();
    EXPECT_TRUE(psi != NULL);

    unsigned char buf[PATH_MAX];
    FILE* streamIn = NULL;
    FILE* streamOut = NULL;
    struct passwd *pw = getpwuid(getuid());
    const char *homeDir = pw->pw_dir;
    char inFilePath [PATH_MAX];
    char outFilePath [PATH_MAX];
    snprintf(inFilePath, PATH_MAX, "%s/iotivity/Readme.scons.txt", homeDir );
    snprintf(outFilePath, PATH_MAX, "%s/Downloads/Readme.scons.out.txt", homeDir );

    streamIn = psi->open(inFilePath, "r");
    streamOut = psi->open(outFilePath, "w");

    if (streamIn && streamOut)
    {
        size_t value = 1;
        while (value)
        {
            value = psi->read(buf, 1, sizeof(buf), streamIn);
            psi->write(buf, 1, value, streamOut);
        }
    }

    if (streamIn)
    {
        psi->close(streamIn);
    }
    if (streamOut)
    {
        psi->close(streamOut);
    }
    psi->unlink(outFilePath);
}
Esempio n. 2
0
/**
 * Gets the Secure Virtual Database from the Persistent Storage
 *
 * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
 * @param data - pointer of the returned Secure Virtual Resource(s)
 * @param size - pointer of the returned size of Secure Virtual Resource(s)
 *
 * @return OCStackResult - result of getting Secure Virtual Resource(s)
 */
OCStackResult GetSecureVirtualDatabaseFromPS(const char *rsrcName, uint8_t **data, size_t *size)
{
    OIC_LOG(DEBUG, TAG, "GetSecureVirtualDatabaseFromPS IN");
    if (!data || *data || !size)
    {
        return OC_STACK_INVALID_PARAM;
    }

    FILE *fp = NULL;
    uint8_t *fsData = NULL;
    size_t fileSize = 0;
    OCStackResult ret = OC_STACK_ERROR;

    OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
    VERIFY_NON_NULL(TAG, ps, ERROR);

    fileSize = GetSVRDatabaseSize(ps);
    OIC_LOG_V(DEBUG, TAG, "File Read Size: %zu", fileSize);
    if (fileSize)
    {
        fsData = (uint8_t *) OICCalloc(1, fileSize);
        VERIFY_NON_NULL(TAG, fsData, ERROR);

        fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
        VERIFY_NON_NULL(TAG, fp, ERROR);
        if (ps->read(fsData, 1, fileSize, fp) == fileSize)
        {
            if (rsrcName)
            {
                CborParser parser;  // will be initialized in |cbor_parser_init|
                CborValue cbor;     // will be initialized in |cbor_parser_init|
                cbor_parser_init(fsData, fileSize, 0, &parser, &cbor);
                CborValue cborValue = {0};
                CborError cborFindResult = cbor_value_map_find_value(&cbor, rsrcName, &cborValue);
                if (CborNoError == cborFindResult && cbor_value_is_byte_string(&cborValue))
                {
                    cborFindResult = cbor_value_dup_byte_string(&cborValue, data, size, NULL);
                    VERIFY_SUCCESS(TAG, CborNoError==cborFindResult, ERROR);
                    ret = OC_STACK_OK;
                }
                // in case of |else (...)|, svr_data not found
            }
            // return everything in case rsrcName is NULL
            else
            {
                *size = fileSize;
                *data = (uint8_t *) OICCalloc(1, fileSize);
                VERIFY_NON_NULL(TAG, *data, ERROR);
                memcpy(*data, fsData, fileSize);
                ret = OC_STACK_OK;
            }
        }
    }
    OIC_LOG(DEBUG, TAG, "GetSecureVirtualDatabaseFromPS OUT");

exit:
    if (fp)
    {
        ps->close(fp);
    }
    OICFree(fsData);
    return ret;
}