From d36e5a59da546502007266cd1d116cbef422b92b Mon Sep 17 00:00:00 2001 From: gwx1278443 Date: Mon, 26 Feb 2024 10:05:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E6=A5=9A=E5=91=8A=E8=AD=A6?= 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 | 5 +- .../mod_fs/class_watcher/watcher_entity.cpp | 3 +- .../js/src/mod_fs/properties/read_text.cpp | 67 +++++++++++-------- .../src/n_async/n_async_work_callback.cpp | 4 +- 4 files changed, 45 insertions(+), 34 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..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 @@ -237,9 +237,8 @@ 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) { - HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); + if (fseek(streamEntity->fp.get(), static_cast(offset), SEEK_SET) < 0) { + HILOGE("Failed to set the offset location of the file stream pointer"); return NError(errno); } } diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp index fb66880bb..4a02867a5 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp @@ -162,7 +162,8 @@ void FileWatcher::GetNotifyEvent(WatcherCallback callback) break; } if (select(notifyFd_ + 1, &fds, nullptr, nullptr, 0) > 0) { - int len, index = 0; + int len; + int index = 0; while (((len = read(notifyFd_, &buf, sizeof(buf))) < 0) && (errno == EINTR)) {}; while (index < len) { event = reinterpret_cast(buf + index); 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 262f52da0..578b773de 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -37,7 +37,7 @@ static tuple> GetReadTextArg(na int64_t len = 0; bool succ = false; bool hasLen = false; - unique_ptr encoding; + unique_ptr encoding { new char[]{ "utf-8" } }; if (op.HasProp("offset") && !op.GetProp("offset").TypeIs(napi_undefined)) { tie(succ, offset) = op.GetProp("offset").ToInt64(); @@ -119,6 +119,32 @@ static NError ReadTextAsync(const std::string &path, std::shared_ptr open_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup + }; + if (open_req == nullptr) { + HILOGE("Failed to request heap memory."); + return -ENOMEM; + } + + return uv_fs_open(nullptr, open_req.get(), path.c_str(), O_RDONLY, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); +} + +static int ReadFromFile(int fd, int64_t offset, string& buffer) +{ + uv_buf_t readbuf = uv_buf_init(const_cast(buffer.c_str()), static_cast(buffer.size())); + std::unique_ptr read_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (read_req == nullptr) { + HILOGE("Failed to request heap memory."); + return -ENOMEM; + } + return uv_fs_read(nullptr, read_req.get(), fd, &readbuf, 1, offset, nullptr); +} + napi_value ReadText::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -142,21 +168,14 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) } OHOS::DistributedFS::FDGuard sfd; - 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 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); + int fd = OpenFile(path.get()); + if (fd < 0) { + HILOGE("Failed to open file by ret: %{public}d", fd); + NError(fd).ThrowErr(env); return nullptr; } - sfd.SetFD(ret); + sfd.SetFD(fd); + struct stat statbf; if ((!sfd) || (fstat(sfd.GetFD(), &statbf) < 0)) { HILOGE("Failed to get stat of file by fd: %{public}d", sfd.GetFD()); @@ -172,22 +191,14 @@ 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); - if (ret < 0) { - HILOGE("Failed to read file by fd: %{public}d", sfd.GetFD()); - NError(errno).ThrowErr(env); + int readRet = ReadFromFile(sfd.GetFD(), offset, buffer); + if (readRet < 0) { + HILOGE("Failed to read file by fd: %{public}d", fd); + NError(readRet).ThrowErr(env); return nullptr; } - return NVal::CreateUTF8String(env, readbuf.base, ret).val_; + return NVal::CreateUTF8String(env, buffer.c_str(), readRet).val_; } napi_value ReadText::Async(napi_env env, napi_callback_info info) @@ -240,4 +251,4 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) } } // namespace ModuleFileIO } // namespace FileManagement -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp b/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp index 3df058b85..84e5ab885 100644 --- a/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp +++ b/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp @@ -45,7 +45,7 @@ NAsyncWorkCallback::~NAsyncWorkCallback() return; } - unique_ptr work(new (std::nothrow) uv_work_t); + std::unique_ptr work = std::make_unique(); if (work == nullptr) { HILOGE("Failed to new uv_work_t"); return; @@ -195,7 +195,7 @@ void NAsyncWorkCallback::ThreadSafeSchedule(NContextCBComplete cbComplete) return; } - unique_ptr work(new (std::nothrow) uv_work_t); + std::unique_ptr work = std::make_unique(); if (!work) { HILOGE("Failed to new uv_work_t"); return; -- Gitee