From 2c111b0d14663f4f7eaa3fabd0f50ad10b9033ea Mon Sep 17 00:00:00 2001 From: zhuhongtao66 Date: Fri, 2 Dec 2022 17:50:09 +0800 Subject: [PATCH] Add prop_n_exporter.cpp and module.cpp Signed-off-by: zhuhongtao66 --- interfaces/kits/js/src/mod_fs/module.cpp | 53 ++++ .../src/mod_fs/properties/prop_n_exporter.cpp | 286 ++++++++++++++++++ .../src/mod_fs/properties/prop_n_exporter.h | 54 ++++ 3 files changed, 393 insertions(+) create mode 100644 interfaces/kits/js/src/mod_fs/module.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h diff --git a/interfaces/kits/js/src/mod_fs/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp new file mode 100644 index 000000000..936045839 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/module.cpp @@ -0,0 +1,53 @@ +/* + * 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 "common_func.h" + +#include +#include + +#include "class_file/file_n_exporter.h" +#include "class_stat/stat_n_exporter.h" +#include "filemgmt_libhilog.h" +#include "properties/prop_n_exporter.h" + +using namespace std; + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +static napi_value Export(napi_env env, napi_value exports) +{ + InitOpenMode(env, exports); + std::vector> products; + products.emplace_back(make_unique(env, exports)); + products.emplace_back(make_unique(env, exports)); + products.emplace_back(make_unique(env, exports)); + + for (auto &&product : products) { + if (!product->Export()) { + HILOGE("INNER BUG. Failed to export class %{public}s for module fileio", product->GetClassName().c_str()); + return nullptr; + } else { + HILOGI("Class %{public}s for module fileio has been exported", product->GetClassName().c_str()); + } + } + return exports; +} + +NAPI_MODULE(fs, Export) +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp new file mode 100644 index 000000000..9022199a9 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -0,0 +1,286 @@ +/* + * 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 "prop_n_exporter.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "../common_func.h" + +#include "filemgmt_libn.h" +#include "../class_file/file_entity.h" +#include "../class_file/file_n_exporter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + HILOGE("Invalid fd"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [res, buf, len, hasPos, pos] = + CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); + if (!res) { + HILOGE("Failed to resolve buf and options"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + ssize_t actLen; + uv_buf_t buffer = uv_buf_init(static_cast(buf), len); + uv_fs_t read_req; + int ret = uv_fs_read(nullptr, &read_req, fd, &buffer, 1, pos, nullptr); + if (ret < 0) { + HILOGE("Failed to read file for %{public}d", ret); + NError(errno).ThrowErr(env); + return nullptr; + } + actLen = read_req.result; + uv_fs_req_cleanup(&read_req); + + return NVal::CreateInt64(env, actLen).val_; +} + +struct AsyncIOReadArg { + ssize_t lenRead { 0 }; +}; + +static NError ReadExec(shared_ptr arg, void *buf, size_t len, int fd, size_t position) +{ + uv_buf_t buffer = uv_buf_init(static_cast(buf), len); + uv_fs_t read_req; + int ret = uv_fs_read(nullptr, &read_req, fd, &buffer, 1, static_cast(position), nullptr); + if (ret < 0) { + HILOGE("Failed to read file for %{public}d", ret); + return NError(errno); + } + arg->lenRead = read_req.result; + uv_fs_req_cleanup(&read_req); + return NError(ERRNO_NOERR); +} + +napi_value PropNExporter::Read(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + HILOGE("Invalid fd"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [res, buf, len, hasPos, pos] = + CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); + if (!res) { + HILOGE("Failed to resolve buf and options"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto arg = make_shared(); + auto cbExec = [arg, buf = buf, len = len, fd = fd, pos = pos, env = env]() -> NError { + return ReadExec(arg, buf, len, fd, pos); + }; + + auto cbCompl = [arg](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt64(env, arg->lenRead) }; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + bool hasOp = false; + if (funcArg.GetArgc() == NARG_CNT::THREE) { + NVal op = NVal(env, funcArg[NARG_POS::THIRD]); + if (op.HasProp("offset") || op.HasProp("length")|| !op.TypeIs(napi_function)) { + hasOp = true; + } + } + if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && hasOp)) { + return NAsyncWorkPromise(env, thisVar).Schedule("FileIORead", cbExec, cbCompl).val_; + } else { + int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); + NVal cb(env, funcArg[cbIdx]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIORead", cbExec, cbCompl).val_; + } + + return NVal::CreateUndefined(env).val_; +} + +NError PropNExporter::WriteExec(shared_ptr arg, void *buf, size_t len, int fd, size_t position) +{ + uv_buf_t buffer = uv_buf_init(static_cast(buf), len); + uv_fs_t write_req; + int ret = uv_fs_write(nullptr, &write_req, fd, &buffer, 1, static_cast(position), nullptr); + if (ret < 0) { + HILOGE("Failed to write file for %{public}d", ret); + return NError(errno); + } + arg->actLen = write_req.result; + uv_fs_req_cleanup(&write_req); + return NError(ERRNO_NOERR); +} + +napi_value PropNExporter::Write(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + HILOGE("Invalid fd"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [res, bufGuard, buf, len, hasPos, position] = + CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); + if (!res) { + HILOGE("Failed to resolve buf and options"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + shared_ptr arg; + if (bufGuard) { + arg = make_shared(move(bufGuard)); + } else { + arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); + } + auto cbExec = [arg, buf = buf, len = len, fd = fd, position = position, env = env]() -> NError { + return WriteExec(arg, buf, len, fd, position); + }; + + auto cbCompl = [arg](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + return { NVal::CreateInt64(env, arg->actLen) }; + } + }; + + NVal thisVar(env, funcArg.GetThisVar()); + bool hasOp = false; + if (funcArg.GetArgc() == NARG_CNT::THREE) { + NVal op = NVal(env, funcArg[NARG_POS::THIRD]); + if (op.HasProp("offset") || op.HasProp("position") || op.HasProp("length") || + op.HasProp("encoding") || !op.TypeIs(napi_function)) { + hasOp = true; + } + } + + if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && hasOp)) { + return NAsyncWorkPromise(env, thisVar).Schedule("FileIOWrite", cbExec, cbCompl).val_; + } else { + int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); + NVal cb(env, funcArg[cbIdx]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIOWrite", cbExec, cbCompl).val_; + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + HILOGE("Invalid fd"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [res, bufGuard, buf, len, hasPos, position] = + CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); + if (!res) { + HILOGE("Failed to resolve buf and options"); + return nullptr; + } + + ssize_t writeLen; + uv_buf_t buffer = uv_buf_init(static_cast(buf), len); + uv_fs_t write_req; + int ret = uv_fs_write(nullptr, &write_req, fd, &buffer, 1, static_cast(position), nullptr); + if (ret < 0) { + HILOGE("Failed to write file for %{public}d", ret); + NError(errno).ThrowErr(env); + return nullptr; + } + writeLen = write_req.result; + uv_fs_req_cleanup(&write_req); + + return NVal::CreateInt64(env, writeLen).val_; +} + +bool PropNExporter::Export() +{ + return exports_.AddProp({ + NVal::DeclareNapiFunction("read", Read), + NVal::DeclareNapiFunction("readSync", ReadSync), + NVal::DeclareNapiFunction("write", Write), + NVal::DeclareNapiFunction("writeSync", WriteSync), + }); +} + +string PropNExporter::GetClassName() +{ + return PropNExporter::className_; +} + +PropNExporter::PropNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} + +PropNExporter::~PropNExporter() {} +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h new file mode 100644 index 000000000..d4352746e --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h @@ -0,0 +1,54 @@ +/* + * 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_PROP_N_EXPORTER_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_PROP_N_EXPORTER_H + +#include "filemgmt_libhilog.h" +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace OHOS::FileManagement::LibN; +struct AsyncIOWrtieArg { + NRef refWriteArrayBuf_; + std::unique_ptr guardWriteStr_; + ssize_t actLen = 0; + + explicit AsyncIOWrtieArg(NVal refWriteArrayBuf) : refWriteArrayBuf_(refWriteArrayBuf) {} + explicit AsyncIOWrtieArg(std::unique_ptr &&guardWriteStr) : guardWriteStr_(move(guardWriteStr)) {} + ~AsyncIOWrtieArg() = default; +}; + +class PropNExporter final : public NExporter { +public: + inline static const std::string className_ = "__properities__"; + + static napi_value ReadSync(napi_env env, napi_callback_info info); + static napi_value WriteSync(napi_env env, napi_callback_info info); + static napi_value Read(napi_env env, napi_callback_info info); + static napi_value Write(napi_env env, napi_callback_info info); + static NError WriteExec(std::shared_ptr arg, void *buf, size_t len, int fd, size_t position); + bool Export() override; + std::string GetClassName() override; + + PropNExporter(napi_env env, napi_value exports); + ~PropNExporter() override; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_PROP_N_EXPORTER_H \ No newline at end of file -- Gitee