From 8bc9d39c00ed73b66bd1202e19e1d8a24faa6c31 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 24 Mar 2025 12:13:30 +0800 Subject: [PATCH 1/2] rename Signed-off-by: root --- 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 | 29 ++++++++++ .../src/mod_fs/properties/ani/rename_ani.cpp | 54 +++++++++++++++++++ .../js/src/mod_fs/properties/ani/rename_ani.h | 35 ++++++++++++ .../js/src/mod_fs/properties/rename_core.cpp | 52 ++++++++++++++++++ .../js/src/mod_fs/properties/rename_core.h | 37 +++++++++++++ 7 files changed, 211 insertions(+) create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/rename_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/rename_core.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 9334220be..2879d2266 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -689,6 +689,7 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/ani/open_ani.cpp", "src/mod_fs/properties/ani/read_ani.cpp", "src/mod_fs/properties/ani/read_text_ani.cpp", + "src/mod_fs/properties/ani/rename_ani.cpp", "src/mod_fs/properties/ani/rmdir_ani.cpp", "src/mod_fs/properties/ani/truncate_ani.cpp", "src/mod_fs/properties/ani/unlink_ani.cpp", @@ -701,6 +702,7 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/open_core.cpp", "src/mod_fs/properties/read_core.cpp", "src/mod_fs/properties/read_text_core.cpp", + "src/mod_fs/properties/rename_core.cpp", "src/mod_fs/properties/rmdir_core.cpp", "src/mod_fs/properties/stat_core.cpp", "src/mod_fs/properties/truncate_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..8c7fa4e9d 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 @@ -29,6 +29,7 @@ #include "open_ani.h" #include "read_ani.h" #include "read_text_ani.h" +#include "rename_ani.h" #include "rmdir_ani.h" #include "stat_ani.h" #include "truncate_ani.h" @@ -84,6 +85,7 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, + ani_native_function { "renameSync", nullptr, reinterpret_cast(RenameAni::RenameSync) }, ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, 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..6f8f395b4 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 @@ -492,6 +492,34 @@ function stat(file: string | number, callback: AsyncCallback): void }); } +function renameSync(oldPath: string, newPath: string): void { + return FileIoImpl.renameSync(oldPath, newPath); +} + +function rename(oldPath: string, newPath: string): Promise { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((oldPath: string, newPath: string): undefined => + FileIoImpl.renameSync(oldPath, newPath), oldPath, newPath); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); +} + +function rename(oldPath: string, newPath: string, callback: AsyncCallback): void { + let promise = taskpool.execute((oldPath: string, newPath: string): undefined => + FileIoImpl.renameSync(oldPath, newPath), oldPath, newPath); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); +} + export interface Filter { suffix?: Array; displayName?: Array; @@ -659,4 +687,5 @@ class FileIoImpl { static native writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number; + static native renameSync(oldPath: string, newPath: string): void; } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.cpp new file mode 100644 index 000000000..373f3016a --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.cpp @@ -0,0 +1,54 @@ +/* + * 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 "rename_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "rename_core.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +using namespace OHOS::FileManagement::ModuleFileIO; + +void RenameAni::RenameSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string src, ani_string dest) +{ + auto [succSrcPath, srcPath] = TypeConverter::ToUTF8String(env, src); + if (!succSrcPath) { + HILOGE("Invalid src"); + ErrorHandler::Throw(env, EINVAL); + return; + } + auto [succDestPath, destPath] = TypeConverter::ToUTF8String(env, dest); + if (!succDestPath) { + HILOGE("Invalid dest"); + ErrorHandler::Throw(env, EINVAL); + return; + } + auto ret = RenameCore::DoRename(srcPath, destPath); + if (!ret.IsSuccess()) { + HILOGE("DoRename failed!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } +} +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.h new file mode 100644 index 000000000..201319f11 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.h @@ -0,0 +1,35 @@ +/* + * 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_RENAME_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_RENAME_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class RenameAni final { +public: + static void RenameSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string src, ani_string dest); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_RENAME_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/rename_core.cpp b/interfaces/kits/js/src/mod_fs/properties/rename_core.cpp new file mode 100644 index 000000000..628ed3607 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/rename_core.cpp @@ -0,0 +1,52 @@ +/* + * 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 "rename_core.h" + +#include +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +FsResult RenameCore::DoRename(const string &src, const string &dest) +{ + if (src.empty()) { + HILOGE("Invalid path"); + return FsResult::Error(EINVAL); + } + if (dest.empty()) { + HILOGE("Invalid path"); + return FsResult::Error(EINVAL); + } + + std::unique_ptr rename_req = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!rename_req) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + int ret = uv_fs_rename(nullptr, rename_req.get(), src.c_str(), dest.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to rename file with path"); + return FsResult::Error(ret); + } + return FsResult::Success(); +} +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/rename_core.h b/interfaces/kits/js/src/mod_fs/properties/rename_core.h new file mode 100644 index 000000000..2367b2105 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/rename_core.h @@ -0,0 +1,37 @@ +/* + * 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_RENAME_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_RENAME_CORE_H + +#include "filemgmt_libfs.h" +#include "filemgmt_libhilog.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +class RenameCore { +public: + static FsResult DoRename(const string &src, const string &dest); +}; + +const string PROCEDURE_RENAME_NAME = "FileIORename"; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_RENAME_CORE_H \ No newline at end of file -- Gitee From 75b87e322a2559edb6c86222d07ca510958a801c Mon Sep 17 00:00:00 2001 From: root Date: Mon, 24 Mar 2025 12:16:37 +0800 Subject: [PATCH 2/2] rename Signed-off-by: root --- interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.cpp | 2 +- interfaces/kits/js/src/mod_fs/properties/rename_core.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.cpp index 373f3016a..a98abab61 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/rename_ani.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * 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 diff --git a/interfaces/kits/js/src/mod_fs/properties/rename_core.cpp b/interfaces/kits/js/src/mod_fs/properties/rename_core.cpp index 628ed3607..d56101bb9 100644 --- a/interfaces/kits/js/src/mod_fs/properties/rename_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/rename_core.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * 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 -- Gitee