From 7d78cbdc41c904943aa083236842fb098e7dec6f Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 24 Mar 2025 11:28:48 +0800 Subject: [PATCH 1/3] mkdtemp Signed-off-by: liyuke --- interfaces/kits/js/BUILD.gn | 2 + .../js/src/mod_fs/ani/bind_function_class.cpp | 2 + .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 30 ++++++++++ .../src/mod_fs/properties/ani/mkdtemp_ani.cpp | 60 +++++++++++++++++++ .../src/mod_fs/properties/ani/mkdtemp_ani.h | 36 +++++++++++ .../js/src/mod_fs/properties/mkdtemp_core.cpp | 44 ++++++++++++++ .../js/src/mod_fs/properties/mkdtemp_core.h | 34 +++++++++++ 7 files changed, 208 insertions(+) create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 9334220be..e8e05e1d6 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -685,6 +685,7 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/ani/copy_file_ani.cpp", "src/mod_fs/properties/ani/listfile_ani.cpp", "src/mod_fs/properties/ani/mkdir_ani.cpp", + "src/mod_fs/properties/ani/mkdtemp_ani.cpp", "src/mod_fs/properties/ani/move_ani.cpp", "src/mod_fs/properties/ani/open_ani.cpp", "src/mod_fs/properties/ani/read_ani.cpp", @@ -697,6 +698,7 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/copy_file_core.cpp", "src/mod_fs/properties/listfile_core.cpp", "src/mod_fs/properties/mkdir_core.cpp", + "src/mod_fs/properties/mkdtemp_core.cpp", "src/mod_fs/properties/move_core.cpp", "src/mod_fs/properties/open_core.cpp", "src/mod_fs/properties/read_core.cpp", diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index 065495c9a..011fc53a3 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -25,6 +25,7 @@ #include "filemgmt_libhilog.h" #include "listfile_ani.h" #include "mkdir_ani.h" +#include "mkdtemp_ani.h" #include "move_ani.h" #include "open_ani.h" #include "read_ani.h" @@ -79,6 +80,7 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, ani_native_function { "mkdirSync", "Lstd/core/String;:V", reinterpret_cast(MkdirkAni::MkdirSync0) }, ani_native_function { "mkdirSync", "Lstd/core/String;Z:V", reinterpret_cast(MkdirkAni::MkdirSync1) }, + ani_native_function { "mkdtempSync", nullptr, reinterpret_cast(MkdtempAni::MkdtempSync) }, ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, diff --git a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets index 67eb95add..882c7deaf 100644 --- a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets +++ b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets @@ -164,6 +164,34 @@ function mkdir(path: string, recursion: boolean, callback: AsyncCallback): }); } +function mkdtempSync(prefix: string): string { + return FileIoImpl.mkdtempSync(prefix); +} + +function mkdtemp(prefix: string): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute(FileIoImpl.mkdtempSync, prefix); + promise.then((ret: NullishType): void => { + let result = ret as string; + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); +} + +function mkdtemp(prefix: string, callback: AsyncCallback): void { + let promise = taskpool.execute(FileIoImpl.mkdtempSync, prefix); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + let result = ret as string; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, ""); + }); +} + function moveFileSync(src: string, dest: string, mode?: number): void { return FileIoImpl.moveFileSync(src, dest, mode); } @@ -641,6 +669,8 @@ class FileIoImpl { static native mkdirSync(path: string, recursion: boolean): void; + static native mkdtempSync(prefix: string): string; + static native moveFileSync(src: String, dest: String, mode?: number): void; static native openSync(path: String, mode?: number): File; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp new file mode 100644 index 000000000..ca42b5b13 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2025 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_ani.h" + +#include + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "mkdtemp_core.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +ani_string MkdtempAni::MkdtempSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path) +{ + auto [succPath, pathStr] = TypeConverter::ToUTF8String(env, path); + if (!succPath) { + HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + + auto ret = MkdtempCore::DoMkdtemp(pathStr); + if (!ret.IsSuccess()) { + HILOGE("Mkdtemp faild"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + const auto &res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.h new file mode 100644 index 000000000..ec826736a --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 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_ANI_MKDTEMP_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_MKDTEMP_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class MkdtempAni final { +public: + static ani_string MkdtempSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path); +}; + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_MKDTEMP_ANI_H diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.cpp new file mode 100644 index 000000000..a615ea24c --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 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_core.h" + +#include "file_utils.h" +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +FsResult MkdtempCore::DoMkdtemp(const std::string &path) +{ + std::unique_ptr mkdtemp_req = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!mkdtemp_req) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + 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"); + return FsResult::Error(ret); + } + + return FsResult::Success(mkdtemp_req->path); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.h b/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.h new file mode 100644 index 000000000..e638655ba --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2025 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_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_MKDTEMP_CORE_H + +#include "filemgmt_libfs.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { + +class MkdtempCore final { +public: + static FsResult DoMkdtemp(const std::string &path); +}; +const std::string PROCEDURE_MKDTEMP_NAME = "FileIOMkdtemp"; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_MKDTEMP_CORE_H \ No newline at end of file -- Gitee From d5a255feb862f8733a486e9612d5cbd40c0b673b Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 24 Mar 2025 19:35:02 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=AD=A3mkdtemp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- .../kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp | 4 ++-- .../kits/js/src/mod_fs/properties/ani/mkdtemp_ani.h | 2 +- .../kits/js/src/mod_fs/properties/mkdtemp_core.cpp | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp index ca42b5b13..7e85bf7e1 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp @@ -27,9 +27,9 @@ namespace FileManagement { namespace ModuleFileIO { namespace ANI { -ani_string MkdtempAni::MkdtempSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path) +ani_string MkdtempAni::MkdtempSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string prefix) { - auto [succPath, pathStr] = TypeConverter::ToUTF8String(env, path); + auto [succPath, pathStr] = TypeConverter::ToUTF8String(env, prefix); if (!succPath) { HILOGE("Invalid path"); ErrorHandler::Throw(env, EINVAL); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.h index ec826736a..cda9c8e12 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.h +++ b/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.h @@ -25,7 +25,7 @@ namespace ANI { class MkdtempAni final { public: - static ani_string MkdtempSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path); + static ani_string MkdtempSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string prefix); }; } // namespace ANI diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.cpp index a615ea24c..487f35c69 100644 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.cpp @@ -23,20 +23,20 @@ namespace FileManagement { namespace ModuleFileIO { using namespace std; -FsResult MkdtempCore::DoMkdtemp(const std::string &path) +FsResult MkdtempCore::DoMkdtemp(const string &path) { - std::unique_ptr mkdtemp_req = { new uv_fs_t, FsUtils::FsReqCleanup }; - if (!mkdtemp_req) { + unique_ptr mkdtempReq = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!mkdtempReq) { HILOGE("Failed to request heap memory."); return FsResult::Error(ENOMEM); } - int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), const_cast(path.c_str()), nullptr); + int ret = uv_fs_mkdtemp(nullptr, mkdtempReq.get(), const_cast(path.c_str()), nullptr); if (ret < 0) { HILOGE("Failed to create a temporary directory with path"); return FsResult::Error(ret); } - return FsResult::Success(mkdtemp_req->path); + return FsResult::Success(move(mkdtempReq->path)); } } // namespace ModuleFileIO -- Gitee From 9233b3aefd6013b7e0f1386e5dd459916e8413c5 Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 24 Mar 2025 19:38:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=AD=A3mkdtemp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp index 7e85bf7e1..42b5a7316 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/mkdtemp_ani.cpp @@ -29,14 +29,14 @@ namespace ANI { ani_string MkdtempAni::MkdtempSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string prefix) { - auto [succPath, pathStr] = TypeConverter::ToUTF8String(env, prefix); + auto [succPath, prefixPath] = TypeConverter::ToUTF8String(env, prefix); if (!succPath) { HILOGE("Invalid path"); ErrorHandler::Throw(env, EINVAL); return nullptr; } - auto ret = MkdtempCore::DoMkdtemp(pathStr); + auto ret = MkdtempCore::DoMkdtemp(prefixPath); if (!ret.IsSuccess()) { HILOGE("Mkdtemp faild"); const auto &err = ret.GetError(); -- Gitee