From 812587159f5cd21c719cb2b17729d1c687995b50 Mon Sep 17 00:00:00 2001 From: gwx1278443 Date: Thu, 30 Nov 2023 10:56:43 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=A4=A7=E5=87=BD=E6=95=B0=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gwx1278443 --- .../mod_fs/class_stream/stream_n_exporter.cpp | 78 +++++++++++------- .../mod_fs/class_stream/stream_n_exporter.h | 8 ++ .../src/mod_fs/properties/prop_n_exporter.cpp | 6 +- .../js/src/mod_fs/properties/read_text.cpp | 80 ++++++++++++------- 4 files changed, 110 insertions(+), 62 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index bf11f3609..a4fa561d4 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -215,62 +215,78 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) return WriteExec(env, funcArg, streamEntity); } -static napi_value ReadExec(napi_env env, NFuncArg &funcArg, StreamEntity *streamEntity) +static bool PerformRead(napi_env env, NFuncArg &funcArg, StreamEntity *streamEntity, ReadArgs &readArgs) { - auto [succ, buf, len, offset] = - CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + auto [succ, rawBuf, len, offset] = CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { HILOGE("Failed to resolve buf and options"); NError(EINVAL).ThrowErr(env); - return nullptr; + return false; } - auto arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::FIRST])); - if (arg == nullptr) { + readArgs.arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::FIRST])); + if (readArgs.arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); - return nullptr; + return false; } - auto cbExec = [arg, buf = buf, len = len, streamEntity, offset = offset]() -> NError { - if (!streamEntity || !streamEntity->fp.get()) { - HILOGE("Stream has been closed in read cbExec possibly"); - return NError(EIO); - } - if (offset >= 0) { - int ret = fseek(streamEntity->fp.get(), static_cast(offset), SEEK_SET); - if (ret < 0) { - HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); - return NError(errno); - } - } - size_t actLen = fread(buf, 1, len, streamEntity->fp.get()); - if ((actLen != static_cast(len) && !feof(streamEntity->fp.get())) || ferror(streamEntity->fp.get())) { - HILOGE("Invalid buffer size and pointer, actlen: %{public}zu", actLen); - return NError(EIO); - } else { - arg->lenRead = actLen; - return NError(ERRNO_NOERR); + + readArgs.buf.resize(len); + readArgs.len = len; + readArgs.offset = offset; + + if (!streamEntity || !streamEntity->fp.get()) { + HILOGE("Stream has been closed in read cbExec possibly"); + return false; + } + + if (offset >= 0) { + if (fseek(streamEntity->fp.get(), static_cast(offset), SEEK_SET) < 0) { + HILOGE("Failed to set the offset location of the file stream pointer"); + NError(errno).ThrowErr(env); + return false; } - }; + } - auto cbCompl = [arg](napi_env env, NError err) -> NVal { + readArgs.actLen = fread(&readArgs.buf[0], 1, len, streamEntity->fp.get()); + bool incompleteRead = readArgs.actLen != static_cast(len) && !feof(streamEntity->fp.get()); + if (incompleteRead || ferror(streamEntity->fp.get())) { + HILOGE("Invalid buffer size and pointer, actlen: %{public}zu", readArgs.actLen); + NError(EIO).ThrowErr(env); + return false; + } + + readArgs.buf.resize(readArgs.actLen); + return true; +} + + +static napi_value ReadExec(napi_env env, NFuncArg &funcArg, StreamEntity *streamEntity) +{ + ReadArgs readArgs; + if (!PerformRead(env, funcArg, streamEntity, readArgs)) { + return nullptr; + } + + auto cbCompl = [&readArgs](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } - return { NVal::CreateInt64(env, arg->lenRead) }; + return { NVal::CreateInt64(env, readArgs.actLen) }; }; 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))) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STREAM_READ_NAME, cbExec, cbCompl).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STREAM_READ_NAME, nullptr, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); NVal cb(env, funcArg[cbIdx]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_STREAM_READ_NAME, cbExec, cbCompl).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_STREAM_READ_NAME, nullptr, cbCompl).val_; } } + napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) { NFuncArg funcArg(env, cbInfo); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h index c68c246ad..906a7f041 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h @@ -51,6 +51,14 @@ struct AsyncReadArg { ~AsyncReadArg() = default; }; +struct ReadArgs { + std::shared_ptr arg; + std::string buf; + size_t len; + off_t offset; + size_t actLen; +}; + struct AsyncWriteArg { NRef refWriteArrayBuf; std::unique_ptr guardWriteStr = nullptr; 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 b631e7814..3f4994852 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 @@ -509,9 +509,7 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) return nullptr; } - bool succ = false; - int32_t fd = 0; - tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + auto[succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ || fd < 0) { HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); @@ -536,7 +534,7 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [arg, buf, len, fd, offset]() -> NError { + auto cbExec = [arg, buf, len, fd = fd, offset]() -> NError { return WriteExec(arg, static_cast(buf), len, fd, offset); }; diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp index da1693ca3..85e6c593d 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -119,6 +119,53 @@ static NError ReadTextAsync(const std::string &path, std::shared_ptr, + OHOS::DistributedFS::FDGuard> OpenFile(napi_env env, const std::string& path) +{ + std::unique_ptr open_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup + }; + if (!open_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return std::make_tuple(std::move(open_req), OHOS::DistributedFS::FDGuard()); + } + + int ret = uv_fs_open(nullptr, open_req.get(), path.c_str(), O_RDONLY, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); + if (ret < 0) { + HILOGE("Failed to open file by ret: %{public}d", ret); + NError(errno).ThrowErr(env); + return std::make_tuple(std::move(open_req), OHOS::DistributedFS::FDGuard()); + } + OHOS::DistributedFS::FDGuard sfd(ret); + return {std::move(open_req), std::move(sfd)}; +} + +std::tuple ReadFromFile(napi_env env, OHOS::DistributedFS::FDGuard& sfd, int64_t offset, int64_t len) +{ + std::string buffer(len, '\0'); + uv_buf_t readbuf = uv_buf_init(const_cast(buffer.c_str()), static_cast(len)); + + std::unique_ptr read_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup + }; + if (!read_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return std::make_tuple(-1, ""); + } + + int ret = uv_fs_read(nullptr, read_req.get(), sfd.GetFD(), &readbuf, 1, offset, nullptr); + if (ret < 0) { + HILOGE("Failed to read file by fd: %{public}d", sfd.GetFD()); + NError(errno).ThrowErr(env); + return std::make_tuple(-1, ""); + } + + return std::make_tuple(ret, buffer); +} + napi_value ReadText::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -141,24 +188,13 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) return nullptr; } - OHOS::DistributedFS::FDGuard sfd; - std::unique_ptr open_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + auto [open_req, sfd] = OpenFile(env, path.get()); if (!open_req) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); return nullptr; } - int ret = uv_fs_open(nullptr, open_req.get(), path.get(), O_RDONLY, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); - if (ret < 0) { - HILOGE("Failed to open file by ret: %{public}d", ret); - NError(errno).ThrowErr(env); - return nullptr; - } - sfd.SetFD(ret); + struct stat statbf; - if ((!sfd) || (fstat(sfd.GetFD(), &statbf) < 0)) { + if (fstat(sfd.GetFD(), &statbf) < 0) { HILOGE("Failed to get stat of file by fd: %{public}d", sfd.GetFD()); NError(errno).ThrowErr(env); return nullptr; @@ -171,23 +207,13 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) } len = (!hasLen || len > statbf.st_size) ? statbf.st_size : len; - string buffer(len, '\0'); - uv_buf_t readbuf = uv_buf_init(const_cast(buffer.c_str()), static_cast(len)); - std::unique_ptr read_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!read_req) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } - ret = uv_fs_read(nullptr, read_req.get(), sfd.GetFD(), &readbuf, 1, offset, nullptr); + + auto [ret, buffer] = ReadFromFile(env, sfd, offset, len); if (ret < 0) { - HILOGE("Failed to read file by fd: %{public}d", sfd.GetFD()); - NError(errno).ThrowErr(env); return nullptr; } - return NVal::CreateUTF8String(env, readbuf.base, ret).val_; + return NVal::CreateUTF8String(env, buffer.c_str(), ret).val_; } napi_value ReadText::Async(napi_env env, napi_callback_info info) -- Gitee From b6668f70e0719ecdf0b932f7d5ab05f365408ee1 Mon Sep 17 00:00:00 2001 From: gwx1278443 Date: Tue, 5 Dec 2023 16:33:09 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=A4=A7=E5=87=BD=E6=95=B0=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gwx1278443 --- .../mod_fs/class_stream/stream_n_exporter.cpp | 85 +++++++++---------- .../mod_fs/class_stream/stream_n_exporter.h | 8 -- .../js/src/mod_fs/properties/read_text.cpp | 61 +++++-------- 3 files changed, 60 insertions(+), 94 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index a4fa561d4..e9ed22ef2 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -215,78 +215,69 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) return WriteExec(env, funcArg, streamEntity); } -static bool PerformRead(napi_env env, NFuncArg &funcArg, StreamEntity *streamEntity, ReadArgs &readArgs) +static tuple PrepareReadArgs(napi_env env, NFuncArg &funcArg) { - auto [succ, rawBuf, len, offset] = CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + auto [succ, buf, len, offset] = CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { HILOGE("Failed to resolve buf and options"); - NError(EINVAL).ThrowErr(env); - return false; - } - - readArgs.arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::FIRST])); - if (readArgs.arg == nullptr) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return false; - } - - readArgs.buf.resize(len); - readArgs.len = len; - readArgs.offset = offset; - - if (!streamEntity || !streamEntity->fp.get()) { - HILOGE("Stream has been closed in read cbExec possibly"); - return false; + return { EINVAL, nullptr, 0, 0 }; } - - if (offset >= 0) { - if (fseek(streamEntity->fp.get(), static_cast(offset), SEEK_SET) < 0) { - HILOGE("Failed to set the offset location of the file stream pointer"); - NError(errno).ThrowErr(env); - return false; - } - } - - readArgs.actLen = fread(&readArgs.buf[0], 1, len, streamEntity->fp.get()); - bool incompleteRead = readArgs.actLen != static_cast(len) && !feof(streamEntity->fp.get()); - if (incompleteRead || ferror(streamEntity->fp.get())) { - HILOGE("Invalid buffer size and pointer, actlen: %{public}zu", readArgs.actLen); - NError(EIO).ThrowErr(env); - return false; - } - - readArgs.buf.resize(readArgs.actLen); - return true; + return { ERRNO_NOERR, buf, len, offset }; } - static napi_value ReadExec(napi_env env, NFuncArg &funcArg, StreamEntity *streamEntity) { - ReadArgs readArgs; - if (!PerformRead(env, funcArg, streamEntity, readArgs)) { + auto [argRet, buf, len, offset] = PrepareReadArgs(env, funcArg); + if (argRet) { + NError(argRet).ThrowErr(env); return nullptr; } + auto arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::FIRST])); + if (arg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + auto cbExec = [arg, buf = buf, len = len, streamEntity, offset = offset]() -> NError { + if (!streamEntity || !streamEntity->fp.get()) { + HILOGE("Stream has been closed in read cbExec possibly"); + return NError(EIO); + } + if (offset >= 0) { + int ret = fseek(streamEntity->fp.get(), static_cast(offset), SEEK_SET); + if (ret < 0) { + HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); + return NError(errno); + } + } + size_t actLen = fread(buf, 1, len, streamEntity->fp.get()); + if ((actLen != static_cast(len) && !feof(streamEntity->fp.get())) || ferror(streamEntity->fp.get())) { + HILOGE("Invalid buffer size and pointer, actlen: %{public}zu", actLen); + return NError(EIO); + } else { + arg->lenRead = actLen; + return NError(ERRNO_NOERR); + } + }; - auto cbCompl = [&readArgs](napi_env env, NError err) -> NVal { + auto cbCompl = [arg](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } - return { NVal::CreateInt64(env, readArgs.actLen) }; + return { NVal::CreateInt64(env, arg->lenRead) }; }; 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))) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STREAM_READ_NAME, nullptr, cbCompl).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STREAM_READ_NAME, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); NVal cb(env, funcArg[cbIdx]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_STREAM_READ_NAME, nullptr, cbCompl).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_STREAM_READ_NAME, cbExec, cbCompl).val_; } } - napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) { NFuncArg funcArg(env, cbInfo); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h index 906a7f041..c68c246ad 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h @@ -51,14 +51,6 @@ struct AsyncReadArg { ~AsyncReadArg() = default; }; -struct ReadArgs { - std::shared_ptr arg; - std::string buf; - size_t len; - off_t offset; - size_t actLen; -}; - struct AsyncWriteArg { NRef refWriteArrayBuf; std::unique_ptr guardWriteStr = nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp index 85e6c593d..f1da4a5db 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -119,51 +119,24 @@ static NError ReadTextAsync(const std::string &path, std::shared_ptr, - OHOS::DistributedFS::FDGuard> OpenFile(napi_env env, const std::string& path) +static tuple OpenFile(const std::string& path) { std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return std::make_tuple(std::move(open_req), OHOS::DistributedFS::FDGuard()); + return { ENOMEM, DistributedFS::FDGuard() }; } int ret = uv_fs_open(nullptr, open_req.get(), path.c_str(), O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); if (ret < 0) { HILOGE("Failed to open file by ret: %{public}d", ret); - NError(errno).ThrowErr(env); - return std::make_tuple(std::move(open_req), OHOS::DistributedFS::FDGuard()); + return { ret, DistributedFS::FDGuard() }; } - OHOS::DistributedFS::FDGuard sfd(ret); - return {std::move(open_req), std::move(sfd)}; -} - -std::tuple ReadFromFile(napi_env env, OHOS::DistributedFS::FDGuard& sfd, int64_t offset, int64_t len) -{ - std::string buffer(len, '\0'); - uv_buf_t readbuf = uv_buf_init(const_cast(buffer.c_str()), static_cast(len)); - - std::unique_ptr read_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup - }; - if (!read_req) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return std::make_tuple(-1, ""); - } - - int ret = uv_fs_read(nullptr, read_req.get(), sfd.GetFD(), &readbuf, 1, offset, nullptr); - if (ret < 0) { - HILOGE("Failed to read file by fd: %{public}d", sfd.GetFD()); - NError(errno).ThrowErr(env); - return std::make_tuple(-1, ""); - } - - return std::make_tuple(ret, buffer); + DistributedFS::FDGuard sfd(ret); + return { ERRNO_NOERR, std::move(sfd) }; } napi_value ReadText::Sync(napi_env env, napi_callback_info info) @@ -188,13 +161,13 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) return nullptr; } - auto [open_req, sfd] = OpenFile(env, path.get()); - if (!open_req) { + auto [openRet, sfd] = OpenFile(path.get()); + if (openRet) { + NError(openRet).ThrowErr(env); return nullptr; } - struct stat statbf; - if (fstat(sfd.GetFD(), &statbf) < 0) { + if ((!sfd) || (fstat(sfd.GetFD(), &statbf) < 0)) { HILOGE("Failed to get stat of file by fd: %{public}d", sfd.GetFD()); NError(errno).ThrowErr(env); return nullptr; @@ -207,13 +180,23 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) } len = (!hasLen || len > statbf.st_size) ? statbf.st_size : len; - - auto [ret, buffer] = ReadFromFile(env, sfd, offset, len); + string buffer(len, '\0'); + uv_buf_t readbuf = uv_buf_init(const_cast(buffer.c_str()), static_cast(len)); + std::unique_ptr read_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!read_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + int ret = uv_fs_read(nullptr, read_req.get(), sfd.GetFD(), &readbuf, 1, offset, nullptr); if (ret < 0) { + HILOGE("Failed to read file by fd: %{public}d", sfd.GetFD()); + NError(errno).ThrowErr(env); return nullptr; } - return NVal::CreateUTF8String(env, buffer.c_str(), ret).val_; + return NVal::CreateUTF8String(env, readbuf.base, ret).val_; } napi_value ReadText::Async(napi_env env, napi_callback_info info) -- Gitee From 93e74bd360264defb077cc38cbf83055db6565fc Mon Sep 17 00:00:00 2001 From: shuaiguo Date: Fri, 8 Dec 2023 08:19:05 +0000 Subject: [PATCH 3/5] update interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp. Signed-off-by: shuaiguo --- .../mod_fs/class_stream/stream_n_exporter.cpp | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index e9ed22ef2..6554d65f5 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -215,23 +215,16 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) return WriteExec(env, funcArg, streamEntity); } -static tuple PrepareReadArgs(napi_env env, NFuncArg &funcArg) +static napi_value ReadExec(napi_env env, NFuncArg &funcArg, StreamEntity *streamEntity) { - auto [succ, buf, len, offset] = CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + auto [succ, buf, len, offset] = + CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { HILOGE("Failed to resolve buf and options"); - return { EINVAL, nullptr, 0, 0 }; - } - return { ERRNO_NOERR, buf, len, offset }; -} - -static napi_value ReadExec(napi_env env, NFuncArg &funcArg, StreamEntity *streamEntity) -{ - auto [argRet, buf, len, offset] = PrepareReadArgs(env, funcArg); - if (argRet) { - NError(argRet).ThrowErr(env); + NError(EINVAL).ThrowErr(env); return nullptr; } + auto arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::FIRST])); if (arg == nullptr) { HILOGE("Failed to request heap memory."); @@ -244,8 +237,7 @@ static napi_value ReadExec(napi_env env, NFuncArg &funcArg, StreamEntity *stream return NError(EIO); } if (offset >= 0) { - int ret = fseek(streamEntity->fp.get(), static_cast(offset), SEEK_SET); - if (ret < 0) { + if (fseek(streamEntity->fp.get(), static_cast(offset), SEEK_SET) < 0) { HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); return NError(errno); } -- Gitee From 51ca792fcae48b0297c3c1c4a52fc94b2e48bd3a Mon Sep 17 00:00:00 2001 From: shuaiguo Date: Fri, 8 Dec 2023 10:16:56 +0000 Subject: [PATCH 4/5] update interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp. Signed-off-by: shuaiguo --- .../kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 6554d65f5..66acb5ebb 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -64,7 +64,7 @@ napi_value StreamNExporter::ReadSync(napi_env env, napi_callback_info cbInfo) if (offset >= 0) { int ret = fseek(filp, static_cast(offset), SEEK_SET); if (ret < 0) { - HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); + HILOGE("Failed to set the offset location of the file stream pointer"); NError(errno).ThrowErr(env); return nullptr; } -- Gitee From f00d6c11d1177430cc21c4f0bb208847853639f5 Mon Sep 17 00:00:00 2001 From: shuaiguo Date: Fri, 8 Dec 2023 10:17:47 +0000 Subject: [PATCH 5/5] update interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp. Signed-off-by: shuaiguo --- .../kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 66acb5ebb..22b8385f8 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -64,7 +64,7 @@ napi_value StreamNExporter::ReadSync(napi_env env, napi_callback_info cbInfo) if (offset >= 0) { int ret = fseek(filp, static_cast(offset), SEEK_SET); if (ret < 0) { - HILOGE("Failed to set the offset location of the file stream pointer"); + HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); NError(errno).ThrowErr(env); return nullptr; } @@ -238,7 +238,7 @@ static napi_value ReadExec(napi_env env, NFuncArg &funcArg, StreamEntity *stream } if (offset >= 0) { if (fseek(streamEntity->fp.get(), static_cast(offset), SEEK_SET) < 0) { - HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); + HILOGE("Failed to set the offset location of the file stream pointer"); return NError(errno); } } -- Gitee