diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index b3c28919618a7a41bc0f05a6f97ec1d746aebf6b..b589c2d361e085676d22f928f5cc970e393ecc9f 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 7a169bf9d9cdfcaa703cf3936a927393fe292fce..b6b9a4e6f54e1c9b1c76ae0abe2eac6522180fb1 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 77ecd5ea533d0875c47bda0cbfc9537999a626d0..4b0c1dd462c2dc5ad8c61c3c34589b55289507e3 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 16329aff9e099dc82c28e3f8ef6ccbb02231d277..bf2cd424430627388a76d090ce4fcaf972aa92ff 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 b2ac7d695ff4aa58eb5bd451e4b8857a34f01283..e5b5d9991ef0dcf878c66be3ce66a328d52f717b 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 963c7c7ab33a4b9a6aa2d6217790922e8dcf5271..b5384dbb0645c157d6efaa7b52e7298c9a17a883 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" @@ -325,19 +326,21 @@ 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; - ret = g_optionArgs.recursion ? RecursiveFunc(path.get(), direntsRes) : 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; } - auto res = DoListFileVector2NV(env, string(path.get()), direntsRes, g_optionArgs.recursion); + auto res = DoListFileVector2NV(env, pathStr, direntsRes, g_optionArgs.recursion); g_optionArgs.Clear(); return res; } @@ -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; @@ -380,7 +384,7 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) return ret ? NError(ret) : NError(ERRNO_NOERR); }; - auto cbCompl = [arg, optionArgsTmp, 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) }; } diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index 823e9603c10092fdc8efe94a97ec559c3524979c..e9a7df706601790971feb36d5a3622e14b584862 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -50,9 +50,11 @@ 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()); + HILOGE("Failed to get stat of file by path %{public}s", pathStr.c_str()); NError(ret).ThrowErr(env); return nullptr; } @@ -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 541fdb6a5576abc3749f55ce3aba44938bcfee8a..2a86752b1f0193ffba3d7a88eb3b09a5d374b666 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 a27edca342fd8330903f15c383e53a4e8cc4f5b3..6b52de2e9a93cb589e95202b9407b9fe446e341e 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 195aa0e29c077052453804217970645d1b27f0c6..0474fbcdd99bfe57b17ecb7ae8225d2b7cb14c86 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 0e2b7413f444835373af0335701baf9edf49478a..2b01bac30a68762cc6cf5ca8e8e655e342327cad 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 0ba33786fe0d223848997109154df5f27ab417a6..36fba9bebc81317531e5afd08d8bbd51e6dd1347 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,7 +81,7 @@ 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); + 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(ret).ThrowErr(env); @@ -121,7 +122,8 @@ 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; } @@ -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) { @@ -214,7 +216,8 @@ 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(ret).ThrowErr(env); @@ -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) { @@ -294,7 +297,9 @@ 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(ret).ThrowErr(env); 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 262f52da0dbf8a3cdd5f82e7e6ccc268dc93cc41..826249eb2dd9003b29d0da81d1008345fb1bd515 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 7a5f6b91906ba96ee73be0219d8edc31b3d334ec..3d7e8b3c7823539c9dcd39f5338d12b6f1bb28c5 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(ret).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 43ecec7cae87926f9b24b11126381a014e7aa175..da2b6316f434ed6776cfeeb98f95cad63984e6d2 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 490d79d36fc8bbe2c85b9b4ea068273eeb362380..8b463f92e26d4ebdf26d22a5b1e3507089d7408a 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(ret); diff --git a/interfaces/kits/js/src/mod_fs/properties/symlink.cpp b/interfaces/kits/js/src/mod_fs/properties/symlink.cpp index 176a19ddd20fc2cf11849bed736b9040c0a8b95a..11f9fef1ac845674203227c8a263e76e8128a791 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(ret).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(ret); diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index a793c2811751816ba184a18be989e79d739eaf6c..858c0129b285f0f834bcdc59108ec34211324a8b 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(ret); } diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 8d3fa0341affa6418f068f2b485a6acb606f371c..e29817997176afcbb3cb9a62aa277ae7d4cfebab 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 c7e6bc4c36ba9f30303aa01e9b2ee87522369ad5..3ab9c768286ece04280d9a103dfa447379511154 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 2dbbbfb0b0ea0df716b688cd6624b94992f0c86a..7c14fec7cea4ae04d7d32676258c728787780096 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