diff --git a/services/huks_standard/huks_service/main/core/src/hks_storage.c b/services/huks_standard/huks_service/main/core/src/hks_storage.c index facbd238..6b9663bd 100644 --- a/services/huks_standard/huks_service/main/core/src/hks_storage.c +++ b/services/huks_standard/huks_service/main/core/src/hks_storage.c @@ -391,19 +391,19 @@ static int32_t HksStorageWriteFile( #endif } -static uint32_t HksStorageReadFile( - const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +static int32_t HksStorageReadFile( + const char *path, const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size) { #ifdef HKS_SUPPORT_THREAD HksStorageFileLock *lock = CreateStorageFileLock(path, fileName); HksStorageFileLockRead(lock); - uint32_t size = HksFileRead(path, fileName, offset, buf, len); + int32_t ret = HksFileRead(path, fileName, offset, blob, size); HksStorageFileUnlockRead(lock); HksStorageFileLockRelease(lock); #else - uint32_t size = HksFileRead(path, fileName, offset, buf, len); + int32_t ret = HksFileRead(path, fileName, offset, blob, size); #endif - return size; + return ret; } #ifdef HKS_ENABLE_CLEAN_FILE @@ -508,11 +508,17 @@ static int32_t CopyKeyBlobFromSrc(const char *srcPath, const char *srcFileName, (void)memset_s(buffer, size, 0, size); + struct HksBlob blob = { .size = size, .data = buffer }; + int32_t ret; do { - size = HksStorageReadFile(srcPath, srcFileName, 0, buffer, size); - if (size == 0) { - HKS_LOG_E("read file failed, ret = %" LOG_PUBLIC "u.", size); + ret = HksStorageReadFile(srcPath, srcFileName, 0, &blob, &size); + if (ret != HKS_SUCCESS) { + if (ret == HKS_ERROR_NO_PERMISSION) { + HKS_LOG_E("Check Permission failed, ret = %" LOG_PUBLIC "d.", ret); + break; + } + HKS_LOG_E("read file failed, ret = %" LOG_PUBLIC "d.", ret); ret = HKS_ERROR_READ_FILE_FAIL; break; } @@ -567,12 +573,15 @@ static int32_t GetKeyBlobFromFile(const char *path, const char *fileName, struct return HKS_ERROR_INSUFFICIENT_DATA; } - size = HksStorageReadFile(path, fileName, 0, keyBlob->data, keyBlob->size); - if (size == 0) { - HKS_LOG_E("file read failed, ret = %" LOG_PUBLIC "u.", size); + int32_t ret = HksStorageReadFile(path, fileName, 0, keyBlob, &size); + if (ret != HKS_SUCCESS) { + if (ret == HKS_ERROR_NO_PERMISSION) { + HKS_LOG_E("Check Permission failed, ret = %" LOG_PUBLIC "d.", ret); + return ret; + } + HKS_LOG_E("read file failed, ret = %" LOG_PUBLIC "d.", ret); return HKS_ERROR_READ_FILE_FAIL; } - keyBlob->size = size; return HKS_SUCCESS; } diff --git a/services/huks_standard/huks_service/main/core/src/hks_storage_lite.c b/services/huks_standard/huks_service/main/core/src/hks_storage_lite.c index c947b220..c530bfcd 100644 --- a/services/huks_standard/huks_service/main/core/src/hks_storage_lite.c +++ b/services/huks_standard/huks_service/main/core/src/hks_storage_lite.c @@ -177,8 +177,10 @@ static int32_t FreshImageBuffer(const char *fileName) uint8_t *buf = (uint8_t *)HksMalloc(totalLen); HKS_IF_NULL_RETURN(buf, HKS_ERROR_MALLOC_FAIL) - fileLen = HksFileRead(HKS_KEY_STORE_PATH, fileName, offset, buf, totalLen); - if (fileLen == 0) { + struct HksBlob blob = { .size = totalLen, .data = buf }; + + int32_t ret = HksFileRead(HKS_KEY_STORE_PATH, fileName, offset, &blob, &fileLen); + if (ret != HKS_SUCCESS) { HKS_FREE_PTR(buf); return HKS_ERROR_READ_FILE_FAIL; } @@ -236,13 +238,13 @@ static int32_t LoadFileToBuffer(const char *fileName) { /* 1. read key info header */ uint32_t offset = HksGetStoreFileOffset(); - uint32_t len = HksFileRead(HKS_KEY_STORE_PATH, fileName, offset, - g_storageImageBuffer.data, g_storageImageBuffer.size); + uint32_t len = 0; + int32_t ret = HksFileRead(HKS_KEY_STORE_PATH, fileName, offset, + &g_storageImageBuffer, &len); - int32_t ret; do { /* 2. file not exist or read nothing, init image */ - if (len == 0) { + if (ret != HKS_SUCCESS) { HKS_LOG_I("file not exist, init buffer."); ret = InitImageBuffer(); HKS_IF_NOT_SUCC_BREAK(ret) /* init fail, need free global buf */ @@ -582,9 +584,10 @@ static int32_t GetRootMaterial(const struct HksBlob *name, struct HksBlob *buffe int32_t ret = GetFileName(name, &fileName); HKS_IF_NOT_SUCC_RETURN(ret, ret) - uint32_t len = HksFileRead(HKS_KEY_STORE_PATH, fileName, 0, buffer->data, buffer->size); + uint32_t len = 0; + ret = HksFileRead(HKS_KEY_STORE_PATH, fileName, 0, buffer, &len); HKS_FREE_PTR(fileName); - if (len == 0) { + if (ret != HKS_SUCCESS) { return HKS_ERROR_READ_FILE_FAIL; } return HKS_SUCCESS; diff --git a/test/unittest/huks_common_test/include/hks_test_file_operator_c.h b/test/unittest/huks_common_test/include/hks_test_file_operator_c.h index b1754142..83cde193 100644 --- a/test/unittest/huks_common_test/include/hks_test_file_operator_c.h +++ b/test/unittest/huks_common_test/include/hks_test_file_operator_c.h @@ -29,12 +29,13 @@ #include "securec.h" #define HKS_MAX_FILE_NAME_LEN 512 +#define REQUIRED_KEY_NOT_AVAILABLE 126 int32_t GetFileName(const char *path, const char *fileName, char *fullFileName, uint32_t fullFileNameLen); int32_t IsFileExist(const char *fileName); -uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len); +int32_t FileRead(const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size); uint32_t FileSize(const char *fileName); diff --git a/test/unittest/huks_common_test/src/hks_test_file_operator_func.c b/test/unittest/huks_common_test/src/hks_test_file_operator_func.c index 7c256f64..9411a9c8 100644 --- a/test/unittest/huks_common_test/src/hks_test_file_operator_func.c +++ b/test/unittest/huks_common_test/src/hks_test_file_operator_func.c @@ -50,33 +50,37 @@ int32_t IsFileExist(const char *fileName) return HKS_SUCCESS; } -uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +int32_t FileRead(const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size) { (void)offset; if (IsFileExist(fileName) != HKS_SUCCESS) { - return 0; + return HKS_ERROR_NOT_EXIST; } char filePath[PATH_MAX + 1] = {0}; (void)realpath(fileName, filePath); if (strstr(filePath, "../") != NULL) { HKS_TEST_LOG_E("invalid filePath, path %s", filePath); - return 0; + return HKS_ERROR_INVALID_ARGUMENT; } FILE *fp = fopen(filePath, "rb"); - if (fp == NULL) { - HKS_TEST_LOG_E("failed to open file"); - return 0; + if (fp == NULL) { + if (errno == REQUIRED_KEY_NOT_AVAILABLE) { + HKS_TEST_LOG_E("Check Permission failed!"); + return HKS_ERROR_NO_PERMISSION; + } + HKS_TEST_LOG_E("open file fail, errno = 0x%x", errno); + return HKS_ERROR_OPEN_FILE_FAIL; } - uint32_t size = fread(buf, 1, len, fp); + uint32_t len = fread(blob->data, 1, blob->size, fp); if (fclose(fp) < 0) { HKS_TEST_LOG_E("failed to close file"); - return 0; + return HKS_ERROR_CLOSE_FILE_FAIL; } - - return size; + *size = len; + return HKS_SUCCESS; } uint32_t FileSize(const char *fileName) diff --git a/test/unittest/huks_lite_test/common/hks_test_file_operator.c b/test/unittest/huks_lite_test/common/hks_test_file_operator.c index e93f38f5..5e942706 100644 --- a/test/unittest/huks_lite_test/common/hks_test_file_operator.c +++ b/test/unittest/huks_lite_test/common/hks_test_file_operator.c @@ -66,7 +66,7 @@ int32_t HksTestIsFileExist(const char *path, const char *fileName) return ret; } #else -static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +static int32_t FileRead(const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size) { /* now offset is 0, but we maybe extend hi1131 file interfaces in the future */ if (offset != 0) { @@ -77,28 +77,27 @@ static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, ui int32_t ret = UtilsFileStat(fileName, &fileSize); if (ret < 0) { HKS_TEST_LOG_E("stat file failed, errno = 0x%x", ret); - return 0; + return HKS_ERROR_INVALID_ARGUMENT; } - if (len > fileSize) { - HKS_TEST_LOG_E("read data over file size!\n, file size = %d\n, buf len = %d\n", fileSize, len); - return 0; + if (blob->size > fileSize) { + HKS_TEST_LOG_E("read data over file size!\n, file size = %d\n, buf len = %d\n", fileSize, blob->size); + return HKS_ERROR_INVALID_ARGUMENT; } int fd = UtilsFileOpen(fileName, O_RDONLY_FS, 0); if (fd < 0) { - HKS_TEST_LOG_E("failed to open file, errno = 0x%x", fd); - return 0; + HKS_LOG_E("open file fail, errno = 0x%" LOG_PUBLIC "x", fd); + return HKS_ERROR_OPEN_FILE_FAIL; } - - ret = UtilsFileRead(fd, buf, len); + ret = UtilsFileRead(fd, blob->data, blob->size); UtilsFileClose(fd); if (ret < 0) { HKS_TEST_LOG_E("failed to read file, errno = 0x%x", ret); - return 0; + return HKS_ERROR_READ_FILE_FAIL; } - - return len; + *size = blob->size; + return HKS_SUCCESS; } static int32_t FileWrite(const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len) @@ -152,21 +151,21 @@ int32_t HksTestFileRemove(const char *path, const char *fileName) } #endif -uint32_t HksTestFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +int32_t HksTestFileRead(const char *path, const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size) { - if ((fileName == NULL) || (buf == NULL) || (len == 0)) { - return 0; + if ((fileName == NULL) || (blob == NULL) || (blob->data == NULL) || (blob->size == 0) || (size == NULL)) { + return HKS_ERROR_INVALID_ARGUMENT; } char *fullFileName = NULL; int32_t ret = GetFullFileName(path, fileName, &fullFileName); if (ret != HKS_SUCCESS) { - return 0; + return ret; } - uint32_t size = FileRead(fullFileName, offset, buf, len); + ret = FileRead(fullFileName, offset, blob, size); HksTestFree(fullFileName); - return size; + return ret; } int32_t HksTestFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len) diff --git a/test/unittest/huks_lite_test/common/include/hks_test_file_operator.h b/test/unittest/huks_lite_test/common/include/hks_test_file_operator.h index b84a8a51..96677d74 100644 --- a/test/unittest/huks_lite_test/common/include/hks_test_file_operator.h +++ b/test/unittest/huks_lite_test/common/include/hks_test_file_operator.h @@ -26,7 +26,7 @@ int32_t HksTestIsFileExist(const char *path, const char *fileName); uint32_t HksTestFileSize(const char *path, const char *fileName); -uint32_t HksTestFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len); +int32_t HksTestFileRead(const char *path, const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size); int32_t HksTestFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len); diff --git a/test/unittest/huks_standard_test/interface_inner_test/sdk_test/include/hks_test_file_operator.h b/test/unittest/huks_standard_test/interface_inner_test/sdk_test/include/hks_test_file_operator.h index 136808c8..d1143b14 100644 --- a/test/unittest/huks_standard_test/interface_inner_test/sdk_test/include/hks_test_file_operator.h +++ b/test/unittest/huks_standard_test/interface_inner_test/sdk_test/include/hks_test_file_operator.h @@ -26,7 +26,7 @@ int32_t HksIsFileExist(const char *path, const char *fileName); uint32_t HksFileSize(const char *path, const char *fileName); -uint32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len); +int32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size); int32_t HksFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len); diff --git a/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_modify_key_test.cpp b/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_modify_key_test.cpp index 528c679f..2c84089a 100644 --- a/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_modify_key_test.cpp +++ b/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_modify_key_test.cpp @@ -68,10 +68,12 @@ static int32_t ModifyKey(struct HksBlob *keyAlias) if (bufOne == nullptr) { return HKS_ERROR_MALLOC_FAIL; } - uint32_t sizeRead = HksFileRead(g_storePath, (char *)keyAlias->data, 0, bufOne, sizeOne); + struct HksBlob blobOne = { .size = sizeOne, .data = bufOne }; + uint32_t sizeRead = 0; + int32_t ret = HksFileRead(g_storePath, (char *)keyAlias->data, 0, &blobOne, &sizeRead); (void)memset_s(bufOne, sizeRead, 0, sizeRead); - int32_t ret = HksFileWrite(g_storePath, (char *)keyAlias->data, 0, bufOne, sizeOne); + ret = HksFileWrite(g_storePath, (char *)keyAlias->data, 0, bufOne, sizeOne); HksTestFree(bufOne); return ret; diff --git a/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_safe_cipher_key_test.cpp b/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_safe_cipher_key_test.cpp index 97a8bb36..9541c80c 100644 --- a/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_safe_cipher_key_test.cpp +++ b/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_safe_cipher_key_test.cpp @@ -66,7 +66,9 @@ static int32_t CompareTwoKey(const struct HksBlob *keyAliasOne, const struct Hks if (bufOne == nullptr) { return HKS_ERROR_MALLOC_FAIL; } - uint32_t sizeRead = HksFileRead(g_storePath, (char *)keyAliasOne->data, 0, bufOne, sizeOne); + struct HksBlob blobOne = { .size = sizeOne, .data = bufOne }; + uint32_t sizeRead = 0; + int32_t ret = HksFileRead(g_storePath, (char *)keyAliasOne->data, 0, &blobOne, &sizeRead); uint32_t sizeTwo = HksFileSize(g_storePath, (char *)keyAliasTwo->data); uint8_t *bufTwo = (uint8_t *)HksTestMalloc(sizeTwo); @@ -74,8 +76,9 @@ static int32_t CompareTwoKey(const struct HksBlob *keyAliasOne, const struct Hks HksTestFree(bufOne); return HKS_ERROR_MALLOC_FAIL; } - sizeRead = HksFileRead(g_storePath, (char *)keyAliasTwo->data, 0, bufTwo, sizeOne); - int32_t ret = memcmp(bufOne, bufTwo, sizeRead); + struct HksBlob blobTwo = { .size = sizeOne, .data = bufTwo }; + ret = HksFileRead(g_storePath, (char *)keyAliasTwo->data, 0, &blobTwo, &sizeRead); + ret = memcmp(bufOne, bufTwo, sizeRead); HksTestFree(bufOne); HksTestFree(bufTwo); return ret; diff --git a/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_safe_compare_key_test.cpp b/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_safe_compare_key_test.cpp index 9aac2900..a37ae0df 100644 --- a/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_safe_compare_key_test.cpp +++ b/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_safe_compare_key_test.cpp @@ -126,7 +126,9 @@ static int32_t CompareKeyData(struct HksBlob *keyAliasOne, struct HksBlob *keyAl if (bufOne == nullptr) { return HKS_ERROR_MALLOC_FAIL; } - [[maybe_unused]] uint32_t sizeRead = HksFileRead(g_storePath, (char *)keyAliasOne->data, 0, bufOne, sizeOne); + struct HksBlob blobOne = { .size = sizeOne, .data = bufOne }; + [[maybe_unused]] uint32_t sizeRead = 0; + int32_t ret = HksFileRead(g_storePath, (char *)keyAliasOne->data, 0, &blobOne, &sizeRead); uint32_t sizeTwo = HksFileSize(g_storePath, (char *)keyAliasTwo->data); uint8_t *bufTwo = (uint8_t *)HksTestMalloc(sizeTwo); @@ -134,8 +136,9 @@ static int32_t CompareKeyData(struct HksBlob *keyAliasOne, struct HksBlob *keyAl HksTestFree(bufOne); return HKS_ERROR_MALLOC_FAIL; } - sizeRead = HksFileRead(g_storePath, (char *)keyAliasTwo->data, 0, bufTwo, sizeOne); - int32_t ret = memcmp(bufOne, bufTwo, sizeOne); + struct HksBlob blobTwo = { .size = sizeOne, .data = bufTwo }; + ret = HksFileRead(g_storePath, (char *)keyAliasTwo->data, 0, &blobTwo, &sizeRead); + ret = memcmp(bufOne, bufTwo, sizeOne); HksTestFree(bufOne); HksTestFree(bufTwo); return ret; diff --git a/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_test_file_operator.c b/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_test_file_operator.c index 667bc100..3086369e 100644 --- a/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_test_file_operator.c +++ b/test/unittest/huks_standard_test/interface_inner_test/sdk_test/src/hks_test_file_operator.c @@ -54,21 +54,21 @@ int32_t HksIsFileExist(const char *path, const char *fileName) return ret; } -uint32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +int32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size) { char *fullFileName = NULL; - if ((fileName == NULL) || (buf == NULL) || (len == 0)) { - return 0; + if ((fileName == NULL) || (blob == NULL) || (blob->data == NULL) || (blob->size == 0) || (size == NULL)) { + return HKS_ERROR_INVALID_ARGUMENT; } int32_t ret = GetFullFileName(path, fileName, &fullFileName); if (ret != HKS_SUCCESS) { - return 0; + return ret; } - uint32_t size = FileRead(fullFileName, offset, buf, len); + ret = FileRead(fullFileName, offset, blob, size); HKS_FREE_PTR(fullFileName); - return size; + return ret; } int32_t HksFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len) diff --git a/test/unittest/huks_standard_test/module_test/utils_test/src/hks_file_operator_test.cpp b/test/unittest/huks_standard_test/module_test/utils_test/src/hks_file_operator_test.cpp index 7867e2dd..6344a77d 100644 --- a/test/unittest/huks_standard_test/module_test/utils_test/src/hks_file_operator_test.cpp +++ b/test/unittest/huks_standard_test/module_test/utils_test/src/hks_file_operator_test.cpp @@ -88,8 +88,8 @@ HWTEST_F(HksFileOperatorTest, HksFileOperatorTest002, TestSize.Level0) HWTEST_F(HksFileOperatorTest, HksFileOperatorTest003, TestSize.Level0) { HKS_LOG_I("enter HksFileOperatorTest003"); - int32_t ret = HksFileRead(nullptr, nullptr, 0, nullptr, 0); - ASSERT_EQ(ret, 0) << "HksFileRead failed, ret = " << ret; + int32_t ret = HksFileRead(nullptr, nullptr, 0, nullptr, nullptr); + ASSERT_EQ(ret, HKS_ERROR_INVALID_ARGUMENT) << "HksFileRead failed, ret = " << ret; } /** diff --git a/utils/file_operator/hks_file_operator.c b/utils/file_operator/hks_file_operator.c index 3808eabb..e584fb88 100644 --- a/utils/file_operator/hks_file_operator.c +++ b/utils/file_operator/hks_file_operator.c @@ -85,32 +85,39 @@ static int32_t IsFileExist(const char *fileName) return HKS_SUCCESS; } -static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +static int32_t FileRead(const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size) { (void)offset; - HKS_IF_NOT_SUCC_RETURN(IsFileExist(fileName), 0) + HKS_IF_NOT_SUCC_RETURN(IsFileExist(fileName), HKS_ERROR_NOT_EXIST) if (strstr(fileName, "../") != NULL) { HKS_LOG_E("invalid filePath, path %" LOG_PUBLIC "s", fileName); - return 0; + return HKS_ERROR_INVALID_ARGUMENT; } char filePath[PATH_MAX + 1] = {0}; if (realpath(fileName, filePath) == NULL) { HKS_LOG_E("invalid filePath, path %" LOG_PUBLIC "s", fileName); - return 0; + return HKS_ERROR_INVALID_ARGUMENT; } FILE *fp = fopen(filePath, "rb"); - HKS_IF_NULL_LOGE_RETURN(fp, 0, "failed to open file, errno = 0x%" LOG_PUBLIC "x", errno) - - uint32_t size = fread(buf, 1, len, fp); - if (fclose(fp) < 0) { - HKS_LOG_E("failed to close file, errno = 0x%" LOG_PUBLIC "x", errno); - return 0; + if (fp == NULL) { + if (errno == REQUIRED_KEY_NOT_AVAILABLE) { + HKS_LOG_E("Check Permission failed!"); + return HKS_ERROR_NO_PERMISSION; + } + HKS_LOG_E("open file fail, errno = 0x%" LOG_PUBLIC "x", errno); + return HKS_ERROR_OPEN_FILE_FAIL; } - return size; + uint32_t len = fread(blob->data, 1, blob->size, fp); + if (fclose(fp) < 0) { + HKS_LOG_E("failed to close file, errno = 0x%" LOG_PUBLIC "x", errno); + return HKS_ERROR_CLOSE_FILE_FAIL; + } + *size = len; + return HKS_SUCCESS; } static uint32_t FileSize(const char *fileName) @@ -132,7 +139,7 @@ static int32_t GetRealPath(const char *fileName, char *filePath) if (memcpy_s(filePath, PATH_MAX, fileName, strlen(fileName)) != EOK) { return HKS_ERROR_INSUFFICIENT_MEMORY; } - + if (strstr(filePath, "../") != NULL) { HKS_LOG_E("invalid filePath!"); return HKS_ERROR_INVALID_KEY_FILE; @@ -471,19 +478,19 @@ int32_t HksDeleteDir(const char *path) return ret; } -uint32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +int32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size) { - if ((fileName == NULL) || (buf == NULL) || (len == 0)) { - return 0; + if ((fileName == NULL) || (blob == NULL) || (blob->data == NULL) || (blob->size == 0) || (size == NULL)) { + return HKS_ERROR_INVALID_ARGUMENT; } char *fullFileName = NULL; int32_t ret = GetFullFileName(path, fileName, &fullFileName); - HKS_IF_NOT_SUCC_RETURN(ret, 0) + HKS_IF_NOT_SUCC_RETURN(ret, ret) - uint32_t size = FileRead(fullFileName, offset, buf, len); + ret = FileRead(fullFileName, offset, blob, size); HKS_FREE_PTR(fullFileName); - return size; + return ret; } int32_t HksFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len) diff --git a/utils/file_operator/hks_file_operator.h b/utils/file_operator/hks_file_operator.h index 615a4cf0..f8f8a361 100644 --- a/utils/file_operator/hks_file_operator.h +++ b/utils/file_operator/hks_file_operator.h @@ -81,7 +81,7 @@ enum HksStoragePathType { extern "C" { #endif -uint32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len); +int32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size); int32_t HksFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len); diff --git a/utils/file_operator/hks_file_operator_lite.c b/utils/file_operator/hks_file_operator_lite.c index 703e0755..1e340670 100644 --- a/utils/file_operator/hks_file_operator_lite.c +++ b/utils/file_operator/hks_file_operator_lite.c @@ -108,7 +108,7 @@ static int32_t IsFileExist(const char *fileName) return HKS_SUCCESS; } -static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +static int32_t FileRead(const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size) { (void)offset; int32_t fd = open(fileName, O_RDONLY); @@ -117,20 +117,20 @@ static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, ui #ifdef HUKS_LOG_MINI_EXT_ENABLED HILOG_ERROR(HILOG_MODULE_SCY, "failed to open file, errno = 0x%{public}X", errno); #endif - return 0; + return HKS_ERROR_OPEN_FILE_FAIL; } - int32_t size = read(fd, buf, len); + int32_t len = read(fd, blob->data, blob->size); close(fd); - if (size < 0) { + if (len < 0) { HKS_LOG_E("failed to read file, errno = 0x%" LOG_PUBLIC "x", errno); #ifdef HUKS_LOG_MINI_EXT_ENABLED HILOG_ERROR(HILOG_MODULE_SCY, "failed to read file, errno = 0x%{public}X", errno); #endif - return 0; + return HKS_ERROR_READ_FILE_FAIL; } - - return (uint32_t)size; + *size = (uint32_t)len; + return HKS_SUCCESS; } static uint32_t FileSize(const char *fileName) @@ -325,40 +325,40 @@ static int32_t IsFileExist(const char *fileName) return HKS_SUCCESS; } -static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +static int32_t FileRead(const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size) { /* now offset is 0, but we maybe extend hi1131 file interfaces in the future */ if (offset != 0) { - return 0; + return HKS_ERROR_INVALID_ARGUMENT; } unsigned int fileSize; int32_t ret = UtilsFileStat(fileName, &fileSize); if (ret < 0) { HKS_LOG_E("stat file failed, errno = 0x%" LOG_PUBLIC "x", ret); - return 0; + return HKS_ERROR_INVALID_ARGUMENT; } - if (len > fileSize) { + if (blob->size > fileSize) { HKS_LOG_E("read data over file size!\n, file size = %" LOG_PUBLIC "d\n, buf len = %" LOG_PUBLIC "d\n", - fileSize, len); - return 0; + fileSize, blob->size); + return HKS_ERROR_INVALID_ARGUMENT; } int fd = UtilsFileOpen(fileName, O_RDONLY_FS, 0); if (fd < 0) { HKS_LOG_E("failed to open file, errno = 0x%" LOG_PUBLIC "x", fd); - return 0; + return HKS_ERROR_OPEN_FILE_FAIL; } - ret = UtilsFileRead(fd, (char *)buf, len); + ret = UtilsFileRead(fd, (char *)blob->data, blob->size); UtilsFileClose(fd); if (ret < 0) { HKS_LOG_E("failed to read file, errno = 0x%" LOG_PUBLIC "x", ret); - return 0; + return HKS_ERROR_READ_FILE_FAIL; } - - return len; + *size = blob->size; + return HKS_SUCCESS; } static int32_t FileWrite(const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len) @@ -407,19 +407,19 @@ static int32_t FileRemove(const char *fileName) #endif -uint32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +int32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size) { - if ((fileName == NULL) || (buf == NULL) || (len == 0)) { - return 0; + if ((fileName == NULL) || (blob == NULL) || (blob->data == NULL) || (blob->size == 0) || (size == NULL)) { + return HKS_ERROR_INVALID_ARGUMENT; } char *fullFileName = NULL; int32_t ret = GetFullFileName(path, fileName, &fullFileName); - HKS_IF_NOT_SUCC_RETURN(ret, 0) + HKS_IF_NOT_SUCC_RETURN(ret, ret) - uint32_t size = FileRead(fullFileName, offset, buf, len); + ret = FileRead(fullFileName, offset, blob, size); HKS_FREE_PTR(fullFileName); - return size; + return ret; } int32_t HksFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len)