From e4a8c1a10fcb17be61c14835adfbfdba36eb9752 Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Sun, 9 Jul 2023 19:42:18 +0800 Subject: [PATCH] add interface GetDfsUriFromLocal Signed-off-by: zhangkaixiang Change-Id: I89e79a3b551e01baad57653999587c212512faca --- BUILD.gn | 5 +- bundle.json | 4 +- interfaces/common/include/log.h | 1 + interfaces/innerkits/native/BUILD.gn | 41 +++++- .../include}/remote_file_share.h | 11 +- .../src/remote_file_share.cpp | 130 +++++++++++++++++- .../remote_file_share/native/BUILD.gn | 44 ------ .../native/remote_file_share_log.h | 35 ----- test/unittest/remote_file_share/BUILD.gn | 4 +- 9 files changed, 181 insertions(+), 94 deletions(-) rename interfaces/innerkits/{remote_file_share/native => native/remote_file_share/include}/remote_file_share.h (75%) rename {services => interfaces/innerkits/native}/remote_file_share/src/remote_file_share.cpp (64%) delete mode 100644 interfaces/innerkits/remote_file_share/native/BUILD.gn delete mode 100644 interfaces/innerkits/remote_file_share/native/remote_file_share_log.h diff --git a/BUILD.gn b/BUILD.gn index 7c57ae43d..1b6471490 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -14,10 +14,7 @@ import("//build/ohos.gni") group("libremotefileshare") { - deps = [ - "interfaces/innerkits/remote_file_share/native:remote_file_share_native", - "interfaces/kits/js:remotefileshare", - ] + deps = [ "interfaces/kits/js:remotefileshare" ] } group("tgt_backup_extension") { diff --git a/bundle.json b/bundle.json index 64feb645c..5186429d4 100644 --- a/bundle.json +++ b/bundle.json @@ -75,12 +75,12 @@ } }, { - "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", + "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/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" + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/remote_file_share/include" } }, { diff --git a/interfaces/common/include/log.h b/interfaces/common/include/log.h index 9f7053a45..207318f30 100644 --- a/interfaces/common/include/log.h +++ b/interfaces/common/include/log.h @@ -29,6 +29,7 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, APP_LOG_DOM #define LOGI(fmt, ...) PRINT_LOG(Info, fmt, ##__VA_ARGS__) #define LOGW(fmt, ...) PRINT_LOG(Warn, fmt, ##__VA_ARGS__) +#define LOGD(fmt, ...) PRINT_LOG(Warn, fmt, ##__VA_ARGS__) #define LOGE(fmt, ...) PRINT_LOG(Error, fmt, ##__VA_ARGS__) #define LOGF(fmt, ...) PRINT_LOG(Fatal, fmt, ##__VA_ARGS__) } // namespace AppFileService diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 520727f89..833d8305d 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/ohos.gni") +import("//foundation/filemanagement/app_file_service/app_file_service.gni") config("file_share_config") { visibility = [ ":*" ] @@ -51,8 +52,46 @@ ohos_prebuilt_etc("file_share_sandbox.json") { module_install_dir = "etc/app_file_service" } +config("remote_file_share_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${utils_system_safwk_path}/native/include", + "remote_file_share/include", + "//third_party/json/include", + "${path_base}/include", + "../../common/include", + ".", + ] +} + +ohos_shared_library("remote_file_share_native") { + sources = [ + "../../common/src/common_func.cpp", + "../../common/src/json_utils.cpp", + "remote_file_share/src/remote_file_share.cpp", + ] + + public_configs = [ ":remote_file_share_config" ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "c_utils:utils", + "hilog:libhilog", + ] + + innerapi_tags = [ "platformsdk_indirect" ] + part_name = "app_file_service" + subsystem_name = "filemanagement" +} + group("app_file_service_native") { - deps = [ ":fileshare_native" ] + deps = [ + ":fileshare_native", + ":remote_file_share_native", + ] } group("etc_files") { diff --git a/interfaces/innerkits/remote_file_share/native/remote_file_share.h b/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h similarity index 75% rename from interfaces/innerkits/remote_file_share/native/remote_file_share.h rename to interfaces/innerkits/native/remote_file_share/include/remote_file_share.h index f16c8d7b9..e94a6ed95 100644 --- a/interfaces/innerkits/remote_file_share/native/remote_file_share.h +++ b/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -24,11 +24,18 @@ namespace ModuleRemoteFileShare { namespace { const std::string SHARE_ALL_DEVICE = "0"; } + +struct HmdfsUriInfo { + std::string uriStr; + size_t fileSize; +}; + class RemoteFileShare { public: RemoteFileShare() {} static int CreateSharePath(const int &fd, std::string &sharePath, - const int &userId, const std::string &deviceId = SHARE_ALL_DEVICE); + const int &userId, const std::string &deviceId = SHARE_ALL_DEVICE); + static int32_t GetDfsUriFromLocal(const std::string &uriStr, const int32_t &userId, struct HmdfsUriInfo &hui); ~RemoteFileShare() {} }; } // namespace ModuleRemoteFileShare diff --git a/services/remote_file_share/src/remote_file_share.cpp b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp similarity index 64% rename from services/remote_file_share/src/remote_file_share.cpp rename to interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp index 865a69415..dd20f224f 100644 --- a/services/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -14,7 +14,6 @@ */ #include "remote_file_share.h" -#include "remote_file_share_log.h" #include #include @@ -24,7 +23,11 @@ #include #include +#include "directory_ex.h" #include "securec.h" +#include "common_func.h" +#include "uri.h" +#include "log.h" namespace OHOS { namespace AppFileService { @@ -33,19 +36,36 @@ namespace { const int HMDFS_CID_SIZE = 64; const int USER_ID_INIT = 100; const unsigned HMDFS_IOC = 0xf2; + const std::string FILE_SCHEME = "file"; + const std::string DISTRIBUTED_DIR_PATH = "/data/storage/el2/distributedfiles"; + const std::string DST_PATH_HEAD = "/data/service/el2/"; + const std::string DST_PATH_MID = "/hmdfs/account/data/"; 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 = "/mnt/hmdfs/"; const std::string LOWER_SHARE_PATH_MID = "/account/device_view/local/services/"; + const std::string SHARE_PATH_DIR = "/.share"; + const std::string REMOTE_SHARE_PATH_DIR = "/.remote_share"; } #define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct HmdfsShareControl) +#define HMDFS_IOC_GET_DST_PATH _IOR(HMDFS_IOC, 3, unsigned int) struct HmdfsShareControl { int fd; char deviceId[HMDFS_CID_SIZE]; }; +struct HmdfsDstInfo { + uint64_t localLen; + uint64_t localPathIndex; + uint64_t distributedLen; + uint64_t distributedPathIndex; + uint64_t bundleNameLen; + uint64_t bundleNameIndex; + uint64_t size; +}; + static std::string GetProcessName() { char pthreadName[PATH_MAX]; @@ -181,7 +201,7 @@ int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, } const std::string PACKAGE_PATH = GetLowerSharePath(userId, processName); - const std::string LOWER_SHARE_PATH = PACKAGE_PATH + "/.share"; + const std::string LOWER_SHARE_PATH = PACKAGE_PATH + SHARE_PATH_DIR; if (CreateShareDir(PACKAGE_PATH) != 0) return errno; if (CreateShareDir(LOWER_SHARE_PATH) != 0) { @@ -189,7 +209,7 @@ int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, return errno; } - const std::string SHARE_PATH = GetSharePath(userId, processName) + "/.share"; + const std::string SHARE_PATH = GetSharePath(userId, processName) + SHARE_PATH_DIR; char realPath[PATH_MAX] = {'\0'}; if (!realpath(SHARE_PATH.c_str(), realPath)) { LOGE("RemoteFileShare::CreateSharePath, realpath failed with %{public}d", errno); @@ -218,6 +238,108 @@ int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, LOGI("RemoteFileShare::CreateSharePath, create %{public}s successfully", sharePath.c_str()); return 0; } + +static int GetDistributedPath(Uri &uri, const int &userId, std::string &distributedPath) +{ + distributedPath = DST_PATH_HEAD + std::to_string(userId) + DST_PATH_MID + + uri.GetAuthority() + REMOTE_SHARE_PATH_DIR + uri.GetPath(); + if (distributedPath.size() >= PATH_MAX) { + return -EINVAL; + } + + return 0; +} + +static bool IsValidPath(const std::string &path) +{ + if (path.find("/./") != std::string::npos || + path.find("/../") != std::string::npos) { + return false; + } + return true; +} + +static std::string GetPhysicalPath(Uri &uri, const std::string &userId) +{ + std::string sandboxPath = uri.GetPath(); + if (!IsValidPath(sandboxPath) || uri.GetScheme() != FILE_SCHEME) { + LOGE("Sandbox path from uri is error with %{public}s", sandboxPath.c_str()); + return ""; + } + + std::string physicalPath = ""; + int ret = CommonFunc::GetPhysicalPath(uri.ToString(), userId, physicalPath); + if (ret != 0) { + LOGE("Get physical path failed with %{public}d", ret); + return ""; + } + return physicalPath; +} + +static void InitHmdfsInfo(struct HmdfsDstInfo &hdi, const std::string &physicalPath, + const std::string &distributedPath, const std::string &bundleName) +{ + hdi.localLen = physicalPath.size() + 1; + hdi.localPathIndex = (uint64_t)(physicalPath.c_str()); + + hdi.distributedLen = distributedPath.size() + 1; + hdi.distributedPathIndex = (uint64_t)(distributedPath.c_str()); + + hdi.bundleNameLen = bundleName.size() + 1; + hdi.bundleNameIndex = (uint64_t)(bundleName.c_str()); + + hdi.size = (uint64_t)&hdi.size; +} + +static void SetHmdfsUriInfo(struct HmdfsUriInfo &hdi, Uri &uri, uint64_t fileSize) +{ + std::string bundleName = uri.GetAuthority(); + std::string path = uri.GetPath(); + + hdi.uriStr = FILE_SCHEME + "://" + bundleName + DISTRIBUTED_DIR_PATH + REMOTE_SHARE_PATH_DIR + path; + hdi.fileSize = fileSize; + return; +} + +int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int32_t &userId, + struct HmdfsUriInfo &hui) +{ + Uri uri(uriStr); + LOGD("GetDfsUriFromLocal begin with %{public}s", uriStr.c_str()); + std::string physicalPath = GetPhysicalPath(uri, std::to_string(userId)); + if (physicalPath == "") { + LOGE("Failed to get physical path"); + return -EINVAL; + } + + std::string distributedPath; + int ret = GetDistributedPath(uri, userId, distributedPath); + if (ret != 0) { + LOGE("Path is too long with %{public}d", ret); + return ret; + } + + struct HmdfsDstInfo hdi; + LOGD("PhysicalPath: %{public}s DistributedPath: %{public}s", physicalPath.c_str(), distributedPath.c_str()); + InitHmdfsInfo(hdi, physicalPath, distributedPath, uri.GetAuthority()); + + std::string ioctlDir = SHAER_PATH_HEAD + std::to_string(userId) + SHAER_PATH_MID; + int32_t dirFd = open(ioctlDir.c_str(), O_RDONLY); + if (dirFd < 0) { + LOGE("Open share path failed with %{public}d", errno); + return errno; + } + + ret = ioctl(dirFd, HMDFS_IOC_GET_DST_PATH, &hdi); + if (ret != 0) { + LOGE("Ioctl failed with %{public}d", errno); + return -errno; + } + + SetHmdfsUriInfo(hui, uri, hdi.size); + LOGD("GetDfsUriFromLocal successfully with %{public}s", hui.uriStr.c_str()); + return 0; +} } // namespace ModuleRemoteFileShare } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/innerkits/remote_file_share/native/BUILD.gn b/interfaces/innerkits/remote_file_share/native/BUILD.gn deleted file mode 100644 index baf5f45c3..000000000 --- a/interfaces/innerkits/remote_file_share/native/BUILD.gn +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright (c) 2022 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. - -import("//build/ohos.gni") -import("//foundation/filemanagement/app_file_service/app_file_service.gni") -config("remote_file_share_config") { - visibility = [ ":*" ] - include_dirs = [ - "include", - "${utils_system_safwk_path}/native/include", - "${path_base}/include", - ".", - ] -} - -ohos_shared_library("remote_file_share_native") { - sources = [ "${app_file_service_path}/services/remote_file_share/src/remote_file_share.cpp" ] - - public_configs = [ ":remote_file_share_config" ] - - external_deps = [ - "c_utils:utils", - "hilog:libhilog", - ] - - defines = [ - "REMOTE_SHARE_FILE_LOG_TAG=\"RemoteFileShare\"", - "LOG_DOMAIN=0xD004312", - ] - - innerapi_tags = [ "platformsdk_indirect" ] - part_name = "app_file_service" - subsystem_name = "filemanagement" -} diff --git a/interfaces/innerkits/remote_file_share/native/remote_file_share_log.h b/interfaces/innerkits/remote_file_share/native/remote_file_share_log.h deleted file mode 100644 index 510e9c751..000000000 --- a/interfaces/innerkits/remote_file_share/native/remote_file_share_log.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2022 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. - */ -#ifndef REMOTE_FILE_SHARE_LOG_H -#define REMOTE_FILE_SHARE_LOG_H - -#include "hilog/log.h" - -namespace OHOS { -namespace AppFileService { -static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_DOMAIN, REMOTE_SHARE_FILE_LOG_TAG}; - -#define PRINT_LOG(Level, fmt, ...) \ - OHOS::HiviewDFX::HiLog::Level(OHOS::AppFileService::LOG_LABEL, "[%{public}s:%{public}d] " fmt, \ - __FUNCTION__, __LINE__, ##__VA_ARGS__) - -#define LOGI(fmt, ...) PRINT_LOG(Info, fmt, ##__VA_ARGS__) -#define LOGW(fmt, ...) PRINT_LOG(Warn, fmt, ##__VA_ARGS__) -#define LOGE(fmt, ...) PRINT_LOG(Error, fmt, ##__VA_ARGS__) -#define LOGF(fmt, ...) PRINT_LOG(Fatal, fmt, ##__VA_ARGS__) -} // namespace AppFileService -} // namespace OHOS - -#endif // REMOTE_FILE_SHARE_LOG_H \ No newline at end of file diff --git a/test/unittest/remote_file_share/BUILD.gn b/test/unittest/remote_file_share/BUILD.gn index 8af8d3df7..f3e9d436c 100644 --- a/test/unittest/remote_file_share/BUILD.gn +++ b/test/unittest/remote_file_share/BUILD.gn @@ -24,13 +24,13 @@ ohos_unittest("remote_file_share_test") { include_dirs = [ "include", - "../../../interfaces/innerkits/remote_file_share/native", + "../../../interfaces/innerkits/native/remote_file_share/include", "${utils_system_safwk_path}/native/include", "${path_base}/include", ] deps = [ - "../../../interfaces/innerkits/remote_file_share/native:remote_file_share_native", + "../../../interfaces/innerkits/native:remote_file_share_native", "//third_party/googletest:gtest_main", ] } -- Gitee