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 b631e7814f8b85fe989108a8a40499fd6a46cb2f..3f4994852d3b40dc33011a383cef020824a7d98a 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 da1693ca3a143e4a718072bb097b50bd643035e2..f1da4a5db74feac0fd4db20097ae2fc73438fce3 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,26 @@ static NError ReadTextAsync(const std::string &path, std::shared_ptr 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."); + 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); + return { ret, DistributedFS::FDGuard() }; + } + DistributedFS::FDGuard sfd(ret); + return { ERRNO_NOERR, std::move(sfd) }; +} + napi_value ReadText::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -141,22 +161,11 @@ 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 }; - if (!open_req) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); + auto [openRet, sfd] = OpenFile(path.get()); + if (openRet) { + NError(openRet).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)) { HILOGE("Failed to get stat of file by fd: %{public}d", sfd.GetFD()); @@ -180,7 +189,7 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - ret = uv_fs_read(nullptr, read_req.get(), sfd.GetFD(), &readbuf, 1, offset, 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);