diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 32f3fa64b8e22b3302779edd0d732225bb153171..4878d5f8dbef464ec1834eee50e71403a132c08d 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "class_stat/stat_entity.h" #include "class_stat/stat_n_exporter.h" @@ -230,6 +231,30 @@ 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) }; +} + +bool CommonFunc::IsDirectoryValid(const std::string& path) +{ + if (!filesystem::is_directory(filesystem::status(path))) { + HILOGE("Invalid directory: %s", path.c_str()); + return false; + } + return true; +} + 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 cc7293ddf6a2fd6f53091775f54d83071cc2877d..1062a8c48005c79cecb446e0e711f2f207a13f88 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -46,9 +46,6 @@ constexpr unsigned int USR_DIRECTORY = 0200000; constexpr unsigned int USR_NOFOLLOW = 0400000; constexpr unsigned int USR_SYNC = 04010000; -const double NS = 1e9; -const double MS = 1e3; - struct FileInfo { bool isPath = false; std::unique_ptr path = { nullptr }; @@ -75,6 +72,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 CommonFunc::IsDirectoryValid(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 3dcaea202ad133ae3aa84fe0904d08299b62cc6e..26c4c4e4dacc0fb2131c7d01a0276c8670f21482 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,42 @@ 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 }; + if (!resGetFirstArg) { + HILOGE("failed to UTF8String of src"); + 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 { EINVAL, move(realpathSrc), move(realpathDest), 0 }; + } + string strPathSrc(static_cast(realpathSrc->ptr)); + if (!CommonFunc::IsDirectoryValid(strPathSrc)) { + return { EINVAL, 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 }; + if (!resGetSecondArg) { + HILOGE("failed to UTF8String of dest"); + 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 { EINVAL, move(realpathSrc), move(realpathDest), 0 }; + } + string strPathDest(static_cast(realpathDest->ptr)); + if (!CommonFunc::IsDirectoryValid(strPathDest)) { + return { EINVAL, 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) { @@ -66,10 +88,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 +259,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 +292,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 +304,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 e046e174517d0d2fb836b0c6b712efc23adc7655..55dafdd5bab4ea48646d362c70d79860b8fed22e 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 = 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 cf8b293266c5dbda2c2a5f82d9af6fae15eaf0d2..83b3c6fbb6a9d29a6b7a6fe1b57185abcfde1b9e 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,38 @@ 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 }; + if (!resGetFirstArg) { + HILOGE("failed to UTF8String of src"); + 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 { EINVAL, move(realpathSrc), move(realpathDest), 0 }; + } + string strPathSrc(static_cast(realpathSrc->ptr)); + if (!CommonFunc::IsDirectoryValid(strPathSrc)) { + return { EINVAL, 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 }; + if (!resGetSecondArg) { + HILOGE("failed to UTF8String of dest"); + 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 { EINVAL, move(realpathSrc), move(realpathDest), 0 }; + } + string strPathDest(static_cast(realpathDest->ptr)); + if (!CommonFunc::IsDirectoryValid(strPathDest)) { + return { EINVAL, move(realpathSrc), move(realpathDest), 0 }; } int mode = 0; if (funcArg.GetArgc() >= NARG_CNT::THREE) { @@ -91,10 +113,10 @@ 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 RestoreTime(const string &srcPath, const string &destPath) @@ -187,7 +209,7 @@ static int RenameDir(const string &src, const string &dest, const int mode, vect } filesystem::path srcPath(src); std::error_code errCode; - filesystem::rename(srcPath, destPath, errCode); + filesystem::rename(src, dest, errCode); if (errCode.value() == EXDEV) { HILOGE("Failed to rename file due to EXDEV"); if (!filesystem::create_directory(destPath, errCode)) { @@ -203,7 +225,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 file or directory, error code: %{public}d", errCode.value()); return errCode.value(); } return ERRNO_NOERR; @@ -337,14 +359,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); - return nullptr; + auto [ret, srcReq, destReq, mode] = ParseJsOperand(env, funcArg); + if (ret) { + NError(ret).ThrowErr(env); } + 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; @@ -369,9 +392,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 +403,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/utimes.cpp b/interfaces/kits/js/src/mod_fs/properties/utimes.cpp index b73e1ab7074d4cb2c857ca0449d398907767dec1..04277c96413e1dc24a1c2e8c2c20e0823064f68c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/utimes.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/utimes.cpp @@ -42,7 +42,7 @@ napi_value Utimes::Sync(napi_env env, napi_callback_info info) return nullptr; } - auto [succGetMtime, mtime] = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();; + auto [succGetMtime, mtime] = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble(); if (!succGetMtime || mtime < 0) { HILOGE("Invalid mtime from JS second argument"); NError(EINVAL).ThrowErr(env);