From 5461b8b36a4b8bde46f197a1e131561d60c7709e Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Mon, 17 Jul 2023 13:10:30 +0000 Subject: [PATCH] =?UTF-8?q?fileuri=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: Ibe837c7e757e1b6d8ebe8f26097c91d2441cfac9 --- bundle.json | 9 ++ interfaces/common/include/common_func.h | 3 + interfaces/common/src/common_func.cpp | 89 ++++++++++++++++++- interfaces/innerkits/native/BUILD.gn | 47 ++++++++++ .../native/file_uri/include/file_uri.h | 39 ++++++++ .../native/file_uri/src/file_uri.cpp | 75 ++++++++++++++++ interfaces/kits/js/BUILD.gn | 1 + .../kits/js/file_uri/get_uri_from_path.cpp | 70 ++------------- .../kits/js/file_uri/get_uri_from_path.h | 8 -- 9 files changed, 268 insertions(+), 73 deletions(-) create mode 100644 interfaces/innerkits/native/file_uri/include/file_uri.h create mode 100644 interfaces/innerkits/native/file_uri/src/file_uri.cpp diff --git a/bundle.json b/bundle.json index cd037d76e..94d46a748 100644 --- a/bundle.json +++ b/bundle.json @@ -74,6 +74,15 @@ "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share/include" } }, + { + "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native:fileuri_native", + "header": { + "header_files": [ + "file_uri.h" + ], + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri/include" + } + }, { "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native:remote_file_share_native", "header": { diff --git a/interfaces/common/include/common_func.h b/interfaces/common/include/common_func.h index cbeb03d3b..da1facccf 100644 --- a/interfaces/common/include/common_func.h +++ b/interfaces/common/include/common_func.h @@ -27,6 +27,9 @@ public: static bool CheckValidPath(const std::string &filePath); static int32_t GetPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath); + static std::string GetSelfBundleName(); + static std::string GetUriFromPath(const std::string &path); + static bool CheckPublicDirPath(const std::string &sandboxPath); }; } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index fae72c9d9..575328f76 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -17,14 +17,21 @@ #include +#include "bundle_info.h" +#include "bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "uri.h" +#include "system_ability_definition.h" + #include "log.h" #include "json_utils.h" -#include "uri.h" using namespace std; namespace OHOS { namespace AppFileService { +using namespace OHOS::AppExecFwk; namespace { const string PACKAGE_NAME_FLAG = ""; const string CURRENT_USER_ID_FLAG = ""; @@ -32,6 +39,11 @@ namespace { const string SANDBOX_PATH_KEY = "sandbox-path"; const string MOUNT_PATH_MAP_KEY = "mount-path-map"; const string SANDBOX_JSON_FILE_PATH = "/etc/app_file_service/file_share_sandbox.json"; + const std::string FILE_SCHEME_PREFIX = "file://"; + const char BACKFLASH = '/'; + const std::vector PUBLIC_DIR_PATHS = { + "/Documents" + }; } std::unordered_map CommonFunc::sandboxPathMap_; @@ -122,6 +134,81 @@ bool CommonFunc::CheckValidPath(const std::string &filePath) return false; } } + +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; + } + + return iface_cast(remoteObject); +} + +string CommonFunc::GetSelfBundleName() +{ + int uid = -1; + uid = IPCSkeleton::GetCallingUid(); + + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + LOGE("GetSelfBundleName: bundle mgr proxy is nullptr."); + return ""; + } + + BundleInfo bundleInfo; + auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); + if (ret != ERR_OK) { + LOGE("GetSelfBundleName: bundleName get fail. uid is %{public}d", uid); + return ""; + } + + return bundleInfo.name; +} + +bool CommonFunc::CheckPublicDirPath(const std::string &sandboxPath) +{ + for (const std::string &path : PUBLIC_DIR_PATHS) { + if (sandboxPath.find(path) == 0) { + return true; + } + } + return false; +} + +static bool NormalizePath(string &path) +{ + if (path.size() <= 0) { + return false; + } + + if (path[0] != BACKFLASH) { + path.insert(0, 1, BACKFLASH); + } + + return true; +} + +string CommonFunc::GetUriFromPath(const string &path) +{ + string realPath = path; + if (!realPath.empty() && !NormalizePath(realPath)) { + LOGE("GetUriFromPath::NormalizePath failed!"); + return ""; + } + + string packageName = GetSelfBundleName(); + realPath = FILE_SCHEME_PREFIX + packageName + realPath; + return realPath; +} } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 833d8305d..6e53db4b5 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -23,6 +23,15 @@ config("file_share_config") { ] } +config("file_uri_config") { + visibility = [ ":*" ] + include_dirs = [ + "file_uri/include", + "//third_party/json/include", + "../../common/include", + ] +} + ohos_shared_library("fileshare_native") { sources = [ "../../common/src/common_func.cpp", @@ -36,9 +45,41 @@ ohos_shared_library("fileshare_native") { "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", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + part_name = "app_file_service" + subsystem_name = "filemanagement" +} + +ohos_shared_library("fileuri_native") { + sources = [ + "../../common/src/common_func.cpp", + "../../common/src/json_utils.cpp", + "file_uri/src/file_uri.cpp", + ] + + public_configs = [ ":file_uri_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", "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", ] part_name = "app_file_service" @@ -78,8 +119,13 @@ ohos_shared_library("remote_file_share_native") { "ability_base:base", "ability_base:want", "ability_base:zuri", + "ability_runtime:abilitykit_native", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", ] innerapi_tags = [ "platformsdk_indirect" ] @@ -90,6 +136,7 @@ ohos_shared_library("remote_file_share_native") { group("app_file_service_native") { deps = [ ":fileshare_native", + ":fileuri_native", ":remote_file_share_native", ] } diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h new file mode 100644 index 000000000..eb76514c3 --- /dev/null +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -0,0 +1,39 @@ +/* + * 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 APP_FILE_SERVICE_FILE_URI_FILE_URI_H +#define APP_FILE_SERVICE_FILE_URI_FILE_URI_H + +#include + +#include "uri.h" +namespace OHOS { +namespace AppFileService { +namespace ModuleFileUri { +class FileUri { +public: + std::string GetName(); + std::string GetPath(); + std::string ToString(); + + explicit FileUri(const std::string &uriOrPath); + ~FileUri() = default; +private: + Uri uri_; +}; +} // ModuleFileUri +} // namespace AppFileService +} // namespace OHOS +#endif // APP_FILE_SERVICE_FILE_URI_FILE_URI_H \ No newline at end of file diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp new file mode 100644 index 000000000..43bc3e990 --- /dev/null +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -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. + */ + +#include "file_uri.h" + +#include + +#include "uri.h" + +#include "common_func.h" +#include "log.h" + +using namespace std; +namespace OHOS { +namespace AppFileService { +namespace ModuleFileUri { +const std::string PATH_SHARE = "/data/storage/el2/share"; +const std::string MODE_RW = "/rw/"; +const std::string MODE_R = "/r/"; +const std::string FILE_SCHEME_PREFIX = "file://"; +string FileUri::GetName() +{ + string sandboxPath = uri_.GetPath(); + size_t posLast = sandboxPath.find_last_of("/"); + if (posLast == string::npos) { + return sandboxPath; + } + + if (posLast == sandboxPath.size()) { + return ""; + } + + return sandboxPath.substr(posLast + 1); +} + +string FileUri::GetPath() +{ + string sandboxPath = uri_.GetPath(); + string realPath = sandboxPath; + string providerBundleName = uri_.GetAuthority(); + string targetBundleName = CommonFunc::GetSelfBundleName(); + if (CommonFunc::CheckPublicDirPath(realPath) || targetBundleName != providerBundleName) { + realPath = PATH_SHARE + MODE_RW + providerBundleName + sandboxPath; + if (access(realPath.c_str(), F_OK) != 0) { + realPath = PATH_SHARE + MODE_R + providerBundleName + sandboxPath; + } + } + + return realPath; +} + +string FileUri::ToString() +{ + return uri_.ToString(); +} + +FileUri::FileUri(const string &uriOrPath): uri_( + (uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath) +) +{} +} +} // namespace AppFileService +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index b93c1e085..58b987fc6 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -75,6 +75,7 @@ ohos_shared_library("fileuri") { ] sources = [ + "../../common/src/common_func.cpp", "file_uri/file_uri_n_exporter.cpp", "file_uri/get_uri_from_path.cpp", ] diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.cpp b/interfaces/kits/js/file_uri/get_uri_from_path.cpp index 0c84d6558..da98d8611 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -14,71 +14,15 @@ */ #include "get_uri_from_path.h" -#include "bundle_info.h" -#include "bundle_mgr_proxy.h" -#include "ipc_skeleton.h" -#include "iservice_registry.h" -#include "log.h" #include "status_receiver_host.h" -#include "system_ability_definition.h" + +#include "common_func.h" +#include "log.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; - } - - return iface_cast(remoteObject); -} - -static string GetBundleName() -{ - int uid = -1; - uid = IPCSkeleton::GetCallingUid(); - - sptr bundleMgrProxy = GetBundleMgrProxy(); - if (!bundleMgrProxy) { - LOGE("GetBundleName: bundle mgr proxy is nullptr."); - return nullptr; - } - - BundleInfo bundleInfo; - auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); - if (ret != ERR_OK) { - LOGE("GetBundleName: bundleName get fail. uid is %{public}d", uid); - return nullptr; - } - - return bundleInfo.name; -} - -static bool NormalizePath(string &path) -{ - if (path.size() <= 0) { - return false; - } - - if (path[0] != SCHEME_PATH_BEGIN) { - path.insert(0, 1, SCHEME_PATH_BEGIN); - } - - return true; -} napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) { @@ -95,15 +39,13 @@ napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) return nullptr; } - string realPath = path.get(); - if (!realPath.empty() && !NormalizePath(realPath)) { - LOGE("GetUriFromPath::NormalizePath failed!"); + std::string uri = CommonFunc::GetUriFromPath(path.get()); + if (uri == "") { + LOGE("GetUriFromPath failed!"); NError(EINVAL).ThrowErr(env); return nullptr; } - string packageName = GetBundleName(); - string uri = SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + packageName + realPath; return NVal::CreateUTF8String(env, uri).val_; } diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.h b/interfaces/kits/js/file_uri/get_uri_from_path.h index 46ee4fc43..8adb88fb9 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.h +++ b/interfaces/kits/js/file_uri/get_uri_from_path.h @@ -22,14 +22,6 @@ namespace OHOS { namespace AppFileService { namespace ModuleFileUri { -using namespace std; - -const string SCHEME = "file"; -const char SCHEME_SEPARATOR = ':'; -const string PATH_SYMBOLS = "//"; -const string FRAGMENT_SYMBOLS = "#"; -const char SCHEME_PATH_BEGIN = '/'; - class GetUriFromPath final { public: static napi_value Sync(napi_env env, napi_callback_info info); -- Gitee