diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 9334220be9d28e01972e60ed84bec52d044d4e8a..646fb40b6f1b5905cbc31de6c4ad0e290245f4de 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", 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 065495c9ade9c4c775ce494fa218d0c382077459..20a5b440301c23cae19daa67eed9ecee09993e56 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 67eb95add30a0cd803f0542ae46c90f870189651..364c474d0fe8e0b63ce2da4e9b08dcc5bdf684bd 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 0000000000000000000000000000000000000000..7ce4a8bfc81fd4fb130d5819365ba492ab186f3f --- /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 "fdatasync_core.h" +#include "file_utils.h" +#include "filemgmt_libhilog.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 0000000000000000000000000000000000000000..ce0b8326125c526b1fc78de7bb0aaba22c67aa71 --- /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 0000000000000000000000000000000000000000..57b479f2bf329f2cd5a97e4852e32d906bc69bcc --- /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 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, 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); + } + + 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 0000000000000000000000000000000000000000..8efb6f90722e0f6c342d58c293a84995ac1efda5 --- /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