From e6521ba7e48d598f0ea9bb44886f2133e2d9c902 Mon Sep 17 00:00:00 2001 From: "zhangkaixiang5@huawei.com" Date: Thu, 23 Mar 2023 20:09:01 +0800 Subject: [PATCH] Unified uri for file_api Signed-off-by: zhangkaixiang5@huawei.com --- interfaces/kits/js/BUILD.gn | 9 ++-- .../js/src/mod_fileio/properties/open.cpp | 7 +-- .../js/src/mod_fileio/properties/open_v9.cpp | 40 ++++++++++++----- .../js/src/mod_fileio/properties/open_v9.h | 1 + .../kits/js/src/mod_fs/properties/open.cpp | 44 ++++++++++++++----- .../kits/js/src/mod_fs/properties/open.h | 1 + 6 files changed, 74 insertions(+), 28 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 85438bf14..c917f36b9 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -96,8 +96,10 @@ ohos_shared_library("fileio") { "//third_party/openssl:libcrypto_shared", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] - defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] + external_deps = [ + "app_file_service:fileuri_native", + "hiviewdfx_hilog_native:libhilog", + ] } ohos_shared_library("fs") { @@ -152,6 +154,8 @@ ohos_shared_library("fs") { "ability_base:zuri", "ability_runtime:abilitykit_native", "access_token:libtokenid_sdk", + "app_file_service:fileuri_native", + "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", "data_share:datashare_common", @@ -181,7 +185,6 @@ ohos_shared_library("hash") { "//third_party/bounds_checking_function:libsec_shared", "//third_party/openssl:libcrypto_shared", ] - defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ohos_shared_library("file") { diff --git a/interfaces/kits/js/src/mod_fileio/properties/open.cpp b/interfaces/kits/js/src/mod_fileio/properties/open.cpp index a024642b2..fb7b748a0 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/open.cpp @@ -18,7 +18,7 @@ #include #include #include -#include "remote_uri.h" +#include "file_uri.h" #include "../../common/napi/n_async/n_async_work_callback.h" #include "../../common/napi/n_async/n_async_work_promise.h" @@ -28,6 +28,7 @@ namespace OHOS { namespace DistributedFS { namespace ModuleFileIO { using namespace std; +using namespace OHOS::AppFileService::ModuleFileUri; int AdaptToAbi(int &flags) { @@ -85,7 +86,7 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) } (void)AdaptToAbi(flags); int fd = -1; - if (ModuleRemoteUri::RemoteUri::IsRemoteUri(path.get(), fd, flags)) { + if (FileUri::IsRemoteUri(path.get(), fd, flags)) { return NVal::CreateInt64(env, fd).val_; } @@ -122,7 +123,7 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) static UniError DoOpenExec(const std::string& path, const int flags, const int mode, shared_ptr arg) { int fd = -1; - if (!ModuleRemoteUri::RemoteUri::IsRemoteUri(path, fd, flags)) { + if (!FileUri::IsRemoteUri(path, fd, flags)) { fd = open(path.c_str(), flags, mode); } *arg = fd; diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp index 0ccb51d75..05342492d 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp @@ -28,6 +28,7 @@ #include "../../common/uni_error.h" #include "../common_func.h" #include "datashare_helper.h" +#include "file_uri.h" #include "remote_uri.h" #include "ability.h" @@ -37,6 +38,7 @@ namespace OHOS { namespace DistributedFS { namespace ModuleFileIO { +using namespace OHOS::AppFileService::ModuleFileUri; using namespace std; static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg) @@ -105,10 +107,19 @@ napi_value OpenV9::Sync(napi_env env, napi_callback_info info) if (!succMode) { return nullptr; } - if (DistributedFS::ModuleRemoteUri::RemoteUri::IsMediaUri(path.get())) { - auto fd = OpenFileByDatashare(env, funcArg[NARG_POS::FIRST], path.get()); + string pathStr = string(path.get()); + FileUri fileUri(pathStr); + if (fileUri.GetAuthority() == MEDIA_BUNDLE_NAME) { + pathStr = FileUri::TransToDataShareUri(pathStr); + if (pathStr.empty()) { + UniError(EINVAL).ThrowErr(env, "Transform to datashare uri error"); + return nullptr; + } + } + if (FileUri::IsMediaUri(pathStr)) { + auto fd = OpenFileByDatashare(env, funcArg[NARG_POS::FIRST], pathStr); if (fd >= 0) { - auto File = InstantiateFile(env, fd, path.get()).val_; + auto File = InstantiateFile(env, fd, pathStr.val_; return File; } UniError(-1, true).ThrowErr(env); @@ -117,13 +128,13 @@ napi_value OpenV9::Sync(napi_env env, napi_callback_info info) uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); uv_fs_t open_req; - int ret = uv_fs_open(loop, &open_req, path.get(), mode, S_IRUSR | + int ret = uv_fs_open(loop, &open_req, pathStr.c_str(), mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, NULL); if (ret < 0) { UniError(errno, true).ThrowErr(env); return nullptr; } - auto File = InstantiateFile(env, open_req.result, path.get()).val_; + auto File = InstantiateFile(env, open_req.result, pathStr).val_; uv_fs_req_cleanup(&open_req); return File; } @@ -153,11 +164,20 @@ napi_value OpenV9::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](napi_env env) -> UniError { - if (DistributedFS::ModuleRemoteUri::RemoteUri::IsMediaUri(path)) { - auto fd = OpenFileByDatashare(env, argv, path); + string pathStr = string(path.get()); + FileUri fileUri(pathStr); + if (fileUri.GetAuthority() == MEDIA_BUNDLE_NAME) { + pathStr = FileUri::TransToDataShareUri(pathStr); + if (pathStr.empty()) { + UniError(EINVAL).ThrowErr(env, "Transform to datashare uri error"); + return nullptr; + } + } + if (FileUri::IsMediaUri(pathStr)) { + auto fd = OpenFileByDatashare(env, argv, pathStr); if (fd >= 0) { arg->fd = fd; - arg->path = path; + arg->path = pathStr; arg->uri = ""; return UniError(ERRNO_NOERR); } @@ -166,13 +186,13 @@ napi_value OpenV9::Async(napi_env env, napi_callback_info info) uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); uv_fs_t open_req; - int ret = uv_fs_open(loop, &open_req, path.c_str(), mode, S_IRUSR | + int ret = uv_fs_open(loop, &open_req, pathStr.c_str(), mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, NULL); if (ret < 0) { return UniError(errno, true); } arg->fd = open_req.result; - arg->path = path; + arg->path = pathStr; arg->uri = ""; uv_fs_req_cleanup(&open_req); return UniError(ERRNO_NOERR); diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.h b/interfaces/kits/js/src/mod_fileio/properties/open_v9.h index ac8921b4c..bb15a978b 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.h +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.h @@ -39,6 +39,7 @@ public: const std::string PROCEDURE_OPEN_NAME = "FileIOOpenV9"; const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; const std::string MEDIA_FILEMODE_READONLY = "r"; +const std::string MEDIA_BUNDLE_NAME = "media"; } // namespace ModuleFileIO } // namespace DistributedFS } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 1bb4350f0..0ca72302e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -20,12 +20,14 @@ #include #include "ability.h" +#include "bundle_info.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 "file_uri.h" #include "ipc_skeleton.h" #include "iservice_registry.h" #include "remote_uri.h" @@ -37,8 +39,8 @@ namespace FileManagement { namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -using namespace OHOS::DistributedFS::ModuleRemoteUri; using namespace OHOS::AppExecFwk; +using namespace OHOS::AppFileService::ModuleFileUri; static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg) { @@ -126,12 +128,13 @@ static string GetBundleNameSelf() HILOGE("Bundle mgr proxy is null ptr."); return nullptr; } - string bundleName; - if (!bundleMgrProxy->GetBundleNameForUid(uid, bundleName)) { + AppExecFwk::BundleInfo bundleInfo; + auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); + if (ret != ERR_OK) { HILOGE("Failed to get bundleNameSelf. uid is %{public}d", uid); return nullptr; } - return bundleName; + return bundleInfo.name; } static string GetPathFromFileUri(string path, string bundleName, unsigned int mode) @@ -165,8 +168,17 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) HILOGE("Invalid mode"); return nullptr; } + string pathStr = string(path.get()); - if (RemoteUri::IsMediaUri(pathStr)) { + FileUri fileUri(pathStr); + if (fileUri.GetAuthority() == MEDIA_BUNDLE_NAME) { + pathStr = FileUri::TransToDataShareUri(pathStr); + if (pathStr.empty()) { + HILOGE("Transform to datashare uri error"); + return nullptr; + } + } + if (FileUri::IsMediaUri(pathStr)) { auto fd = OpenFileByDatashare(env, funcArg[NARG_POS::FIRST], pathStr, mode); if (fd >= 0) { auto file = InstantiateFile(env, fd, pathStr, true).val_; @@ -175,9 +187,9 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) 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); + } else if (FileUri::IsFileUri(pathStr)) { + FileUri fileUri = FileUri(pathStr); + pathStr = GetPathFromFileUri(fileUri.GetPath(), fileUri.GetAuthority(), mode); } std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -226,7 +238,15 @@ napi_value Open::Async(napi_env env, napi_callback_info info) auto argv = funcArg[NARG_POS::FIRST]; auto cbExec = [arg, argv, path = string(path.get()), mode = mode, env = env]() -> NError { string pathStr = path; - if (RemoteUri::IsMediaUri(path)) { + FileUri fileUri(pathStr); + if (fileUri.GetAuthority() == MEDIA_BUNDLE_NAME) { + pathStr = FileUri::TransToDataShareUri(pathStr); + if (pathStr.empty()) { + HILOGE("Transform to datashare uri error"); + return NError(ERRNO_NOERR); + } + } + if (FileUri::IsMediaUri(path)) { auto fd = OpenFileByDatashare(env, argv, path, mode); if (fd >= 0) { arg->fd = fd; @@ -236,9 +256,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); + } else if (FileUri::IsFileUri(path)) { + FileUri fileUri = FileUri(path); + pathStr = GetPathFromFileUri(fileUri.GetPath(), fileUri.GetAuthority(), mode); } std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; diff --git a/interfaces/kits/js/src/mod_fs/properties/open.h b/interfaces/kits/js/src/mod_fs/properties/open.h index f758f77e0..8dd6f206d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.h +++ b/interfaces/kits/js/src/mod_fs/properties/open.h @@ -42,6 +42,7 @@ 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/"; +const std::string MEDIA_BUNDLE_NAME = "media"; } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS -- Gitee