From dbe227076f340e1ff50ddc00bf9f58def4213d9c Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 24 Mar 2025 11:07:10 +0800 Subject: [PATCH 1/4] fdatasync Signed-off-by: liyuke --- .../js/src/mod_fs/ani/bind_function_class.cpp | 2 + .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 28 +++++++++++ .../mod_fs/properties/ani/fdatasync_ani.cpp | 44 ++++++++++++++++++ .../src/mod_fs/properties/ani/fdatasync_ani.h | 36 +++++++++++++++ .../src/mod_fs/properties/fdatasync_core.cpp | 46 +++++++++++++++++++ .../js/src/mod_fs/properties/fdatasync_core.h | 29 ++++++++++++ 6 files changed, 185 insertions(+) create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h 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..20a5b4403 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 @@ -21,6 +21,7 @@ #include "bind_function.h" #include "close_ani.h" #include "copy_file_ani.h" +#include "fdatasync_ani.h" #include "file_ani.h" #include "filemgmt_libhilog.h" #include "listfile_ani.h" @@ -76,6 +77,7 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, + ani_native_function { "fdatasyncSync", nullptr, reinterpret_cast(FDataSyncAni::FDataSyncSync) }, 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) }, 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..364c474d0 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 @@ -104,6 +104,32 @@ function mkdirSync(path: string): void { return FileIoImpl.mkdirSync(path) } +function fdatasyncSync(fd: number): void { + return FileIoImpl.fdatasyncSync(fd) +} + +function fdatasync(fd: number): Promise { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((fd: number): undefined => FileIoImpl.fdatasyncSync(fd), fd); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e as BusinessError); + }); + }); +} + +function fdatasync(fd: number, callback: AsyncCallback): void { + let promise = taskpool.execute((fd: number): undefined => FileIoImpl.fdatasyncSync(fd), fd); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); +} + function mkdirSync(path: string, recursion: boolean): void { return FileIoImpl.mkdirSync(path, recursion) } @@ -635,6 +661,8 @@ class FileIoImpl { static native copyFileSync(src: string | number, dest: string | number, mode?: number): void; + static native fdatasyncSync(fd: number): void; + static native listFileSync(path: string, options?: ListFileOptions): string[]; static native mkdirSync(path: string): void; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp new file mode 100644 index 000000000..cea817cdb --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.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 "fdatasync_ani.h" + +#include + +#include "error_handler.h" +#include "file_utils.h" +#include "filemgmt_libhilog.h" +#include "fdatasync_core.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +void FDataSyncAni::FDataSyncSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd) +{ + auto ret = FDataSyncCore::DoFDataSync(static_cast(fd)); + if (!ret.IsSuccess()) { + HILOGE("Fdatasync 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/fdatasync_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.h new file mode 100644 index 000000000..ce0b83261 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_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_FDATASYNC_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FDATASYNC_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class FDataSyncAni final { +public: + static void FDataSyncSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd); +}; + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FDATASYNC_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp new file mode 100644 index 000000000..83777e836 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp @@ -0,0 +1,46 @@ +/* + * 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 "fdatasync_core.h" + +#include +#include +#include +#include +#include + +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; + +FsResult FDataSyncCore::DoFDataSync(const int32_t &fd) +{ + std::unique_ptr fdatasync_req = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!fdatasync_req) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + + int ret = uv_fs_fdatasync(nullptr, fdatasync_req.get(), fd, nullptr); + if (ret < 0) { + HILOGE("Failed to transfer data associated with file descriptor: %{public}d, ret:%{public}d", fd, ret); + return FsResult::Error(ret); + } + + return FsResult::Success(); +} + +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h new file mode 100644 index 000000000..8efb6f907 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h @@ -0,0 +1,29 @@ +/* + * 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_FDATASYNC_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDATASYNC_CORE_H + +#include "filemgmt_libfs.h" +#include "fs_utils.h" + +namespace OHOS::FileManagement::ModuleFileIO { +class FDataSyncCore final { +public: + static FsResult DoFDataSync(const int32_t &fd); +}; +const std::string PROCEDURE_FDATASYNC_NAME = "FileIOFdatasync"; +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDATASYNC_CORE_H \ No newline at end of file -- Gitee From 82272771d0d3a542ba0a8ba593760c70e9245a2e Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 24 Mar 2025 11:22:21 +0800 Subject: [PATCH 2/4] =?UTF-8?q?gn=E6=96=87=E4=BB=B6=E6=B7=BB=E5=8A=A0fdata?= =?UTF-8?q?sync?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- interfaces/kits/js/BUILD.gn | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 9334220be..646fb40b6 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -683,6 +683,7 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/ani/access_ani.cpp", "src/mod_fs/properties/ani/close_ani.cpp", "src/mod_fs/properties/ani/copy_file_ani.cpp", + "src/mod_fs/properties/ani/fdatasync_ani.cpp", "src/mod_fs/properties/ani/listfile_ani.cpp", "src/mod_fs/properties/ani/mkdir_ani.cpp", "src/mod_fs/properties/ani/move_ani.cpp", @@ -695,6 +696,7 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/ani/write_ani.cpp", "src/mod_fs/properties/close_core.cpp", "src/mod_fs/properties/copy_file_core.cpp", + "src/mod_fs/properties/fdatasync_core.cpp", "src/mod_fs/properties/listfile_core.cpp", "src/mod_fs/properties/mkdir_core.cpp", "src/mod_fs/properties/move_core.cpp", -- Gitee From bbcc66501417e33ae54c45da1d5dfccfee229f97 Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 24 Mar 2025 17:41:34 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=B0=8F=E9=A9=BC=E5=B3=B0=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E4=BF=AE=E6=94=B9?= 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/fdatasync_core.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp index 83777e836..57b479f2b 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp @@ -28,13 +28,13 @@ using namespace std; FsResult FDataSyncCore::DoFDataSync(const int32_t &fd) { - std::unique_ptr fdatasync_req = { new uv_fs_t, FsUtils::FsReqCleanup }; - if (!fdatasync_req) { + std::unique_ptr fDataSyncReq = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!fDataSyncReq) { HILOGE("Failed to request heap memory."); return FsResult::Error(ENOMEM); } - int ret = uv_fs_fdatasync(nullptr, fdatasync_req.get(), fd, nullptr); + int ret = uv_fs_fdatasync(nullptr, fDataSyncReq.get(), fd, nullptr); if (ret < 0) { HILOGE("Failed to transfer data associated with file descriptor: %{public}d, ret:%{public}d", fd, ret); return FsResult::Error(ret); -- Gitee From a4ca08e6d2d036ce16890550df03c0dffb5d298c Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 31 Mar 2025 08:11:17 +0000 Subject: [PATCH 4/4] update interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp. Signed-off-by: liyuke --- interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp index cea817cdb..7ce4a8bfc 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp @@ -18,9 +18,9 @@ #include #include "error_handler.h" +#include "fdatasync_core.h" #include "file_utils.h" #include "filemgmt_libhilog.h" -#include "fdatasync_core.h" namespace OHOS { namespace FileManagement { -- Gitee