From 961111a78a757a2b0c86276d4308b2808391fe8b Mon Sep 17 00:00:00 2001 From: Science Date: Wed, 31 May 2023 10:44:03 +0000 Subject: [PATCH] cd .ssh/ Abstract function called ParsePathFromUri and applie it to various interfaces. Signed-off-by: Science Change-Id: I83b92e9a5ddd2c0435254f422010a62a1c528c76 --- interfaces/kits/js/src/mod_fs/common_func.cpp | 90 ++++++++++++++++--- interfaces/kits/js/src/mod_fs/common_func.h | 3 + .../js/src/mod_fs/properties/copy_file.cpp | 10 ++- .../kits/js/src/mod_fs/properties/copydir.cpp | 17 ++-- .../src/mod_fs/properties/create_stream.cpp | 2 +- .../js/src/mod_fs/properties/listfile.cpp | 27 +++--- .../kits/js/src/mod_fs/properties/lstat.cpp | 12 +-- .../kits/js/src/mod_fs/properties/mkdtemp.cpp | 4 +- .../kits/js/src/mod_fs/properties/move.cpp | 9 +- .../kits/js/src/mod_fs/properties/movedir.cpp | 15 ++-- .../kits/js/src/mod_fs/properties/open.cpp | 59 +----------- .../src/mod_fs/properties/prop_n_exporter.cpp | 39 ++++---- .../js/src/mod_fs/properties/read_text.cpp | 7 +- .../kits/js/src/mod_fs/properties/rename.cpp | 8 +- .../js/src/mod_fs/properties/rmdirent.cpp | 5 +- .../kits/js/src/mod_fs/properties/stat.cpp | 3 +- .../kits/js/src/mod_fs/properties/symlink.cpp | 8 +- .../js/src/mod_fs/properties/truncate.cpp | 5 +- .../kits/js/src/mod_fs/properties/watcher.cpp | 5 +- .../kits/native/remote_uri/remote_uri.cpp | 2 + .../kits/native/remote_uri/remote_uri.h | 1 - 21 files changed, 195 insertions(+), 136 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index b3c289196..b589c2d36 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -21,12 +21,21 @@ #include #include + +#include "bundle_info.h" +#include "bundle_mgr_proxy.h" #include "class_stat/stat_entity.h" #include "class_stat/stat_n_exporter.h" #include "class_stream/stream_entity.h" #include "class_stream/stream_n_exporter.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 { @@ -165,20 +174,81 @@ void CommonFunc::fs_req_cleanup(uv_fs_t* req) string CommonFunc::GetModeFromFlags(unsigned int flags) { - const string RDONLY = "r"; - const string WRONLY = "w"; - const string APPEND = "a"; - const string TRUNC = "t"; - string mode = RDONLY; - mode += (((flags & O_RDWR) == O_RDWR) ? WRONLY : ""); - mode = (((flags & O_WRONLY) == O_WRONLY) ? WRONLY : mode); - if (mode != RDONLY) { - mode += ((flags & O_TRUNC) ? TRUNC : ""); - mode += ((flags & O_APPEND) ? APPEND : ""); + const string readOnly = "r"; + const string writeOnly = "w"; + const string append = "a"; + const string trunc = "t"; + string mode = readOnly; + mode += (((flags & O_RDWR) == O_RDWR) ? writeOnly : ""); + mode = (((flags & O_WRONLY) == O_WRONLY) ? writeOnly : mode); + if (mode != readOnly) { + mode += ((flags & O_TRUNC) ? trunc : ""); + mode += ((flags & O_APPEND) ? append : ""); } return mode; } +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; + } + AppExecFwk::BundleInfo bundleInfo; + auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); + if (ret != ERR_OK) { + HILOGE("Failed to get bundleNameSelf."); + return nullptr; + } + return bundleInfo.name; +} + +static string GetPathFromFileUri(string path, string bundleName, unsigned int mode) +{ + const string pathShare = "/data/storage/el2/share"; + const string modeReadWrite = "/rw/"; + const string modeRead = "/r/"; + if (bundleName != GetBundleNameSelf()) { + if ((mode & O_WRONLY) == O_WRONLY || (mode & O_RDWR) == O_RDWR) { + path = pathShare + modeReadWrite + bundleName + path; + } else { + path = pathShare + modeRead + bundleName + path; + } + } + return path; +} + +string CommonFunc::ParsePathFromUri(const string &path, unsigned int mode) +{ + string pathStr = path; + if (DistributedFS::ModuleRemoteUri::RemoteUri::IsFileUri(path)) { + DistributedFS::ModuleRemoteUri::RemoteUri remoteUri = DistributedFS::ModuleRemoteUri::RemoteUri(path); + pathStr = GetPathFromFileUri(remoteUri.GetPath(), remoteUri.GetAuthority(), mode); + } + return pathStr; +} + tuple, unique_ptr> CommonFunc::GetCopyPathArg(napi_env env, napi_value srcPath, napi_value dstPath) diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 7a169bf9d..b6b9a4e6f 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -16,6 +16,8 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_FUNC_H #define INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_FUNC_H +#include + #include "fd_guard.h" #include "n_val.h" #include "uv.h" @@ -58,6 +60,7 @@ struct CommonFunc { napi_value dstPath); static void fs_req_cleanup(uv_fs_t* req); static std::string GetModeFromFlags(unsigned int flags); + static std::string ParsePathFromUri(const std::string& path, unsigned int mode = O_RDONLY); }; } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp index 77ecd5ea5..4b0c1dd46 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -34,13 +34,15 @@ using namespace OHOS::FileManagement::LibN; static NError IsAllPath(FileInfo& srcFile, FileInfo& destFile) { + string srcPathStr = CommonFunc::ParsePathFromUri(string(srcFile.path.get())); + string destPathStr = CommonFunc::ParsePathFromUri(string(destFile.path.get())); std::unique_ptr copyfile_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!copyfile_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); } - int ret = uv_fs_copyfile(nullptr, copyfile_req.get(), srcFile.path.get(), destFile.path.get(), + int ret = uv_fs_copyfile(nullptr, copyfile_req.get(), srcPathStr.c_str(), destPathStr.c_str(), UV_FS_COPYFILE_FICLONE, nullptr); if (ret < 0) { HILOGE("Failed to copy file when all parameters are paths"); @@ -69,13 +71,14 @@ static NError SendFileCore(FileInfo& srcFdg, FileInfo& destFdg, struct stat& sta static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) { if (srcFile.isPath) { + string srcPathStr = CommonFunc::ParsePathFromUri(string(srcFile.path.get())); std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); } - int ret = uv_fs_open(nullptr, open_req.get(), srcFile.path.get(), O_RDWR, + int ret = uv_fs_open(nullptr, open_req.get(), srcPathStr.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); if (ret < 0) { HILOGE("Failed to open srcFile with ret: %{public}d", ret); @@ -95,13 +98,14 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) } if (destFile.isPath) { + string destPathStr = CommonFunc::ParsePathFromUri(string(destFile.path.get())); std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); } - int ret = uv_fs_open(nullptr, open_req.get(), destFile.path.get(), O_RDWR | O_CREAT, statbf.st_mode, nullptr); + int ret = uv_fs_open(nullptr, open_req.get(), destPathStr.c_str(), O_RDWR | O_CREAT, statbf.st_mode, nullptr); if (ret < 0) { HILOGE("Failed to open destFile with ret: %{public}d", ret); return NError(errno); diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 16329aff9..bf2cd4244 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -44,20 +44,22 @@ static bool AllowToCopy(const string &src, const string &dest) return true; } -static tuple, unique_ptr, int> ParseAndCheckJsOperand(napi_env env, +static tuple, unique_ptr, int> ParseAndCheckJsOperand(napi_env env, const NFuncArg &funcArg) { auto [resGetFirstArg, src, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!resGetFirstArg || !filesystem::is_directory(filesystem::status(src.get()))) { + string srcPathStr = CommonFunc::ParsePathFromUri(string(src.get())); + if (!resGetFirstArg || !filesystem::is_directory(filesystem::status(srcPathStr.c_str()))) { HILOGE("Invalid src"); return { false, nullptr, nullptr, 0 }; } auto [resGetSecondArg, dest, unused] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); - if (!resGetSecondArg || !filesystem::is_directory(filesystem::status(dest.get()))) { + string destPathStr = CommonFunc::ParsePathFromUri(string(dest.get())); + if (!resGetSecondArg || !filesystem::is_directory(filesystem::status(destPathStr.c_str()))) { HILOGE("Invalid dest"); return { false, nullptr, nullptr, 0 }; } - if (!AllowToCopy(src.get(), dest.get())) { + if (!AllowToCopy(srcPathStr.c_str(), destPathStr.c_str())) { return { false, nullptr, nullptr, 0 }; } int mode = 0; @@ -69,7 +71,7 @@ static tuple, unique_ptr, int> ParseAndCheckJsO return { false, nullptr, nullptr, 0 }; } } - return { true, move(src), move(dest), mode }; + return { true, make_unique(srcPathStr), make_unique(destPathStr), mode }; } static int MakeDir(const string &path) @@ -249,7 +251,7 @@ napi_value CopyDir::Sync(napi_env env, napi_callback_info info) } vector errfiles = {}; - int ret = CopyDirFunc(src.get(), dest.get(), mode, errfiles); + int ret = CopyDirFunc(*src, *dest, mode, errfiles); if (ret == EEXIST && mode == DIRMODE_FILE_COPY_THROW_ERR) { NError(ret).ThrowErrAddData(env, EEXIST, PushErrFilesInData(env, errfiles)); return nullptr; @@ -285,7 +287,8 @@ napi_value CopyDir::Async(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode, arg]() -> NError { + + auto cbExec = [srcPath = *src, destPath = *dest, mode = mode, arg]() -> NError { arg->errNo = CopyDirFunc(srcPath, destPath, mode, arg->errfiles); if (arg->errNo) { return NError(arg->errNo); diff --git a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp index b2ac7d695..e5b5d9991 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -42,7 +42,7 @@ static tuple GetCreateStreamArgs(napi_env env, const NFunc return { false, "", "" }; } - return { true, path.get(), mode.get() }; + return { true, CommonFunc::ParsePathFromUri(string(path.get())), mode.get() }; } napi_value CreateStream::Sync(napi_env env, napi_callback_info info) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 3311f6d97..b5384dbb0 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -23,6 +23,7 @@ #include #include +#include "common_func.h" #include "file_utils.h" #include "filemgmt_libhilog.h" @@ -202,7 +203,7 @@ static bool FilterLastModifyTime(const double lastModifiedAfter, const struct di HILOGE("Failed to stat file."); return false; } - if (lastModifiedAfter > time(&info.st_mtime)) { + if (lastModifiedAfter > static_cast(info.st_mtime)) { return false; } return true; @@ -301,14 +302,13 @@ static int RecursiveFunc(const string &path, vector &dirents) return ERRNO_NOERR; } -static napi_value DoListFileVector2NV(napi_env env, const string &path, vector &dirents) +static napi_value DoListFileVector2NV(napi_env env, const string &path, vector &dirents, bool recursion) { - if (g_optionArgs.recursion) { + if (recursion) { for (size_t i = 0; i < dirents.size(); i++) { dirents[i] = dirents[i].substr(path.length()); } } - g_optionArgs.Clear(); return NVal::CreateArrayString(env, dirents).val_; } @@ -326,20 +326,23 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - if (!GetOptionArg(env, funcArg, g_optionArgs, string(path.get()))) { + string pathStr = CommonFunc::ParsePathFromUri(string(path.get())); + if (!GetOptionArg(env, funcArg, g_optionArgs, pathStr)) { HILOGE("Invalid options"); NError(EINVAL).ThrowErr(env); return nullptr; } vector direntsRes; int ret = 0; - g_optionArgs.recursion ? ret = RecursiveFunc(path.get(), direntsRes) : - ret = FilterFileRes(path.get(), direntsRes); + ret = g_optionArgs.recursion ? RecursiveFunc(pathStr.c_str(), direntsRes) : + FilterFileRes(pathStr.c_str(), direntsRes); if (ret) { NError(ret).ThrowErr(env); return nullptr; } - return DoListFileVector2NV(env, string(path.get()), direntsRes); + auto res = DoListFileVector2NV(env, pathStr, direntsRes, g_optionArgs.recursion); + g_optionArgs.Clear(); + return res; } napi_value ListFile::Async(napi_env env, napi_callback_info info) @@ -359,7 +362,8 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) } OptionArgs optionArgsTmp = {}; - if (!GetOptionArg(env, funcArg, optionArgsTmp, string(path.get()))) { + string pathStr = CommonFunc::ParsePathFromUri(string(path.get())); + if (!GetOptionArg(env, funcArg, optionArgsTmp, pathStr)) { HILOGE("Invalid options"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -376,14 +380,15 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) int ret = 0; ret = g_optionArgs.recursion ? RecursiveFunc(g_optionArgs.path, arg->dirents) : FilterFileRes(g_optionArgs.path, arg->dirents); + g_optionArgs.Clear(); return ret ? NError(ret) : NError(ERRNO_NOERR); }; - auto cbCompl = [arg, path = string(path.get())](napi_env env, NError err) -> NVal { + auto cbCompl = [arg, optionArgsTmp, path = pathStr](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } - return { env, DoListFileVector2NV(env, path, arg->dirents) }; + return { env, DoListFileVector2NV(env, path, arg->dirents, optionArgsTmp.recursion) }; }; NVal thisVar(env, funcArg.GetThisVar()); diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index 407b59515..e9a7df706 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -50,10 +50,12 @@ napi_value Lstat::Sync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - int ret = uv_fs_lstat(nullptr, lstat_req.get(), pathPtr.get(), nullptr); + + string pathStr = CommonFunc::ParsePathFromUri(string(pathPtr.get())); + int ret = uv_fs_lstat(nullptr, lstat_req.get(), pathStr.c_str(), nullptr); if (ret < 0) { - HILOGE("Failed to get stat of file by path %{public}s", pathPtr.get()); - NError(errno).ThrowErr(env); + HILOGE("Failed to get stat of file by path %{public}s", pathStr.c_str()); + NError(ret).ThrowErr(env); return nullptr; } @@ -72,7 +74,7 @@ static NError LstatExec(shared_ptr arg, string path) int ret = uv_fs_lstat(nullptr, lstat_req.get(), path.c_str(), nullptr); if (ret < 0) { HILOGE("Failed to get stat of file by path: %{public}s, ret: %{public}d", path.c_str(), ret); - return NError(errno); + return NError(ret); } else { arg->stat_ = lstat_req->statbuf; return NError(ERRNO_NOERR); @@ -95,7 +97,7 @@ napi_value Lstat::Async(napi_env env, napi_callback_info info) return nullptr; } - string path = tmp.get(); + string path = CommonFunc::ParsePathFromUri(string(tmp.get())); auto arg = CreateSharedPtr(); if (arg == nullptr) { HILOGE("Failed to request heap memory."); diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp index 13c22b6d4..9c734ae49 100755 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -41,7 +41,7 @@ napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info) return nullptr; } - string path = tmp.get(); + string path = CommonFunc::ParsePathFromUri(string(tmp.get())); std::unique_ptr mkdtemp_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!mkdtemp_req) { @@ -99,7 +99,7 @@ napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [path = string(tmp.get()), arg]() -> NError { + auto cbExec = [path = CommonFunc::ParsePathFromUri(string(tmp.get())), arg]() -> NError { return MkdTempExec(arg, path); }; auto cbComplete = [arg](napi_env env, NError err) -> NVal { diff --git a/interfaces/kits/js/src/mod_fs/properties/move.cpp b/interfaces/kits/js/src/mod_fs/properties/move.cpp index 5592b6868..a1e592a00 100644 --- a/interfaces/kits/js/src/mod_fs/properties/move.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/move.cpp @@ -152,7 +152,9 @@ napi_value Move::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - int ret = MoveFile(string(src.get()), string(dest.get()), mode); + string srcPathStr = CommonFunc::ParsePathFromUri(string(src.get()), mode); + string destPathStr = CommonFunc::ParsePathFromUri(string(dest.get()), mode); + int ret = MoveFile(srcPathStr, destPathStr, mode); if (ret) { NError(ret).ThrowErr(env); return nullptr; @@ -173,8 +175,9 @@ napi_value Move::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - - auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode]() -> NError { + string srcPathStr = CommonFunc::ParsePathFromUri(string(src.get()), mode); + string destPathStr = CommonFunc::ParsePathFromUri(string(dest.get()), mode); + auto cbExec = [srcPath = srcPathStr, destPath = destPathStr, mode = mode]() -> NError { int ret = MoveFile(srcPath, destPath, mode); if (ret) { return NError(ret); diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index aeb71a8d9..fdafa1b7a 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -60,15 +60,17 @@ static bool RmDirectory(const string &path) return true; } -static tuple, unique_ptr, int> ParseJsOperand(napi_env env, const NFuncArg& funcArg) +static tuple, unique_ptr, int> ParseJsOperand(napi_env env, const NFuncArg& funcArg) { auto [resGetFirstArg, src, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!resGetFirstArg || !filesystem::is_directory(filesystem::status(src.get()))) { + string srcPathStr = CommonFunc::ParsePathFromUri(string(src.get())); + if (!resGetFirstArg || !filesystem::is_directory(filesystem::status(srcPathStr.c_str()))) { HILOGE("Invalid src"); return { false, nullptr, nullptr, 0 }; } auto [resGetSecondArg, dest, unused] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); - if (!resGetSecondArg || !filesystem::is_directory(filesystem::status(dest.get()))) { + string destPathStr = CommonFunc::ParsePathFromUri(string(dest.get())); + if (!resGetSecondArg || !filesystem::is_directory(filesystem::status(destPathStr.c_str()))) { HILOGE("Invalid dest"); return { false, nullptr, nullptr, 0 }; } @@ -81,7 +83,7 @@ static tuple, unique_ptr, int> ParseJsOperand(n return { false, nullptr, nullptr, 0 }; } } - return { true, move(src), move(dest), mode }; + return { true, make_unique(srcPathStr), make_unique(destPathStr), mode }; } static int CopyAndDeleteFile(const string &src, const string &dest) @@ -288,9 +290,8 @@ napi_value MoveDir::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - vector errfiles = {}; - int ret = MoveDirFunc(src.get(), dest.get(), mode, errfiles); + int ret = MoveDirFunc(*src, *dest, mode, errfiles); if (ret == EEXIST && mode == DIRMODE_FILE_THROW_ERR) { NError(ret).ThrowErrAddData(env, EEXIST, GetErrData(env, errfiles)); return nullptr; @@ -326,7 +327,7 @@ napi_value MoveDir::Async(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode, arg]() -> NError { + auto cbExec = [srcPath = *src, destPath = *dest, mode = mode, arg]() -> NError { arg->errNo = MoveDirFunc(srcPath, destPath, mode, arg->errfiles); if (arg->errNo) { return NError(arg->errNo); diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index ea5f82bc1..d26e7102c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -139,54 +139,6 @@ static int OpenFileByDatashare(string path, unsigned int flags) 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; - } - AppExecFwk::BundleInfo bundleInfo; - auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); - if (ret != ERR_OK) { - HILOGE("Failed to get bundleNameSelf."); - return nullptr; - } - return bundleInfo.name; -} - -static string GetPathFromFileUri(string path, string bundleName, unsigned 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); @@ -216,9 +168,6 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) HILOGE("Failed to open file by Datashare"); NError(-ret).ThrowErr(env); return nullptr; - } else if (RemoteUri::IsFileUri(pathStr)) { - RemoteUri remoteUri = RemoteUri(pathStr); - pathStr = GetPathFromFileUri(remoteUri.GetPath(), remoteUri.GetAuthority(), mode); } else if (RemoteUri::IsRemoteUri(pathStr, fd, mode)) { if (fd >= 0) { auto file = InstantiateFile(env, fd, pathStr, true).val_; @@ -228,6 +177,7 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) NError(E_INVAL).ThrowErr(env); return nullptr; } + pathStr = CommonFunc::ParsePathFromUri(path.get(), mode); std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { @@ -278,7 +228,6 @@ 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; int fd = -1; if (RemoteUri::IsMediaUri(path)) { int ret = OpenFileByDatashare(path, mode); @@ -290,9 +239,6 @@ napi_value Open::Async(napi_env env, napi_callback_info info) } HILOGE("Failed to open file by Datashare"); return NError(-ret); - } else if (RemoteUri::IsFileUri(path)) { - RemoteUri remoteUri = RemoteUri(path); - pathStr = GetPathFromFileUri(remoteUri.GetPath(), remoteUri.GetAuthority(), mode); } else if (RemoteUri::IsRemoteUri(path, fd, mode)) { if (fd >= 0) { arg->fd = fd; @@ -303,6 +249,7 @@ napi_value Open::Async(napi_env env, napi_callback_info info) HILOGE("Failed to open file by RemoteUri"); return NError(E_INVAL); } + string pathStr = CommonFunc::ParsePathFromUri(path, mode); std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { @@ -343,4 +290,4 @@ napi_value Open::Async(napi_env env, napi_callback_info info) } } // namespace ModuleFileIO } // namespace FileManagement -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index b7573c89e..36fba9beb 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -73,6 +73,7 @@ napi_value PropNExporter::AccessSync(napi_env env, napi_callback_info info) } bool isAccess = false; + string pathStr = CommonFunc::ParsePathFromUri(string(path.get())); std::unique_ptr access_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!access_req) { @@ -80,10 +81,10 @@ napi_value PropNExporter::AccessSync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - int ret = uv_fs_access(nullptr, access_req.get(), path.get(), 0, nullptr); - if (ret < 0 && errno != ENOENT) { + int ret = uv_fs_access(nullptr, access_req.get(), pathStr.c_str(), 0, nullptr); + if (ret < 0 && (string_view(uv_err_name(ret)) != "ENOENT")) { HILOGE("Failed to access file by path"); - NError(errno).ThrowErr(env); + NError(ret).ThrowErr(env); return nullptr; } if (ret == 0) { @@ -121,11 +122,12 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) HILOGE("Failed to request heap memory."); return NError(ENOMEM); } - int ret = uv_fs_access(nullptr, access_req.get(), path.c_str(), 0, nullptr); + string pathStr = CommonFunc::ParsePathFromUri(path); + int ret = uv_fs_access(nullptr, access_req.get(), pathStr.c_str(), 0, nullptr); if (ret == 0) { result->isAccess = true; } - return (ret < 0 && errno != ENOENT) ? NError(errno) : NError(ERRNO_NOERR); + return (ret < 0 && (string_view(uv_err_name(ret)) != "ENOENT")) ? NError(ret) : NError(ERRNO_NOERR); }; auto cbComplete = [result](napi_env env, NError err) -> NVal { @@ -160,7 +162,7 @@ napi_value PropNExporter::Unlink(napi_env env, napi_callback_info info) return nullptr; } - auto cbExec = [path = string(tmp.get())]() -> NError { + auto cbExec = [path = CommonFunc::ParsePathFromUri(string(tmp.get()))]() -> NError { std::unique_ptr unlink_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!unlink_req) { @@ -170,7 +172,7 @@ napi_value PropNExporter::Unlink(napi_env env, napi_callback_info info) int ret = uv_fs_unlink(nullptr, unlink_req.get(), path.c_str(), nullptr); if (ret < 0) { HILOGE("Failed to unlink with path"); - return NError(errno); + return NError(ret); } return NError(ERRNO_NOERR); }; @@ -214,10 +216,11 @@ napi_value PropNExporter::UnlinkSync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - int ret = uv_fs_unlink(nullptr, unlink_req.get(), path.get(), nullptr); + string pathStr = CommonFunc::ParsePathFromUri(string(path.get())); + int ret = uv_fs_unlink(nullptr, unlink_req.get(), pathStr.c_str(), nullptr); if (ret < 0) { HILOGE("Failed to unlink with path"); - NError(errno).ThrowErr(env); + NError(ret).ThrowErr(env); return nullptr; } @@ -240,7 +243,7 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) return nullptr; } - auto cbExec = [path = string(tmp.get())]() -> NError { + auto cbExec = [path = CommonFunc::ParsePathFromUri(string(tmp.get()))]() -> NError { std::unique_ptr mkdir_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!mkdir_req) { @@ -250,7 +253,7 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.c_str(), DIR_DEFAULT_PERM, nullptr); if (ret < 0) { HILOGE("Failed to create directory"); - return NError(errno); + return NError(ret); } return NError(ERRNO_NOERR); }; @@ -294,10 +297,12 @@ napi_value PropNExporter::MkdirSync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.get(), DIR_DEFAULT_PERM, nullptr); + + string pathStr = CommonFunc::ParsePathFromUri(string(path.get())); + int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), pathStr.c_str(), DIR_DEFAULT_PERM, nullptr); if (ret < 0) { HILOGE("Failed to create directory"); - NError(errno).ThrowErr(env); + NError(ret).ThrowErr(env); return nullptr; } @@ -345,7 +350,7 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) int ret = uv_fs_read(nullptr, read_req.get(), fd, &buffer, 1, offset, nullptr); if (ret < 0) { HILOGE("Failed to read file for %{public}d", ret); - NError(errno).ThrowErr(env); + NError(ret).ThrowErr(env); return nullptr; } @@ -364,7 +369,7 @@ static NError ReadExec(shared_ptr arg, char *buf, size_t len, in int ret = uv_fs_read(nullptr, read_req.get(), fd, &buffer, 1, offset, nullptr); if (ret < 0) { HILOGE("Failed to read file for %{public}d", ret); - return NError(errno); + return NError(ret); } arg->lenRead = ret; return NError(ERRNO_NOERR); @@ -439,7 +444,7 @@ static NError WriteExec(shared_ptr arg, char *buf, size_t len, int ret = uv_fs_write(nullptr, write_req.get(), fd, &buffer, 1, offset, nullptr); if (ret < 0) { HILOGE("Failed to write file for %{public}d", ret); - return NError(errno); + return NError(ret); } arg->actLen = ret; return NError(ERRNO_NOERR); @@ -544,7 +549,7 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) int ret = uv_fs_write(nullptr, write_req.get(), fd, &buffer, 1, offset, nullptr); if (ret < 0) { HILOGE("Failed to write file for %{public}d", ret); - NError(errno).ThrowErr(env); + NError(ret).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp index 262f52da0..826249eb2 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -149,8 +149,8 @@ napi_value ReadText::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(), O_RDONLY, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); + int ret = uv_fs_open(nullptr, open_req.get(), CommonFunc::ParsePathFromUri(string(path.get()), O_RDONLY).c_str(), + O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); if (ret < 0) { HILOGE("Failed to open file by ret: %{public}d", ret); NError(errno).ThrowErr(env); @@ -217,7 +217,8 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [path = string(path.get()), arg, offset = offset, hasLen = hasLen, len = len]() -> NError { + auto cbExec = [path = CommonFunc::ParsePathFromUri(string(path.get())), + arg, offset = offset, hasLen = hasLen, len = len]() -> NError { return ReadTextAsync(path, arg, offset, hasLen, len); }; diff --git a/interfaces/kits/js/src/mod_fs/properties/rename.cpp b/interfaces/kits/js/src/mod_fs/properties/rename.cpp index dd05e02f8..2051800d0 100755 --- a/interfaces/kits/js/src/mod_fs/properties/rename.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/rename.cpp @@ -58,7 +58,9 @@ napi_value Rename::Sync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - int ret = uv_fs_rename(nullptr, rename_req.get(), src.get(), dest.get(), nullptr); + string srcPathStr = CommonFunc::ParsePathFromUri(string(src.get())); + string destPathStr = CommonFunc::ParsePathFromUri(string(dest.get())); + int ret = uv_fs_rename(nullptr, rename_req.get(), srcPathStr.c_str(), destPathStr.c_str(), nullptr); if (ret < 0) { HILOGE("Failed to rename file with path"); NError(errno).ThrowErr(env); @@ -91,7 +93,9 @@ napi_value Rename::Async(napi_env env, napi_callback_info info) return nullptr; } - auto cbExec = [opath = string(src.get()), npath = string(dest.get())]() -> NError { + string srcPathStr = CommonFunc::ParsePathFromUri(string(src.get())); + string destPathStr = CommonFunc::ParsePathFromUri(string(dest.get())); + auto cbExec = [opath = srcPathStr, npath = destPathStr]() -> NError { std::unique_ptr rename_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!rename_req) { diff --git a/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp b/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp index 43ecec7ca..da2b6316f 100755 --- a/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp @@ -23,6 +23,7 @@ #include #include +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -107,7 +108,7 @@ napi_value Rmdirent::Sync(napi_env env, napi_callback_info info) return nullptr; } - auto err = RmDirent(string(path.get())); + auto err = RmDirent(CommonFunc::ParsePathFromUri(string(path.get()))); if (err) { err.ThrowErr(env); return nullptr; @@ -132,7 +133,7 @@ napi_value Rmdirent::Async(napi_env env, napi_callback_info info) return nullptr; } - auto cbExec = [tmpPath = string(path.get())]() -> NError { + auto cbExec = [tmpPath = CommonFunc::ParsePathFromUri(string(path.get()))]() -> NError { return RmDirent(tmpPath); }; auto cbCompl = [](napi_env env, NError err) -> NVal { diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index 2d60b89c2..51b8a661c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -56,7 +56,8 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs static NError CheckFsStat(const FileInfo &fileInfo, uv_fs_t* req) { if (fileInfo.isPath) { - int ret = uv_fs_stat(nullptr, req, fileInfo.path.get(), nullptr); + string pathStr = CommonFunc::ParsePathFromUri(string(fileInfo.path.get())); + int ret = uv_fs_stat(nullptr, req, pathStr.c_str(), nullptr); if (ret < 0) { HILOGE("Failed to stat file with path"); return NError(errno); diff --git a/interfaces/kits/js/src/mod_fs/properties/symlink.cpp b/interfaces/kits/js/src/mod_fs/properties/symlink.cpp index 62881ee16..e416bf348 100755 --- a/interfaces/kits/js/src/mod_fs/properties/symlink.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/symlink.cpp @@ -67,7 +67,9 @@ napi_value Symlink::Sync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - int ret = uv_fs_symlink(nullptr, symlink_req.get(), oldPath.c_str(), newPath.c_str(), 0, nullptr); + string oldPathStr = CommonFunc::ParsePathFromUri(oldPath); + string newPathStr = CommonFunc::ParsePathFromUri(newPath); + int ret = uv_fs_symlink(nullptr, symlink_req.get(), oldPathStr.c_str(), newPathStr.c_str(), 0, nullptr); if (ret < 0) { HILOGE("Failed to create a link for old path"); NError(errno).ThrowErr(env); @@ -100,7 +102,9 @@ napi_value Symlink::Async(napi_env env, napi_callback_info info) HILOGE("Failed to request heap memory."); return NError(ENOMEM); } - int ret = uv_fs_symlink(nullptr, symlink_req.get(), oldPath.c_str(), newPath.c_str(), 0, nullptr); + string oldPathStr = CommonFunc::ParsePathFromUri(oldPath); + string newPathStr = CommonFunc::ParsePathFromUri(newPath); + int ret = uv_fs_symlink(nullptr, symlink_req.get(), oldPathStr.c_str(), newPathStr.c_str(), 0, nullptr); if (ret < 0) { HILOGE("Failed to create a link for old path"); return NError(errno); diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index 695b6a45c..bb0257719 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -57,8 +57,9 @@ static NError TruncateCore(napi_env env, FileInfo &fileInfo, int64_t truncateLen HILOGE("Failed to request heap memory."); return NError(ENOMEM); } - int ret = uv_fs_open(nullptr, open_req.get(), fileInfo.path.get(), O_RDWR, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); + int ret = uv_fs_open(nullptr, open_req.get(), + CommonFunc::ParsePathFromUri(string(fileInfo.path.get()), O_RDWR).c_str(), + O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); if (ret < 0) { return NError(errno); } diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index abb167ec7..32fcddc4f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -20,6 +20,8 @@ #include #include #include + +#include "common_func.h" #include "ipc_skeleton.h" #include "file_utils.h" #include "filemgmt_libhilog.h" @@ -98,6 +100,7 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } + string pathStr = CommonFunc::ParsePathFromUri(string(filename.get())); watcherEntity->data_ = CreateUniquePtr(NVal(env, funcArg[NARG_POS::THIRD])); if (watcherEntity->data_ == nullptr) { HILOGE("Failed to request heap memory."); @@ -106,7 +109,7 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) } watcherEntity->data_->events = events; watcherEntity->data_->env = env; - watcherEntity->data_->filename = string(filename.get()); + watcherEntity->data_->filename = pathStr; watcherEntity->data_->fd = fd; watcherEntity->watcherPtr_ = watcherPtr; diff --git a/interfaces/kits/native/remote_uri/remote_uri.cpp b/interfaces/kits/native/remote_uri/remote_uri.cpp index c7e6bc4c3..3ab9c7682 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.cpp +++ b/interfaces/kits/native/remote_uri/remote_uri.cpp @@ -1,3 +1,4 @@ + /* * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,6 +28,7 @@ namespace DistributedFS { namespace ModuleRemoteUri { using namespace std; +std::setRemoteUri::fdFromBinder; bool RemoteUri::IsMediaUri(const string &uriString) { diff --git a/interfaces/kits/native/remote_uri/remote_uri.h b/interfaces/kits/native/remote_uri/remote_uri.h index 2dbbbfb0b..7c14fec7c 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.h +++ b/interfaces/kits/native/remote_uri/remote_uri.h @@ -50,7 +50,6 @@ public: static bool IsFileUri(const string &uriString); ~RemoteUri() {} }; -std::setRemoteUri::fdFromBinder; } // namespace ModuleRemoteUri } // namespace DistributedFS } // namespace OHOS -- Gitee