From 8ce172101609812ba2d0af33ef0900bf98089064 Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Sat, 3 Sep 2022 10:38:03 +0000 Subject: [PATCH] modify the path to create_share_path Signed-off-by: zhangkaixiang --- bundle.json | 11 +++++ .../remote_file_share/native/BUILD.gn | 7 ++- .../native/remote_file_share.h | 7 ++- .../src/remote_file_share.cpp | 43 +++++++++++++------ 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/bundle.json b/bundle.json index 287695427..571944b86 100644 --- a/bundle.json +++ b/bundle.json @@ -29,6 +29,17 @@ ], "service_group": [] }, + "inner_kits": [ + { + "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", + "header": { + "header_files": [ + "remote_file_share.h" + ], + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native" + } + } + ], "test": [ "//foundation/filemanagement/app_file_service/test/fuzztest:fuzztest" ] diff --git a/interfaces/innerkits/remote_file_share/native/BUILD.gn b/interfaces/innerkits/remote_file_share/native/BUILD.gn index 706bf36b0..39837981d 100644 --- a/interfaces/innerkits/remote_file_share/native/BUILD.gn +++ b/interfaces/innerkits/remote_file_share/native/BUILD.gn @@ -13,16 +13,21 @@ import("//build/ohos.gni") -ohos_shared_library("remote_file_share_native") { +config("remote_file_share_config") { + visibility = [ ":*" ] include_dirs = [ "include", "//utils/system/safwk/native/include", "//commonlibrary/c_utils/base/include", "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native", ] +} +ohos_shared_library("remote_file_share_native") { sources = [ "//foundation/filemanagement/app_file_service/services/remote_file_share/src/remote_file_share.cpp" ] + public_configs = [ ":remote_file_share_config" ] + external_deps = [ "c_utils:utils", "hiviewdfx_hilog_native:libhilog", diff --git a/interfaces/innerkits/remote_file_share/native/remote_file_share.h b/interfaces/innerkits/remote_file_share/native/remote_file_share.h index 589e29f4d..f16c8d7b9 100644 --- a/interfaces/innerkits/remote_file_share/native/remote_file_share.h +++ b/interfaces/innerkits/remote_file_share/native/remote_file_share.h @@ -21,11 +21,14 @@ namespace OHOS { namespace AppFileService { namespace ModuleRemoteFileShare { +namespace { + const std::string SHARE_ALL_DEVICE = "0"; +} class RemoteFileShare { public: RemoteFileShare() {} - int CreateSharePath(const int &fd, std::string &sharePath, - const int &userId, const std::string &deviceId = ""); + static int CreateSharePath(const int &fd, std::string &sharePath, + const int &userId, const std::string &deviceId = SHARE_ALL_DEVICE); ~RemoteFileShare() {} }; } // namespace ModuleRemoteFileShare diff --git a/services/remote_file_share/src/remote_file_share.cpp b/services/remote_file_share/src/remote_file_share.cpp index dfee443c1..2e8eb47ca 100644 --- a/services/remote_file_share/src/remote_file_share.cpp +++ b/services/remote_file_share/src/remote_file_share.cpp @@ -31,11 +31,12 @@ namespace AppFileService { namespace ModuleRemoteFileShare { namespace { const int HMDFS_CID_SIZE = 64; + const int USER_ID_INIT = 100; const unsigned HMDFS_IOC = 0xf2; const std::string SHAER_PATH_HEAD = "/mnt/hmdfs/"; const std::string SHAER_PATH_MID = "/account/merge_view/services/"; - const std::string LOWER_SHARE_PATH_HEAD = "/data/service/el2/"; - const std::string LOWER_SHARE_PATH_MID = "/hmdfs/account/services/"; + const std::string LOWER_SHARE_PATH_HEAD = "/mnt/hmdfs/"; + const std::string LOWER_SHARE_PATH_MID = "/account/device_view/local/services/"; } #define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct HmdfsShareControl) @@ -107,24 +108,26 @@ static std::string GetLowerSharePath(const int &userId, const std::string &packa return LOWER_SHARE_PATH_HEAD + std::to_string(userId) + LOWER_SHARE_PATH_MID + packageName; } -static bool DeleteShareDir(const std::string PACKAGE_PATH, const std::string SHARE_PATH_LOWER) +static bool DeleteShareDir(const std::string PACKAGE_PATH, const std::string SHARE_PATH) { bool result = true; - if (access(SHARE_PATH_LOWER.c_str(), F_OK) == 0) { - int ret = rmdir(SHARE_PATH_LOWER.c_str()); + if (access(SHARE_PATH.c_str(), F_OK) == 0) { + int ret = rmdir(SHARE_PATH.c_str()); if (ret != 0) { LOGE("RemoteFileShare::DeleteShareDir, delete dir failed with %{public}d", errno); result = false; + } else { + LOGI("RemoteFileShare::DeleteShareDir, delete %{public}s path successfully", SHARE_PATH.c_str()); } - LOGI("RemoteFileShare::DeleteShareDir, delete %{public}s path successfully", SHARE_PATH_LOWER.c_str()); } if (access(PACKAGE_PATH.c_str(), F_OK) == 0) { int ret = rmdir(PACKAGE_PATH.c_str()); if (ret != 0) { LOGE("RemoteFileShare::DeleteShareDir, delete dir failed with %{public}d", errno); result = false; + } else { + LOGI("RemoteFileShare::DeleteShareDir, delete %{public}s path successfully", PACKAGE_PATH.c_str()); } - LOGI("RemoteFileShare::DeleteShareDir, delete %{public}s path successfully", PACKAGE_PATH.c_str()); } return result; } @@ -138,6 +141,7 @@ static int CreateShareFile(struct HmdfsShareControl &shareControl, const char* f return errno; } + memset_s(shareControl.deviceId, HMDFS_CID_SIZE, '\0', HMDFS_CID_SIZE); if (memcpy_s(shareControl.deviceId, HMDFS_CID_SIZE, deviceId.c_str(), deviceId.size()) != 0) { LOGE("RemoteFileShare::CreateShareFile, memcpy_s failed with %{public}d", errno); close(dirFd); @@ -153,11 +157,22 @@ static int CreateShareFile(struct HmdfsShareControl &shareControl, const char* f return 0; } +static int CheckInputValidity(const int &fd, const int &userId, const std::string &deviceId) +{ + return (fd < 0) || (userId < USER_ID_INIT) || (deviceId != SHARE_ALL_DEVICE && + deviceId.size() != HMDFS_CID_SIZE); +} + int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, const int &userId, const std::string &deviceId) { struct HmdfsShareControl shareControl; shareControl.fd = fd; + + if (CheckInputValidity(fd, userId, deviceId) != 0) { + LOGE("RemoteFileShare::CreateSharePath, invalid argument with %{public}d", EINVAL); + return EINVAL; + } const std::string processName = GetProcessName(); if (processName == "") { @@ -166,11 +181,11 @@ int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, } const std::string PACKAGE_PATH = GetLowerSharePath(userId, processName); - const std::string SHARE_PATH_LOWER = PACKAGE_PATH + "/.share"; + const std::string LOWER_SHARE_PATH = PACKAGE_PATH + "/.share"; if (CreateShareDir(PACKAGE_PATH) != 0) return errno; - if (CreateShareDir(SHARE_PATH_LOWER) != 0) { - DeleteShareDir(PACKAGE_PATH, SHARE_PATH_LOWER); + if (CreateShareDir(LOWER_SHARE_PATH) != 0) { + DeleteShareDir(PACKAGE_PATH, LOWER_SHARE_PATH); return errno; } @@ -178,20 +193,22 @@ int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, char realPath[PATH_MAX] = {'\0'}; if (!realpath(SHARE_PATH.c_str(), realPath)) { LOGE("RemoteFileShare::CreateSharePath, realpath failed with %{public}d", errno); - DeleteShareDir(PACKAGE_PATH, SHARE_PATH_LOWER); + DeleteShareDir(PACKAGE_PATH, LOWER_SHARE_PATH); return errno; } if (CreateShareFile(shareControl, realPath, deviceId) != 0) { LOGE("RemoteFileShare::CreateSharePath, create share file failed with %{public}d", errno); - DeleteShareDir(PACKAGE_PATH, SHARE_PATH_LOWER); + /* When the file is exist, we should not delete the dictionary */ + if (errno != EEXIST) + DeleteShareDir(PACKAGE_PATH, LOWER_SHARE_PATH); return errno; } std::string file_name = GetFileName(shareControl.fd); if (file_name == "") { LOGE("RemoteFileShare::CreateSharePath, get error file name"); - DeleteShareDir(PACKAGE_PATH, SHARE_PATH_LOWER); + DeleteShareDir(PACKAGE_PATH, LOWER_SHARE_PATH); return EBADF; } sharePath = SHARE_PATH + "/" + file_name; -- Gitee