diff --git a/bundle.json b/bundle.json index 844f0f0644737a6a933071268ea929cd525a93e3..dbf37010436d05a9fb0fa3a8048b1a292aa0d541 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 4f420fad0ae5fb513c265717ce38f90ad7962c7a..ac97d9b9cc6893d0f093c1dc8528dd329a710afd 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", @@ -133,6 +135,7 @@ ohos_shared_library("fs") { "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" ] @@ -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 @@ -170,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 new file mode 100644 index 0000000000000000000000000000000000000000..de5f26af43abe1426309f9fb1595733d2b5ea7b9 --- /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 0000000000000000000000000000000000000000..eafcebee28262e9aa443d1482ee6806a00661394 --- /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 0000000000000000000000000000000000000000..53b79d6216b2f3c7fceb4a2a9bbb92bd9a8ed773 --- /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/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp index 7975920422e8b84624e2127539a618942486f2e3..dfff0f0911e5b2655cf57c662b95d9ca6f0035f8 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 @@ -42,6 +43,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/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 4bcccb9e494c0a9135ba33f412a162fdf5d9285d..e2821973f3304bd633f3dfc44759d3394e2140a0 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,13 +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" @@ -232,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; @@ -244,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); }; @@ -268,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_; } } @@ -279,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; @@ -292,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_; } @@ -567,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), @@ -587,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) @@ -607,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 new file mode 100644 index 0000000000000000000000000000000000000000..ec036b35719dec8fedce561bf2a4b9fcc1fb4ec9 --- /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 0000000000000000000000000000000000000000..244d86c852367fee075c7715596c5a75820fb50e --- /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