diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 90795c6f80b8e4dd4e7ccf84ba9873a133fb3a53..f3a9c59e769f56d24f9cb88241d781fa71cc2d8e 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 bb9aa36fb8e34877828e2074413485cc38a6b04b..840d3901417fa3f96d5dcbb94dd30b0e4cd73177 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.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 @@ -20,18 +20,25 @@ #include #include "ability.h" +#include "bundle_mgr_proxy.h" #include "class_file/file_entity.h" #include "class_file/file_n_exporter.h" #include "common_func.h" #include "datashare_helper.h" #include "filemgmt_libhilog.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) { @@ -92,6 +99,53 @@ 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("Failed to get system ability mgr."); + return nullptr; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + HILOGE("Failed to get bundle manager proxy."); + return nullptr; + } + return iface_cast(remoteObject); +} + +static string GetBundleNameSelf() +{ + int uid = -1; + uid = IPCSkeleton::GetCallingUid(); + + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + HILOGE("Bundle mgr proxy is null ptr."); + return nullptr; + } + string bundleName; + if (!bundleMgrProxy->GetBundleNameForUid(uid, bundleName)) { + HILOGE("Failed to get bundleNameSelf. uid is %{public}d", uid); + return nullptr; + } + return bundleName; +} + +static string GetPathFromFileUri(string path, string bundleName, int mode) +{ + if (bundleName != GetBundleNameSelf()) { + if ((mode & O_WRONLY) == O_WRONLY || (mode & O_RDWR) == O_RDWR) { + path = PATH_SHARE + MODE_RW + bundleName + path; + } else { + path = PATH_SHARE + MODE_R + bundleName + path; + } + } + return path; +} + napi_value Open::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -111,15 +165,19 @@ 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); + string pathStr = string(path.get()); + if (RemoteUri::IsMediaUri(pathStr)) { + 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(pathStr)) { + RemoteUri remoteUri = RemoteUri(pathStr); + pathStr = GetPathFromFileUri(remoteUri.GetPath(), remoteUri.GetAuthority(), mode); } std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -128,14 +186,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, 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; } @@ -167,7 +225,8 @@ 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)) { + string pathStr = path; + if (RemoteUri::IsMediaUri(path)) { auto fd = OpenFileByDatashare(env, argv, path, mode); if (fd >= 0) { arg->fd = fd; @@ -177,6 +236,9 @@ 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(path)) { + RemoteUri remoteUri = RemoteUri(path); + pathStr = GetPathFromFileUri(remoteUri.GetPath(), remoteUri.GetAuthority(), mode); } std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -184,14 +246,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, 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 7ff6662b99e58e3d4a1ab355351edc10ab4e0e86..f758f77e01befa922be1182fa18f64245295ea94 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.h +++ b/interfaces/kits/js/src/mod_fs/properties/open.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 @@ -38,6 +38,10 @@ public: const std::string PROCEDURE_OPEN_NAME = "FileIOOpen"; const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; +const std::string FILE_DATA_URI = "file://"; +const std::string PATH_SHARE = "/data/storage/el2/share"; +const std::string MODE_RW = "/rw/"; +const std::string MODE_R = "/r/"; } // 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 2fe67fab149c32fdb1982ba421ef67e78a7fe770..b205bec287dd6c7a097da327eaf831f113201221 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.cpp +++ b/interfaces/kits/native/remote_uri/remote_uri.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 @@ -41,6 +41,11 @@ bool RemoteUri::IsMediaUri(const string &uriString) return false; } +bool RemoteUri::IsFileUri(const string &uriString) +{ + return RemoteUri(uriString).GetScheme() == 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 b8250fa77b0d3ac16e0038db10cb8b50fb4d0ff4..87da0624182a8645c00fcb89933c9cb605bf2e20 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.h +++ b/interfaces/kits/native/remote_uri/remote_uri.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 @@ -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 = "/"; @@ -45,6 +46,7 @@ public: static int ConvertUri(const int &fd, string &remoteUri); static int OpenRemoteUri(const string &remoteUri); static bool IsMediaUri(const string &uriString); + static bool IsFileUri(const string &uriString); ~RemoteUri() {} }; std::setRemoteUri::fdFromBinder;