From 5c4307c7652eea7812a7e0f398701095a1aa2fc1 Mon Sep 17 00:00:00 2001 From: futurezhou Date: Fri, 6 Jan 2023 12:48:46 +0800 Subject: [PATCH] Add mkdtemp interface for mod_fs Signed-off-by: futurezhou --- interfaces/kits/js/BUILD.gn | 0 .../kits/js/src/mod_fs/properties/mkdtemp.cpp | 105 ++++++++++++++++++ .../kits/js/src/mod_fs/properties/mkdtemp.h | 34 ++++++ .../src/mod_fs/properties/prop_n_exporter.cpp | 2 +- .../src/mod_fs/properties/prop_n_exporter.h | 0 5 files changed, 140 insertions(+), 1 deletion(-) mode change 100644 => 100755 interfaces/kits/js/BUILD.gn create mode 100755 interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp create mode 100755 interfaces/kits/js/src/mod_fs/properties/mkdtemp.h mode change 100644 => 100755 interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp mode change 100644 => 100755 interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn old mode 100644 new mode 100755 diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp new file mode 100755 index 000000000..ddc209a54 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -0,0 +1,105 @@ +/* + * 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 + +#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, uv_fs_req_cleanup }; + 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: %{public}s", path.c_str()); + NError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUTF8String(env, mkdtemp_req->path).val_; +} + +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 = make_shared(); + auto cbExec = [path = tmp.get(), arg]() -> NError { + std::unique_ptr mkdtemp_req = { new uv_fs_t, uv_fs_req_cleanup }; + int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), const_cast(path), nullptr); + if (ret < 0) { + HILOGE("Failed to create a temporary directory with path: %{public}s", path); + return NError(errno); + } else { + *arg = mkdtemp_req->path; + return NError(ERRNO_NOERR); + } + }; + + auto cbComplete = [arg](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + return NVal::CreateUTF8String(env, *arg); + } + }; + + const string procedureName = "FileIOmkdtemp"; + size_t argc = funcArg.GetArgc(); + NVal thisVar(env, funcArg.GetThisVar()); + if (argc == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } +} +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.h b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.h new file mode 100755 index 000000000..ac968c32e --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.h @@ -0,0 +1,34 @@ +/* + * 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. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_MKDTEMP_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_MKDTEMP_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace OHOS::FileManagement::LibN; + +class Mkdtemp final { +public: + static napi_value Sync(napi_env env, napi_callback_info info); + static napi_value Async(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_MKDTEMP_H \ No newline at end of file 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 old mode 100644 new mode 100755 index 62baa5206..978af1132 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -297,4 +297,4 @@ PropNExporter::PropNExporter(napi_env env, napi_value exports) : NExporter(env, PropNExporter::~PropNExporter() {} } // namespace ModuleFileIO } // namespace FileManagement -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h old mode 100644 new mode 100755 -- Gitee