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 bf11f36096fd4280b51e9c3c97054179dce56e9f..22b8385f8656ec7c8030c63aa82789b976ab789a 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/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 48904387cc78a04be8b9132464b9b637156ca6d2..542aaea07c3763afb611312d5a2369229a8096e4 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 @@ -510,9 +510,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); @@ -537,7 +535,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 da1693ca3a143e4a718072bb097b50bd643035e2..7354353bfb05b3e17c9d23146fe5a46c13034818 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,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(), buffer.size()).val_; } napi_value ReadText::Async(napi_env env, napi_callback_info info)