From 634f927ea5729db6e1fd9f85b07c049937f246f9 Mon Sep 17 00:00:00 2001 From: "zhangkaixiang5@huawei.com" Date: Mon, 30 Jan 2023 15:49:40 +0800 Subject: [PATCH] Add OpenFileUri Function,Update GetBundleName Function. Signed-off-by: zhangkaixiang5@huawei.com --- interfaces/kits/js/BUILD.gn | 2 + .../kits/js/src/mod_fs/properties/open.cpp | 91 +++++++++++++++++-- .../kits/js/src/mod_fs/properties/open.h | 1 + .../kits/native/remote_uri/remote_uri.cpp | 10 +- .../kits/native/remote_uri/remote_uri.h | 4 +- 5 files changed, 95 insertions(+), 13 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 90795c6f8..f3a9c59e7 100755 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -147,10 +147,12 @@ ohos_shared_library("fs") { external_deps = [ "ability_base:zuri", "ability_runtime:abilitykit_native", + "bundle_framework:appexecfwk_core", "c_utils:utils", "data_share:datashare_common", "data_share:datashare_consumer", "ipc:ipc_core", + "samgr:samgr_proxy", ] } diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index dc97db32c..33a786b90 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -21,18 +21,25 @@ #include "../common_func.h" #include "ability.h" +#include "bundle_mgr_proxy.h" #include "class_file/file_entity.h" #include "class_file/file_n_exporter.h" #include "datashare_helper.h" #include "filemgmt_libhilog.h" #include "filemgmt_libn.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" #include "remote_uri.h" +#include "status_receiver_host.h" +#include "system_ability_definition.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +using namespace OHOS::DistributedFS::ModuleRemoteUri; +using namespace OHOS::AppExecFwk; static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg) { @@ -93,6 +100,59 @@ static int OpenFileByDatashare(napi_env env, napi_value argv, string path, int f return fd; } +static sptr GetBundleMgrProxy() +{ + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + HILOGE("fail to get system ability mgr."); + return nullptr; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + HILOGE("fail to get bundle manager proxy."); + return nullptr; + } + + HILOGI("GetBundleMgrProxy: get bundle manager proxy success."); + return iface_cast(remoteObject); +} + +static string GetBundleNameSelf() +{ + int uid = -1; + uid = IPCSkeleton::GetCallingUid(); + + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + HILOGE("bundle mgr proxy is nullptr."); + return nullptr; + } + string bundleName; + if (!bundleMgrProxy->GetBundleNameForUid(uid, bundleName)) { + HILOGE("GetBundleNameSelf: bundleName get fail. uid is %{public}d", uid); + return nullptr; + } + HILOGI("GetBundleNameSelf: uid is %{public}d, bundleName is %{public}s", uid, bundleName.c_str()); + return bundleName; +} + +static string GetPathFromFileUri(string path, string bundleName, int mode) +{ + string pathShare = "/data/storage/el2/share/"; + string modeRW = "/rw"; + string modeR = "/r"; + if (bundleName != GetBundleNameSelf()) { + if (mode & O_WRONLY) { + path = pathShare + modeRW + bundleName + path; + } else { + path = pathShare + modeR + bundleName + path; + } + } + return path; +} + napi_value Open::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -112,15 +172,23 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) HILOGE("Invalid mode"); return nullptr; } - if (DistributedFS::ModuleRemoteUri::RemoteUri::IsMediaUri(path.get())) { - auto fd = OpenFileByDatashare(env, funcArg[NARG_POS::FIRST], path.get(), mode); + + RemoteUri remoteUri = RemoteUri(path.get()); + string uriScheme = remoteUri.GetScheme(); + string uriPath = remoteUri.GetPath(); + string bundleName = remoteUri.GetAuthority(); + string pathStr = string(path.get()); + if (RemoteUri::IsMediaUri(uriScheme, uriPath)) { + auto fd = OpenFileByDatashare(env, funcArg[NARG_POS::FIRST], pathStr, mode); if (fd >= 0) { - auto file = InstantiateFile(env, fd, path.get(), true).val_; + auto file = InstantiateFile(env, fd, pathStr, true).val_; return file; } HILOGE("Failed to open file by Datashare"); NError(-1).ThrowErr(env); return nullptr; + } else if (RemoteUri::IsFileUri(uriScheme)) { + pathStr = GetPathFromFileUri(uriPath, bundleName, mode); } std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -129,14 +197,14 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - int ret = uv_fs_open(nullptr, open_req.get(), path.get(), mode, S_IRUSR | + int ret = uv_fs_open(nullptr, open_req.get(), pathStr.c_str(), mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); if (ret < 0) { HILOGE("Failed to open file for libuv error %{public}d", ret); NError(errno).ThrowErr(env); return nullptr; } - auto file = InstantiateFile(env, ret, path.get(), false).val_; + auto file = InstantiateFile(env, ret, pathStr, false).val_; return file; } @@ -168,7 +236,12 @@ napi_value Open::Async(napi_env env, napi_callback_info info) auto arg = make_shared(); auto argv = funcArg[NARG_POS::FIRST]; auto cbExec = [arg, argv, path = string(path.get()), mode = mode, env = env]() -> NError { - if (DistributedFS::ModuleRemoteUri::RemoteUri::IsMediaUri(path)) { + RemoteUri remoteUri = RemoteUri(path); + string uriScheme = remoteUri.GetScheme(); + string uriPath = remoteUri.GetPath(); + string bundleName = remoteUri.GetAuthority(); + string pathStr = path; + if (RemoteUri::IsMediaUri(uriScheme, uriPath)) { auto fd = OpenFileByDatashare(env, argv, path, mode); if (fd >= 0) { arg->fd = fd; @@ -178,6 +251,8 @@ napi_value Open::Async(napi_env env, napi_callback_info info) } HILOGE("Failed to open file by Datashare"); return NError(-1); + } else if (RemoteUri::IsFileUri(uriScheme)) { + pathStr = GetPathFromFileUri(path, bundleName, mode); } std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -185,14 +260,14 @@ napi_value Open::Async(napi_env env, napi_callback_info info) HILOGE("Failed to request heap memory."); return NError(ERRNO_NOERR); } - int ret = uv_fs_open(nullptr, open_req.get(), path.c_str(), mode, S_IRUSR | + int ret = uv_fs_open(nullptr, open_req.get(), pathStr.c_str(), mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); if (ret < 0) { HILOGE("Failed to open file for libuv error %{public}d", ret); return NError(errno); } arg->fd = ret; - arg->path = path; + arg->path = pathStr; arg->uri = ""; return NError(ERRNO_NOERR); }; diff --git a/interfaces/kits/js/src/mod_fs/properties/open.h b/interfaces/kits/js/src/mod_fs/properties/open.h index 4103e5408..49d53607a 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.h +++ b/interfaces/kits/js/src/mod_fs/properties/open.h @@ -38,6 +38,7 @@ public: const std::string PROCEDURE_OPEN_NAME = "FileIOOpen"; const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; +const std::string FILE_DATA_URI = "file://"; } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/native/remote_uri/remote_uri.cpp b/interfaces/kits/native/remote_uri/remote_uri.cpp index 2fe67fab1..88552195f 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.cpp +++ b/interfaces/kits/native/remote_uri/remote_uri.cpp @@ -28,11 +28,8 @@ namespace ModuleRemoteUri { using namespace std; -bool RemoteUri::IsMediaUri(const string &uriString) +bool RemoteUri::IsMediaUri(const string scheme, const string path) { - RemoteUri remoteUri = RemoteUri(uriString); - string scheme = remoteUri.GetScheme(); - string path = remoteUri.GetPath(); std::size_t len = MEDIA.length(); if (path.length() > len) { string media = path.substr(0, len); @@ -41,6 +38,11 @@ bool RemoteUri::IsMediaUri(const string &uriString) return false; } +bool RemoteUri::IsFileUri(const string scheme) +{ + return scheme == SCHEME_FILE; +} + static bool IsAllDigits(string fdStr) { for (size_t i = 0; i < fdStr.size(); i++) { diff --git a/interfaces/kits/native/remote_uri/remote_uri.h b/interfaces/kits/native/remote_uri/remote_uri.h index b8250fa77..621e82a9a 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.h +++ b/interfaces/kits/native/remote_uri/remote_uri.h @@ -32,6 +32,7 @@ const string FD_TAG = "="; const string REMOTE_URI_TAG = "fdFromBinder"; const string SCHEME_TAG = ":"; const string SCHEME = "datashare"; +const string SCHEME_FILE = "file"; const string ZERO_FD = "0"; const string MEDIA = "/media/"; const string PATH_SYMBOL = "/"; @@ -44,7 +45,8 @@ public: static bool IsRemoteUri(const string& path, int &fd, const int& flags = O_RDONLY); static int ConvertUri(const int &fd, string &remoteUri); static int OpenRemoteUri(const string &remoteUri); - static bool IsMediaUri(const string &uriString); + static bool IsMediaUri(const string scheme, const string path); + static bool IsFileUri(const string scheme); ~RemoteUri() {} }; std::setRemoteUri::fdFromBinder; -- Gitee