From 10938db120310efc5180a3bb4aa3d08f764b8ce5 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Mon, 10 Oct 2022 22:09:28 +0800 Subject: [PATCH 1/4] fileio_add_rw Signed-off-by: zhuhongtao666 --- interfaces/kits/js/BUILD.gn | 32 ++ .../kits/js/src/mod_fileio/common_func.cpp | 83 ++++- .../kits/js/src/mod_fileio/common_func.h | 6 + .../kits/js/src/mod_fileio/module_v9.cpp | 50 +++ .../js/src/mod_fileio/prop_n_exporter_v9.cpp | 301 ++++++++++++++++++ .../js/src/mod_fileio/prop_n_exporter_v9.h | 55 ++++ 6 files changed, 526 insertions(+), 1 deletion(-) create mode 100644 interfaces/kits/js/src/mod_fileio/module_v9.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 36e610de8..093bfb436 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -99,6 +99,37 @@ ohos_shared_library("fileio") { external_deps = [ "hiviewdfx_hilog_native:libhilog" ] } +ohos_shared_library("fs") { + subsystem_name = "filemanagement" + part_name = "file_api" + + relative_install_dir = "module/file" + + include_dirs = [ + "//foundation/filemanagement/file_api/interfaces/kits/js/src/common/file_helper", + "//foundation/filemanagement/file_api/interfaces/kits/js/src/common/napi", + "//foundation/filemanagement/file_api/interfaces/kits/js/src/common/napi/n_async", + ] + + sources = file_common_src + sources += [ + "src/mod_fileio/class_file/file_n_exporter.cpp", + "src/mod_fileio/class_stat_v9/stat_n_exporter_v9.cpp", + "src/mod_fileio/common_func.cpp", + "src/mod_fileio/module_v9.cpp", + "src/mod_fileio/properties/prop_n_exporter_v9.cpp", + "src/mod_fileio/properties/open_v9.cpp", + "src/mod_fileio/properties/stat_v9.cpp", + "src/mod_fileio/properties/truncate_v9.cpp", + ] + + deps = [ + "//foundation/arkui/napi:ace_napi", + ] + + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] +} + ohos_shared_library("file") { subsystem_name = "filemanagement" part_name = "file_api" @@ -241,6 +272,7 @@ group("build_kits_js") { ":environment", ":file", ":fileio", + ":fs", ":securitylabel", ":statfs", ] diff --git a/interfaces/kits/js/src/mod_fileio/common_func.cpp b/interfaces/kits/js/src/mod_fileio/common_func.cpp index b54748f12..9f06d018e 100644 --- a/interfaces/kits/js/src/mod_fileio/common_func.cpp +++ b/interfaces/kits/js/src/mod_fileio/common_func.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -257,6 +257,87 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW return { true, move(bufferGuard), retBuf, retLen, hasPos, retPos }; } + +tuple CommonFunc::GetReadArgV9(napi_env env, + napi_value readBuf, + napi_value option) +{ + bool succ = false; + int64_t retLen; + bool posAssigned = false; + int64_t position; + + NVal txt(env, readBuf); + void *buf = nullptr; + int64_t bufLen; + tie(succ, buf, bufLen) = txt.ToArraybuffer(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid read buffer, expect arraybuffer"); + return { false, nullptr, 0, posAssigned, position }; + } + NVal op = NVal(env, option); + tie(succ, retLen) = GetActualLen(env, bufLen, 0, op); + if (!succ) { + return { false, nullptr, 0, posAssigned, position }; + } + + if (op.HasProp("offset")) { + tie(succ, position) = op.GetProp("offset").ToInt64(); + if (succ && position >= 0) { + posAssigned = true; + } else { + UniError(EINVAL).ThrowErr(env, "option.offset shall be positive number"); + return { false, nullptr, 0, posAssigned, position }; + } + } + + return { true, buf, retLen, posAssigned, position }; +} + +tuple, void *, int64_t, bool, int64_t> CommonFunc::GetWriteArgV9(napi_env env, + napi_value argWBuf, + napi_value argOption) +{ + int64_t retLen; + bool hasPos = false; + int64_t retPos; + /* To get write buffer */ + bool succ = false; + void *buf = nullptr; + int64_t bufLen; + NVal op(env, argOption); + NVal jsBuffer(env, argWBuf); + unique_ptr bufferGuard; + tie(succ, bufferGuard, bufLen) = DecodeString(env, jsBuffer, op.GetProp("encoding")); + if (!succ) { + tie(succ, buf, bufLen) = NVal(env, argWBuf).ToArraybuffer(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Illegal write buffer or encoding"); + return { false, nullptr, nullptr, 0, hasPos, retPos }; + } + } else { + buf = bufferGuard.get(); + } + tie(succ, retLen) = GetActualLen(env, bufLen, 0, op); + if (!succ) { + return { false, nullptr, nullptr, 0, hasPos, retPos }; + } + + /* To parse options - Where to begin writing */ + if (op.HasProp("offset")) { + int32_t position = 0; + tie(succ, position) = op.GetProp("offset").ToInt32(); + if (!succ || position < 0) { + UniError(EINVAL).ThrowErr(env, "option.offset shall be positive number"); + return { false, nullptr, nullptr, 0, hasPos, retPos }; + } + hasPos = true; + retPos = position; + } else { + retPos = INVALID_POSITION; + } + return { true, move(bufferGuard), buf, retLen, hasPos, retPos }; +} } // namespace ModuleFileIO } // namespace DistributedFS } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fileio/common_func.h b/interfaces/kits/js/src/mod_fileio/common_func.h index 337826536..d75647041 100644 --- a/interfaces/kits/js/src/mod_fileio/common_func.h +++ b/interfaces/kits/js/src/mod_fileio/common_func.h @@ -28,9 +28,15 @@ struct CommonFunc { static std::tuple GetReadArg(napi_env env, napi_value readBuf, napi_value option); + static std::tuple GetReadArgV9(napi_env env, + napi_value readBuf, + napi_value option); static std::tuple, void *, int64_t, bool, int64_t> GetWriteArg(napi_env env, napi_value argWBuf, napi_value argOption); + static std::tuple, void *, int64_t, bool, int64_t> GetWriteArgV9(napi_env env, + napi_value argWBuf, + napi_value argOption); static std::tuple, std::unique_ptr> GetCopyPathArg(napi_env env, napi_value srcPath, napi_value dstPath); diff --git a/interfaces/kits/js/src/mod_fileio/module_v9.cpp b/interfaces/kits/js/src/mod_fileio/module_v9.cpp new file mode 100644 index 000000000..7f2bb4bce --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/module_v9.cpp @@ -0,0 +1,50 @@ +/* + * 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 +#include + +#include "../common/log.h" +#include "class_file/file_n_exporter.h" +#include "class_stat_v9/stat_n_exporter_v9.h" +#include "properties/prop_n_exporter_v9.h" + +using namespace std; + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +static napi_value Export(napi_env env, napi_value 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 { + HILOGE("Class %{public}s for module fileio has been exported", product->GetClassName().c_str()); + } + } + return exports; +} + +NAPI_MODULE(fs, Export) +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.cpp b/interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.cpp new file mode 100644 index 000000000..e3beb3f0d --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.cpp @@ -0,0 +1,301 @@ +/* + * 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_v9.h" + +#include +#include +#include +#include +#include +#include + +#include "../common_func.h" +#include "open_v9.h" +#include "stat_v9.h" +#include "truncate_v9.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +napi_value PropNExporterV9::ReadSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + + int fd; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid fd"); + return nullptr; + } + + void *buf = nullptr; + int64_t len; + bool hasPos = false; + int64_t pos; + tie(succ, buf, len, hasPos, pos) = + CommonFunc::GetReadArgV9(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); + if (!succ) { + return nullptr; + } + + ssize_t actLen; + if (hasPos) { + actLen = pread(fd, buf, len, pos); + } else { + actLen = read(fd, buf, len); + } + if (actLen == -1) { + UniError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateInt64(env, actLen).val_; +} + +struct AsyncIOReadArg { + ssize_t lenRead { 0 }; +}; + +napi_value PropNExporterV9::Read(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + void *buf = nullptr; + int64_t len; + int fd; + bool hasPos = false; + int64_t pos; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid fd"); + return nullptr; + } + + tie(succ, buf, len, hasPos, pos) = + CommonFunc::GetReadArgV9(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Failed GetReadArg"); + return nullptr; + } + + auto arg = make_shared(); + auto cbExec = [arg, buf, len, fd, hasPos, pos](napi_env env) -> UniError { + ssize_t actLen; + if (hasPos) { + actLen = pread(fd, buf, len, pos); + } else { + actLen = read(fd, buf, len); + } + if (actLen == -1) { + return UniError(errno); + } else { + arg->lenRead = actLen; + return UniError(ERRNO_NOERR); + } + }; + + auto cbCompl = [arg](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt64(env, arg->lenRead) }; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + size_t argc = funcArg.GetArgc(); + bool hasOp = false; + if (argc == NARG_CNT::THREE) { + NVal op = NVal(env, funcArg[NARG_POS::THIRD]); + if (op.HasProp("offset") || op.HasProp("length")) { + hasOp = true; + } + } + if (argc == NARG_CNT::TWO || (argc == NARG_CNT::THREE && hasOp)) { + return NAsyncWorkPromise(env, thisVar).Schedule("FileIORead", cbExec, cbCompl).val_; + } else { + int cbIdx = ((argc == 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_; +} + +UniError PropNExporterV9::WriteExec(shared_ptr arg, void *buf, size_t len, int fd, size_t position) +{ + if (position == (size_t)INVALID_POSITION) { + arg->actLen = write(fd, buf, len); + } else { + arg->actLen = pwrite(fd, buf, len, position); + } + + if (arg->actLen == -1) { + return UniError(errno); + } else { + return UniError(ERRNO_NOERR); + } +} + +napi_value PropNExporterV9::Write(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + int fd; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid fd"); + return nullptr; + } + + unique_ptr bufGuard; + void *buf = nullptr; + size_t len; + size_t position; + bool hasPos = false; + tie(succ, bufGuard, buf, len, hasPos, position) = + CommonFunc::GetWriteArgV9(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Failed GetWriteArg"); + 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, len, fd, position](napi_env env) -> UniError { + return WriteExec(arg, buf, len, fd, position); + }; + + auto cbCompl = [arg](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + return { NVal::CreateInt64(env, arg->actLen) }; + } + }; + + NVal thisVar(env, funcArg.GetThisVar()); + bool hasOp = false; + size_t argc = funcArg.GetArgc(); + if (argc == NARG_CNT::THREE) { + NVal op = NVal(env, funcArg[NARG_POS::THIRD]); + if (op.HasProp("offset") || op.HasProp("position") || op.HasProp("length") || op.HasProp("encoding")) { + hasOp = true; + } + } + + if (argc == NARG_CNT::TWO || (argc == NARG_CNT::THREE && hasOp)) { + return NAsyncWorkPromise(env, thisVar).Schedule("FileIOWrite", cbExec, cbCompl).val_; + } else { + int cbIdx = ((argc == 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 PropNExporterV9::WriteSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + int fd; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid fd"); + return nullptr; + } + + void *buf = nullptr; + size_t len; + size_t position; + unique_ptr bufGuard; + bool hasPos = false; + tie(succ, bufGuard, buf, len, hasPos, position) = + CommonFunc::GetWriteArgV9(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); + if (!succ) { + return nullptr; + } + + ssize_t writeLen; + if (position == (size_t)INVALID_POSITION) { + writeLen = write(fd, buf, len); + } else { + writeLen = pwrite(fd, buf, len, position); + } + + if (writeLen == -1) { + UniError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateInt64(env, writeLen).val_; +} + +bool PropNExporterV9::Export() +{ + return exports_.AddProp({ + NVal::DeclareNapiFunction("open", OpenV9::Async), + NVal::DeclareNapiFunction("openSync", OpenV9::Sync), + NVal::DeclareNapiFunction("read", Read), + NVal::DeclareNapiFunction("readSync", ReadSync), + NVal::DeclareNapiFunction("stat", StatV9::Async), + NVal::DeclareNapiFunction("statSync", StatV9::Sync), + NVal::DeclareNapiFunction("truncate", TruncateV9::Async), + NVal::DeclareNapiFunction("truncateSync", TruncateV9::Sync), + NVal::DeclareNapiFunction("write", Write), + NVal::DeclareNapiFunction("writeSync", WriteSync), + }); +} + +string PropNExporterV9::GetClassName() +{ + return PropNExporterV9::className_; +} + +PropNExporterV9::PropNExporterV9(napi_env env, napi_value exports) : NExporter(env, exports) {} + +PropNExporterV9::~PropNExporterV9() {} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.h b/interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.h new file mode 100644 index 000000000..832e26ef4 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.h @@ -0,0 +1,55 @@ +/* + * 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_FILEIO_PROPERTIES_PROP_N_EXPORTER_V9_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_PROP_N_EXPORTER_V9_H + +#include "../../common/napi/n_async/n_ref.h" +#include "../../common/napi/n_exporter.h" +#include "../../common/napi/n_val.h" +#include "../../common/uni_error.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +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 PropNExporterV9 final : public NExporter { +public: + inline static const std::string className_ = "__properitiesV9__"; + + 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 UniError WriteExec(std::shared_ptr arg, void *buf, size_t len, int fd, size_t position); + bool Export() override; + std::string GetClassName() override; + + PropNExporterV9(napi_env env, napi_value exports); + ~PropNExporterV9() override; +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file -- Gitee From f573a0b372affe23506e0ba06ec53d510ab38a27 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Mon, 10 Oct 2022 22:28:26 +0800 Subject: [PATCH 2/4] fileio_add_rw Signed-off-by: zhuhongtao666 --- interfaces/kits/js/BUILD.gn | 5 ----- interfaces/kits/js/src/mod_fileio/module_v9.cpp | 4 ---- .../mod_fileio/{ => properties}/prop_n_exporter_v9.cpp | 9 --------- .../src/mod_fileio/{ => properties}/prop_n_exporter_v9.h | 0 4 files changed, 18 deletions(-) rename interfaces/kits/js/src/mod_fileio/{ => properties}/prop_n_exporter_v9.cpp (95%) rename interfaces/kits/js/src/mod_fileio/{ => properties}/prop_n_exporter_v9.h (100%) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 093bfb436..82e10cf5c 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -113,14 +113,9 @@ ohos_shared_library("fs") { sources = file_common_src sources += [ - "src/mod_fileio/class_file/file_n_exporter.cpp", - "src/mod_fileio/class_stat_v9/stat_n_exporter_v9.cpp", "src/mod_fileio/common_func.cpp", "src/mod_fileio/module_v9.cpp", "src/mod_fileio/properties/prop_n_exporter_v9.cpp", - "src/mod_fileio/properties/open_v9.cpp", - "src/mod_fileio/properties/stat_v9.cpp", - "src/mod_fileio/properties/truncate_v9.cpp", ] deps = [ diff --git a/interfaces/kits/js/src/mod_fileio/module_v9.cpp b/interfaces/kits/js/src/mod_fileio/module_v9.cpp index 7f2bb4bce..fe757490e 100644 --- a/interfaces/kits/js/src/mod_fileio/module_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/module_v9.cpp @@ -17,8 +17,6 @@ #include #include "../common/log.h" -#include "class_file/file_n_exporter.h" -#include "class_stat_v9/stat_n_exporter_v9.h" #include "properties/prop_n_exporter_v9.h" using namespace std; @@ -30,8 +28,6 @@ static napi_value Export(napi_env env, napi_value 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()) { diff --git a/interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp similarity index 95% rename from interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.cpp rename to interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp index e3beb3f0d..910c3178f 100644 --- a/interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp @@ -23,9 +23,6 @@ #include #include "../common_func.h" -#include "open_v9.h" -#include "stat_v9.h" -#include "truncate_v9.h" namespace OHOS { namespace DistributedFS { @@ -275,14 +272,8 @@ napi_value PropNExporterV9::WriteSync(napi_env env, napi_callback_info info) bool PropNExporterV9::Export() { return exports_.AddProp({ - NVal::DeclareNapiFunction("open", OpenV9::Async), - NVal::DeclareNapiFunction("openSync", OpenV9::Sync), NVal::DeclareNapiFunction("read", Read), NVal::DeclareNapiFunction("readSync", ReadSync), - NVal::DeclareNapiFunction("stat", StatV9::Async), - NVal::DeclareNapiFunction("statSync", StatV9::Sync), - NVal::DeclareNapiFunction("truncate", TruncateV9::Async), - NVal::DeclareNapiFunction("truncateSync", TruncateV9::Sync), NVal::DeclareNapiFunction("write", Write), NVal::DeclareNapiFunction("writeSync", WriteSync), }); diff --git a/interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.h b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.h similarity index 100% rename from interfaces/kits/js/src/mod_fileio/prop_n_exporter_v9.h rename to interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.h -- Gitee From 59373602c36e28a009afb741918f045804e290e2 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Mon, 10 Oct 2022 22:41:48 +0800 Subject: [PATCH 3/4] fileio_add_rw Signed-off-by: zhuhongtao666 --- .../kits/js/src/mod_fileio/common_func.cpp | 67 +++++++++++-------- .../properties/prop_n_exporter_v9.cpp | 20 +++--- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/interfaces/kits/js/src/mod_fileio/common_func.cpp b/interfaces/kits/js/src/mod_fileio/common_func.cpp index 9f06d018e..e64202391 100644 --- a/interfaces/kits/js/src/mod_fileio/common_func.cpp +++ b/interfaces/kits/js/src/mod_fileio/common_func.cpp @@ -40,10 +40,12 @@ static tuple GetActualBuf(napi_env env, void *rawBuf, int64_t if (op.HasProp("offset")) { tie(succ, opOffset) = op.GetProp("offset").ToInt64(); if (!succ || opOffset < 0) { - UniError(EINVAL).ThrowErr(env, "Invalid option.offset, positive integer is desired"); + HILOGE("Invalid option.offset, positive integer is desired"); + UniError(EINVAL).ThrowErr(env); return { false, nullptr, opOffset }; } else if (opOffset > bufLen) { - UniError(EINVAL).ThrowErr(env, "Invalid option.offset, buffer limit exceeded"); + HILOGE("Invalid option.offset, buffer limit exceeded"); + UniError(EINVAL).ThrowErr(env); return { false, nullptr, opOffset }; } else { realBuf = static_cast(rawBuf) + opOffset; @@ -64,13 +66,15 @@ static tuple GetActualLen(napi_env env, int64_t bufLen, int64_t bu int64_t opLength; tie(succ, opLength) = op.GetProp("length").ToInt64(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid option.length, expect integer"); + HILOGE("Invalid option.length, expect integer"); + UniError(EINVAL).ThrowErr(env); return { false, 0 }; } if (opLength < 0) { retLen = bufLen - bufOff; } else if (opLength + bufOff > bufLen) { - UniError(EINVAL).ThrowErr(env, "Invalid option.length, buffer limit exceeded"); + HILOGE("Invalid option.length, buffer limit exceeded"); + UniError(EINVAL).ThrowErr(env); return { false, 0 }; } else { retLen = opLength; @@ -113,8 +117,8 @@ int CommonFunc::ConvertJsFlags(int &flags) } tuple, unique_ptr> CommonFunc::GetCopyPathArg(napi_env env, - napi_value srcPath, - napi_value dstPath) + napi_value srcPath, + napi_value dstPath) { bool succ = false; unique_ptr src; @@ -128,8 +132,7 @@ tuple, unique_ptr> CommonFunc::GetCopyPathArg(n if (!succ) { return { false, nullptr, nullptr }; } - - return make_tuple(true, move(src), move(dest)); + return make_tuple(succ, move(src), move(dest)); } tuple CommonFunc::GetReadArg(napi_env env, @@ -138,17 +141,18 @@ tuple CommonFunc::GetReadArg(napi_env { bool succ = false; void *retBuf = nullptr; - int64_t retLen = 0; + int64_t retLen; bool posAssigned = false; - int64_t position = 0; + int64_t position; NVal txt(env, readBuf); void *buf = nullptr; - int64_t bufLen = 0; + int64_t bufLen; int offset = 0; tie(succ, buf, bufLen) = txt.ToArraybuffer(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid read buffer, expect arraybuffer"); + HILOGE("Invalid read buffer, expect arraybuffer"); + UniError(EINVAL).ThrowErr(env); return { false, nullptr, 0, posAssigned, position, offset }; } @@ -169,7 +173,8 @@ tuple CommonFunc::GetReadArg(napi_env if (succ && position >= 0) { posAssigned = true; } else { - UniError(EINVAL).ThrowErr(env, "option.position shall be positive number"); + HILOGE("option.position shall be positive number"); + UniError(EINVAL).ThrowErr(env); return { false, nullptr, 0, posAssigned, position, offset }; } } @@ -204,18 +209,20 @@ static tuple, int64_t> DecodeString(napi_env env, NVal } } +// Is everything ok? Do we need to free memory? What's the three args required by fwrite? Where to start writing? tuple, void *, int64_t, bool, int64_t> CommonFunc::GetWriteArg(napi_env env, napi_value argWBuf, napi_value argOption) { void *retBuf = nullptr; - int64_t retLen = 0; + int64_t retLen; bool hasPos = false; - int64_t retPos = 0; + int64_t retPos; + /* To get write buffer */ bool succ = false; void *buf = nullptr; - int64_t bufLen = 0; + int64_t bufLen; NVal op(env, argOption); NVal jsBuffer(env, argWBuf); unique_ptr bufferGuard; @@ -223,7 +230,8 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW if (!succ) { tie(succ, buf, bufLen) = NVal(env, argWBuf).ToArraybuffer(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Illegal write buffer or encoding"); + HILOGE("Illegal write buffer or encoding"); + UniError(EINVAL).ThrowErr(env); return { false, nullptr, nullptr, 0, hasPos, retPos }; } } else { @@ -246,7 +254,8 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW int32_t position = 0; tie(succ, position) = op.GetProp("position").ToInt32(); if (!succ || position < 0) { - UniError(EINVAL).ThrowErr(env, "option.position shall be positive number"); + HILOGE("option.position shall be positive number"); + UniError(EINVAL).ThrowErr(env); return { false, nullptr, nullptr, 0, hasPos, retPos }; } hasPos = true; @@ -254,13 +263,12 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW } else { retPos = INVALID_POSITION; } - return { true, move(bufferGuard), retBuf, retLen, hasPos, retPos }; } tuple CommonFunc::GetReadArgV9(napi_env env, - napi_value readBuf, - napi_value option) + napi_value readBuf, + napi_value option) { bool succ = false; int64_t retLen; @@ -272,7 +280,8 @@ tuple CommonFunc::GetReadArgV9(napi_env en int64_t bufLen; tie(succ, buf, bufLen) = txt.ToArraybuffer(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid read buffer, expect arraybuffer"); + HILOGE("Invalid read buffer, expect arraybuffer"); + UniError(EINVAL).ThrowErr(env); return { false, nullptr, 0, posAssigned, position }; } NVal op = NVal(env, option); @@ -286,7 +295,8 @@ tuple CommonFunc::GetReadArgV9(napi_env en if (succ && position >= 0) { posAssigned = true; } else { - UniError(EINVAL).ThrowErr(env, "option.offset shall be positive number"); + HILOGE("option.offset shall be positive number"); + UniError(EINVAL).ThrowErr(env); return { false, nullptr, 0, posAssigned, position }; } } @@ -295,8 +305,8 @@ tuple CommonFunc::GetReadArgV9(napi_env en } tuple, void *, int64_t, bool, int64_t> CommonFunc::GetWriteArgV9(napi_env env, - napi_value argWBuf, - napi_value argOption) + napi_value argWBuf, + napi_value argOption) { int64_t retLen; bool hasPos = false; @@ -312,7 +322,8 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW if (!succ) { tie(succ, buf, bufLen) = NVal(env, argWBuf).ToArraybuffer(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Illegal write buffer or encoding"); + HILOGE("Illegal write buffer or encoding"); + UniError(EINVAL).ThrowErr(env); return { false, nullptr, nullptr, 0, hasPos, retPos }; } } else { @@ -328,7 +339,8 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW int32_t position = 0; tie(succ, position) = op.GetProp("offset").ToInt32(); if (!succ || position < 0) { - UniError(EINVAL).ThrowErr(env, "option.offset shall be positive number"); + HILOGE("option.offset shall be positive number"); + UniError(EINVAL).ThrowErr(env); return { false, nullptr, nullptr, 0, hasPos, retPos }; } hasPos = true; @@ -338,6 +350,7 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW } return { true, move(bufferGuard), buf, retLen, hasPos, retPos }; } + } // namespace ModuleFileIO } // namespace DistributedFS } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp index 910c3178f..80e384e3d 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp @@ -34,7 +34,7 @@ napi_value PropNExporterV9::ReadSync(napi_env env, napi_callback_info info) NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + UniError(EINVAL).ThrowErr(env); return nullptr; } @@ -43,7 +43,7 @@ napi_value PropNExporterV9::ReadSync(napi_env env, napi_callback_info info) int fd; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid fd"); + UniError(EINVAL).ThrowErr(env); return nullptr; } @@ -79,7 +79,7 @@ napi_value PropNExporterV9::Read(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + UniError(EINVAL).ThrowErr(env); return nullptr; } @@ -91,14 +91,14 @@ napi_value PropNExporterV9::Read(napi_env env, napi_callback_info info) int64_t pos; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid fd"); + UniError(EINVAL).ThrowErr(env); return nullptr; } tie(succ, buf, len, hasPos, pos) = CommonFunc::GetReadArgV9(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Failed GetReadArg"); + UniError(EINVAL).ThrowErr(env); return nullptr; } @@ -164,7 +164,7 @@ napi_value PropNExporterV9::Write(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + UniError(EINVAL).ThrowErr(env); return nullptr; } @@ -172,7 +172,7 @@ napi_value PropNExporterV9::Write(napi_env env, napi_callback_info info) int fd; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid fd"); + UniError(EINVAL).ThrowErr(env); return nullptr; } @@ -184,7 +184,7 @@ napi_value PropNExporterV9::Write(napi_env env, napi_callback_info info) tie(succ, bufGuard, buf, len, hasPos, position) = CommonFunc::GetWriteArgV9(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Failed GetWriteArg"); + UniError(EINVAL).ThrowErr(env); return nullptr; } @@ -231,7 +231,7 @@ napi_value PropNExporterV9::WriteSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + UniError(EINVAL).ThrowErr(env); return nullptr; } @@ -239,7 +239,7 @@ napi_value PropNExporterV9::WriteSync(napi_env env, napi_callback_info info) int fd; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid fd"); + UniError(EINVAL).ThrowErr(env); return nullptr; } -- Gitee From b25a33c5cb71d223789826104b17189bff095f50 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Mon, 10 Oct 2022 22:44:44 +0800 Subject: [PATCH 4/4] fileio_add_rw Signed-off-by: zhuhongtao666 --- interfaces/kits/js/BUILD.gn | 4 +- .../kits/js/src/mod_fileio/common_func.cpp | 30 ++++----- .../kits/js/src/mod_fileio/common_func.h | 6 +- .../properties/prop_n_exporter_v9.cpp | 67 ++++++++++--------- .../properties/prop_n_exporter_v9.h | 4 ++ 5 files changed, 53 insertions(+), 58 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 82e10cf5c..370255508 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -118,9 +118,7 @@ ohos_shared_library("fs") { "src/mod_fileio/properties/prop_n_exporter_v9.cpp", ] - deps = [ - "//foundation/arkui/napi:ace_napi", - ] + deps = [ "//foundation/arkui/napi:ace_napi" ] external_deps = [ "hiviewdfx_hilog_native:libhilog" ] } diff --git a/interfaces/kits/js/src/mod_fileio/common_func.cpp b/interfaces/kits/js/src/mod_fileio/common_func.cpp index e64202391..1d098f6f1 100644 --- a/interfaces/kits/js/src/mod_fileio/common_func.cpp +++ b/interfaces/kits/js/src/mod_fileio/common_func.cpp @@ -117,8 +117,8 @@ int CommonFunc::ConvertJsFlags(int &flags) } tuple, unique_ptr> CommonFunc::GetCopyPathArg(napi_env env, - napi_value srcPath, - napi_value dstPath) + napi_value srcPath, + napi_value dstPath) { bool succ = false; unique_ptr src; @@ -132,7 +132,7 @@ tuple, unique_ptr> CommonFunc::GetCopyPathArg(n if (!succ) { return { false, nullptr, nullptr }; } - return make_tuple(succ, move(src), move(dest)); + return make_tuple(true, move(src), move(dest)); } tuple CommonFunc::GetReadArg(napi_env env, @@ -141,13 +141,13 @@ tuple CommonFunc::GetReadArg(napi_env { bool succ = false; void *retBuf = nullptr; - int64_t retLen; + int64_t retLen = 0; bool posAssigned = false; - int64_t position; + int64_t position = 0; NVal txt(env, readBuf); void *buf = nullptr; - int64_t bufLen; + int64_t bufLen = 0; int offset = 0; tie(succ, buf, bufLen) = txt.ToArraybuffer(); if (!succ) { @@ -209,20 +209,18 @@ static tuple, int64_t> DecodeString(napi_env env, NVal } } -// Is everything ok? Do we need to free memory? What's the three args required by fwrite? Where to start writing? tuple, void *, int64_t, bool, int64_t> CommonFunc::GetWriteArg(napi_env env, napi_value argWBuf, napi_value argOption) { void *retBuf = nullptr; - int64_t retLen; + int64_t retLen = 0; bool hasPos = false; - int64_t retPos; + int64_t retPos = 0; - /* To get write buffer */ bool succ = false; void *buf = nullptr; - int64_t bufLen; + int64_t bufLen = 0; NVal op(env, argOption); NVal jsBuffer(env, argWBuf); unique_ptr bufferGuard; @@ -249,7 +247,6 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW return { false, nullptr, nullptr, 0, hasPos, retPos }; } - /* To parse options - Where to begin writing */ if (op.HasProp("position")) { int32_t position = 0; tie(succ, position) = op.GetProp("position").ToInt32(); @@ -267,8 +264,7 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW } tuple CommonFunc::GetReadArgV9(napi_env env, - napi_value readBuf, - napi_value option) + napi_value readBuf, napi_value option) { bool succ = false; int64_t retLen; @@ -305,13 +301,12 @@ tuple CommonFunc::GetReadArgV9(napi_env en } tuple, void *, int64_t, bool, int64_t> CommonFunc::GetWriteArgV9(napi_env env, - napi_value argWBuf, - napi_value argOption) + napi_value argWBuf, napi_value argOption) { int64_t retLen; bool hasPos = false; int64_t retPos; - /* To get write buffer */ + bool succ = false; void *buf = nullptr; int64_t bufLen; @@ -334,7 +329,6 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW return { false, nullptr, nullptr, 0, hasPos, retPos }; } - /* To parse options - Where to begin writing */ if (op.HasProp("offset")) { int32_t position = 0; tie(succ, position) = op.GetProp("offset").ToInt32(); diff --git a/interfaces/kits/js/src/mod_fileio/common_func.h b/interfaces/kits/js/src/mod_fileio/common_func.h index d75647041..5c0adb2e1 100644 --- a/interfaces/kits/js/src/mod_fileio/common_func.h +++ b/interfaces/kits/js/src/mod_fileio/common_func.h @@ -35,11 +35,9 @@ struct CommonFunc { napi_value argWBuf, napi_value argOption); static std::tuple, void *, int64_t, bool, int64_t> GetWriteArgV9(napi_env env, - napi_value argWBuf, - napi_value argOption); + napi_value argWBuf, napi_value argOption); static std::tuple, std::unique_ptr> GetCopyPathArg(napi_env env, - napi_value srcPath, - napi_value dstPath); + napi_value srcPath, napi_value dstPath); }; } // namespace ModuleFileIO } // namespace DistributedFS diff --git a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp index 80e384e3d..6efa9e8df 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp @@ -39,8 +39,7 @@ napi_value PropNExporterV9::ReadSync(napi_env env, napi_callback_info info) } bool succ = false; - - int fd; + int fd = 0; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { UniError(EINVAL).ThrowErr(env); @@ -48,9 +47,9 @@ napi_value PropNExporterV9::ReadSync(napi_env env, napi_callback_info info) } void *buf = nullptr; - int64_t len; + int64_t len = 0; bool hasPos = false; - int64_t pos; + int64_t pos = 0; tie(succ, buf, len, hasPos, pos) = CommonFunc::GetReadArgV9(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { @@ -75,9 +74,24 @@ struct AsyncIOReadArg { ssize_t lenRead { 0 }; }; +static UniError ReadExec(shared_ptr arg, void *buf, size_t len, int fd, size_t position) +{ + if (position == (size_t)INVALID_POSITION) { + arg->lenRead = write(fd, buf, len); + } else { + arg->lenRead = pwrite(fd, buf, len, position); + } + + if (arg->lenRead == -1) { + return UniError(errno); + } else { + return UniError(ERRNO_NOERR); + } +} + napi_value PropNExporterV9::Read(napi_env env, napi_callback_info info) { - NFuncArg funcArg(env, info); + NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) { UniError(EINVAL).ThrowErr(env); return nullptr; @@ -85,10 +99,10 @@ napi_value PropNExporterV9::Read(napi_env env, napi_callback_info info) bool succ = false; void *buf = nullptr; - int64_t len; - int fd; + int64_t len = 0; + int fd = 0; bool hasPos = false; - int64_t pos; + int64_t pos = 0; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { UniError(EINVAL).ThrowErr(env); @@ -103,19 +117,8 @@ napi_value PropNExporterV9::Read(napi_env env, napi_callback_info info) } auto arg = make_shared(); - auto cbExec = [arg, buf, len, fd, hasPos, pos](napi_env env) -> UniError { - ssize_t actLen; - if (hasPos) { - actLen = pread(fd, buf, len, pos); - } else { - actLen = read(fd, buf, len); - } - if (actLen == -1) { - return UniError(errno); - } else { - arg->lenRead = actLen; - return UniError(ERRNO_NOERR); - } + auto cbExec = [arg, buf, len, fd, pos](napi_env env) -> UniError { + return ReadExec(arg, buf, len, fd, pos); }; auto cbCompl = [arg](napi_env env, UniError err) -> NVal { @@ -126,18 +129,17 @@ napi_value PropNExporterV9::Read(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - size_t argc = funcArg.GetArgc(); bool hasOp = false; - if (argc == NARG_CNT::THREE) { + if (funcArg.GetArgc() == NARG_CNT::THREE) { NVal op = NVal(env, funcArg[NARG_POS::THIRD]); if (op.HasProp("offset") || op.HasProp("length")) { hasOp = true; } } - if (argc == NARG_CNT::TWO || (argc == NARG_CNT::THREE && hasOp)) { + if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && hasOp)) { return NAsyncWorkPromise(env, thisVar).Schedule("FileIORead", cbExec, cbCompl).val_; } else { - int cbIdx = ((argc == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); + 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_; } @@ -178,8 +180,8 @@ napi_value PropNExporterV9::Write(napi_env env, napi_callback_info info) unique_ptr bufGuard; void *buf = nullptr; - size_t len; - size_t position; + size_t len = 0; + size_t position = 0; bool hasPos = false; tie(succ, bufGuard, buf, len, hasPos, position) = CommonFunc::GetWriteArgV9(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); @@ -208,18 +210,17 @@ napi_value PropNExporterV9::Write(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); bool hasOp = false; - size_t argc = funcArg.GetArgc(); - if (argc == NARG_CNT::THREE) { + 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")) { hasOp = true; } } - if (argc == NARG_CNT::TWO || (argc == NARG_CNT::THREE && hasOp)) { + if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && hasOp)) { return NAsyncWorkPromise(env, thisVar).Schedule("FileIOWrite", cbExec, cbCompl).val_; } else { - int cbIdx = ((argc == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); + 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_; } @@ -244,8 +245,8 @@ napi_value PropNExporterV9::WriteSync(napi_env env, napi_callback_info info) } void *buf = nullptr; - size_t len; - size_t position; + size_t len = 0; + size_t position = 0; unique_ptr bufGuard; bool hasPos = false; tie(succ, bufGuard, buf, len, hasPos, position) = diff --git a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.h b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.h index 832e26ef4..c7d0d9619 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.h +++ b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.h @@ -16,8 +16,12 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_PROP_N_EXPORTER_V9_H #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_PROP_N_EXPORTER_V9_H +#include "../../common/log.h" +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" #include "../../common/napi/n_async/n_ref.h" #include "../../common/napi/n_exporter.h" +#include "../../common/napi/n_func_arg.h" #include "../../common/napi/n_val.h" #include "../../common/uni_error.h" -- Gitee