From ae1cdfc68a483fd5651d64543f8ad78747264af5 Mon Sep 17 00:00:00 2001 From: "zhangkaixiang5@huawei.com" Date: Mon, 16 Jan 2023 11:55:18 +0800 Subject: [PATCH] Add CreateShareFile and DeleteShareFile Interface. Update IsMediaUri and GetBundleName Function. Signed-off-by: zhangkaixiang5@huawei.com --- BUILD.gn | 1 + bundle.json | 9 + .../innerkits/file_share/native/BUILD.gn | 53 ++++ .../innerkits/file_share/native/file_share.h | 72 +++++ .../file_share/native/file_share_log.h | 36 +++ .../remote_file_share/native/BUILD.gn | 0 .../native/remote_file_share.h | 0 .../native/remote_file_share_log.h | 0 interfaces/kits/js/@ohos.remotefileshare.d.ts | 0 interfaces/kits/js/BUILD.gn | 10 +- interfaces/kits/js/common/log.h | 0 .../js/file_share/fileshare_n_exporter.cpp | 0 .../kits/js/file_share/fileshare_n_exporter.h | 0 .../js/file_share/grant_uri_permission.cpp | 8 +- .../kits/js/file_share/grant_uri_permission.h | 0 .../kits/js/file_uri/file_uri_n_exporter.cpp | 0 .../kits/js/file_uri/file_uri_n_exporter.h | 0 .../kits/js/file_uri/get_uri_from_path.cpp | 46 ++- .../kits/js/file_uri/get_uri_from_path.h | 0 .../remotefileshare_n_exporter.cpp | 0 .../remotefileshare_n_exporter.h | 0 .../remotefileshare_napi.cpp | 0 .../remote_file_share/remotefileshare_napi.h | 0 services/file_share/src/file_share.cpp | 291 ++++++++++++++++++ .../src/remote_file_share.cpp | 0 test/fuzztest/BUILD.gn | 0 test/fuzztest/remotefileshare_fuzzer/BUILD.gn | 0 .../remotefileshare_fuzzer/corpus/init | 0 .../remotefileshare_fuzzer/project.xml | 0 .../remotefileshare_fuzzer.cpp | 0 .../remotefileshare_fuzzer.h | 0 test/unittest/BUILD.gn | 6 +- test/unittest/file_share_native/BUILD.gn | 75 +++++ .../file_share_native_test.cpp | 54 ++++ .../file_share_native/file_share_test.cpp | 57 ++++ test/unittest/remote_file_share/BUILD.gn | 0 .../remote_file_share_test.cpp | 0 test/unittest/resource/ohos_test.xml | 0 .../resource/remote_file_share_test.txt | 0 39 files changed, 710 insertions(+), 8 deletions(-) create mode 100755 interfaces/innerkits/file_share/native/BUILD.gn create mode 100755 interfaces/innerkits/file_share/native/file_share.h create mode 100755 interfaces/innerkits/file_share/native/file_share_log.h mode change 100644 => 100755 interfaces/innerkits/remote_file_share/native/BUILD.gn mode change 100644 => 100755 interfaces/innerkits/remote_file_share/native/remote_file_share.h mode change 100644 => 100755 interfaces/innerkits/remote_file_share/native/remote_file_share_log.h mode change 100644 => 100755 interfaces/kits/js/@ohos.remotefileshare.d.ts mode change 100644 => 100755 interfaces/kits/js/BUILD.gn mode change 100644 => 100755 interfaces/kits/js/common/log.h mode change 100644 => 100755 interfaces/kits/js/file_share/fileshare_n_exporter.cpp mode change 100644 => 100755 interfaces/kits/js/file_share/fileshare_n_exporter.h mode change 100644 => 100755 interfaces/kits/js/file_share/grant_uri_permission.cpp mode change 100644 => 100755 interfaces/kits/js/file_share/grant_uri_permission.h mode change 100644 => 100755 interfaces/kits/js/file_uri/file_uri_n_exporter.cpp mode change 100644 => 100755 interfaces/kits/js/file_uri/file_uri_n_exporter.h mode change 100644 => 100755 interfaces/kits/js/file_uri/get_uri_from_path.cpp mode change 100644 => 100755 interfaces/kits/js/file_uri/get_uri_from_path.h mode change 100644 => 100755 interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.cpp mode change 100644 => 100755 interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.h mode change 100644 => 100755 interfaces/kits/js/remote_file_share/remotefileshare_napi.cpp mode change 100644 => 100755 interfaces/kits/js/remote_file_share/remotefileshare_napi.h create mode 100755 services/file_share/src/file_share.cpp mode change 100644 => 100755 services/remote_file_share/src/remote_file_share.cpp mode change 100644 => 100755 test/fuzztest/BUILD.gn mode change 100644 => 100755 test/fuzztest/remotefileshare_fuzzer/BUILD.gn mode change 100644 => 100755 test/fuzztest/remotefileshare_fuzzer/corpus/init mode change 100644 => 100755 test/fuzztest/remotefileshare_fuzzer/project.xml mode change 100644 => 100755 test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.cpp mode change 100644 => 100755 test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.h mode change 100644 => 100755 test/unittest/BUILD.gn create mode 100755 test/unittest/file_share_native/BUILD.gn create mode 100755 test/unittest/file_share_native/file_share_native_test.cpp create mode 100755 test/unittest/file_share_native/file_share_test.cpp mode change 100644 => 100755 test/unittest/remote_file_share/BUILD.gn mode change 100644 => 100755 test/unittest/remote_file_share/remote_file_share_test.cpp mode change 100644 => 100755 test/unittest/resource/ohos_test.xml mode change 100644 => 100755 test/unittest/resource/remote_file_share_test.txt diff --git a/BUILD.gn b/BUILD.gn index 2c041cd26..f96b2e73e 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -16,6 +16,7 @@ import("//build/ohos.gni") group("libremotefileshare") { deps = [ "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", + "//foundation/filemanagement/app_file_service/interfaces/innerkits/file_share/native:file_share_native", "//foundation/filemanagement/app_file_service/interfaces/kits/js:remotefileshare", ] } diff --git a/bundle.json b/bundle.json index 8e6c15411..119089476 100644 --- a/bundle.json +++ b/bundle.json @@ -32,6 +32,15 @@ "service_group": [] }, "inner_kits": [ + { + "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/file_share/native:file_share_native", + "header": { + "header_files": [ + "file_share.h" + ], + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/file_share/native" + } + }, { "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", "header": { diff --git a/interfaces/innerkits/file_share/native/BUILD.gn b/interfaces/innerkits/file_share/native/BUILD.gn new file mode 100755 index 000000000..d3d46d250 --- /dev/null +++ b/interfaces/innerkits/file_share/native/BUILD.gn @@ -0,0 +1,53 @@ +# 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", + "//utils/system/safwk/native/include", + "//commonlibrary/c_utils/base/include", + "//foundation/filemanagement/app_file_service/interfaces/innerkits/file_share/native", + "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//foundation/ability/ability_base/interfaces/kits/native/uri/include", + ] +} + +ohos_shared_library("file_share_native") { + sources = [ "//foundation/filemanagement/app_file_service/services/file_share/src/file_share.cpp" ] + + public_configs = [ ":file_share_config" ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "ability_runtime:abilitykit_native", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + # relative_install_dir = "module" + # sys/lib64/module + # sys/lib64 + + part_name = "app_file_service" + subsystem_name = "filemanagement" +} diff --git a/interfaces/innerkits/file_share/native/file_share.h b/interfaces/innerkits/file_share/native/file_share.h new file mode 100755 index 000000000..c3abaf478 --- /dev/null +++ b/interfaces/innerkits/file_share/native/file_share.h @@ -0,0 +1,72 @@ +/* + * 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. + */ + +#ifndef FILE_SHARE_H +#define FILE_SHARE_H + +#include +#include +#include "accesstoken_kit.h" +#include "hap_token_info.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileShare { +using namespace std; +using TokenId = OHOS::Security::AccessToken::AccessTokenID; + +namespace { +const string FILE_SCHEME = "file"; +const string DATA_STORAGE_PATH = "/data/storage/"; +const string PACKAGE_NAME_FLAG = ""; +const string CURRENT_USER_ID_FLAG = ""; +const string DATA_APP_EL2_PATH = "/data/app/el2/"; +const string SHARE_R_PATH = "/r/"; +const string SHARE_RW_PATH = "/rw/"; +const string SHARE_PATH = "/share/"; + +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" +}; + +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//" +}; +} + +class FileShare { +public: + FileShare() {} + static int CreateShareFile(const string &uri, TokenId tokenID, int flag); + static int DeleteShareFile(const string &uri, TokenId tokenID, int flag); + ~FileShare() {} +}; +} // namespace ModuleFileShare +} // namespace AppFileService +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/interfaces/innerkits/file_share/native/file_share_log.h b/interfaces/innerkits/file_share/native/file_share_log.h new file mode 100755 index 000000000..7353db72b --- /dev/null +++ b/interfaces/innerkits/file_share/native/file_share_log.h @@ -0,0 +1,36 @@ +/* + * 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. + */ +#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/interfaces/innerkits/remote_file_share/native/BUILD.gn b/interfaces/innerkits/remote_file_share/native/BUILD.gn old mode 100644 new mode 100755 diff --git a/interfaces/innerkits/remote_file_share/native/remote_file_share.h b/interfaces/innerkits/remote_file_share/native/remote_file_share.h old mode 100644 new mode 100755 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 old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/@ohos.remotefileshare.d.ts b/interfaces/kits/js/@ohos.remotefileshare.d.ts old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn old mode 100644 new mode 100755 index ddd3de47a..6e4a29c48 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -84,11 +84,17 @@ ohos_shared_library("fileuri") { ] deps = [ - "//foundation/bundlemanager/bundle_framework/interfaces/kits/native/bundle:bundle_ndk", "//foundation/filemanagement/file_api/utils/filemgmt_libn:filemgmt_libn", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "ability_runtime:abilitykit_native", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] relative_install_dir = "module/file" diff --git a/interfaces/kits/js/common/log.h b/interfaces/kits/js/common/log.h old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/file_share/fileshare_n_exporter.cpp b/interfaces/kits/js/file_share/fileshare_n_exporter.cpp old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/file_share/fileshare_n_exporter.h b/interfaces/kits/js/file_share/fileshare_n_exporter.h old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/file_share/grant_uri_permission.cpp b/interfaces/kits/js/file_share/grant_uri_permission.cpp old mode 100644 new mode 100755 index 249964ffc..b6b7a514d --- a/interfaces/kits/js/file_share/grant_uri_permission.cpp +++ b/interfaces/kits/js/file_share/grant_uri_permission.cpp @@ -25,6 +25,7 @@ using namespace OHOS::DataShare; using namespace OHOS::FileManagement::LibN; +using namespace OHOS::DistributedFS::ModuleRemoteUri; namespace OHOS { namespace AppFileService { @@ -59,7 +60,7 @@ namespace ModuleFileShare { return rowNum; } - static string GetModeFromFlag(int flag) + static string GetModeFromFlag(unsigned int flag) { string mode = ""; if (flag & OHOS::AAFwk::Want::FLAG_AUTH_READ_URI_PERMISSION) { @@ -82,7 +83,10 @@ namespace ModuleFileShare { return nullptr; } - if (!DistributedFS::ModuleRemoteUri::RemoteUri::IsMediaUri(path.get())) { + RemoteUri remoteUri = RemoteUri(path.get()); + string uriScheme = remoteUri.GetScheme(); + string uriPath = remoteUri.GetPath(); + if (!RemoteUri::IsMediaUri(uriScheme, uriPath)) { LOGE("FileShare::GetJSArgs path parameter format error!"); NError(EINVAL).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/file_share/grant_uri_permission.h b/interfaces/kits/js/file_share/grant_uri_permission.h old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.h b/interfaces/kits/js/file_uri/file_uri_n_exporter.h old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.cpp b/interfaces/kits/js/file_uri/get_uri_from_path.cpp old mode 100644 new mode 100755 index abba57d32..9c152ab22 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -13,18 +13,58 @@ * limitations under the License. */ #include "get_uri_from_path.h" + +#include "bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" #include "log.h" -#include "native_interface_bundle.h" +#include "status_receiver_host.h" +#include "system_ability_definition.h" namespace OHOS { namespace AppFileService { namespace ModuleFileUri { using namespace OHOS::FileManagement::LibN; +using namespace OHOS::AppExecFwk; + +static sptr GetBundleMgrProxy() +{ + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + LOGE("fail to get system ability mgr."); + return nullptr; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + LOGE("fail to get bundle manager proxy."); + return nullptr; + } + + LOGI("GetBundleMgrProxy: get bundle manager proxy success."); + return iface_cast(remoteObject); +} static string GetBundleName() { - OH_NativeBundle_ApplicationInfo nativeApplicationInfo = OH_NativeBundle_GetCurrentApplicationInfo(); - return nativeApplicationInfo.bundleName; + int uid = -1; + uid = IPCSkeleton::GetCallingUid(); + + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + LOGE("GetBundleName: bundle mgr proxy is nullptr."); + return nullptr; + } + + string bundleName; + if (!bundleMgrProxy->GetBundleNameForUid(uid, bundleName)) { + LOGE("GetBundleName: bundleName get fail. uid is %{public}d", uid); + return nullptr; + } + + LOGI("GetBundleName: uid is %{public}d, bundleName is %{public}s", uid, bundleName.c_str()); + return bundleName; } napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.h b/interfaces/kits/js/file_uri/get_uri_from_path.h old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.cpp b/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.cpp old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.h b/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.h old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/remote_file_share/remotefileshare_napi.cpp b/interfaces/kits/js/remote_file_share/remotefileshare_napi.cpp old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/remote_file_share/remotefileshare_napi.h b/interfaces/kits/js/remote_file_share/remotefileshare_napi.h old mode 100644 new mode 100755 diff --git a/services/file_share/src/file_share.cpp b/services/file_share/src/file_share.cpp new file mode 100755 index 000000000..242480ca4 --- /dev/null +++ b/services/file_share/src/file_share.cpp @@ -0,0 +1,291 @@ +/* + * 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 "file_share.h" +#include +#include +#include +#include +#include +#include "bundle_mgr_interface.h" +#include "bundlemgr/bundle_mgr_proxy.h" +#include "file_share_log.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "status_receiver_host.h" +#include "system_ability_definition.h" +#include "uri.h" + + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileShare { +using namespace OHOS::AppExecFwk; + +struct FileShareInfo { + string providerBundleName; + string targetBundleName; + string providerLowerPath; + string providerSandboxPath; + string sharePath; + string currentUid; +}; + +static int GetTargetInfo(TokenId tokenID, string &bundleName, string ¤tUid) +{ + Security::AccessToken::HapTokenInfo hapInfo; + int result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenID, hapInfo); + if (result != 0) { + LOGE("FileShare::GetTargetBundleName, failed to get hap token info %{public}d", result); + return result; + } + bundleName = hapInfo.bundleName; + currentUid = to_string(hapInfo.userID); + LOGI("FileShare::GetTargetBundleName, bundle Name is %{public}s", bundleName.c_str()); + LOGI("FileShare::GetTargetBundleName, current uid is %{public}s", currentUid.c_str()); + return 0; +} + +static void GetProviderBundleName(string uriStr, string &bundleName) +{ + Uri uri(uriStr); + bundleName = uri.GetAuthority(); +} + +static bool IsExistDir(const string &path) +{ + struct stat buf = {}; + if (stat(path.c_str(), &buf) != 0) { + return false; + } + return S_ISDIR(buf.st_mode); +} + +static bool IsExistFile(const string &path) +{ + struct stat buf = {}; + if (stat(path.c_str(), &buf) != 0) { + return false; + } + return S_ISREG(buf.st_mode); +} + +static int GetProviderPath(const string &uriStr, TokenId tokenID, FileShareInfo &info) +{ + Uri uri(uriStr); + string pathInProvider = uri.GetPath(); + string::size_type pos = pathInProvider.find(DATA_STORAGE_PATH); + if (pos == string::npos) { + return -EINVAL; + } + + 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; + } + } + } + + if (lowerPathHead.empty() || lowerPathTail.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); + } + LOGI("FileShare::GetProviderPath, providerLowerPath is %{public}s", (lowerPathHead + lowerPathTail).c_str()); + info.providerLowerPath = lowerPathHead + lowerPathTail; + info.providerSandboxPath = pathInProvider; + return 0; +} + +static string GetModePath(int flag) +{ + if ((flag & 0x2) == 0x2) { + return SHARE_RW_PATH; + } + return SHARE_R_PATH; +} + +static void GetSharePath(FileShareInfo &info, int flag) +{ + string shareModePath = GetModePath(flag); + info.sharePath = DATA_APP_EL2_PATH + info.currentUid + SHARE_PATH +info.targetBundleName + + shareModePath + info.providerBundleName + info.providerSandboxPath; +} + +static int GetFileShareInfo(const string &uri, TokenId tokenId, int flag, FileShareInfo &info) +{ + int ret = 0; + ret = GetTargetInfo(tokenId, info.targetBundleName, info.currentUid); + if (ret!= 0) { + return ret; + } + + GetProviderBundleName(uri, info.providerBundleName); + ret = GetProviderPath(uri, tokenId, info); + if (ret!= 0) { + return ret; + } + + GetSharePath(info, flag); + return 0; +} + +static bool MakeDir(const string &path, int flag) +{ + string::size_type index = 0; + string subPath; + do { + index = path.find('/', index + 1); + if (index == string::npos) { + subPath = path; + } else { + subPath = path.substr(0, index); + } + + if (access(subPath.c_str(), 0) != 0) { + if (mkdir(subPath.c_str(), flag) != 0) { + return false; + } + } + } while (index != string::npos); + + LOGE("FileShare::MakeDir, make dir is %{public}s", subPath.c_str()); + return true; +} + +string IncludeTrailingPathDelimiter(const string& path) +{ + if (path.rfind("/") != path.size() - 1) { + return path + "/"; + } + return path; +} + +bool RemoveFile(const string& fileName) +{ + if (access(fileName.c_str(), F_OK) == 0) { + return remove(fileName.c_str()) == 0; + } + + return true; +} + +static bool RemoveDir(const string& path) +{ + string subPath; + bool ret = true; + DIR *dir = opendir(path.c_str()); + if (dir == nullptr) { + return false; + } + + while (true) { + struct dirent *ptr = readdir(dir); + if (ptr == nullptr) { + break; + } + + if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) { + continue; + } + subPath = IncludeTrailingPathDelimiter(path) + string(ptr->d_name); + + subPath = path + string(ptr->d_name); + if (ptr->d_type == DT_DIR) { + ret = RemoveDir(subPath); + } else { + if (access(subPath.c_str(), F_OK) == 0) { + if (remove(subPath.c_str()) != 0) { + closedir(dir); + return false; + } + } + } + } + closedir(dir); + + return ret; +} + +bool DeleteDir(const string &path) +{ + if (IsExistFile(path)) { + return RemoveFile(path); + } + if (IsExistDir(path)) { + return RemoveDir(path); + } + return false; +} + +int FileShare::CreateShareFile(const string &uri, TokenId tokenID, int flag) +{ + FileShareInfo info; + int ret = GetFileShareInfo(uri, tokenID, flag, info); + if (ret!= 0) { + return ret; + } + + if (IsExistFile(info.providerLowerPath)) { + size_t posLast = info.sharePath.find_last_of("/"); + string sharePathDir = info.sharePath.substr(0, posLast); + if (!MakeDir(sharePathDir.c_str(), flag)) { + LOGE("FileShare::CreateShareFile, make dir failed with %{public}d", errno); + return errno; + } + if (link(info.providerLowerPath.c_str(), info.sharePath.c_str()) != 0) { + LOGE("FileShare::CreateShareFile, symlink failed with %{public}d", errno); + return errno; + } + LOGI("FileShare::CreateShareFile, Create Share File Successful!"); + return 0; + } + LOGE("FileShare::CreateShareFile, Create Share File Failed! File not Exist!"); + return -EEXIST; +} + +int FileShare::DeleteShareFile(const string &uri, Security::AccessToken::AccessTokenID tokenID, int flag) +{ + FileShareInfo info; + int ret = GetFileShareInfo(uri, tokenID, flag, info); + if (ret!= 0) { + return ret; + } + + string currentUid = info.currentUid; + string sharePath = DATA_APP_EL2_PATH + currentUid + SHARE_PATH + info.targetBundleName; + if (!DeleteDir(sharePath)) { + LOGE("FileShare::DeleteShareFile, delete dir failed with %{public}d", errno); + return errno; + } + LOGI("FileShare::DeleteShareFile, delete Share File Success"); + return 0; +} +} // namespace ModuleFileShare +} // namespace AppFileService +} // namespace OHOS diff --git a/services/remote_file_share/src/remote_file_share.cpp b/services/remote_file_share/src/remote_file_share.cpp old mode 100644 new mode 100755 diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn old mode 100644 new mode 100755 diff --git a/test/fuzztest/remotefileshare_fuzzer/BUILD.gn b/test/fuzztest/remotefileshare_fuzzer/BUILD.gn old mode 100644 new mode 100755 diff --git a/test/fuzztest/remotefileshare_fuzzer/corpus/init b/test/fuzztest/remotefileshare_fuzzer/corpus/init old mode 100644 new mode 100755 diff --git a/test/fuzztest/remotefileshare_fuzzer/project.xml b/test/fuzztest/remotefileshare_fuzzer/project.xml old mode 100644 new mode 100755 diff --git a/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.cpp b/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.cpp old mode 100644 new mode 100755 diff --git a/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.h b/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.h old mode 100644 new mode 100755 diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn old mode 100644 new mode 100755 index d81880b40..2187f0004 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -13,5 +13,9 @@ group("unittest") { testonly = true - deps = [ "remote_file_share:remote_file_share_test" ] + deps = [ + "remote_file_share:remote_file_share_test", + "file_share_native:file_share_test", + "file_share_native:createFileShare", + ] } diff --git a/test/unittest/file_share_native/BUILD.gn b/test/unittest/file_share_native/BUILD.gn new file mode 100755 index 000000000..45ec0eb6e --- /dev/null +++ b/test/unittest/file_share_native/BUILD.gn @@ -0,0 +1,75 @@ +# 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/test.gni") + +ohos_unittest("file_share_test") { + module_out_path = "filemanagement/app_file_service" + sources = [ + "file_share_test.cpp", + ] + include_dirs = [ + "//utils/system/safwk/native/include", + "//commonlibrary/c_utils/base/include", + ] + + deps = [ + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "app_file_service:file_share_native", + "ability_runtime:abilitykit_native", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +ohos_executable("createFileShare") { + + sources = [ + "file_share_native_test.cpp", + ] + include_dirs = [ + "include", + "../../../interfaces/innerkits/file_share", + "//utils/system/safwk/native/include", + "//commonlibrary/c_utils/base/include", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "app_file_service:file_share_native", + "ability_runtime:abilitykit_native", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + relative_install_dir = "module" + + part_name = "app_file_service" + subsystem_name = "filemanagement" +} diff --git a/test/unittest/file_share_native/file_share_native_test.cpp b/test/unittest/file_share_native/file_share_native_test.cpp new file mode 100755 index 000000000..d97f692b5 --- /dev/null +++ b/test/unittest/file_share_native/file_share_native_test.cpp @@ -0,0 +1,54 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include "native/file_share.h" + + +using namespace std; +using namespace OHOS::AppFileService::ModuleFileShare; + +int main(int argc, char* argv[]) { + cout << "CreateShareFile start2!" << endl; + unsigned int tokenID; + string uri; + if (argc == 4) { + for (int i = 0; i < 4; i++) { + cout << "argument[" << i << "] is: " << argv[i] << endl; + } + tokenID = atoi(argv[1]); + uri = argv[2]; + int flag = atoi(argv[3]); + cout << "tokenID:"<< tokenID << endl; + cout << "uri:"<< uri << endl; + cout << "flag:"<< flag << endl; + int ret = 0; + ret = FileShare::CreateShareFile(uri, tokenID, flag); + if (ret != 0) { + cout << "CreateShareFile error with " << ret < +#include +#include +#include +#include +#include +#include +#include "file_share.h" + +namespace { + using namespace std; + using namespace OHOS::AppFileService::ModuleFileShare; + + const int E_OK = 0; + + class FileShareTest : public testing::Test { + public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; + }; + + /** + * @tc.name: file_share_test_0000 + * @tc.desc: Test function of FileShare() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileShareTest, file_share_FileShare_0000, testing::ext::TestSize.Level1) + { + string uri = "file://com.example.demoa/data/storage/el2/base/files/1.txt"; + unsigned int tokenID = 100; + int flag = 1; + int ret = FileShare::CreateShareFile(uri, tokenID, flag); + EXPECT_EQ(ret, E_OK); + ret = FileShare::DeleteShareFile(uri, tokenID, flag); + EXPECT_EQ(ret, E_OK); + } +} \ No newline at end of file diff --git a/test/unittest/remote_file_share/BUILD.gn b/test/unittest/remote_file_share/BUILD.gn old mode 100644 new mode 100755 diff --git a/test/unittest/remote_file_share/remote_file_share_test.cpp b/test/unittest/remote_file_share/remote_file_share_test.cpp old mode 100644 new mode 100755 diff --git a/test/unittest/resource/ohos_test.xml b/test/unittest/resource/ohos_test.xml old mode 100644 new mode 100755 diff --git a/test/unittest/resource/remote_file_share_test.txt b/test/unittest/resource/remote_file_share_test.txt old mode 100644 new mode 100755 -- Gitee