From 18c2e5a242a802045c691ffd2056e059dcbbdd47 Mon Sep 17 00:00:00 2001 From: shuaiguo991102 Date: Wed, 18 Oct 2023 16:54:01 +0800 Subject: [PATCH 1/2] GetRealPath Signed-off-by: shuaiguo991102 --- interfaces/kits/js/src/mod_fs/common_func.cpp | 15 ++++++ interfaces/kits/js/src/mod_fs/common_func.h | 2 + .../js/src/mod_fs/properties/listfile.cpp | 42 +++++++++------- .../kits/js/src/mod_fs/properties/mkdtemp.cpp | 0 .../kits/js/src/mod_fs/properties/movedir.cpp | 49 ++++++++++++------- 5 files changed, 74 insertions(+), 34 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 32f3fa64b..834dc0473 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -230,6 +230,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 93de17160..e2c23b91c 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -72,6 +72,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/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index e046e1745..758d69fb6 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,14 @@ 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; + std::vector direntsRes; + ret = 0; ret = g_optionArgs.recursion ? RecursiveFunc(path.get(), direntsRes) : FilterFileRes(path.get(), direntsRes); if (ret) { NError(ret).ThrowErr(env); @@ -365,9 +372,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/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp old mode 100755 new mode 100644 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); -- Gitee From 988ad83f38529fad129d7b323ba6b142e6927d2e Mon Sep 17 00:00:00 2001 From: shuaiguo Date: Wed, 25 Oct 2023 01:06:57 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20inte?= =?UTF-8?q?rfaces/kits/js/src/mod=5Ffs/properties/mkdtemp.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kits/js/src/mod_fs/properties/mkdtemp.cpp | 124 ------------------ 1 file changed, 124 deletions(-) delete mode 100644 interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp deleted file mode 100644 index 8c8a4dc29..000000000 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "mkdtemp.h" - -#include "common_func.h" -#include "file_utils.h" -#include "filemgmt_libhilog.h" - -namespace OHOS { -namespace FileManagement { -namespace ModuleFileIO { -using namespace std; -using namespace OHOS::FileManagement::LibN; - -napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!resGetFirstArg) { - HILOGE("Invalid path"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - string path = tmp.get(); - std::unique_ptr mkdtemp_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!mkdtemp_req) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } - int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), const_cast(path.c_str()), nullptr); - if (ret < 0) { - HILOGE("Failed to create a temporary directory with path"); - NError(ret).ThrowErr(env); - return nullptr; - } - - return NVal::CreateUTF8String(env, mkdtemp_req->path).val_; -} - -static NError MkdTempExec(shared_ptr arg, const string &path) -{ - std::unique_ptr mkdtemp_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!mkdtemp_req) { - HILOGE("Failed to request heap memory."); - return NError(ENOMEM); - } - int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), path.c_str(), nullptr); - if (ret < 0) { - HILOGE("Failed to create a temporary directory with path"); - return NError(ret); - } else { - *arg = mkdtemp_req->path; - return NError(ERRNO_NOERR); - } -} - -napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!resGetFirstArg) { - HILOGE("Invalid path"); - NError(EINVAL).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 { - return MkdTempExec(arg, path); - }; - auto cbComplete = [arg](napi_env env, NError err) -> NVal { - if (err) { - return { env, err.GetNapiErr(env) }; - } else { - return NVal::CreateUTF8String(env, *arg); - } - }; - - size_t argc = funcArg.GetArgc(); - NVal thisVar(env, funcArg.GetThisVar()); - if (argc == NARG_CNT::ONE) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_MKDTEMP_NAME, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[NARG_POS::SECOND]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_MKDTEMP_NAME, cbExec, cbComplete).val_; - } -} -} // namespace ModuleFileIO -} // namespace FileManagement -} // namespace OHOS -- Gitee