From fb12f9593c6b805946e0d0ae56aefcadd83cb49c Mon Sep 17 00:00:00 2001 From: zhuhongtao66 Date: Wed, 21 Dec 2022 17:42:33 +0800 Subject: [PATCH] Add fsync interface for mod_fs Signed-off-by: zhuhongtao66 --- .../kits/js/src/mod_fs/properties/fsync.cpp | 100 ++++++++++++++++++ .../kits/js/src/mod_fs/properties/fsync.h | 28 +++++ 2 files changed, 128 insertions(+) create mode 100644 interfaces/kits/js/src/mod_fs/properties/fsync.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/fsync.h diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync.cpp b/interfaces/kits/js/src/mod_fs/properties/fsync.cpp new file mode 100644 index 000000000..85405dfe4 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fsync.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2022 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 "fsync.h" + +#include +#include +#include + +#include "filemgmt_libhilog.h" +#include "uv.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +napi_value Fsync::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, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!resGetFirstArg) { + HILOGE("Invalid fd"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + std::unique_ptr fsync_req = {new uv_fs_t, uv_fs_req_cleanup}; + int ret = uv_fs_fsync(nullptr, fsync_req.get(), fd, nullptr); + if (ret < 0) { + HILOGE("Failed to transfer data associated with file descriptor: %{public}d", fd); + NError(errno).ThrowErr(env); + return nullptr; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value Fsync::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, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!resGetFirstArg) { + HILOGE("Invalid fd"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = [fd = fd]() -> NError { + std::unique_ptr fsync_req = {new uv_fs_t, uv_fs_req_cleanup}; + int ret = uv_fs_fsync(nullptr, fsync_req.get(), fd, nullptr); + if (ret < 0) { + HILOGE("Failed to transfer data associated with file descriptor: %{public}d", fd); + return NError(errno); + } else { + return NError(ERRNO_NOERR); + } + }; + + auto cbComplete = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } else { + return NVal::CreateUndefined(env); + } + }; + + const string procedureName = "FileIOFsync"; + 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 OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync.h b/interfaces/kits/js/src/mod_fs/properties/fsync.h new file mode 100644 index 000000000..e03390978 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fsync.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022 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_FSYNC_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FSYNC_H + +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::ModuleFileIO { +class Fsync 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 OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FSYNC_H \ No newline at end of file -- Gitee