From 64c04aab62fdfcff24a2f521b080343e06ea75b3 Mon Sep 17 00:00:00 2001 From: bubble_mao Date: Wed, 11 Oct 2023 19:36:00 +0800 Subject: [PATCH 1/7] update Signed-off-by: bubble_mao --- interfaces/kits/js/BUILD.gn | 4 + .../readeriterator_entity.h | 38 ++++ .../readeriterator_n_exporter.cpp | 119 +++++++++++++ .../readeriterator_n_exporter.h | 38 ++++ .../src/mod_fs/properties/prop_n_exporter.cpp | 3 + .../js/src/mod_fs/properties/read_lines.cpp | 163 ++++++++++++++++++ .../js/src/mod_fs/properties/read_lines.h | 36 ++++ 7 files changed, 401 insertions(+) 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/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 194ea2ece..edd80c008 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -109,6 +109,7 @@ 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", @@ -120,6 +121,7 @@ ohos_shared_library("fs") { "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", @@ -140,6 +142,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 @@ -174,6 +177,7 @@ ohos_shared_library("fs") { "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 new file mode 100644 index 000000000..de5f26af4 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_entity.h @@ -0,0 +1,38 @@ +/* + * 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_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 { +namespace FileManagement { +namespace ModuleFileIO { + +struct ReaderIteratorEntity { + void *iterator = nullptr; + int64_t offset = 0; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#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 new file mode 100644 index 000000000..eafcebee2 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp @@ -0,0 +1,119 @@ +/* + * 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 "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->iterator); + if (str == nullptr && readerIteratorEntity->offset != 0) { + HILOGE("Failed to get next line"); + NError(errno).ThrowErr(env); + return nullptr; + } + + NVal objReaderIteratorResult = NVal::CreateObject(env); + objReaderIteratorResult.AddProp("done", NVal::CreateBool(env, readerIteratorEntity->offset == 0).val_); + if (str != nullptr) { + objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, str->str, str->len).val_); + readerIteratorEntity->offset -= str->len; + } else { + objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, "").val_); + } + StrFree(str); + + return objReaderIteratorResult.val_; +} + +bool ReaderIteratorNExporter::Export() +{ + vector props = { + NVal::DeclareNapiFunction("next", Next), + }; + + string className = GetClassName(); + + 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(UNKROWN_ERR).ThrowErr(exports_.env_); + return false; + } + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + HILOGE("Failed to save class"); + NError(UNKROWN_ERR).ThrowErr(exports_.env_); + return false; + } + + return exports_.AddProp(className, classValue); +} + +string ReaderIteratorNExporter::GetClassName() +{ + 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..53b79d621 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/readeriterator_n_exporter.h @@ -0,0 +1,38 @@ +/* + * 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_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; + std::string GetClassName() override; + + 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/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 160ce2e69..50c7797e5 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" @@ -609,6 +610,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 new file mode 100644 index 000000000..ec036b357 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp @@ -0,0 +1,163 @@ +/* + * 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 "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" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +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 ENOMEM; + } + + int ret = uv_fs_stat(nullptr, stat_req.get(), path.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to get file stat by path"); + return ret; + } + + offset = stat_req->statbuf.st_size; + return ERRNO_NOERR; +} + +static NVal InstantiateReaderIterator(napi_env env, void *iterator, int64_t offset) +{ + 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(UNKROWN_ERR).ThrowErr(env); + return NVal(); + } + + auto readerIteratorEntity = NClass::GetEntityOf(env, objReaderIterator); + if (!readerIteratorEntity) { + HILOGE("Failed to get readerIteratorEntity"); + NError(EIO).ThrowErr(env); + return NVal(); + } + + readerIteratorEntity->iterator = iterator; + readerIteratorEntity->offset = offset; + + return { env, objReaderIterator }; +} + +struct ReaderIteratorArg +{ + void *iterator = nullptr; + int64_t offset = 0; +}; + +napi_value ReadLines::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("Invalid path from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + 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); + } + return NError(ERRNO_NOERR); + }; + + auto cbCompl = [arg](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return InstantiateReaderIterator(env, arg->iterator, arg->offset); + }; + + 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) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + 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.get()); + if (errno != 0) { + HILOGE("Failed to ReadLinesSync"); + NError(errno).ThrowErr(env); + return nullptr; + } + + 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 +} // 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 a084315dbea2e9093b261d18ac5bca31e534be37 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Wed, 11 Oct 2023 17:53:44 +0800 Subject: [PATCH 2/7] readlines revise Signed-off-by: zhuhongtao666 --- .../src/mod_fs/class_file/file_n_exporter.cpp | 4 +- interfaces/kits/js/src/mod_fs/module.cpp | 2 + .../kits/js/src/mod_fs/properties/lstat.cpp | 4 +- .../src/mod_fs/properties/prop_n_exporter.cpp | 95 +++++++++++++------ 4 files changed, 73 insertions(+), 32 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index 76930257e..0fe49afad 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -77,7 +77,7 @@ static tuple> Re } int ret = uv_fs_realpath(nullptr, realpath_req.get(), srcPath.c_str(), nullptr); if (ret < 0) { - HILOGE("Failed to realpath, ret: %{public}d", ret); + HILOGE("Failed to realpath, ret: %{public}d, path: %{public}s", ret, srcPath.c_str()); return { ret, move(realpath_req)}; } return { ERRNO_NOERR, move(realpath_req) }; @@ -139,7 +139,7 @@ napi_value FileNExporter::GetName(napi_env env, napi_callback_info info) string path = string(static_cast(realPath->ptr)); auto pos = path.find_last_of('/'); if (pos == string::npos) { - HILOGE("Failed to split filename from path"); + HILOGE("Failed to split filename from path, path: %{public}s", path.c_str()); NError(ENOENT).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp index a276ac5ac..16795efec 100644 --- a/interfaces/kits/js/src/mod_fs/module.cpp +++ b/interfaces/kits/js/src/mod_fs/module.cpp @@ -22,6 +22,7 @@ #include "class_stat/stat_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 @@ -43,6 +44,7 @@ static napi_value Export(napi_env env, napi_value 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/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index 29ac3498e..c0669fbd6 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -52,7 +52,7 @@ napi_value Lstat::Sync(napi_env env, napi_callback_info info) } int ret = uv_fs_lstat(nullptr, lstat_req.get(), pathPtr.get(), nullptr); if (ret < 0) { - HILOGE("Failed to get stat of file, ret: %{public}d", ret); + HILOGE("Failed to get stat of file by path %{public}s", pathPtr.get()); NError(ret).ThrowErr(env); return nullptr; } @@ -71,7 +71,7 @@ static NError LstatExec(shared_ptr arg, const string &path) } int ret = uv_fs_lstat(nullptr, lstat_req.get(), path.c_str(), nullptr); if (ret < 0) { - HILOGE("Failed to get stat of file, ret: %{public}d", ret); + HILOGE("Failed to get stat of file by path: %{public}s, ret: %{public}d", path.c_str(), ret); return NError(ret); } else { arg->stat_ = lstat_req->statbuf; 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 50c7797e5..5cdab0bdf 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" @@ -234,7 +237,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; @@ -246,18 +249,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); }; @@ -270,10 +292,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_; } } @@ -281,7 +305,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; @@ -294,20 +318,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_; } @@ -569,6 +606,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), @@ -589,6 +627,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) -- Gitee From afb72a27cdb8120cb28c6d2d9d65f3b8acace553 Mon Sep 17 00:00:00 2001 From: bubble_mao Date: Wed, 11 Oct 2023 19:42:09 +0800 Subject: [PATCH 3/7] update Signed-off-by: bubble_mao --- .../src/mod_fs/class_file/file_n_exporter.cpp | 4 +- .../kits/js/src/mod_fs/properties/lstat.cpp | 4 +- .../src/mod_fs/properties/prop_n_exporter.cpp | 95 ++++++------------- 3 files changed, 32 insertions(+), 71 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index 0fe49afad..76930257e 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -77,7 +77,7 @@ static tuple> Re } int ret = uv_fs_realpath(nullptr, realpath_req.get(), srcPath.c_str(), nullptr); if (ret < 0) { - HILOGE("Failed to realpath, ret: %{public}d, path: %{public}s", ret, srcPath.c_str()); + HILOGE("Failed to realpath, ret: %{public}d", ret); return { ret, move(realpath_req)}; } return { ERRNO_NOERR, move(realpath_req) }; @@ -139,7 +139,7 @@ napi_value FileNExporter::GetName(napi_env env, napi_callback_info info) string path = string(static_cast(realPath->ptr)); auto pos = path.find_last_of('/'); if (pos == string::npos) { - HILOGE("Failed to split filename from path, path: %{public}s", path.c_str()); + HILOGE("Failed to split filename from path"); NError(ENOENT).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index c0669fbd6..29ac3498e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -52,7 +52,7 @@ napi_value Lstat::Sync(napi_env env, napi_callback_info info) } int ret = uv_fs_lstat(nullptr, lstat_req.get(), pathPtr.get(), nullptr); if (ret < 0) { - HILOGE("Failed to get stat of file by path %{public}s", pathPtr.get()); + HILOGE("Failed to get stat of file, ret: %{public}d", ret); NError(ret).ThrowErr(env); return nullptr; } @@ -71,7 +71,7 @@ static NError LstatExec(shared_ptr arg, const string &path) } int ret = uv_fs_lstat(nullptr, lstat_req.get(), path.c_str(), nullptr); if (ret < 0) { - HILOGE("Failed to get stat of file by path: %{public}s, ret: %{public}d", path.c_str(), ret); + HILOGE("Failed to get stat of file, ret: %{public}d", ret); return NError(ret); } else { arg->stat_ = lstat_req->statbuf; 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 5cdab0bdf..50c7797e5 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,17 +32,14 @@ #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" @@ -237,7 +234,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::THREE)) { + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -249,37 +246,18 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - 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; + + 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); } - 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); - } + 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); } return NError(ERRNO_NOERR); }; @@ -292,12 +270,10 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - - if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && - !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { + if (funcArg.GetArgc() == NARG_CNT::ONE) { return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_MKDIR_NAME, cbExec, cbCompl).val_; } else { - NVal cb(env, funcArg[funcArg.GetArgc() - 1]); + NVal cb(env, funcArg[NARG_POS::SECOND]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_MKDIR_NAME, cbExec, cbCompl).val_; } } @@ -305,7 +281,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, NARG_CNT::TWO)) { + if (!funcArg.InitArgs(NARG_CNT::ONE)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -318,33 +294,20 @@ napi_value PropNExporter::MkdirSync(napi_env env, napi_callback_info info) 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; - } + 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; } + return NVal::CreateUndefined(env).val_; } @@ -606,7 +569,6 @@ 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), @@ -627,7 +589,6 @@ 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) -- Gitee From 51fa12aac3265f899b12384e98d5692023d29d70 Mon Sep 17 00:00:00 2001 From: bubble_mao Date: Wed, 11 Oct 2023 20:48:30 +0800 Subject: [PATCH 4/7] update Signed-off-by: bubble_mao --- .../readeriterator_entity.h | 6 ------ .../js/src/mod_fs/properties/read_lines.cpp | 17 +++++++++++------ 2 files changed, 11 insertions(+), 12 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 de5f26af4..ff8a2c9c7 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 @@ -16,12 +16,6 @@ #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 { 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 ec036b357..b5b5438dd 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp @@ -99,13 +99,18 @@ napi_value ReadLines::Async(napi_env env, napi_callback_info info) } auto arg = std::make_shared(); - auto cbExec = [arg = arg, path = tmp.get()]() -> NError { + auto cbExec = [arg, path = tmp.get()]() -> NError { arg->iterator = ::ReaderIterator(path); - GetFileSize(string(path), arg->offset); if (errno != 0) { - HILOGE("Failed to ReadLines"); + HILOGE("Failed to read lines of the file"); return NError(errno); } + int ret = GetFileSize(string(path), arg->offset); + if (ret != 0) { + HILOGE("Failed to get size of the file"); + return NError(ret); + } + return NError(ERRNO_NOERR); }; @@ -142,8 +147,8 @@ napi_value ReadLines::Sync(napi_env env, napi_callback_info info) } void *iterator = ::ReaderIterator(path.get()); - if (errno != 0) { - HILOGE("Failed to ReadLinesSync"); + if (errno != 0 || iterator == nullptr) { + HILOGE("Failed to read lines of the file"); NError(errno).ThrowErr(env); return nullptr; } @@ -151,7 +156,7 @@ napi_value ReadLines::Sync(napi_env env, napi_callback_info info) int64_t offset = 0; int ret = GetFileSize(string(path.get()), offset); if (ret != 0) { - HILOGE("Failed to get file size"); + HILOGE("Failed to get size of the file"); return nullptr; } -- Gitee From 3029d36357ce12fc6533e611ecd7ab66fcef1cf0 Mon Sep 17 00:00:00 2001 From: bubble_mao Date: Thu, 12 Oct 2023 21:07:03 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=96=B0=E5=A2=9Eoption=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bubble_mao --- interfaces/kits/js/BUILD.gn | 4 +- .../js/src/mod_fs/properties/read_lines.cpp | 73 +++++++++++++++---- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index edd80c008..a57cf6712 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -120,8 +120,8 @@ ohos_shared_library("fs") { 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/class_stat/stat_n_exporter.cpp", "src/mod_fs/common_func.cpp", "src/mod_fs/module.cpp", "src/mod_fs/properties/close.cpp", @@ -140,9 +140,9 @@ ohos_shared_library("fs") { cflags_cc = [ "-std=c++17" ] deps = [ + "${file_api_path}/interfaces/kits/rust:rust_file", "${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/properties/read_lines.cpp b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp index b5b5438dd..4506d4f58 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp @@ -29,10 +29,27 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +static tuple> GetReadLinesArg(napi_env env, napi_value argOption) +{ + NVal option(env, argOption); + unique_ptr encoding; + + if (option.HasProp("encoding")) { + bool succ = false; + tie(succ, encoding, ignore) = option.GetProp("encoding").ToUTF8String("utf-8"); + string_view encodingStr(encoding.get()); + if (!succ || encodingStr != "utf-8") { + HILOGE("Illegal option.encoding parameter"); + return { false, nullptr }; + } + } + + return { true, move(encoding) }; +} + 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 ENOMEM; @@ -85,28 +102,42 @@ struct ReaderIteratorArg napi_value ReadLines::Async(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; } - auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!succ) { + auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succPath) { HILOGE("Invalid path from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto arg = std::make_shared(); - auto cbExec = [arg, path = tmp.get()]() -> NError { + if (funcArg.GetArgc() >= NARG_CNT::TWO) { + auto [succOption, encoding] = GetReadLinesArg(env, funcArg[NARG_POS::SECOND]); + if (!succOption) { + HILOGE("Invalid encoding from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + } + + auto arg = CreateSharedPtr(); + if (arg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + auto cbExec = [arg, path = path.get()]() -> NError { arg->iterator = ::ReaderIterator(path); - if (errno != 0) { + if (arg->iterator == nullptr) { HILOGE("Failed to read lines of the file"); return NError(errno); } - int ret = GetFileSize(string(path), arg->offset); - if (ret != 0) { + int ret = GetFileSize(path, arg->offset); + if (ret < 0) { HILOGE("Failed to get size of the file"); return NError(ret); } @@ -122,10 +153,11 @@ napi_value ReadLines::Async(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_READLINES_NAME, cbExec, cbCompl).val_; } else { - NVal cb(env, funcArg[NARG_POS::SECOND]); + NVal cb(env, funcArg[((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD)]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_READLINES_NAME, cbExec, cbCompl).val_; } } @@ -133,28 +165,37 @@ napi_value ReadLines::Async(napi_env env, napi_callback_info info) napi_value ReadLines::Sync(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; } - auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!succ) { + auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succPath) { HILOGE("Invalid path from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } + if (funcArg.GetArgc() == NARG_CNT::TWO) { + auto [succOption, encoding] = GetReadLinesArg(env, funcArg[NARG_POS::SECOND]); + if (!succOption) { + HILOGE("Invalid encoding from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + } + void *iterator = ::ReaderIterator(path.get()); - if (errno != 0 || iterator == nullptr) { + if (iterator == nullptr) { HILOGE("Failed to read lines of the file"); NError(errno).ThrowErr(env); return nullptr; } int64_t offset = 0; - int ret = GetFileSize(string(path.get()), offset); + int ret = GetFileSize(path.get(), offset); if (ret != 0) { HILOGE("Failed to get size of the file"); return nullptr; -- Gitee From dcc75f69d057bd5b5d36b4b8ac0d591f85485728 Mon Sep 17 00:00:00 2001 From: bubble_mao Date: Thu, 12 Oct 2023 21:25:49 +0800 Subject: [PATCH 6/7] update Signed-off-by: bubble_mao --- .../class_readeriterator/readeriterator_n_exporter.cpp | 4 +++- interfaces/kits/js/src/mod_fs/properties/read_lines.cpp | 6 +++--- 2 files changed, 6 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 eafcebee2..e2dd07766 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 @@ -15,9 +15,9 @@ #include "readeriterator_n_exporter.h" -#include "readeriterator_entity.h" #include "filemgmt_libhilog.h" #include "file_utils.h" +#include "readeriterator_entity.h" #include "rust_file.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -58,6 +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"); + NError(EINVAL).ThrowErr(env); return nullptr; } @@ -77,6 +78,7 @@ napi_value ReaderIteratorNExporter::Next(napi_env env, napi_callback_info info) objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, "").val_); } StrFree(str); + (void)NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); 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 4506d4f58..b16669466 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp @@ -16,8 +16,8 @@ #include -#include "class_readeriterator/readeriterator_n_exporter.h" #include "class_readeriterator/readeriterator_entity.h" +#include "class_readeriterator/readeriterator_n_exporter.h" #include "common_func.h" #include "file_utils.h" #include "filemgmt_libhilog.h" @@ -133,7 +133,7 @@ napi_value ReadLines::Async(napi_env env, napi_callback_info info) auto cbExec = [arg, path = path.get()]() -> NError { arg->iterator = ::ReaderIterator(path); if (arg->iterator == nullptr) { - HILOGE("Failed to read lines of the file"); + HILOGE("Failed to read lines of the file, error:%{public}d", errno); return NError(errno); } int ret = GetFileSize(path, arg->offset); @@ -189,7 +189,7 @@ napi_value ReadLines::Sync(napi_env env, napi_callback_info info) void *iterator = ::ReaderIterator(path.get()); if (iterator == nullptr) { - HILOGE("Failed to read lines of the file"); + HILOGE("Failed to read lines of the file, error:%{public}d", errno); NError(errno).ThrowErr(env); return nullptr; } -- Gitee From ee3e2fdd18158bc7eb580413b0a364eafe687552 Mon Sep 17 00:00:00 2001 From: bubble_mao Date: Sat, 14 Oct 2023 10:29:22 +0800 Subject: [PATCH 7/7] issue fixed Signed-off-by: bubble_mao --- interfaces/kits/js/BUILD.gn | 4 +- .../readeriterator_n_exporter.cpp | 10 +-- .../src/mod_fs/properties/prop_n_exporter.cpp | 2 +- .../js/src/mod_fs/properties/read_lines.cpp | 76 +++++++++---------- 4 files changed, 45 insertions(+), 47 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index a57cf6712..318932161 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -109,7 +109,6 @@ 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", @@ -120,7 +119,6 @@ ohos_shared_library("fs") { sources = [ "src/common/file_helper/fd_guard.cpp", "src/mod_fs/class_file/file_n_exporter.cpp", - "src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp", "src/mod_fs/class_stat/stat_n_exporter.cpp", "src/mod_fs/common_func.cpp", "src/mod_fs/module.cpp", @@ -140,7 +138,6 @@ ohos_shared_library("fs") { cflags_cc = [ "-std=c++17" ] deps = [ - "${file_api_path}/interfaces/kits/rust:rust_file", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", "${utils_path}/filemgmt_libn:filemgmt_libn", ] @@ -163,6 +160,7 @@ ohos_shared_library("fs") { include_dirs += [ "${file_api_path}/interfaces/kits/rust/include" ] sources += [ "src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp", + "src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp", "src/mod_fs/class_stream/flush.cpp", "src/mod_fs/class_stream/stream_n_exporter.cpp", "src/mod_fs/class_watcher/watcher_entity.cpp", 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 e2dd07766..d7ee0c856 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 @@ -41,7 +41,7 @@ napi_value ReaderIteratorNExporter::Constructor(napi_env env, napi_callback_info } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(readerIteratorEntity))) { HILOGE("Failed to set reader iterator entity"); - NError(EINVAL).ThrowErr(env); + NError(UNKROWN_ERR).ThrowErr(env); return nullptr; } return funcArg.GetThisVar(); @@ -58,27 +58,27 @@ 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"); - NError(EINVAL).ThrowErr(env); + NError(UNKROWN_ERR).ThrowErr(env); return nullptr; } Str *str = NextLine(readerIteratorEntity->iterator); if (str == nullptr && readerIteratorEntity->offset != 0) { - HILOGE("Failed to get next line"); + HILOGE("Failed to get next line, error:%{public}d", errno); NError(errno).ThrowErr(env); return nullptr; } NVal objReaderIteratorResult = NVal::CreateObject(env); - objReaderIteratorResult.AddProp("done", NVal::CreateBool(env, readerIteratorEntity->offset == 0).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_); readerIteratorEntity->offset -= str->len; } else { objReaderIteratorResult.AddProp("value", NVal::CreateUTF8String(env, "").val_); + (void)NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); } StrFree(str); - (void)NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); return objReaderIteratorResult.val_; } 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 50c7797e5..44b4ca8ff 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,7 +35,6 @@ #include "lstat.h" #include "mkdtemp.h" #include "open.h" -#include "read_lines.h" #include "rename.h" #include "rmdirent.h" #include "stat.h" @@ -52,6 +51,7 @@ #include "lseek.h" #include "move.h" #include "movedir.h" +#include "read_lines.h" #include "read_text.h" #include "symlink.h" #include "watcher.h" 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 b16669466..9e485aa57 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines.cpp @@ -29,25 +29,22 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -static tuple> GetReadLinesArg(napi_env env, napi_value argOption) +static int CheckOptionArg(napi_env env, napi_value argOption) { NVal option(env, argOption); - unique_ptr encoding; - if (option.HasProp("encoding")) { - bool succ = false; - tie(succ, encoding, ignore) = option.GetProp("encoding").ToUTF8String("utf-8"); + auto [succ, encoding, ignore] = option.GetProp("encoding").ToUTF8String("utf-8"); string_view encodingStr(encoding.get()); if (!succ || encodingStr != "utf-8") { - HILOGE("Illegal option.encoding parameter"); - return { false, nullptr }; + return EINVAL; } } - return { true, move(encoding) }; + return ERRNO_NOERR; } -static int GetFileSize(const string &path, int64_t &offset) { +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) { @@ -61,7 +58,7 @@ static int GetFileSize(const string &path, int64_t &offset) { return ret; } - offset = stat_req->statbuf.st_size; + offset = static_cast(stat_req->statbuf.st_size); return ERRNO_NOERR; } @@ -83,22 +80,36 @@ static NVal InstantiateReaderIterator(napi_env env, void *iterator, int64_t offs auto readerIteratorEntity = NClass::GetEntityOf(env, objReaderIterator); if (!readerIteratorEntity) { HILOGE("Failed to get readerIteratorEntity"); - NError(EIO).ThrowErr(env); + NError(UNKROWN_ERR).ThrowErr(env); return NVal(); } readerIteratorEntity->iterator = iterator; readerIteratorEntity->offset = offset; - return { env, objReaderIterator }; } -struct ReaderIteratorArg -{ +struct ReaderIteratorArg { void *iterator = nullptr; int64_t offset = 0; }; +static NError AsyncExec(ReaderIteratorArg &readerIterator, char *pathStr) +{ + readerIterator.iterator = ::ReaderIterator(pathStr); + if (readerIterator.iterator == nullptr) { + HILOGE("Failed to read lines of the file, error: %{public}d", errno); + return NError(errno); + } + int ret = GetFileSize(pathStr, readerIterator.offset); + if (ret < 0) { + HILOGE("Failed to get size of the file"); + return NError(ret); + } + + return NError(ERRNO_NOERR); +} + napi_value ReadLines::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -116,11 +127,11 @@ napi_value ReadLines::Async(napi_env env, napi_callback_info info) } if (funcArg.GetArgc() >= NARG_CNT::TWO) { - auto [succOption, encoding] = GetReadLinesArg(env, funcArg[NARG_POS::SECOND]); - if (!succOption) { - HILOGE("Invalid encoding from JS first argument"); - NError(EINVAL).ThrowErr(env); - return nullptr; + int ret = CheckOptionArg(env, funcArg[NARG_POS::SECOND]); + if (ret) { + HILOGE("Invalid option.encoding parameter"); + NError(ret).ThrowErr(env); + return nullptr; } } @@ -131,18 +142,7 @@ napi_value ReadLines::Async(napi_env env, napi_callback_info info) return nullptr; } auto cbExec = [arg, path = path.get()]() -> NError { - arg->iterator = ::ReaderIterator(path); - if (arg->iterator == nullptr) { - HILOGE("Failed to read lines of the file, error:%{public}d", errno); - return NError(errno); - } - int ret = GetFileSize(path, arg->offset); - if (ret < 0) { - HILOGE("Failed to get size of the file"); - return NError(ret); - } - - return NError(ERRNO_NOERR); + return AsyncExec(*arg, path); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { @@ -179,17 +179,17 @@ napi_value ReadLines::Sync(napi_env env, napi_callback_info info) } if (funcArg.GetArgc() == NARG_CNT::TWO) { - auto [succOption, encoding] = GetReadLinesArg(env, funcArg[NARG_POS::SECOND]); - if (!succOption) { - HILOGE("Invalid encoding from JS first argument"); - NError(EINVAL).ThrowErr(env); - return nullptr; + int ret = CheckOptionArg(env, funcArg[NARG_POS::SECOND]); + if (ret) { + HILOGE("Invalid option.encoding parameter"); + NError(ret).ThrowErr(env); + return nullptr; } } - void *iterator = ::ReaderIterator(path.get()); + auto iterator = ::ReaderIterator(path.get()); if (iterator == nullptr) { - HILOGE("Failed to read lines of the file, error:%{public}d", errno); + HILOGE("Failed to read lines of the file, error: %{public}d", errno); NError(errno).ThrowErr(env); return nullptr; } -- Gitee