From b32c938284e7fb9803bb73edec47c358cf0b4117 Mon Sep 17 00:00:00 2001 From: fengjq Date: Sat, 7 Oct 2023 19:36:47 +0800 Subject: [PATCH] Add realPath for dir path Signed-off-by: fengjq --- interfaces/kits/js/src/mod_fs/common_func.cpp | 15 ++++++ interfaces/kits/js/src/mod_fs/common_func.h | 2 + .../kits/js/src/mod_fs/properties/copydir.cpp | 48 ++++++++++++------ .../kits/js/src/mod_fs/properties/mkdtemp.cpp | 18 +++++-- .../kits/js/src/mod_fs/properties/movedir.cpp | 49 ++++++++++++------- .../src/mod_fs/properties/prop_n_exporter.cpp | 17 +++++-- 6 files changed, 109 insertions(+), 40 deletions(-) mode change 100755 => 100644 interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 94f4cf5c5..acaa61592 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -204,6 +204,21 @@ void CommonFunc::fs_req_cleanup(uv_fs_t* req) } } +tuple> CommonFunc::GetRealPath(const string &path) +{ + std::unique_ptr realpath_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!realpath_req) { + HILOGE("Failed to request heap memory."); + return { ENOMEM, move(realpath_req) }; + } + int ret = uv_fs_realpath(nullptr, realpath_req.get(), path.c_str(), nullptr); + if (ret < 0) { + return { ret, move(realpath_req) }; + } + return { ERRNO_NOERR, move(realpath_req) }; +} + string CommonFunc::GetModeFromFlags(unsigned int flags) { const string readMode = "r"; diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index f332321c1..e436e29a2 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -71,6 +71,8 @@ struct CommonFunc { napi_value srcPath, napi_value dstPath); static void fs_req_cleanup(uv_fs_t* req); + static std::tuple> + GetRealPath(const std::string &path); static std::string GetModeFromFlags(unsigned int flags); static bool CheckPublicDirPath(const std::string &sandboxPath); static std::string Decode(const std::string &uri); diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 3dcaea202..5db7182df 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -31,6 +31,7 @@ namespace FileManagement { namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +using UvRequest = std::unique_ptr; static int RecurCopyDir(const string &srcPath, const string &destPath, const int mode, vector &errfiles); @@ -44,21 +45,33 @@ static bool AllowToCopy(const string &src, const string &dest) return true; } -static tuple, unique_ptr, int> ParseAndCheckJsOperand(napi_env env, - const NFuncArg &funcArg) +static tuple ParseAndCheckJsOperand(napi_env env, const NFuncArg &funcArg) { + UvRequest realpathSrc = { nullptr, CommonFunc::fs_req_cleanup }; + UvRequest realpathDest = { nullptr, CommonFunc::fs_req_cleanup }; auto [resGetFirstArg, src, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!resGetFirstArg || !filesystem::is_directory(filesystem::status(src.get()))) { HILOGE("Invalid src"); - return { false, nullptr, nullptr, 0 }; + return { EINVAL, move(realpathSrc), move(realpathDest), 0 }; + } + int ret = 0; + tie(ret, realpathSrc) = CommonFunc::GetRealPath(src.get()); + if (ret) { + HILOGE("Failed to get absolute path from src, ret: %{public}d", ret); + return { ret, move(realpathSrc), move(realpathDest), 0 }; } auto [resGetSecondArg, dest, unused] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); if (!resGetSecondArg || !filesystem::is_directory(filesystem::status(dest.get()))) { HILOGE("Invalid dest"); - return { false, nullptr, nullptr, 0 }; + return { EINVAL, move(realpathSrc), move(realpathDest), 0 }; + } + tie(ret, realpathDest) = CommonFunc::GetRealPath(dest.get()); + if (ret) { + HILOGE("Failed to get absolute path from dest, ret: %{public}d", ret); + return { ret, move(realpathSrc), move(realpathDest), 0 }; } - if (!AllowToCopy(src.get(), dest.get())) { - return { false, nullptr, nullptr, 0 }; + if (!AllowToCopy(static_cast(realpathSrc->ptr), static_cast(realpathDest->ptr))) { + return { EINVAL, move(realpathSrc), move(realpathDest), 0 }; } int mode = 0; if (funcArg.GetArgc() >= NARG_CNT::THREE) { @@ -66,10 +79,10 @@ static tuple, unique_ptr, int> ParseAndCheckJsO tie(resGetThirdArg, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(mode); if (!resGetThirdArg || (mode < COPYMODE_MIN || mode > COPYMODE_MAX)) { HILOGE("Invalid mode"); - return { false, nullptr, nullptr, 0 }; + return { EINVAL, move(realpathSrc), move(realpathDest), 0 }; } } - return { true, move(src), move(dest), mode }; + return { ERRNO_NOERR, move(realpathSrc), move(realpathDest), mode }; } static int MakeDir(const string &path) @@ -237,14 +250,16 @@ napi_value CopyDir::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succ, src, dest, mode] = ParseAndCheckJsOperand(env, funcArg); - if (!succ) { - NError(EINVAL).ThrowErr(env); + auto [ret, srcReq, destReq, mode] = ParseAndCheckJsOperand(env, funcArg); + if (ret) { + NError(ret).ThrowErr(env); return nullptr; } vector errfiles = {}; - int ret = CopyDirFunc(src.get(), dest.get(), mode, errfiles); + string src(static_cast(srcReq->ptr)); + string dest(static_cast(destReq->ptr)); + 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; @@ -268,9 +283,9 @@ napi_value CopyDir::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succ, src, dest, mode] = ParseAndCheckJsOperand(env, funcArg); - if (!succ) { - NError(EINVAL).ThrowErr(env); + auto [ret, srcReq, destReq, mode] = ParseAndCheckJsOperand(env, funcArg); + if (!ret) { + NError(ret).ThrowErr(env); return nullptr; } @@ -280,7 +295,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 = string(static_cast(srcReq->ptr)), + destPath = string(static_cast(destReq->ptr)), 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/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp old mode 100755 new mode 100644 index 8c8a4dc29..fff4f4f78 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -41,7 +41,12 @@ napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info) return nullptr; } - string path = tmp.get(); + auto [ret, realpathSrc] = CommonFunc::GetRealPath(tmp.get()); + if (ret) { + HILOGE("Failed to get absolute path"); + NError(ret).ThrowErr(env); + return nullptr; + } std::unique_ptr mkdtemp_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!mkdtemp_req) { @@ -49,7 +54,7 @@ napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), const_cast(path.c_str()), nullptr); + ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), static_cast(realpathSrc->ptr), nullptr); if (ret < 0) { HILOGE("Failed to create a temporary directory with path"); NError(ret).ThrowErr(env); @@ -92,14 +97,19 @@ napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - + auto [ret, realpathSrc] = CommonFunc::GetRealPath(tmp.get()); + if (ret) { + HILOGE("Failed to get absolute path"); + NError(ret).ThrowErr(env); + return nullptr; + } auto arg = CreateSharedPtr(); if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [path = string(tmp.get()), arg]() -> NError { + auto cbExec = [path = string(static_cast(realpathSrc->ptr)), 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/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 6f845c17d..dc0ba3bf5 100644 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -32,6 +32,7 @@ namespace FileManagement { namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +using UvRequest = std::unique_ptr; static int RecurMoveDir(const string &srcPath, const string &destPath, const int mode, vector &errfiles); @@ -73,17 +74,30 @@ static int RemovePath(const string& pathStr) return ERRNO_NOERR; } -static tuple, unique_ptr, int> ParseJsOperand(napi_env env, const NFuncArg& funcArg) +static tuple ParseJsOperand(napi_env env, const NFuncArg& funcArg) { + UvRequest realpathSrc = { nullptr, CommonFunc::fs_req_cleanup }; + UvRequest realpathDest = { nullptr, CommonFunc::fs_req_cleanup }; auto [resGetFirstArg, src, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!resGetFirstArg || !filesystem::is_directory(filesystem::status(src.get()))) { HILOGE("Invalid src"); - return { false, nullptr, nullptr, 0 }; + return { EINVAL, move(realpathSrc), move(realpathDest), 0 }; + } + int ret = 0; + tie(ret, realpathSrc) = CommonFunc::GetRealPath(src.get()); + if (ret) { + HILOGE("Failed to get absolute path from src, ret: %{public}d", ret); + return { ret, move(realpathSrc), move(realpathDest), 0 }; } auto [resGetSecondArg, dest, unused] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); if (!resGetSecondArg || !filesystem::is_directory(filesystem::status(dest.get()))) { HILOGE("Invalid dest"); - return { false, nullptr, nullptr, 0 }; + return { EINVAL, move(realpathSrc), move(realpathDest), 0 }; + } + tie(ret, realpathDest) = CommonFunc::GetRealPath(src.get()); + if (ret) { + HILOGE("Failed to get absolute path from dest, ret: %{public}d", ret); + return { ret, move(realpathSrc), move(realpathDest), 0 }; } int mode = 0; if (funcArg.GetArgc() >= NARG_CNT::THREE) { @@ -91,16 +105,15 @@ static tuple, unique_ptr, int> ParseJsOperand(n tie(resGetThirdArg, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(mode); if (!resGetThirdArg || (mode < DIRMODE_MIN || mode > DIRMODE_MAX)) { HILOGE("Invalid mode"); - return { false, nullptr, nullptr, 0 }; + return { EINVAL, move(realpathSrc), move(realpathDest), 0 }; } } - return { true, move(src), move(dest), mode }; + return { ERRNO_NOERR, move(realpathSrc), move(realpathDest), mode }; } static int CopyAndDeleteFile(const string &src, const string &dest) { - std::unique_ptr stat_req = { - new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + UvRequest stat_req = { new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!stat_req) { HILOGE("Failed to request heap memory."); return ENOMEM; @@ -124,8 +137,7 @@ static int CopyAndDeleteFile(const string &src, const string &dest) HILOGE("Failed to copy file, error code: %{public}d", errCode.value()); return errCode.value(); } - std::unique_ptr utime_req = { - new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + UvRequest utime_req = { new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!utime_req) { HILOGE("Failed to request heap memory."); return ENOMEM; @@ -323,14 +335,16 @@ napi_value MoveDir::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succ, src, dest, mode] = ParseJsOperand(env, funcArg); - if (!succ) { - NError(EINVAL).ThrowErr(env); + auto [ret, srcReq, destReq, mode] = ParseJsOperand(env, funcArg); + if (ret) { + NError(ret).ThrowErr(env); return nullptr; } + string src(static_cast(srcReq->ptr)); + string dest(static_cast(destReq->ptr)); vector errfiles = {}; - int ret = MoveDirFunc(src.get(), dest.get(), mode, errfiles); + ret = MoveDirFunc(src, dest, mode, errfiles); if (ret == EEXIST && mode == DIRMODE_FILE_THROW_ERR) { NError(ret).ThrowErrAddData(env, EEXIST, GetErrData(env, errfiles)); return nullptr; @@ -355,9 +369,9 @@ napi_value MoveDir::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succ, src, dest, mode] = ParseJsOperand(env, funcArg); - if (!succ) { - NError(EINVAL).ThrowErr(env); + auto [ret, srcReq, destReq, mode] = ParseJsOperand(env, funcArg); + if (ret) { + NError(ret).ThrowErr(env); return nullptr; } auto arg = CreateSharedPtr(); @@ -366,7 +380,8 @@ 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 = string(static_cast(srcReq->ptr)), + destPath = string(static_cast(destReq->ptr)), 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/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 4bcccb9e4..2c69297fd 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 @@ -244,8 +244,14 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } + auto [ret, realpathSrc] = CommonFunc::GetRealPath(tmp.get()); + if (ret) { + HILOGE("Failed to get absolute path"); + NError(ret).ThrowErr(env); + return nullptr; + } - auto cbExec = [path = string(tmp.get())]() -> NError { + auto cbExec = [path = string(static_cast(realpathSrc->ptr))]() -> NError { std::unique_ptr mkdir_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!mkdir_req) { @@ -291,7 +297,12 @@ napi_value PropNExporter::MkdirSync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - + auto [ret, realpathSrc] = CommonFunc::GetRealPath(path.get()); + if (ret) { + HILOGE("Failed to get absolute path"); + NError(ret).ThrowErr(env); + return nullptr; + } std::unique_ptr mkdir_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!mkdir_req) { @@ -299,7 +310,7 @@ 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); + ret = uv_fs_mkdir(nullptr, mkdir_req.get(), static_cast(realpathSrc->ptr), DIR_DEFAULT_PERM, nullptr); if (ret < 0) { HILOGE("Failed to create directory"); NError(ret).ThrowErr(env); -- Gitee