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 160ce2e6908c4b8a97ca09e9d6907959b3a42379..e4543d33de678ef74b5156bafd92ddc24bff4221 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 @@ -52,6 +52,7 @@ #include "move.h" #include "movedir.h" #include "read_text.h" +#include "rust_file.h" #include "symlink.h" #include "watcher.h" #endif @@ -233,7 +234,7 @@ napi_value PropNExporter::UnlinkSync(napi_env env, napi_callback_info info) napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -245,22 +246,24 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - - auto cbExec = [path = string(tmp.get())]() -> NError { - std::unique_ptr mkdir_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!mkdir_req) { - HILOGE("Failed to request heap memory."); - return NError(ENOMEM); + MakeDirectionMode recursionMode = SINGLE; + if (funcArg.GetArgc() >= NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_boolean)) { + auto [success, recursion] = NVal(env, funcArg[NARG_POS::SECOND]).ToBool(false); + if (!success) { + HILOGE("Invalid recursion mode"); + NError(EINVAL).ThrowErr(env); + return nullptr; } - int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.c_str(), DIR_DEFAULT_PERM, nullptr); - if (ret < 0) { - HILOGE("Failed to create directory"); - return NError(ret); + recursionMode = static_cast(recursion); + } + + auto cbExec = [path = string(tmp.get()), recursionMode]() -> NError { + ::Mkdirs(const_cast(path.c_str()), recursionMode); + if (errno != 0) { + return NError(errno); } - return NError(ERRNO_NOERR); + return NError(ERRNO_NOERR); }; - auto cbCompl = [](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; @@ -269,10 +272,12 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ONE) { + + if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_MKDIR_NAME, cbExec, cbCompl).val_; } else { - NVal cb(env, funcArg[NARG_POS::SECOND]); + NVal cb(env, funcArg[funcArg.GetArgc() - 1]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_MKDIR_NAME, cbExec, cbCompl).val_; } } @@ -280,7 +285,7 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) napi_value PropNExporter::MkdirSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE)) { + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -293,20 +298,22 @@ napi_value PropNExporter::MkdirSync(napi_env env, napi_callback_info info) return nullptr; } - std::unique_ptr mkdir_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!mkdir_req) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; + MakeDirectionMode recursionMode = SINGLE; + if (funcArg.GetArgc() == NARG_CNT::TWO) { + auto [success, recursion] = NVal(env, funcArg[NARG_POS::SECOND]).ToBool(false); + if (!success) { + HILOGE("Invalid recursion mode"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + recursionMode = static_cast(recursion); } - int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.get(), DIR_DEFAULT_PERM, nullptr); - if (ret < 0) { - HILOGE("Failed to create directory"); - NError(ret).ThrowErr(env); + ::Mkdirs(const_cast(path.get()), recursionMode); + if (errno != 0) { + HILOGE("Failed to mkdirs, error:%{public}d", errno); + NError(errno).ThrowErr(env); return nullptr; } - return NVal::CreateUndefined(env).val_; }