Signed-off-by: weishaoxiong <weishaoxiong2@huawei.com>
This commit is contained in:
weishaoxiong 2024-02-26 10:00:55 +08:00
parent 99a754edb1
commit daeb13f1d1
5 changed files with 60 additions and 45 deletions

View File

@ -92,13 +92,13 @@ int32_t ErrorCodeConversion(int32_t sandboxManagerErrorCode,
int32_t ErrorCodeConversion(int32_t sandboxManagerErrorCode)
{
if (sandboxManagerErrorCode == SANDBOX_MANAGER_OK) {
return 0;
}
if (sandboxManagerErrorCode == PERMISSION_DENIED) {
LOGE("The app does not have the authorization URI permission");
return FileManagement::LibN::E_PERMISSION;
}
if (sandboxManagerErrorCode == SANDBOX_MANAGER_OK) {
return 0;
}
return FileManagement::LibN::E_UNKNOWN_ERROR;
}
} // namespace
@ -134,11 +134,12 @@ void FilePermission::ParseErrorResults(const vector<uint32_t> &resultCodes,
void FilePermission::ParseErrorResults(const vector<bool> &resultCodes, vector<bool> &errorResults)
{
auto count = resultCodes.size();
if (count == 0) {
auto resultCodeSize = resultCodes.size();
if (resultCodeSize == 0) {
return;
}
for (size_t i = 0, j = 0; i < errorResults.size() && j < count; i++) {
auto errorResultSize = errorResults.size();
for (size_t i = 0, j = 0; i < errorResultSize && j < resultCodeSize; i++) {
if (errorResults[i]) {
errorResults[i] = resultCodes[j++];
}
@ -173,7 +174,7 @@ vector<PolicyInfo> FilePermission::GetPathPolicyInfoFromUriPolicyInfo(const vect
vector<bool> &errorResults)
{
vector<PolicyInfo> pathPolicies;
for (auto uriPolicy : uriPolicies) {
for (const auto &uriPolicy : uriPolicies) {
Uri uri(uriPolicy.uri);
string path = uri.GetPath();
if (!CheckValidUri(uriPolicy.uri) || access(path.c_str(), F_OK) != 0) {
@ -183,7 +184,9 @@ vector<PolicyInfo> FilePermission::GetPathPolicyInfoFromUriPolicyInfo(const vect
string currentUserId = to_string(IPCSkeleton::GetCallingTokenID() / AppExecFwk::Constants::BASE_USER_RANGE);
int32_t ret = SandboxHelper::GetPhysicalPath(uri.ToString(), currentUserId, path);
if (ret != 0) {
errorResults.emplace_back(false);
LOGE("Failed to get physical path, errorcode: %{public}d", ret);
continue;
}
PolicyInfo policyInfo = {path, uriPolicy.mode};
pathPolicies.emplace_back(policyInfo);

View File

@ -35,6 +35,7 @@ using namespace std;
namespace {
const std::string FILE_ACCESS_PERMISSION = "ohos.permission.FILE_ACCESS_PERSIST";
const char *g_fullMountEnableParameter = "const.filemanager.full_mount.enable";
constexpr int32_t MAX_ARRAY_SIZE = 500;
static bool CheckPermission(const string &permission)
{
@ -108,6 +109,10 @@ static napi_status GetUriPoliciesArg(napi_env env, napi_value agrv, std::vector<
LOGE("get array length failed");
return status;
}
if (count > MAX_ARRAY_SIZE) {
LOGE("The length of the array is extra-long");
return napi_invalid_arg;
}
for (uint32_t i = 0; i < count; i++) {
napi_handle_scope scope;
status = napi_open_handle_scope(env, &scope);
@ -120,7 +125,6 @@ static napi_status GetUriPoliciesArg(napi_env env, napi_value agrv, std::vector<
LOGE("get element failed");
return status;
}
UriPolicyInfo uriPolicy;
napi_value uriValue;
napi_value modeValue;
status = napi_get_named_property(env, object, "uri", &uriValue);
@ -139,8 +143,7 @@ static napi_status GetUriPoliciesArg(napi_env env, napi_value agrv, std::vector<
LOGE("the argument error");
return napi_invalid_arg;
}
uriPolicy.uri = str.get();
uriPolicy.mode = mode;
UriPolicyInfo uriPolicy {.uri = str.get(), .mode = mode};
uriPolicies.emplace_back(uriPolicy);
status = napi_close_handle_scope(env, scope);
if (status != napi_ok) {

View File

@ -16,13 +16,13 @@
#ifndef FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_FILE_SHARE_ERROR_CODE_H
#define FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_FILE_SHARE_ERROR_CODE_H
#include <cstdint>
enum FileManagement_ErrCode {
E_NO_ERROR = 0,
E_PERMISSION = 201,
E_PARAMS = 401,
E_DEVICE_NOT_SUPPORT = 801,
E_EPERM = 13900001,
E_ENOMEM = 13900011,
E_UNKNOWN_ERROR = 13900042
};

View File

@ -46,4 +46,4 @@ ohos_shared_library("ohfileshare") {
relative_install_dir = "ndk"
part_name = "app_file_service"
subsystem_name = "filemanagement"
}
}

View File

@ -23,12 +23,14 @@
#include "securec.h"
#include "tokenid_kit.h"
#include <cstdint>
#include <iostream>
#include <cstdlib>
#include <iostream>
constexpr int32_t FOO_MAX_LEN = 24000; // sizeof(FileShare_PolicyErrorResult) * 500
constexpr int32_t MAX_ARRAY_SIZE = 500;
constexpr int32_t FOO_MAX_LEN = 24000; // sizeof(FileShare_PolicyErrorResult) * MAX_ARRAY_SIZE
const std::string FILE_ACCESS_PERMISSION = "ohos.permission.FILE_ACCESS_PERSIST";
const char *g_fullMountEnableParameter = "const.filemanager.full_mount.enable";
const std::string FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable";
using Exec = std::function<int(const std::vector<OHOS::AppFileService::UriPolicyInfo> &uriPolicies,
std::deque<struct OHOS::AppFileService::PolicyErrorResult> &errorResults)>;
static bool CheckPermission(const std::string &permission)
@ -41,7 +43,7 @@ static bool CheckPermission(const std::string &permission)
static bool CheckFileManagerFullMountEnable()
{
char value[] = "false";
int retSystem = GetParameter(g_fullMountEnableParameter, "false", value, sizeof(value));
int retSystem = GetParameter(FULL_MOUNT_ENABLE_PARAMETER.c_str(), "false", value, sizeof(value));
if (retSystem > 0 && !strcmp(value, "true")) {
LOGI("The full mount enable parameter is true");
return true;
@ -50,12 +52,12 @@ static bool CheckFileManagerFullMountEnable()
return false;
}
bool ConvertPolicyInfo(const FileShare_PolicyInfo *policies,
static bool ConvertPolicyInfo(const FileShare_PolicyInfo *policies,
int policyNum,
std::vector<OHOS::AppFileService::UriPolicyInfo> &uriPolicies)
{
if (policies == nullptr || policyNum <= 0) {
LOGE("The policies pointer is nullptr or policyNum is 0");
if (policies == nullptr || policyNum <= 0 || policyNum > MAX_ARRAY_SIZE) {
LOGE("The policies pointer is nullptr or policyNum is abnormal");
return false;
}
for (int32_t i = 0; i < policyNum; i++) {
@ -76,58 +78,66 @@ bool ConvertPolicyInfo(const FileShare_PolicyInfo *policies,
return true;
}
bool ConvertPolicyErrorResult(const std::deque<OHOS::AppFileService::PolicyErrorResult> &errorResults,
static bool ConvertPolicyErrorResult(const std::deque<OHOS::AppFileService::PolicyErrorResult> &errorResults,
FileShare_PolicyErrorResult **result,
unsigned int *resultNum)
unsigned int &resultNum)
{
*resultNum = 0;
resultNum = 0;
auto count = errorResults.size();
auto memorySize = count * sizeof(FileShare_PolicyErrorResult);
if (memorySize == 0 || memorySize > FOO_MAX_LEN) {
LOGE("malloc size is abnormal.");
LOGE("The size of the return value array is abnormal");
return false;
}
*result = (FileShare_PolicyErrorResult *)malloc(memorySize);
if (*result == nullptr) {
LOGE("*result is nullptr");
LOGE("Failed to apply for FileShare_PolicyErrorResult array memory");
return false;
}
for (uint32_t i = 0; i < count; i++) {
int size = errorResults[i].uri.size() + 1;
(*result)[i].uri = (char *)malloc(size);
if ((*result)[i].uri == nullptr) {
LOGE("Failed to apply for URI memory");
return false;
}
auto ret = strcpy_s((*result)[i].uri, size, errorResults[i].uri.c_str());
if (ret != 0) {
LOGE("strcpy uri failed uri:%{public}s, errno:%{public}d", errorResults[i].uri.c_str(), ret);
*resultNum = i;
LOGE("Copy uri failed uri:%{public}s, errno:%{public}d", errorResults[i].uri.c_str(), ret);
free((*result)[i].uri);
return false;
}
(*result)[i].code = static_cast<FileShare_PolicyErrorCode>(errorResults[i].code);
size = errorResults[i].message.size() + 1;
(*result)[i].message = (char *)malloc(size);
if ((*result)[i].message == nullptr) {
LOGE("Failed to apply for message memory");
free((*result)[i].uri);
return false;
}
ret = strcpy_s((*result)[i].message, size, errorResults[i].message.c_str());
if (ret != 0) {
LOGE("strcpy message failed message:%{public}s, errno:%{public}d", errorResults[i].uri.c_str(), ret);
*resultNum = i;
LOGE("Copy message failed message:%{public}s, errno:%{public}d", errorResults[i].uri.c_str(), ret);
free((*result)[i].uri);
free((*result)[i].message);
return false;
}
resultNum++;
}
*resultNum = count;
return true;
}
bool ConvertPolicyErrorResultBool(const std::vector<bool> &errorResults, bool **result)
static bool ConvertPolicyErrorResultBool(const std::vector<bool> &errorResults, bool **result)
{
auto count = errorResults.size();
auto memorySize = count * sizeof(bool);
if (memorySize == 0 || memorySize > FOO_MAX_LEN) {
LOGE("malloc size is abnormal.");
LOGE("The size of the return value array is abnormal");
return false;
}
*result = (bool *)malloc(memorySize);
if (*result == nullptr) {
LOGE("Failed to apply for bool array memory");
return false;
}
for (uint32_t i = 0; i < count; i++) {
@ -136,7 +146,7 @@ bool ConvertPolicyErrorResultBool(const std::vector<bool> &errorResults, bool **
return true;
}
FileManagement_ErrCode ErrorCodeConversion(int32_t errorCode)
static FileManagement_ErrCode ErrorCodeConversion(int32_t errorCode)
{
FileManagement_ErrCode errCode = E_UNKNOWN_ERROR;
switch (errorCode) {
@ -158,21 +168,20 @@ FileManagement_ErrCode ErrorCodeConversion(int32_t errorCode)
return errCode;
}
FileManagement_ErrCode ExecAction(const FileShare_PolicyInfo *policies,
void OH_FileShare_ReleasePolicyErrorResult(FileShare_PolicyErrorResult *result, unsigned int num);
static FileManagement_ErrCode ExecAction(const FileShare_PolicyInfo *policies,
unsigned int policyNum,
FileShare_PolicyErrorResult **result,
unsigned int *resultNum,
Exec exec)
{
(*resultNum) = 0;
if (!CheckPermission(FILE_ACCESS_PERMISSION)) {
return E_PERMISSION;
}
if (!CheckFileManagerFullMountEnable()) {
return E_DEVICE_NOT_SUPPORT;
}
if (!CheckPermission(FILE_ACCESS_PERMISSION)) {
return E_PERMISSION;
}
std::vector<OHOS::AppFileService::UriPolicyInfo> uriPolicies;
if (!ConvertPolicyInfo(policies, policyNum, uriPolicies)) {
return E_PARAMS;
@ -182,8 +191,9 @@ FileManagement_ErrCode ExecAction(const FileShare_PolicyInfo *policies,
if (ret == E_NO_ERROR) {
return E_NO_ERROR;
}
if (!ConvertPolicyErrorResult(errorResults, result, resultNum)) {
return E_UNKNOWN_ERROR;
if (!ConvertPolicyErrorResult(errorResults, result, *resultNum)) {
OH_FileShare_ReleasePolicyErrorResult(*result, *resultNum);
return E_ENOMEM;
}
return ret;
}
@ -227,13 +237,12 @@ FileManagement_ErrCode OH_FileShare_CheckPersistentPermission(const FileShare_Po
unsigned int *resultNum)
{
*resultNum = 0;
if (!CheckPermission(FILE_ACCESS_PERMISSION)) {
return E_PERMISSION;
}
if (!CheckFileManagerFullMountEnable()) {
return E_DEVICE_NOT_SUPPORT;
}
if (!CheckPermission(FILE_ACCESS_PERMISSION)) {
return E_PERMISSION;
}
std::vector<OHOS::AppFileService::UriPolicyInfo> uriPolicies;
if (!ConvertPolicyInfo(policies, policyNum, uriPolicies)) {
return E_PARAMS;
@ -244,7 +253,7 @@ FileManagement_ErrCode OH_FileShare_CheckPersistentPermission(const FileShare_Po
return ErrorCodeConversion(ret);
}
if (!ConvertPolicyErrorResultBool(errorResults, result)) {
return E_UNKNOWN_ERROR;
return E_ENOMEM;
}
*resultNum = errorResults.size();
return E_NO_ERROR;