From 86724e55fb5b84bd7101cf842904a11219768bcf Mon Sep 17 00:00:00 2001 From: gwx1278443 Date: Tue, 31 Oct 2023 14:29:43 +0800 Subject: [PATCH] getrealpath Signed-off-by: gwx1278443 --- interfaces/kits/js/src/mod_fs/common_func.cpp | 33 +++++++++ interfaces/kits/js/src/mod_fs/common_func.h | 3 + .../kits/js/src/mod_fs/properties/copydir.cpp | 68 ++++++++++++++----- .../js/src/mod_fs/properties/listfile.cpp | 39 ++++++----- .../kits/js/src/mod_fs/properties/movedir.cpp | 65 +++++++++++++----- 5 files changed, 156 insertions(+), 52 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 32f3fa64b..a3f6020fb 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -40,6 +40,7 @@ namespace FileManagement { namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +using UvRequest = std::unique_ptr; namespace { const std::vector PUBLIC_DIR_PATHS = { @@ -230,6 +231,38 @@ void CommonFunc::fs_req_cleanup(uv_fs_t* req) } } +tuple CommonFunc::GetRealPath(const string &path) +{ + UvRequest 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) { + HILOGE("Failed to get realpath."); + return { ret, move(realpath_req) }; + } + return { ERRNO_NOERR, move(realpath_req) }; +} + +bool CommonFunc::IsValidDirectory(const string &path) +{ + UvRequest req(new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup); + if (!req) { + HILOGE("Failed to allocate memory for uv_fs_t"); + return false; + } + + auto ret = uv_fs_stat(nullptr, req.get(), path.c_str(), nullptr); + if (ret) { + HILOGE("Failed to stat directory, error code: %{public}d", ret); + return false; + } + + return (req->statbuf.st_mode & S_IFDIR); +} + 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 cc7293ddf..6ab3893b0 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -75,6 +75,9 @@ 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 bool IsValidDirectory(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..a51124969 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,32 +45,60 @@ 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) { auto [resGetFirstArg, src, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!resGetFirstArg || !filesystem::is_directory(filesystem::status(src.get()))) { + if (!resGetFirstArg) { + HILOGE("Failed to convert src to a UTF-8 string"); + return { EINVAL, UvRequest{nullptr, CommonFunc::fs_req_cleanup}, + UvRequest{nullptr, CommonFunc::fs_req_cleanup}, 0 }; + } + + auto [resSrcPath, realpathSrc] = CommonFunc::GetRealPath(string(src.get())); + if (resSrcPath != ERRNO_NOERR) { + HILOGE("Failed to get real path of source, ret: %{public}d", resSrcPath); + return { resSrcPath, move(realpathSrc), UvRequest{nullptr, CommonFunc::fs_req_cleanup}, 0 }; + } + + bool ret = CommonFunc::IsValidDirectory(string(static_cast(realpathSrc->ptr))); + if (!ret) { HILOGE("Invalid src"); - return { false, nullptr, nullptr, 0 }; + return { ENOTDIR, move(realpathSrc), UvRequest{nullptr, CommonFunc::fs_req_cleanup}, 0 }; } + auto [resGetSecondArg, dest, unused] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); - if (!resGetSecondArg || !filesystem::is_directory(filesystem::status(dest.get()))) { + if (!resGetSecondArg) { + HILOGE("Failed to convert dest to a UTF-8 string"); + return { EINVAL, move(realpathSrc), UvRequest{nullptr, CommonFunc::fs_req_cleanup}, 0 }; + } + + auto [resDestPath, realpathDest] = CommonFunc::GetRealPath(string(dest.get())); + if (resDestPath != ERRNO_NOERR) { + HILOGE("Failed to get real path of destination, ret: %{public}d", resDestPath); + return { resDestPath, move(realpathSrc), move(realpathDest), 0 }; + } + + ret = CommonFunc::IsValidDirectory(string(static_cast(realpathDest->ptr))); + if (!ret) { HILOGE("Invalid dest"); - return { false, nullptr, nullptr, 0 }; + return { ENOTDIR, move(realpathSrc), move(realpathDest), 0 }; } - if (!AllowToCopy(src.get(), dest.get())) { - return { false, nullptr, nullptr, 0 }; + + if (!AllowToCopy(string(static_cast(realpathSrc->ptr)), + string(static_cast(realpathDest->ptr)))) { + return { EINVAL, move(realpathSrc), move(realpathDest), 0 }; } + int mode = 0; if (funcArg.GetArgc() >= NARG_CNT::THREE) { bool resGetThirdArg = false; 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 +266,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 +299,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 +311,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/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index e046e1745..c71a3eb33 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" @@ -100,14 +101,14 @@ static bool GetFileFilterParam(const NVal &argv, FileFilter *filter) return true; } -static bool GetOptionParam(const NVal &argv, OptionArgs *optionArgs) +static int GetOptionParam(const NVal &argv, OptionArgs *optionArgs) { bool succ = false; if (argv.HasProp("listNum")) { tie(succ, optionArgs->listNum) = argv.GetProp("listNum").ToInt64(0); if (!succ || optionArgs->listNum < 0) { HILOGE("Failed to get listNum prop"); - return false; + return EINVAL; } } @@ -115,7 +116,7 @@ static bool GetOptionParam(const NVal &argv, OptionArgs *optionArgs) tie(succ, optionArgs->recursion) = argv.GetProp("recursion").ToBool(false); if (!succ) { HILOGE("Failed to get recursion prop."); - return false; + return EINVAL; } } @@ -125,28 +126,33 @@ static bool GetOptionParam(const NVal &argv, OptionArgs *optionArgs) auto ret = GetFileFilterParam(filterProp, &optionArgs->filter); if (!ret) { HILOGE("Failed to get filter prop."); - return false; + return EINVAL; } } } - return true; + return ERRNO_NOERR; } -static bool GetOptionArg(napi_env env, const NFuncArg &funcArg, OptionArgs &optionArgs, const string &path) +static int GetOptionArg(napi_env env, const NFuncArg &funcArg, OptionArgs &optionArgs, const string &path) { - optionArgs.path = path; + auto [ret, realpathSrc] = CommonFunc::GetRealPath(path); + if (ret) { + HILOGE("Failed to get absolute path"); + return ret; + } + optionArgs.path = std::string(static_cast(realpathSrc->ptr)); if (funcArg.GetArgc() == NARG_CNT::ONE) { - return true; + return ERRNO_NOERR; } if (funcArg.GetArgc() >= NARG_CNT::TWO) { auto options = NVal(env, funcArg[NARG_POS::SECOND]); if (options.TypeIs(napi_object)) { return GetOptionParam(options, &optionArgs); } else if (options.TypeIs(napi_undefined) || options.TypeIs(napi_function)) { - return true; + return ERRNO_NOERR; } } - return false; + return EINVAL; } static bool FilterSuffix(const vector &suffixs, const struct dirent &filename) @@ -331,13 +337,13 @@ 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()))) { + int ret = GetOptionArg(env, funcArg, g_optionArgs, string(path.get())); + if (ret) { HILOGE("Invalid options"); - NError(EINVAL).ThrowErr(env); + NError(ret).ThrowErr(env); return nullptr; } vector direntsRes; - int ret = 0; ret = g_optionArgs.recursion ? RecursiveFunc(path.get(), direntsRes) : FilterFileRes(path.get(), direntsRes); if (ret) { NError(ret).ThrowErr(env); @@ -365,9 +371,10 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) } OptionArgs optionArgsTmp = {}; - if (!GetOptionArg(env, funcArg, optionArgsTmp, string(path.get()))) { - HILOGE("Invalid options"); - NError(EINVAL).ThrowErr(env); + int ret = GetOptionArg(env, funcArg, optionArgsTmp, string(path.get())); + if (ret) { + HILOGE("Failed to get arguments"); + NError(ret).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index cf8b29326..3db8c227d 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,28 +74,55 @@ 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) { auto [resGetFirstArg, src, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!resGetFirstArg || !filesystem::is_directory(filesystem::status(src.get()))) { + if (!resGetFirstArg) { + HILOGE("Failed to convert src to a UTF-8 string"); + return { EINVAL, UvRequest{nullptr, CommonFunc::fs_req_cleanup}, + UvRequest{nullptr, CommonFunc::fs_req_cleanup}, 0 }; + } + + auto [resSrcPath, realpathSrc] = CommonFunc::GetRealPath(string(src.get())); + if (resSrcPath != ERRNO_NOERR) { + HILOGE("Failed to get real path of source, ret: %{public}d", resSrcPath); + return { resSrcPath, move(realpathSrc), UvRequest{nullptr, CommonFunc::fs_req_cleanup}, 0 }; + } + + bool ret = CommonFunc::IsValidDirectory(string(static_cast(realpathSrc->ptr))); + if (!ret) { HILOGE("Invalid src"); - return { false, nullptr, nullptr, 0 }; + return { ENOTDIR, move(realpathSrc), UvRequest{nullptr, CommonFunc::fs_req_cleanup}, 0 }; } + auto [resGetSecondArg, dest, unused] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); - if (!resGetSecondArg || !filesystem::is_directory(filesystem::status(dest.get()))) { + if (!resGetSecondArg) { + HILOGE("Failed to convert dest to a UTF-8 string"); + return { EINVAL, move(realpathSrc), UvRequest{nullptr, CommonFunc::fs_req_cleanup}, 0 }; + } + + auto [resDestPath, realpathDest] = CommonFunc::GetRealPath(string(dest.get())); + if (resDestPath != ERRNO_NOERR) { + HILOGE("Failed to get real path of destination, ret: %{public}d", resDestPath); + return { resDestPath, move(realpathSrc), move(realpathDest), 0 }; + } + + ret = CommonFunc::IsValidDirectory(string(static_cast(realpathDest->ptr))); + if (!ret) { HILOGE("Invalid dest"); - return { false, nullptr, nullptr, 0 }; + return { ENOTDIR, move(realpathSrc), move(realpathDest), 0 }; } + int mode = 0; if (funcArg.GetArgc() >= NARG_CNT::THREE) { bool resGetThirdArg = false; 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 RestoreTime(const string &srcPath, const string &destPath) @@ -203,7 +231,7 @@ static int RenameDir(const string &src, const string &dest, const int mode, vect return RecurMoveDir(src, dest, mode, errfiles); } if (errCode.value() != 0) { - HILOGE("Failed to rename file, error code: %{public}d", errCode.value()); + HILOGE("Failed to rename directory, error code: %{public}d", errCode.value()); return errCode.value(); } return ERRNO_NOERR; @@ -337,15 +365,15 @@ 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; } - vector errfiles = {}; + deque errfiles = {}; int ret = MoveDirFunc(src.get(), dest.get(), mode, errfiles); - if (ret == EEXIST && mode == DIRMODE_FILE_THROW_ERR) { + if (ret == EEXIST) { NError(ret).ThrowErrAddData(env, EEXIST, GetErrData(env, errfiles)); return nullptr; } else if (ret) { @@ -356,7 +384,7 @@ napi_value MoveDir::Sync(napi_env env, napi_callback_info info) } struct MoveDirArgs { - vector errfiles; + deque errfiles; int errNo = 0; ~MoveDirArgs() = default; }; @@ -369,9 +397,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(); @@ -380,7 +408,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); -- Gitee