diff --git a/BUILD.gn b/BUILD.gn index c70a22860ba06c0c465c5f8781bbf613cd91451a..dbf5c9cc60587c6d60e26be93ce0b08071da6aee 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -15,8 +15,6 @@ import("//build/ohos.gni") group("libremotefileshare") { deps = [ - "interfaces/innerkits/native/file_share:fileshare_native", - "interfaces/innerkits/remote_file_share/native:remote_file_share_native", "interfaces/kits/js:remotefileshare", ] } diff --git a/bundle.json b/bundle.json index 80107caf947ab6e6fc93c17487bd867923a43b21..3ddad452604990416e0c0bd044522588cf8a633e 100644 --- a/bundle.json +++ b/bundle.json @@ -32,6 +32,7 @@ "hitrace", "hilog", "ipc", + "hitrace_native", "init", "napi", "safwk", @@ -51,6 +52,7 @@ "base_group": [], "fwk_group": [ "//foundation/filemanagement/app_file_service:libremotefileshare", + "//foundation/filemanagement/app_file_service/interfaces/innerkits/native:app_file_service_native", "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileshare", "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileuri", "//foundation/filemanagement/app_file_service/interfaces/kits/js:backup" @@ -64,7 +66,7 @@ }, "inner_kits": [ { - "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share:fileshare_native", + "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native:fileshare_native", "header": { "header_files": [ "file_share.h" @@ -73,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/innerkits/remote_file_share/native/remote_file_share_log.h b/interfaces/common/include/common_func.h similarity index 47% rename from interfaces/innerkits/remote_file_share/native/remote_file_share_log.h rename to interfaces/common/include/common_func.h index 510e9c7511ba28b832274b7058269b8df0cd90e3..59978a01b8163627d8c77484bd07f450e077c165 100644 --- a/interfaces/innerkits/remote_file_share/native/remote_file_share_log.h +++ b/interfaces/common/include/common_func.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 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 @@ -12,24 +12,21 @@ * 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" +#ifndef APP_FILE_SERVICE_COMMON_FUNC +#define APP_FILE_SERVICE_COMMON_FUNC + +#include 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__) +class CommonFunc { +public: + static bool CheckValidPath(const std::string &filePath); + static int32_t GetPhysicalPath(const std::string &fileUri, const std::string &userId, + std::string &physicalPath); +}; } // namespace AppFileService } // namespace OHOS -#endif // REMOTE_FILE_SHARE_LOG_H \ No newline at end of file +#endif // APP_FILE_SERVICE_COMMON_FUNC \ No newline at end of file diff --git a/interfaces/innerkits/native/common/log.h b/interfaces/common/include/log.h similarity index 93% rename from interfaces/innerkits/native/common/log.h rename to interfaces/common/include/log.h index 7504b3f37f3bada618cf97ba4f60d5f76e17d021..01df08ea305f8cd0fcce936eeda09e68fecd2d8a 100644 --- a/interfaces/innerkits/native/common/log.h +++ b/interfaces/common/include/log.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef LOG_H -#define LOG_H +#ifndef APP_FILE_SERVICE_LOG_H +#define APP_FILE_SERVICE_LOG_H #include "hilog/log.h" namespace OHOS { @@ -33,4 +33,4 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, APP_LOG_DOM } // namespace AppFileService } // namespace OHOS -#endif // LOG_H \ No newline at end of file +#endif // APP_FILE_SERVICE_LOG_H \ No newline at end of file diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6ddc38278c643e20bb1e31086eb3423dba55da0 --- /dev/null +++ b/interfaces/common/src/common_func.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 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 + * + * 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 "common_func.h" + +#include +#include "uri.h" + +using namespace std; + +namespace OHOS { +namespace AppFileService { +const string PACKAGE_NAME_FLAG = ""; +const string CURRENT_USER_ID_FLAG = ""; +const vector SANDBOX_PATH = { + "/data/storage/el1/bundle", + "/data/storage/el2/base", + "/data/storage/el1/database", + "/data/storage/el2/database", + "/data/storage/el1/base", + "/data/storage/ark-cache", + "/data/storage/ark-profile", + "/data/storage/el2/distributedfiles", + "/mnt/data/fuse", + "/storage/local/Documents" +}; + +const vector LOWER_PATH = { + "/data/app/el1/bundle/public/", + "/data/app/el2//base/", + "/data/app/el1//database/", + "/data/app/el2//database/", + "/data/app/el1//base/", + "/data/local/ark-cache/", + "/data/local/ark-profile//", + "/mnt/hmdfs//account/merge_view/data/", + "/mnt/sandbox//mnt/data/fuse", + "/mnt/hmdfs//account/merge_view/files/Documents" +}; + +static string GetLowerPath(string &lowerPathHead, const string &lowerPathTail, + const string &userId, const string &bundleName) +{ + if (lowerPathHead.find(CURRENT_USER_ID_FLAG) != string::npos) { + lowerPathHead = lowerPathHead.replace(lowerPathHead.find(CURRENT_USER_ID_FLAG), + CURRENT_USER_ID_FLAG.length(), userId); + } + + if (lowerPathHead.find(PACKAGE_NAME_FLAG) != string::npos) { + lowerPathHead = lowerPathHead.replace(lowerPathHead.find(PACKAGE_NAME_FLAG), + PACKAGE_NAME_FLAG.length(), bundleName); + } + + return lowerPathHead + lowerPathTail; +} + +int32_t CommonFunc::GetPhysicalPath(const std::string &fileUri, const std::string &userId, + std::string &physicalPath) +{ + Uri uri(fileUri); + string bundleName = uri.GetAuthority(); + string sandboxPath = uri.GetPath(); + string lowerPathTail = ""; + string lowerPathHead = ""; + + for (size_t i = 0; i < SANDBOX_PATH.size(); i++) { + if (sandboxPath.length() >= SANDBOX_PATH[i].length()) { + string sandboxPathTemp = sandboxPath.substr(0, SANDBOX_PATH[i].length()); + if (sandboxPathTemp == SANDBOX_PATH[i]) { + lowerPathHead = LOWER_PATH[i]; + lowerPathTail = sandboxPath.substr(SANDBOX_PATH[i].length()); + break; + } + } + } + + if (lowerPathHead == "") { + return -EINVAL; + } else { + physicalPath = GetLowerPath(lowerPathHead, lowerPathTail, userId, bundleName); + return 0; + } +} + +bool CommonFunc::CheckValidPath(const std::string &filePath) +{ + if (filePath.empty() || filePath.size() >= PATH_MAX) { + return false; + } + + char realPath[PATH_MAX]{'\0'}; + if (realpath(filePath.c_str(), realPath) != nullptr && + strncmp(realPath, filePath.c_str(), filePath.size()) == 0) { + return true; + } else { + return false; + } +} +} // namespace AppFileService +} // namespace OHOS + diff --git a/interfaces/innerkits/remote_file_share/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn similarity index 53% rename from interfaces/innerkits/remote_file_share/native/BUILD.gn rename to interfaces/innerkits/native/BUILD.gn index baf5f45c310442b8a27ca99dc35a58f3b5224634..660bf024083786f6381845355c19f0149bfa5d81 100644 --- a/interfaces/innerkits/remote_file_share/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 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 @@ -13,32 +13,70 @@ import("//build/ohos.gni") import("//foundation/filemanagement/app_file_service/app_file_service.gni") + +config("file_share_config") { + visibility = [ ":*" ] + include_dirs = [ + "file_share/include", + "../../common/include", + ] +} + +ohos_shared_library("fileshare_native") { + sources = [ + "file_share/src/file_share.cpp", + "../../common/src/common_func.cpp", + ] + + public_configs = [ ":file_share_config" ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "hilog:libhilog", + ] + + part_name = "app_file_service" + subsystem_name = "filemanagement" +} + config("remote_file_share_config") { visibility = [ ":*" ] include_dirs = [ - "include", + "remote_file_share/include", "${utils_system_safwk_path}/native/include", "${path_base}/include", + "../../common/include", ".", ] } ohos_shared_library("remote_file_share_native") { - sources = [ "${app_file_service_path}/services/remote_file_share/src/remote_file_share.cpp" ] + sources = [ + "remote_file_share/src/remote_file_share.cpp", + "../../common/src/common_func.cpp", + ] public_configs = [ ":remote_file_share_config" ] external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", "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" } + +group("app_file_service_native") { + deps = [ + ":fileshare_native", + ":remote_file_share_native" ] +} \ No newline at end of file diff --git a/interfaces/innerkits/native/file_share/BUILD.gn b/interfaces/innerkits/native/file_share/BUILD.gn deleted file mode 100644 index 07ff0407b32a7db66f7f79fde9b058343e71a3f3..0000000000000000000000000000000000000000 --- a/interfaces/innerkits/native/file_share/BUILD.gn +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright (c) 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 -# -# 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") - -config("file_share_config") { - visibility = [ ":*" ] - include_dirs = [ - "include", - "../common", - ] -} - -ohos_shared_library("fileshare_native") { - sources = [ "src/file_share.cpp" ] - - public_configs = [ ":file_share_config" ] - - external_deps = [ - "ability_base:base", - "ability_base:want", - "ability_base:zuri", - "access_token:libaccesstoken_sdk", - "c_utils:utils", - "hilog:libhilog", - ] - - part_name = "app_file_service" - subsystem_name = "filemanagement" -} diff --git a/interfaces/innerkits/native/file_share/include/file_share.h b/interfaces/innerkits/native/file_share/include/file_share.h index 8f7c3430204f60e1fbbef530ccb0cacdaf013265..cf1178c6fb89429e7c3fd19a943b1de936ba199b 100644 --- a/interfaces/innerkits/native/file_share/include/file_share.h +++ b/interfaces/innerkits/native/file_share/include/file_share.h @@ -24,48 +24,6 @@ namespace OHOS { namespace AppFileService { using namespace std; -#define DIR_MODE (S_IRWXU | S_IXGRP | S_IXOTH) -#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) -#define READ_URI_PERMISSION OHOS::AAFwk::Want::FLAG_AUTH_READ_URI_PERMISSION -#define WRITE_URI_PERMISSION OHOS::AAFwk::Want::FLAG_AUTH_WRITE_URI_PERMISSION - -enum SHARE_FILE_TYPE { DIR_TYPE = 0, FILE_TYPE }; - -namespace { -const string FILE_SCHEME = "file"; -const string PACKAGE_NAME_FLAG = ""; -const string CURRENT_USER_ID_FLAG = ""; -const string DATA_APP_EL2_PATH = "/data/service/el2/"; -const string SHARE_R_PATH = "/r/"; -const string SHARE_RW_PATH = "/rw/"; -const string SHARE_PATH = "/share/"; -const string DLP_MANAGER_BUNDLE_NAME = "com.ohos.dlpmanager"; - -const vector SANDBOX_PATH = { - "/data/storage/el1/bundle", - "/data/storage/el2/base", - "/data/storage/el1/database", - "/data/storage/el2/database", - "/data/storage/el1/base", - "/data/storage/ark-cache", - "/data/storage/ark-profile", - "/data/storage/el2/distributedfiles", - "/mnt/data/fuse" -}; - -const vector LOWER_PATH = { - "/data/app/el1/bundle/public/", - "/data/app/el2//base/", - "/data/app/el1//database/", - "/data/app/el2//database/", - "/data/app/el1//base/", - "/data/local/ark-cache/", - "/data/local/ark-profile//", - "/mnt/hmdfs//account/merge_view/data/", - "/mnt/sandbox//mnt/data/fuse" -}; -} - #ifdef __cplusplus #if __cplusplus extern "C" { diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index f7cb9ba01acfab930aa96a2d167bbaf4ea00dda6..22b56a4fec891df4cdaab277103e684040e62813 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -23,12 +23,30 @@ #include #include "accesstoken_kit.h" +#include "common_func.h" #include "hap_token_info.h" #include "log.h" #include "uri.h" namespace OHOS { namespace AppFileService { +#define DIR_MODE (S_IRWXU | S_IXGRP | S_IXOTH) +#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) +#define READ_URI_PERMISSION OHOS::AAFwk::Want::FLAG_AUTH_READ_URI_PERMISSION +#define WRITE_URI_PERMISSION OHOS::AAFwk::Want::FLAG_AUTH_WRITE_URI_PERMISSION + +enum ShareFileType { + DIR_TYPE = 0, + FILE_TYPE = 1, +}; + +namespace { +const string FILE_SCHEME = "file"; +const string DATA_APP_EL2_PATH = "/data/service/el2/"; +const string SHARE_R_PATH = "/r/"; +const string SHARE_RW_PATH = "/rw/"; +const string SHARE_PATH = "/share/"; +} struct FileShareInfo { string providerBundleName_; @@ -38,7 +56,7 @@ struct FileShareInfo { vector sharePath_; vector isExist_; string currentUid_; - SHARE_FILE_TYPE type_; + ShareFileType type_; }; static int32_t GetTargetInfo(uint32_t tokenId, string &bundleName, string ¤tUid) @@ -59,10 +77,11 @@ static int32_t GetTargetInfo(uint32_t tokenId, string &bundleName, string &curre return 0; } -static void GetProviderBundleName(string uriStr, string &bundleName) +static void GetProviderInfo(string uriStr, FileShareInfo &info) { Uri uri(uriStr); - bundleName = uri.GetAuthority(); + info.providerBundleName_ = uri.GetAuthority(); + info.providerSandboxPath_ = uri.GetPath(); } static bool IsExistDir(const string &path) @@ -84,54 +103,6 @@ static bool IsExistFile(const string &path) return S_ISREG(buf.st_mode); } -static int32_t GetLowerPath(string &lowerPathHead, const string &lowerPathTail, FileShareInfo &info) -{ - if (lowerPathHead.empty()) { - return -EINVAL; - } - - if (lowerPathHead.find(CURRENT_USER_ID_FLAG) != string::npos) { - lowerPathHead = lowerPathHead.replace(lowerPathHead.find(CURRENT_USER_ID_FLAG), - CURRENT_USER_ID_FLAG.length(), info.currentUid_); - } - - if (lowerPathHead.find(PACKAGE_NAME_FLAG) != string::npos) { - lowerPathHead = lowerPathHead.replace(lowerPathHead.find(PACKAGE_NAME_FLAG), - PACKAGE_NAME_FLAG.length(), info.providerBundleName_); - } - - info.providerLowerPath_ = lowerPathHead + lowerPathTail; - return 0; -} - -static int32_t GetProviderPath(const string &uriStr, FileShareInfo &info) -{ - Uri uri(uriStr); - string pathInProvider = uri.GetPath(); - size_t num = SANDBOX_PATH.size(); - string lowerPathTail = "", lowerPathHead = ""; - - for (size_t i = 0; i < num; i++) { - if (pathInProvider.length() >= SANDBOX_PATH[i].length()) { - string sandboxPathTemp = pathInProvider.substr(0, SANDBOX_PATH[i].length()); - if (sandboxPathTemp == SANDBOX_PATH[i]) { - lowerPathHead = LOWER_PATH[i]; - lowerPathTail = pathInProvider.substr(SANDBOX_PATH[i].length()); - break; - } - } - } - - int32_t ret = GetLowerPath(lowerPathHead, lowerPathTail, info); - if (ret != 0) { - LOGE("Get lower path failed with %{public}d", ret); - return ret; - } - - info.providerSandboxPath_ = pathInProvider; - return 0; -} - static void GetSharePath(FileShareInfo &info, uint32_t flag) { string shareRPath = DATA_APP_EL2_PATH + info.currentUid_ + SHARE_PATH +info.targetBundleName_ + @@ -152,10 +123,10 @@ static void GetSharePath(FileShareInfo &info, uint32_t flag) static int32_t GetShareFileType(FileShareInfo &info) { if (IsExistFile(info.providerLowerPath_)) { - info.type_ = FILE_TYPE; + info.type_ = ShareFileType::FILE_TYPE; return 0; } else if (IsExistDir(info.providerLowerPath_)) { - info.type_ = DIR_TYPE; + info.type_ = ShareFileType::DIR_TYPE; return 0; } return -ENOENT; @@ -170,8 +141,9 @@ static int32_t GetFileShareInfo(const string &uri, uint32_t tokenId, uint32_t fl return ret; } - GetProviderBundleName(uri, info.providerBundleName_); - ret = GetProviderPath(uri, info); + GetProviderInfo(uri, info); + + ret = CommonFunc::GetPhysicalPath(uri, info.currentUid_, info.providerLowerPath_); if (ret != 0) { LOGE("Failed to get lower path %{public}d", ret); return ret; @@ -182,6 +154,7 @@ static int32_t GetFileShareInfo(const string &uri, uint32_t tokenId, uint32_t fl LOGE("Failed to get share file type %{public}d", ret); return ret; } + GetSharePath(info, flag); return 0; } @@ -209,24 +182,9 @@ static bool MakeDir(const string &path) return true; } -static bool CheckValidPath(const string &filePath) -{ - if (filePath.size() >= PATH_MAX) { - return false; - } - - char realPath[PATH_MAX]{'\0'}; - if (realpath(filePath.c_str(), realPath) != nullptr && - strncmp(realPath, filePath.c_str(), filePath.size()) == 0) { - return true; - } else { - return false; - } -} - static int32_t PreparePreShareDir(FileShareInfo &info) { - if (!CheckValidPath(info.providerLowerPath_)) { + if (!CommonFunc::CheckValidPath(info.providerLowerPath_)) { LOGE("Invalid share path with %{private}s", info.providerLowerPath_.c_str()); return -EINVAL; } @@ -267,15 +225,15 @@ int32_t CreateShareFile(const string &uri, uint32_t tokenId, uint32_t flag) continue; } - if (info.type_ == FILE_TYPE) { + if (info.type_ == ShareFileType::FILE_TYPE) { if ((ret = creat(info.sharePath_[i].c_str(), FILE_MODE)) < 0) { LOGE("Create file failed with %{public}d", errno); return -errno; } close(ret); - } else if (mkdir(info.sharePath_[i].c_str(), DIR_MODE) != 0) { - LOGE("Create dir failed with %{public}d", errno); - return -errno; + } else { + LOGE("Invalid argument not support dir to share"); + return -EINVAL; } if (mount(info.providerLowerPath_.c_str(), info.sharePath_[i].c_str(), 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 86% 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 f16c8d7b9fafbe8435a67037b6b21221ec392de0..dff1049d964ce94d90563e4be051270fd3959636 100644 --- a/interfaces/innerkits/remote_file_share/native/remote_file_share.h +++ b/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h @@ -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); + static int GetDfsUriFromLocal(const std::string &uriStr, const int &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 70% 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 865a694155839b8426257ef7caa7de15a63d84f9..920f5136152db448f8d2e89e29e6fbafa32ddba6 100644 --- a/services/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -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,32 @@ namespace { const int HMDFS_CID_SIZE = 64; const int USER_ID_INIT = 100; const unsigned HMDFS_IOC = 0xf2; + const std::string URI_HEAD = "file://"; + const std::string URI_MID = "/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 SHAER_FILE_PATH = "/account/merge_view/data/share.txt"; 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) +#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 localPath; + uint64_t distributedLen; + uint64_t distributedPath; +}; + static std::string GetProcessName() { char pthreadName[PATH_MAX]; @@ -163,6 +179,32 @@ static int CheckInputValidity(const int &fd, const int &userId, const std::strin deviceId.size() != HMDFS_CID_SIZE); } +static int GetDistributedPath(const std::string &bundleName, const int &userId, + const std::string &physicalPath, std::string &distributedPath) +{ + distributedPath = DST_PATH_HEAD + std::to_string(userId) + + DST_PATH_MID + bundleName + physicalPath; + if (distributedPath.size() >= PATH_MAX) { + return -EINVAL; + } + else { + return 0; + } +} + +size_t GetFileSize(const std::string &fileName) +{ + size_t fileSize; + struct stat statBuf; + + if (fileName.empty()) { + return 0; + } + stat(fileName.c_str(), &statBuf); + fileSize = statBuf.st_size; + return fileSize; +} + int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, const int &userId, const std::string &deviceId) { @@ -218,6 +260,68 @@ int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, LOGI("RemoteFileShare::CreateSharePath, create %{public}s successfully", sharePath.c_str()); return 0; } + +int RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, + const int &userId, struct HmdfsUriInfo &hui) +{ + int ret; + const char *physicalChar; + const char *distributedChar; + Uri uri(uriStr); + std::string bundleName = uri.GetAuthority(); + std::string physicalPath; + std::string distributedPath; + std::string usingFile = SHAER_PATH_HEAD + + std::to_string(userId) + + SHAER_FILE_PATH; + struct HmdfsDstInfo hdi; + ret = CommonFunc::GetPhysicalPath(uriStr, + std::to_string(userId), physicalPath); + if (ret != 0) { + LOGE("Failed to get lower path %{public}d", ret); + return ret; + } + if (!CommonFunc::CheckValidPath(physicalPath)) { + LOGE("Illegal path"); + return -EINVAL; + } + + ret = GetDistributedPath(bundleName, userId, physicalPath, distributedPath); + if (ret != 0) { + LOGE("Path is too long %{public}d", ret); + return ret; + } + + ret = ForceCreateDirectory(distributedPath.substr(0, + distributedPath.find_last_of('/'))); + if (ret == 0) { + LOGE("Open distributed path failed"); + return ret; + } + + int32_t hmdfsDevFd = open(usingFile.c_str(), O_RDONLY | O_CREAT); + if (hmdfsDevFd < 0) { + LOGE("RemoteFileShare::CreateShareFile, open share path failed with %{public}d", errno); + return errno; + } + + physicalChar = physicalPath.c_str(); + distributedChar = distributedPath.c_str(); + hdi.localLen = physicalPath.size() + 1; + hdi.localPath = (uint64_t)(physicalChar); + hdi.distributedLen = distributedPath.size() + 1; + hdi.distributedPath = (uint64_t)(distributedChar); + + ret = ioctl(hmdfsDevFd, HMDFS_IOC_GET_DST_PATH, &hdi); + if (ret != 0) { + LOGE("Ioctl failed %{public}d", ret); + return ret; + } + hui.fileSize = GetFileSize(physicalPath); + hui.uriStr = URI_HEAD + bundleName + URI_MID + physicalPath; + return 0; + +} } // namespace ModuleRemoteFileShare } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 5c68b54c5a9236662ab094b39cd109ea3052454a..b93c1e0850ecf5274dc474ea807e5edab017812f 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -39,7 +39,7 @@ ohos_shared_library("remotefileshare") { ohos_shared_library("fileshare") { include_dirs = [ ".", - "common", + "../../common/include", ] sources = [ @@ -71,7 +71,7 @@ ohos_shared_library("fileshare") { ohos_shared_library("fileuri") { include_dirs = [ ".", - "common", + "../../common/include", ] sources = [ diff --git a/interfaces/kits/js/common/log.h b/interfaces/kits/js/common/log.h deleted file mode 100644 index f9d3ece923ff4b0d30f3ad084309fd52c52ef4b3..0000000000000000000000000000000000000000 --- a/interfaces/kits/js/common/log.h +++ /dev/null @@ -1,36 +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 LOG_H -#define LOG_H -#include "hilog/log.h" - -namespace OHOS { -namespace AppFileService { -const unsigned int APP_LOG_DOMAIN = 0xD004313; -const char APP_LOG_TAG[] = "AppFileService"; -static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, APP_LOG_DOMAIN, APP_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 // 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 8af8d3df729976c272b396e478b48fc37da930bc..f3e9d436c1b43e213316417041be5e084bb765d4 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", ] }