nativeapi_kv_impl.c 4.9 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "nativeapi_kv_impl.h"
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <securec.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "nativeapi_config.h"

static char g_kvFolder[FILE_NAME_MAX_LEN + 1] = {0};

L
l00518380 已提交
30
static bool IsValidValue(const char* value)
W
wenjun 已提交
31
{
L
l00518380 已提交
32 33 34 35 36
    if (value == NULL) {
        return false;
    }
    size_t valueLen = strnlen(value, VALUE_MAX_LEN + 1);
    if ((valueLen == 0) || (valueLen > VALUE_MAX_LEN)) {
W
wenjun 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        return false;
    }
    return true;
}

static int GetKvFolder(const char* dataPath)
{
    if (dataPath == NULL) {
        return ERROR_CODE_PARAM;
    }
    if (memset_s(g_kvFolder, sizeof(g_kvFolder), 0x0, sizeof(g_kvFolder)) != EOK) {
        return ERROR_CODE_GENERAL;
    }
    if (sprintf_s(g_kvFolder, sizeof(g_kvFolder), "%s/%s", dataPath, DEFAULT_FOLDER_PATH) < 0) {
        return ERROR_CODE_GENERAL;
    }
    return NATIVE_SUCCESS;
}

static int GetRealPath(const char* originPath, char* trustPath, size_t tPathLen)
{
    if (realpath(originPath, trustPath) != NULL) {
        return NATIVE_SUCCESS;
    }
    if (errno == ENOENT) {
        if (strncpy_s(trustPath, tPathLen, originPath, strlen(originPath)) == EOK) {
            return NATIVE_SUCCESS;
        }
    }
    return ERROR_CODE_GENERAL;
}

int InitKv(const char* dataPath)
{
    int ret = GetKvFolder(dataPath);
    if (ret != NATIVE_SUCCESS) {
        return ret;
    }
    if (access(g_kvFolder, F_OK) == F_OK) {
        return NATIVE_SUCCESS;
    }
    if (mkdir(g_kvFolder, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
        return ERROR_CODE_GENERAL;
    }
    return NATIVE_SUCCESS;
}

int GetValue(const char* key, char* value)
{
    if ((key == NULL) || (value == NULL)) {
        return ERROR_CODE_PARAM;
    }
    char* resolvePath = (char *)malloc(PATH_MAX);
    if (resolvePath == NULL) {
        return ERROR_CODE_GENERAL;
    }
    if (GetRealPath(key, resolvePath, PATH_MAX) != NATIVE_SUCCESS) {
        free(resolvePath);
        return ERROR_CODE_GENERAL;
    }
    int fileHandle = open(resolvePath, O_RDONLY, S_IRUSR);
    free(resolvePath);
    resolvePath = NULL;
    if (fileHandle < 0) {
        return (-errno);
    }
    int readLen = read(fileHandle, value, VALUE_MAX_LEN);
    close(fileHandle);
    fileHandle = -1;
    if (readLen < 0) {
        return ERROR_CODE_IO;
    }
    value[readLen] = '\0';
    return NATIVE_SUCCESS;
}

int SetValue(const char* key, const char* value)
{
    if ((key == NULL) || (!IsValidValue(value))) {
        return ERROR_CODE_PARAM;
    }
    char* resolvePath = (char *)malloc(PATH_MAX);
    if (resolvePath == NULL) {
        return ERROR_CODE_GENERAL;
    }
    if (GetRealPath(key, resolvePath, PATH_MAX) != NATIVE_SUCCESS) {
        free(resolvePath);
        return ERROR_CODE_GENERAL;
    }
    int fd = open(resolvePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    free(resolvePath);
    resolvePath = NULL;
    if (fd < 0) {
        return (-errno);
    }
132
    int ret = write(fd, value, strlen(value));
W
wenjun 已提交
133
    close(fd);
134
    return (ret < 0) ? ERROR_CODE_IO : NATIVE_SUCCESS;
W
wenjun 已提交
135 136 137 138 139 140 141
}

int DeleteValue(const char* key)
{
    if (key == NULL) {
        return ERROR_CODE_PARAM;
    }
142
    if (unlink(key) != NATIVE_SUCCESS) {
W
wenjun 已提交
143 144 145 146 147 148 149 150 151 152 153 154
        return (-errno);
    }
    return NATIVE_SUCCESS;
}

int ClearKVStore(const char* dataPath)
{
    int ret = GetKvFolder(dataPath);
    if (ret != NATIVE_SUCCESS) {
        return ret;
    }
    ret = ERROR_CODE_GENERAL;
L
l00518380 已提交
155
    DIR* fileDir = opendir(g_kvFolder);
W
wenjun 已提交
156 157 158
    if (fileDir == NULL) {
        return ret;
    }
L
l00518380 已提交
159
    struct dirent* dir = readdir(fileDir);
W
wenjun 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    char* fullPath = (char *)malloc(FILE_NAME_MAX_LEN + 1);
    if (fullPath == NULL) {
        goto EXIT;
    }
    while (dir != NULL) {
        if (memset_s(fullPath, FILE_NAME_MAX_LEN + 1, 0x0, FILE_NAME_MAX_LEN + 1) != EOK) {
            goto EXIT;
        }
        if (sprintf_s(fullPath, FILE_NAME_MAX_LEN + 1, "%s/%s", g_kvFolder, dir->d_name) < 0) {
            goto EXIT;
        }
        if (unlink(fullPath) != 0) {
            goto EXIT;
        }
        dir = readdir(fileDir);
    }
    ret = NATIVE_SUCCESS;
EXIT:
    free(fullPath);
    closedir(fileDir);
    return ret;
}