From 69fdcf284ef3924497eef4ce1a73bd0f01c05af4 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Wed, 27 Sep 2023 23:22:05 +0800 Subject: [PATCH 1/6] =?UTF-8?q?readlines=E6=8E=A5=E5=8F=A3=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuhongtao666 --- bundle.json | 3 +- interfaces/kits/js/BUILD.gn | 4 + .../readeriterator_entity.h | 43 +++++ .../readeriterator_n_exporter.cpp | 115 +++++++++++++ .../readeriterator_n_exporter.h | 42 +++++ interfaces/kits/js/src/mod_fs/module.cpp | 2 + .../src/mod_fs/properties/prop_n_exporter.cpp | 3 + .../js/src/mod_fs/properties/read_lines.cpp | 158 ++++++++++++++++++ .../js/src/mod_fs/properties/read_lines.h | 36 ++++ 9 files changed, 405 insertions(+), 1 deletion(-) create mode 100644 interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h create mode 100644 interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp create mode 100644 interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/read_lines.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/read_lines.h diff --git a/bundle.json b/bundle.json index 844f0f064..dbf370104 100644 --- a/bundle.json +++ b/bundle.json @@ -36,7 +36,8 @@ "ipc", "napi", "samgr", - "app_file_service" + "app_file_service", + "build_framework" ], "third_party": [ "bounds_checking_function", diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 4f420fad0..625b22775 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -114,12 +114,14 @@ ohos_shared_library("fs") { "${src_path}/mod_fs", "${utils_path}/common/include", "//third_party/libuv/include", + "${file_api_path}/interfaces/kits/rust/include", ] sources = [ "src/common/file_helper/fd_guard.cpp", "src/mod_fs/class_file/file_n_exporter.cpp", "src/mod_fs/class_stat/stat_n_exporter.cpp", + "src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp", "src/mod_fs/common_func.cpp", "src/mod_fs/module.cpp", "src/mod_fs/properties/close.cpp", @@ -129,6 +131,7 @@ ohos_shared_library("fs") { "src/mod_fs/properties/mkdtemp.cpp", "src/mod_fs/properties/open.cpp", "src/mod_fs/properties/prop_n_exporter.cpp", + "src/mod_fs/properties/read_lines.cpp", "src/mod_fs/properties/rename.cpp", "src/mod_fs/properties/rmdirent.cpp", "src/mod_fs/properties/stat.cpp", @@ -140,6 +143,7 @@ ohos_shared_library("fs") { deps = [ "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", "${utils_path}/filemgmt_libn:filemgmt_libn", + "${file_api_path}/interfaces/kits/rust:rust_file", ] use_exceptions = true diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h new file mode 100644 index 000000000..49af69428 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022-2023 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_CLASS_FILE_READ_ITEROR_ENTITY_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_FILE_READ_ITEROR_ENTITY_H + +#include +#include +#include +#include +#include +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { + +struct ReaderIteratorResult { + bool done { false }; + std::string value { "" }; + ReaderIteratorResult() {} + ~ReaderIteratorResult() = default; +}; + +struct ReaderIteratorEntity { + ReaderIteratorResult result; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_FILE_READ_ITEROR_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp new file mode 100644 index 000000000..d2785d777 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2022-2023 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 "readeriterator_n_exporter.h" +#include "readeriterator_entity.h" +#include "filemgmt_libhilog.h" +#include "file_utils.h" +#include "rust_file.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +napi_value ReaderIteratorNExporter::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto readerIteratorEntity = CreateUniquePtr(); + if (readerIteratorEntity == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(readerIteratorEntity))) { + HILOGE("Failed to set reader iterator entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + return funcArg.GetThisVar(); +} + +napi_value ReaderIteratorNExporter::Next(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto readerIteratorEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!readerIteratorEntity) { + HILOGE("Failed to get reader iterator entity"); + return nullptr; + } + + Str *str = NextLine(&(readerIteratorEntity->result)); + + NVal objReaderIteratorResult = NVal::CreateObject(env); + objReaderIteratorResult.AddProp("done", NVal::CreateBool(env, str == nullptr).val_); + objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, str->str, str->len).val_); + StrFree(str); + + return objReaderIteratorResult.val_; +} + +bool ReaderIteratorNExporter::Export() +{ + vector props = { + NVal::DeclareNapiFunction("next", Next), + }; + +#ifdef WIN_PLATFORM + string className = GetNExporterName(); +#else + string className = GetClassName(); +#endif + bool succ = false; + napi_value classValue = nullptr; + tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, ReaderIteratorNExporter::Constructor, + std::move(props)); + if (!succ) { + HILOGE("Failed to define class"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + HILOGE("Failed to save class"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + + return exports_.AddProp(className, classValue); +} + +#ifdef WIN_PLATFORM +string ReaderIteratorNExporter::GetNExporterName() +#else +string ReaderIteratorNExporter::GetClassName() +#endif +{ + return ReaderIteratorNExporter::className_; +} + +ReaderIteratorNExporter::ReaderIteratorNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} + +ReaderIteratorNExporter::~ReaderIteratorNExporter() {} +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h new file mode 100644 index 000000000..30c5d7021 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022-2023 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_CLASS_READERITERATOR_N_EXPORTER_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_READERITERATOR_N_EXPORTER_H + +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace OHOS::FileManagement::LibN; + +class ReaderIteratorNExporter final : public NExporter { +public: + inline static const std::string className_ = "ReaderIterator"; + + bool Export() override; +#ifdef WIN_PLATFORM + std::string GetNExporterName() override; +#else + std::string GetClassName() override; +#endif + + static napi_value Constructor(napi_env env, napi_callback_info info); + static napi_value Next(napi_env env, napi_callback_info info); + + ReaderIteratorNExporter(napi_env env, napi_value exports); + ~ReaderIteratorNExporter() override; +}; +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_READERITERATOR_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp index 797592042..4f31876fe 100644 --- a/interfaces/kits/js/src/mod_fs/module.cpp +++ b/interfaces/kits/js/src/mod_fs/module.cpp @@ -20,6 +20,7 @@ #include "class_file/file_n_exporter.h" #include "class_stat/stat_n_exporter.h" +#include "class_readeriterator/readeriterator_n_exporter.h" #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) #include "class_randomaccessfile/randomaccessfile_n_exporter.h" #include "class_stream/stream_n_exporter.h" @@ -40,6 +41,7 @@ static napi_value Export(napi_env env, napi_value exports) products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); + products.emplace_back(make_unique(env, exports)); #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); 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 index 4bcccb9e4..2a9d12dfc 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -35,6 +35,7 @@ #include "lstat.h" #include "mkdtemp.h" #include "open.h" +#include "read_lines.h" #include "rename.h" #include "rmdirent.h" #include "stat.h" @@ -577,6 +578,8 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("openSync", Open::Sync), NVal::DeclareNapiFunction("read", Read), NVal::DeclareNapiFunction("readSync", ReadSync), + NVal::DeclareNapiFunction("readLines", ReadLines::Async), + NVal::DeclareNapiFunction("readLinesSync", ReadLines::Sync), NVal::DeclareNapiFunction("rename", Rename::Async), NVal::DeclareNapiFunction("renameSync", Rename::Sync), NVal::DeclareNapiFunction("rmdir", Rmdirent::Async), diff --git a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp new file mode 100644 index 000000000..7a7af7ec5 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2023 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 "read_lines.h" +#include +#include "common_func.h" +#include "file_utils.h" +#include "filemgmt_libhilog.h" +#include "rust_file.h" +#include "class_readeriterator/readeriterator_n_exporter.h" +#include "class_readeriterator/readeriterator_entity.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +static NVal InstantiateReaderIterator(napi_env env, void *iterator) +{ + if (iterator == nullptr) { + HILOGE("Invalid argument iterator"); + NError(EINVAL).ThrowErr(env); + return NVal(); + } + + napi_value objReaderIterator = NClass::InstantiateClass(env, ReaderIteratorNExporter::className_, {}); + if (!objReaderIterator) { + HILOGE("Failed to instantiate class ReaderIterator"); + NError(EIO).ThrowErr(env); + return NVal(); + } + + auto readerIteratorEntity = NClass::GetEntityOf(env, objReaderIterator); + if (!readerIteratorEntity) { + HILOGE("Failed to get readerIteratorEntity"); + NError(EIO).ThrowErr(env); + return NVal(); + } + + ReaderIteratorResult *it = static_cast(iterator); + readerIteratorEntity->result.done = it->done; + readerIteratorEntity->result.value = it->value; + + return { env, objReaderIterator }; +} + +struct PtrHolder +{ + void *arg; +}; + +napi_value ReadLines::Async(napi_env env, napi_callback_info info) +{ + HILOGI("ReadLines::Async enter"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("ReadLines, Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("ReadLines, Invalid path from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + char *path = tmp.get(); + int ret = access(path, F_OK); + if (ret < 0) { + HILOGE("ReadLines, No such file or directory"); + NError(ENOENT).ThrowErr(env); + return nullptr; + } + + auto holder = std::make_shared(); + auto cbExec = [holder = holder, path = path]() -> NError { + holder->arg = ::ReaderIterator(path); + if (holder->arg == nullptr) { + HILOGE("Failed to ReadLines, Unknown error"); + return NError(UNKROWN_ERR); + } + return NError(ERRNO_NOERR); + }; + + auto cbCompl = [holder = holder](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return InstantiateReaderIterator(env, holder->arg); + }; + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_READLINES_NAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_READLINES_NAME, cbExec, cbCompl).val_; + } +} + +napi_value ReadLines::Sync(napi_env env, napi_callback_info info) +{ + HILOGI("ReadLines::Sync enter"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("ReadLinesSync, Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("ReadLinesSync, Invalid path from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + char *path = tmp.get(); + int ret = access(path, F_OK); + if (ret < 0) { + HILOGE("ReadLinesSync, No such file or directory"); + NError(ENOENT).ThrowErr(env); + return nullptr; + } + ret = access(path, R_OK); + if (ret == EACCES) { + HILOGE("ReadLinesSync, Permission denied"); + NError(EACCES).ThrowErr(env); + return nullptr; + } + + void *iterator = ReaderIterator(path); + if (iterator == nullptr) { + HILOGE("Failed to ReadLinesSync, Unknown error"); + NError(UNKROWN_ERR).ThrowErr(env); + return nullptr; + } + + return InstantiateReaderIterator(env, iterator).val_; +} + +} // ModuleFileIO +} // FileManagement +} // OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/read_lines.h b/interfaces/kits/js/src/mod_fs/properties/read_lines.h new file mode 100644 index 000000000..244d86c85 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 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_READ_LINES_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_READ_LINES_H + +#include +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { + +class ReadLines final { +public: + static napi_value Async(napi_env env, napi_callback_info info); + static napi_value Sync(napi_env env, napi_callback_info info); +}; + +const std::string PROCEDURE_READLINES_NAME = "FileIOReadLines"; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_READ_LINES_H \ No newline at end of file -- Gitee From 31861025ccc81c594a6dc0c85435edd103276c9d Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Sat, 7 Oct 2023 11:34:47 +0800 Subject: [PATCH 2/6] readlines revise Signed-off-by: zhuhongtao666 --- interfaces/kits/js/BUILD.gn | 6 +- .../readeriterator_entity.h | 9 +- .../readeriterator_n_exporter.cpp | 18 ++-- .../readeriterator_n_exporter.h | 6 +- interfaces/kits/js/src/mod_fs/module.cpp | 4 +- .../src/mod_fs/properties/prop_n_exporter.cpp | 99 +++++++++++++------ .../js/src/mod_fs/properties/read_lines.cpp | 54 +++------- 7 files changed, 103 insertions(+), 93 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 625b22775..ac97d9b9c 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -109,12 +109,12 @@ ohos_shared_library("fs") { relative_install_dir = "module/file" include_dirs = [ + "${file_api_path}/interfaces/kits/rust/include", "${src_path}/common", "${src_path}/common/file_helper", "${src_path}/mod_fs", "${utils_path}/common/include", "//third_party/libuv/include", - "${file_api_path}/interfaces/kits/rust/include", ] sources = [ @@ -131,11 +131,11 @@ ohos_shared_library("fs") { "src/mod_fs/properties/mkdtemp.cpp", "src/mod_fs/properties/open.cpp", "src/mod_fs/properties/prop_n_exporter.cpp", - "src/mod_fs/properties/read_lines.cpp", "src/mod_fs/properties/rename.cpp", "src/mod_fs/properties/rmdirent.cpp", "src/mod_fs/properties/stat.cpp", "src/mod_fs/properties/truncate.cpp", + "src/mod_fs/properties/utimes.cpp", ] cflags_cc = [ "-std=c++17" ] @@ -174,8 +174,10 @@ ohos_shared_library("fs") { "src/mod_fs/properties/dup.cpp", "src/mod_fs/properties/fdopen_stream.cpp", "src/mod_fs/properties/listfile.cpp", + "src/mod_fs/properties/lseek.cpp", "src/mod_fs/properties/move.cpp", "src/mod_fs/properties/movedir.cpp", + "src/mod_fs/properties/read_lines.cpp", "src/mod_fs/properties/read_text.cpp", "src/mod_fs/properties/symlink.cpp", "src/mod_fs/properties/watcher.cpp", diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h index 49af69428..6de83540b 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2023 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 @@ -13,14 +13,15 @@ * limitations under the License. */ -#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_FILE_READ_ITEROR_ENTITY_H -#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_FILE_READ_ITEROR_ENTITY_H +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_READERITERATOR_ENTITY_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_READERITERATOR_ENTITY_H #include #include #include #include #include + #include "filemgmt_libhilog.h" namespace OHOS { @@ -40,4 +41,4 @@ struct ReaderIteratorEntity { } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS -#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_FILE_READ_ITEROR_ENTITY_H \ No newline at end of file +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_READERITERATOR_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp index d2785d777..50174f249 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2023 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 @@ -14,6 +14,7 @@ */ #include "readeriterator_n_exporter.h" + #include "readeriterator_entity.h" #include "filemgmt_libhilog.h" #include "file_utils.h" @@ -57,7 +58,7 @@ napi_value ReaderIteratorNExporter::Next(napi_env env, napi_callback_info info) auto readerIteratorEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!readerIteratorEntity) { HILOGE("Failed to get reader iterator entity"); - return nullptr; + return NError(UNKROWN_ERR); } Str *str = NextLine(&(readerIteratorEntity->result)); @@ -76,35 +77,28 @@ bool ReaderIteratorNExporter::Export() NVal::DeclareNapiFunction("next", Next), }; -#ifdef WIN_PLATFORM - string className = GetNExporterName(); -#else string className = GetClassName(); -#endif + bool succ = false; napi_value classValue = nullptr; tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, ReaderIteratorNExporter::Constructor, std::move(props)); if (!succ) { HILOGE("Failed to define class"); - NError(EIO).ThrowErr(exports_.env_); + NError(UNKROWN_ERR).ThrowErr(exports_.env_); return false; } succ = NClass::SaveClass(exports_.env_, className, classValue); if (!succ) { HILOGE("Failed to save class"); - NError(EIO).ThrowErr(exports_.env_); + NError(UNKROWN_ERR).ThrowErr(exports_.env_); return false; } return exports_.AddProp(className, classValue); } -#ifdef WIN_PLATFORM -string ReaderIteratorNExporter::GetNExporterName() -#else string ReaderIteratorNExporter::GetClassName() -#endif { return ReaderIteratorNExporter::className_; } diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h index 30c5d7021..f1370fa10 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2023 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 @@ -26,11 +26,7 @@ public: inline static const std::string className_ = "ReaderIterator"; bool Export() override; -#ifdef WIN_PLATFORM - std::string GetNExporterName() override; -#else std::string GetClassName() override; -#endif static napi_value Constructor(napi_env env, napi_callback_info info); static napi_value Next(napi_env env, napi_callback_info info); diff --git a/interfaces/kits/js/src/mod_fs/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp index 4f31876fe..dfff0f091 100644 --- a/interfaces/kits/js/src/mod_fs/module.cpp +++ b/interfaces/kits/js/src/mod_fs/module.cpp @@ -20,9 +20,9 @@ #include "class_file/file_n_exporter.h" #include "class_stat/stat_n_exporter.h" -#include "class_readeriterator/readeriterator_n_exporter.h" #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) #include "class_randomaccessfile/randomaccessfile_n_exporter.h" +#include "class_readeriterator/readeriterator_n_exporter.h" #include "class_stream/stream_n_exporter.h" #include "class_watcher/watcher_n_exporter.h" #endif @@ -41,9 +41,9 @@ static napi_value Export(napi_env env, napi_value exports) products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); - products.emplace_back(make_unique(env, exports)); #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) products.emplace_back(make_unique(env, exports)); + products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); #endif 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 index 2a9d12dfc..e2821973f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -32,14 +32,17 @@ #include "fsync.h" #include "js_native_api.h" #include "js_native_api_types.h" +#include "lseek.h" #include "lstat.h" #include "mkdtemp.h" #include "open.h" #include "read_lines.h" #include "rename.h" #include "rmdirent.h" +#include "rust_file.h" #include "stat.h" #include "truncate.h" +#include "utimes.h" #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) #include "copy_file.h" @@ -233,7 +236,7 @@ napi_value PropNExporter::UnlinkSync(napi_env env, napi_callback_info info) napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -245,18 +248,37 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - - auto cbExec = [path = string(tmp.get())]() -> NError { - std::unique_ptr mkdir_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!mkdir_req) { - HILOGE("Failed to request heap memory."); - return NError(ENOMEM); + bool singleLevel = true; + bool recursionMode = false; + if (funcArg.GetArgc() >= NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_boolean)) { + singleLevel = false; + auto [success, recursion] = NVal(env, funcArg[NARG_POS::SECOND]).ToBool(false); + if (!success) { + HILOGE("Invalid recursion mode"); + NError(EINVAL).ThrowErr(env); + return nullptr; } - int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.c_str(), DIR_DEFAULT_PERM, nullptr); - if (ret < 0) { - HILOGE("Failed to create directory"); - return NError(ret); + recursionMode = recursion; + } + + auto cbExec = [path = string(tmp.get()), recursionMode, singleLevel]() -> NError { + if (singleLevel) { + std::unique_ptr mkdir_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!mkdir_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.c_str(), DIR_DEFAULT_PERM, nullptr); + if (ret < 0) { + HILOGE("Failed to create directory"); + return NError(ret); + } + } else { + ::Mkdirs(const_cast(path.c_str()), static_cast(recursionMode)); + if (errno != 0){ + return NError(errno); + } } return NError(ERRNO_NOERR); }; @@ -269,10 +291,12 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ONE) { + + if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_MKDIR_NAME, cbExec, cbCompl).val_; } else { - NVal cb(env, funcArg[NARG_POS::SECOND]); + NVal cb(env, funcArg[funcArg.GetArgc() - 1]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_MKDIR_NAME, cbExec, cbCompl).val_; } } @@ -280,7 +304,7 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) napi_value PropNExporter::MkdirSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE)) { + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -293,20 +317,33 @@ napi_value PropNExporter::MkdirSync(napi_env env, napi_callback_info info) return nullptr; } - std::unique_ptr mkdir_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!mkdir_req) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } - int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.get(), DIR_DEFAULT_PERM, nullptr); - if (ret < 0) { - HILOGE("Failed to create directory"); - NError(ret).ThrowErr(env); - return nullptr; + if (funcArg.GetArgc() == NARG_CNT::ONE) { + std::unique_ptr mkdir_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!mkdir_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.get(), DIR_DEFAULT_PERM, nullptr); + if (ret < 0) { + HILOGE("Failed to create directory"); + NError(ret).ThrowErr(env); + return nullptr; + } + } else { + auto [success, recursion] = NVal(env, funcArg[NARG_POS::SECOND]).ToBool(false); + if (!success) { + HILOGE("Invalid recursion mode"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + ::Mkdirs(const_cast(path.get()), static_cast(recursion)); + if (errno != 0){ + NError(errno).ThrowErr(env); + return nullptr; + } } - return NVal::CreateUndefined(env).val_; } @@ -568,6 +605,7 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("fdatasyncSync", Fdatasync::Sync), NVal::DeclareNapiFunction("fsync", Fsync::Async), NVal::DeclareNapiFunction("fsyncSync", Fsync::Sync), + NVal::DeclareNapiFunction("lseek", Lseek::Sync), NVal::DeclareNapiFunction("lstat", Lstat::Async), NVal::DeclareNapiFunction("lstatSync", Lstat::Sync), NVal::DeclareNapiFunction("mkdir", Mkdir), @@ -578,8 +616,6 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("openSync", Open::Sync), NVal::DeclareNapiFunction("read", Read), NVal::DeclareNapiFunction("readSync", ReadSync), - NVal::DeclareNapiFunction("readLines", ReadLines::Async), - NVal::DeclareNapiFunction("readLinesSync", ReadLines::Sync), NVal::DeclareNapiFunction("rename", Rename::Async), NVal::DeclareNapiFunction("renameSync", Rename::Sync), NVal::DeclareNapiFunction("rmdir", Rmdirent::Async), @@ -590,6 +626,7 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("truncateSync", Truncate::Sync), NVal::DeclareNapiFunction("unlink", Unlink), NVal::DeclareNapiFunction("unlinkSync", UnlinkSync), + NVal::DeclareNapiFunction("utimes", Utimes::Sync), NVal::DeclareNapiFunction("write", Write), NVal::DeclareNapiFunction("writeSync", WriteSync), #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) @@ -610,6 +647,8 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("moveDirSync", MoveDir::Sync), NVal::DeclareNapiFunction("moveFile", Move::Async), NVal::DeclareNapiFunction("moveFileSync", Move::Sync), + NVal::DeclareNapiFunction("readLines", ReadLines::Async), + NVal::DeclareNapiFunction("readLinesSync", ReadLines::Sync), NVal::DeclareNapiFunction("readText", ReadText::Async), NVal::DeclareNapiFunction("readTextSync", ReadText::Sync), NVal::DeclareNapiFunction("symlink", Symlink::Async), diff --git a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp index 7a7af7ec5..d466d6e8c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp @@ -13,13 +13,15 @@ * limitations under the License. */ #include "read_lines.h" + #include + +#include "class_readeriterator/readeriterator_n_exporter.h" +#include "class_readeriterator/readeriterator_entity.h" #include "common_func.h" #include "file_utils.h" #include "filemgmt_libhilog.h" #include "rust_file.h" -#include "class_readeriterator/readeriterator_n_exporter.h" -#include "class_readeriterator/readeriterator_entity.h" namespace OHOS { namespace FileManagement { @@ -38,7 +40,7 @@ static NVal InstantiateReaderIterator(napi_env env, void *iterator) napi_value objReaderIterator = NClass::InstantiateClass(env, ReaderIteratorNExporter::className_, {}); if (!objReaderIterator) { HILOGE("Failed to instantiate class ReaderIterator"); - NError(EIO).ThrowErr(env); + NError(UNKROWN_ERR).ThrowErr(env); return NVal(); } @@ -49,7 +51,7 @@ static NVal InstantiateReaderIterator(napi_env env, void *iterator) return NVal(); } - ReaderIteratorResult *it = static_cast(iterator); + ReaderIteratorResult *it = reinterpret_cast(iterator); readerIteratorEntity->result.done = it->done; readerIteratorEntity->result.value = it->value; @@ -63,35 +65,26 @@ struct PtrHolder napi_value ReadLines::Async(napi_env env, napi_callback_info info) { - HILOGI("ReadLines::Async enter"); NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - HILOGE("ReadLines, Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); return nullptr; } auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { - HILOGE("ReadLines, Invalid path from JS first argument"); + HILOGE("Invalid path from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - char *path = tmp.get(); - int ret = access(path, F_OK); - if (ret < 0) { - HILOGE("ReadLines, No such file or directory"); - NError(ENOENT).ThrowErr(env); - return nullptr; - } - auto holder = std::make_shared(); auto cbExec = [holder = holder, path = path]() -> NError { holder->arg = ::ReaderIterator(path); - if (holder->arg == nullptr) { - HILOGE("Failed to ReadLines, Unknown error"); - return NError(UNKROWN_ERR); + if (errno != 0) { + HILOGE("Failed to ReadLines"); + return NError(errno); } return NError(ERRNO_NOERR); }; @@ -114,39 +107,24 @@ napi_value ReadLines::Async(napi_env env, napi_callback_info info) napi_value ReadLines::Sync(napi_env env, napi_callback_info info) { - HILOGI("ReadLines::Sync enter"); NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE)) { - HILOGE("ReadLinesSync, Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); return nullptr; } auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { - HILOGE("ReadLinesSync, Invalid path from JS first argument"); + HILOGE("Invalid path from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - char *path = tmp.get(); - int ret = access(path, F_OK); - if (ret < 0) { - HILOGE("ReadLinesSync, No such file or directory"); - NError(ENOENT).ThrowErr(env); - return nullptr; - } - ret = access(path, R_OK); - if (ret == EACCES) { - HILOGE("ReadLinesSync, Permission denied"); - NError(EACCES).ThrowErr(env); - return nullptr; - } - void *iterator = ReaderIterator(path); - if (iterator == nullptr) { - HILOGE("Failed to ReadLinesSync, Unknown error"); - NError(UNKROWN_ERR).ThrowErr(env); + if (errno != 0) { + HILOGE("Failed to ReadLinesSync"); + NError(errno).ThrowErr(env); return nullptr; } -- Gitee From e5a56d81ba24ecd6fe9c7a6d1e00a461ad13c0f5 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Sat, 7 Oct 2023 15:43:07 +0800 Subject: [PATCH 3/6] update Signed-off-by: zhuhongtao666 --- .../class_readeriterator/readeriterator_n_exporter.cpp | 2 +- interfaces/kits/js/src/mod_fs/properties/read_lines.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp index 50174f249..d4d628e6c 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp @@ -58,7 +58,7 @@ napi_value ReaderIteratorNExporter::Next(napi_env env, napi_callback_info info) auto readerIteratorEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!readerIteratorEntity) { HILOGE("Failed to get reader iterator entity"); - return NError(UNKROWN_ERR); + return nullptr; } Str *str = NextLine(&(readerIteratorEntity->result)); diff --git a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp index d466d6e8c..f114bfa50 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp @@ -80,7 +80,7 @@ napi_value ReadLines::Async(napi_env env, napi_callback_info info) } auto holder = std::make_shared(); - auto cbExec = [holder = holder, path = path]() -> NError { + auto cbExec = [holder = holder, path = tmp.get()]() -> NError { holder->arg = ::ReaderIterator(path); if (errno != 0) { HILOGE("Failed to ReadLines"); @@ -114,14 +114,14 @@ napi_value ReadLines::Sync(napi_env env, napi_callback_info info) return nullptr; } - auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { HILOGE("Invalid path from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - void *iterator = ReaderIterator(path); + void *iterator = ReaderIterator(path.get()); if (errno != 0) { HILOGE("Failed to ReadLinesSync"); NError(errno).ThrowErr(env); -- Gitee From 042bf21bb05b50d1e86a70c256ceb1d5fd295573 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Mon, 9 Oct 2023 18:04:01 +0800 Subject: [PATCH 4/6] code review Signed-off-by: zhuhongtao666 --- .../class_readeriterator/readeriterator_n_exporter.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp index d4d628e6c..8a3842247 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp @@ -65,8 +65,12 @@ napi_value ReaderIteratorNExporter::Next(napi_env env, napi_callback_info info) NVal objReaderIteratorResult = NVal::CreateObject(env); objReaderIteratorResult.AddProp("done", NVal::CreateBool(env, str == nullptr).val_); - objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, str->str, str->len).val_); - StrFree(str); + if (str != nullptr) { + objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, str->str, str->len).val_); + StrFree(str); + } else { + objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, "").val_); + } return objReaderIteratorResult.val_; } -- Gitee From 9a8e713c9079777579148373c6188097f6224bd2 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Wed, 11 Oct 2023 00:09:53 +0800 Subject: [PATCH 5/6] update Signed-off-by: zhuhongtao666 --- .../readeriterator_entity.h | 10 +---- .../readeriterator_n_exporter.cpp | 14 +++++-- .../js/src/mod_fs/properties/read_lines.cpp | 39 +++++++++++++++---- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h index 6de83540b..349e7d458 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h @@ -28,15 +28,9 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { -struct ReaderIteratorResult { - bool done { false }; - std::string value { "" }; - ReaderIteratorResult() {} - ~ReaderIteratorResult() = default; -}; - struct ReaderIteratorEntity { - ReaderIteratorResult result; + void *readerIteratorEntity_ = nullptr; + int64_t offset = 0; }; } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp index 8a3842247..c498f6b87 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp @@ -61,16 +61,22 @@ napi_value ReaderIteratorNExporter::Next(napi_env env, napi_callback_info info) return nullptr; } - Str *str = NextLine(&(readerIteratorEntity->result)); - + Str *str = NextLine(readerIteratorEntity->readerIteratorEntity_); + if (str == nullptr && readerIteratorEntity->offset != 0) { + HILOGE("Failed to NextLine"); + NError(errno).ThrowErr(env); + return nullptr; + } + NVal objReaderIteratorResult = NVal::CreateObject(env); - objReaderIteratorResult.AddProp("done", NVal::CreateBool(env, str == nullptr).val_); + objReaderIteratorResult.AddProp("done", NVal::CreateBool(env, readerIteratorEntity->offset == 0).val_); if (str != nullptr) { objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, str->str, str->len).val_); - StrFree(str); + readerIteratorEntity->offset -= str->len; } else { objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, "").val_); } + StrFree(str); return objReaderIteratorResult.val_; } diff --git a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp index f114bfa50..7a6da1d67 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp @@ -29,7 +29,31 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -static NVal InstantiateReaderIterator(napi_env env, void *iterator) +int64_t GetFileSize(const char *filePath) +{ + std::unique_ptr fs_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!fs_req) { + HILOGE("Failed to request heap memory."); + return 0; + } + + int ret = uv_fs_stat(nullptr, fs_req.get(), filePath, nullptr); + if (ret < 0) { + HILOGE("Failed to stat path:%{public}s", filePath); + return 0; + } + + uv_stat_t *st = uv_fs_get_statbuf(fs_req.get()); + if (st == nullptr) { + HILOGE("Failed to get statbuf"); + return 0; + } + + return st->st_size; +} + +static NVal InstantiateReaderIterator(napi_env env, void *iterator, int64_t offset) { if (iterator == nullptr) { HILOGE("Invalid argument iterator"); @@ -51,9 +75,8 @@ static NVal InstantiateReaderIterator(napi_env env, void *iterator) return NVal(); } - ReaderIteratorResult *it = reinterpret_cast(iterator); - readerIteratorEntity->result.done = it->done; - readerIteratorEntity->result.value = it->value; + readerIteratorEntity->readerIteratorEntity_ = iterator; + readerIteratorEntity->offset = offset; return { env, objReaderIterator }; } @@ -89,11 +112,11 @@ napi_value ReadLines::Async(napi_env env, napi_callback_info info) return NError(ERRNO_NOERR); }; - auto cbCompl = [holder = holder](napi_env env, NError err) -> NVal { + auto cbCompl = [holder = holder, path = tmp.get()](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } - return InstantiateReaderIterator(env, holder->arg); + return InstantiateReaderIterator(env, holder->arg, GetFileSize(path)); }; NVal thisVar(env, funcArg.GetThisVar()); @@ -121,14 +144,14 @@ napi_value ReadLines::Sync(napi_env env, napi_callback_info info) return nullptr; } - void *iterator = ReaderIterator(path.get()); + void *iterator = ::ReaderIterator(path.get()); if (errno != 0) { HILOGE("Failed to ReadLinesSync"); NError(errno).ThrowErr(env); return nullptr; } - return InstantiateReaderIterator(env, iterator).val_; + return InstantiateReaderIterator(env, iterator, GetFileSize(path.get())).val_; } } // ModuleFileIO -- Gitee From a61bc174e8810420bcabe89104a1ab74e2336848 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Wed, 11 Oct 2023 14:42:49 +0800 Subject: [PATCH 6/6] readlines revise Signed-off-by: zhuhongtao666 --- .../readeriterator_entity.h | 4 +- .../readeriterator_n_exporter.cpp | 6 +-- .../readeriterator_n_exporter.h | 2 +- .../js/src/mod_fs/properties/read_lines.cpp | 54 ++++++++++--------- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h index 349e7d458..de5f26af4 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * Copyright (c) 2023 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 @@ -29,7 +29,7 @@ namespace FileManagement { namespace ModuleFileIO { struct ReaderIteratorEntity { - void *readerIteratorEntity_ = nullptr; + void *iterator = nullptr; int64_t offset = 0; }; } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp index c498f6b87..eafcebee2 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * Copyright (c) 2023 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 @@ -61,9 +61,9 @@ napi_value ReaderIteratorNExporter::Next(napi_env env, napi_callback_info info) return nullptr; } - Str *str = NextLine(readerIteratorEntity->readerIteratorEntity_); + Str *str = NextLine(readerIteratorEntity->iterator); if (str == nullptr && readerIteratorEntity->offset != 0) { - HILOGE("Failed to NextLine"); + HILOGE("Failed to get next line"); NError(errno).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h index f1370fa10..53b79d621 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * Copyright (c) 2023 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 diff --git a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp index 7a6da1d67..ec036b357 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp @@ -29,28 +29,23 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -int64_t GetFileSize(const char *filePath) -{ - std::unique_ptr fs_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!fs_req) { +static int GetFileSize(const string &path, int64_t &offset) { + std::unique_ptr stat_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + + if (!stat_req) { HILOGE("Failed to request heap memory."); - return 0; + return ENOMEM; } - int ret = uv_fs_stat(nullptr, fs_req.get(), filePath, nullptr); + int ret = uv_fs_stat(nullptr, stat_req.get(), path.c_str(), nullptr); if (ret < 0) { - HILOGE("Failed to stat path:%{public}s", filePath); - return 0; - } - - uv_stat_t *st = uv_fs_get_statbuf(fs_req.get()); - if (st == nullptr) { - HILOGE("Failed to get statbuf"); - return 0; + HILOGE("Failed to get file stat by path"); + return ret; } - return st->st_size; + offset = stat_req->statbuf.st_size; + return ERRNO_NOERR; } static NVal InstantiateReaderIterator(napi_env env, void *iterator, int64_t offset) @@ -75,15 +70,16 @@ static NVal InstantiateReaderIterator(napi_env env, void *iterator, int64_t offs return NVal(); } - readerIteratorEntity->readerIteratorEntity_ = iterator; + readerIteratorEntity->iterator = iterator; readerIteratorEntity->offset = offset; return { env, objReaderIterator }; } -struct PtrHolder +struct ReaderIteratorArg { - void *arg; + void *iterator = nullptr; + int64_t offset = 0; }; napi_value ReadLines::Async(napi_env env, napi_callback_info info) @@ -102,9 +98,10 @@ napi_value ReadLines::Async(napi_env env, napi_callback_info info) return nullptr; } - auto holder = std::make_shared(); - auto cbExec = [holder = holder, path = tmp.get()]() -> NError { - holder->arg = ::ReaderIterator(path); + auto arg = std::make_shared(); + auto cbExec = [arg = arg, path = tmp.get()]() -> NError { + arg->iterator = ::ReaderIterator(path); + GetFileSize(string(path), arg->offset); if (errno != 0) { HILOGE("Failed to ReadLines"); return NError(errno); @@ -112,11 +109,11 @@ napi_value ReadLines::Async(napi_env env, napi_callback_info info) return NError(ERRNO_NOERR); }; - auto cbCompl = [holder = holder, path = tmp.get()](napi_env env, NError err) -> NVal { + auto cbCompl = [arg](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } - return InstantiateReaderIterator(env, holder->arg, GetFileSize(path)); + return InstantiateReaderIterator(env, arg->iterator, arg->offset); }; NVal thisVar(env, funcArg.GetThisVar()); @@ -151,7 +148,14 @@ napi_value ReadLines::Sync(napi_env env, napi_callback_info info) return nullptr; } - return InstantiateReaderIterator(env, iterator, GetFileSize(path.get())).val_; + int64_t offset = 0; + int ret = GetFileSize(string(path.get()), offset); + if (ret != 0) { + HILOGE("Failed to get file size"); + return nullptr; + } + + return InstantiateReaderIterator(env, iterator, offset).val_; } } // ModuleFileIO -- Gitee