From 408a800caa760d54f675961eec6c5d5b4b1353b0 Mon Sep 17 00:00:00 2001 From: zhuhongtao66 Date: Wed, 21 Dec 2022 17:38:25 +0800 Subject: [PATCH] Add fdatasync interface for mod_fs Signed-off-by: zhuhongtao66 --- .../js/src/mod_fs/properties/fdatasync.cpp | 103 ++++++++++++++++++ .../kits/js/src/mod_fs/properties/fdatasync.h | 28 +++++ 2 files changed, 131 insertions(+) create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdatasync.h diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp b/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp new file mode 100644 index 000000000..8def72111 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp @@ -0,0 +1,103 @@ +/* + * 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 "fdatasync.h" + +#include +#include +#include +#include + +#include + +#include "filemgmt_libhilog.h" +#include "uv.h" + + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +napi_value Fdatasync::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 fdatasync_req = {new uv_fs_t, uv_fs_req_cleanup}; + 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); + NError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value Fdatasync::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 fdatasync_req = {new uv_fs_t, uv_fs_req_cleanup}; + 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", fd); + return NError(errno); + } else { + return NError(ERRNO_NOERR); + } + }; + + auto cbComplCallback = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return {NVal::CreateUndefined(env)}; + }; + + const string procedureName = "FileIOFdatasync"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_; + } +} +} // OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync.h b/interfaces/kits/js/src/mod_fs/properties/fdatasync.h new file mode 100644 index 000000000..f4c3ba0e5 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync.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_FDATASYNC_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDATASYNC_H + +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::ModuleFileIO { +class Fdatasync 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_FDATASYNC_H \ No newline at end of file -- Gitee