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 2cc0aedb8bb538b62d16700b957f0c069ace1fba..44ba3246b0882cd4d07e7c4820e35eb141c1aa15 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 @@ -93,6 +93,7 @@ napi_value StreamNExporter::CloseSync(napi_env env, napi_callback_info info) return nullptr; } streamEntity->fp.reset(); + (void)NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); return NVal::CreateUndefined(env).val_; } @@ -174,7 +175,7 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info info) } auto arg = make_shared(move(bufGuard)); - if (arg == nullptr) { + if (!arg) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -241,7 +242,7 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info info) } auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); - if (arg == nullptr) { + if (!arg) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -309,7 +310,8 @@ napi_value StreamNExporter::Close(napi_env env, napi_callback_info info) } }; - auto cbCompl = [](napi_env env, NError err) -> NVal { + auto cbCompl = [arg = funcArg.GetThisVar()](napi_env env, NError err) -> NVal { + (void)NClass::RemoveEntityOfFinal(env, arg); if (err) { return { env, err.GetNapiErr(env) }; } else { diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index b6dd6cbb644e91ae233621dd60f6c9ece0a2398e..cd26696c719ee2cd44be250056b6b12ba6a0deb8 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -122,6 +122,15 @@ int CommonFunc::ConvertJsFlags(int &flags) return flagsABI; } +void CommonFunc::fs_req_cleanup(uv_fs_t* req) +{ + uv_fs_req_cleanup(req); + if (req) { + delete req; + req = nullptr; + } +} + tuple, unique_ptr> CommonFunc::GetCopyPathArg(napi_env env, napi_value srcPath, napi_value dstPath) diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 91a12a41eaf71853a6ad97d4fabd884bb97f75a5..272dd63107151efcacd6360beefc1cfc3de18ada 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -18,6 +18,7 @@ #include "fd_guard.h" #include "n_val.h" +#include "uv.h" namespace OHOS { namespace FileManagement { @@ -54,6 +55,7 @@ struct CommonFunc { static std::tuple, std::unique_ptr> GetCopyPathArg(napi_env env, napi_value srcPath, napi_value dstPath); + static void fs_req_cleanup(uv_fs_t* req); }; } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/properties/close.cpp b/interfaces/kits/js/src/mod_fs/properties/close.cpp index b7525c66016cf3c8e16d69a33185f88973d17bdd..195feb69da5d8f3bb4bdd4b46a19997a36e9a7c0 100755 --- a/interfaces/kits/js/src/mod_fs/properties/close.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/close.cpp @@ -18,8 +18,8 @@ #include #include #include -#include +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -73,7 +73,13 @@ napi_value Close::Sync(napi_env env, napi_callback_info info) } if (fileStruct.isFd) { - std::unique_ptr close_req = { new uv_fs_t, uv_fs_req_cleanup }; + std::unique_ptr close_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!close_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } int ret = uv_fs_close(nullptr, close_req.get(), fileStruct.fd, nullptr); if (ret < 0) { HILOGE("Failed to close file with fd: %{public}d, ret: %{public}d", fileStruct.fd, ret); @@ -83,6 +89,7 @@ napi_value Close::Sync(napi_env env, napi_callback_info info) } else { fileStruct.fileEntity->fd_.reset(); } + (void)NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); return NVal::CreateUndefined(env).val_; } @@ -105,7 +112,12 @@ napi_value Close::Async(napi_env env, napi_callback_info info) auto cbExec = [fileStruct = fileStruct]() -> NError { if (fileStruct.isFd) { - std::unique_ptr close_req = { new uv_fs_t, uv_fs_req_cleanup }; + std::unique_ptr close_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!close_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } int ret = uv_fs_close(nullptr, close_req.get(), fileStruct.fd, nullptr); if (ret < 0) { HILOGE("Failed to close file with ret: %{public}d", ret); @@ -117,7 +129,8 @@ napi_value Close::Async(napi_env env, napi_callback_info info) return NError(ERRNO_NOERR); }; - auto cbComplete = [](napi_env env, NError err) -> NVal { + auto cbComplete = [arg = funcArg.GetThisVar()](napi_env env, NError err) -> NVal { + (void)NClass::RemoveEntityOfFinal(env, arg); if (err) { return { env, err.GetNapiErr(env) }; } else { diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp index d64cf0dee82a4a5b61fd0f0d1da5d17d1a670369..029d1472b66d459d19cfeb528b76d5a675c58b07 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -22,8 +22,8 @@ #include #include #include -#include +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -34,7 +34,12 @@ using namespace OHOS::FileManagement::LibN; static NError IsAllPath(FileInfo& srcFile, FileInfo& destFile) { - std::unique_ptr copyfile_req = { new uv_fs_t, uv_fs_req_cleanup }; + std::unique_ptr copyfile_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!copyfile_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } int ret = uv_fs_copyfile(nullptr, copyfile_req.get(), srcFile.path.get(), destFile.path.get(), UV_FS_COPYFILE_FICLONE, nullptr); if (ret < 0) { @@ -47,14 +52,19 @@ static NError IsAllPath(FileInfo& srcFile, FileInfo& destFile) static NError SendFileCore(FileInfo& srcFile, FileInfo& destFile) { if (srcFile.isPath) { - std::unique_ptr open_req = { new uv_fs_t, uv_fs_req_cleanup }; + std::unique_ptr open_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!open_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } int ret = uv_fs_open(nullptr, open_req.get(), srcFile.path.get(), O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); if (ret < 0) { HILOGE("Failed to open srcFile with ret: %{public}d", ret); return NError(errno); } - srcFile.fdg.SetFD(open_req.get()->result, true); + srcFile.fdg.SetFD(ret, true); } struct stat statbf; @@ -64,16 +74,26 @@ static NError SendFileCore(FileInfo& srcFile, FileInfo& destFile) } if (destFile.isPath) { - std::unique_ptr open_req = { new uv_fs_t, uv_fs_req_cleanup }; + std::unique_ptr open_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!open_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } int ret = uv_fs_open(nullptr, open_req.get(), destFile.path.get(), O_RDWR | O_CREAT, statbf.st_mode, nullptr); if (ret < 0) { HILOGE("Failed to open destFile with ret: %{public}d", ret); return NError(errno); } - destFile.fdg.SetFD(open_req.get()->result, true); + destFile.fdg.SetFD(ret, true); } - std::unique_ptr sendfile_req = { new uv_fs_t, uv_fs_req_cleanup }; + std::unique_ptr sendfile_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!sendfile_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } int ret = uv_fs_sendfile(nullptr, sendfile_req.get(), srcFile.fdg.GetFD(), destFile.fdg.GetFD(), 0, statbf.st_size, nullptr); if (ret < 0) { diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp b/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp index 8def7211140a3cfa1e23895b68f14eee1d235d1d..f5dbc0f4f24b670de0a610e7c6e71abd6fcaf3ee 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp @@ -19,12 +19,10 @@ #include #include #include - #include +#include "common_func.h" #include "filemgmt_libhilog.h" -#include "uv.h" - namespace OHOS::FileManagement::ModuleFileIO { using namespace std; @@ -46,7 +44,13 @@ napi_value Fdatasync::Sync(napi_env env, napi_callback_info info) return nullptr; } - std::unique_ptr fdatasync_req = {new uv_fs_t, uv_fs_req_cleanup}; + std::unique_ptr fdatasync_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!fdatasync_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } int ret = uv_fs_fdatasync(nullptr, fdatasync_req.get(), fd, nullptr); if (ret < 0) { HILOGE("Failed to transfer data associated with file descriptor: %{public}d, ret:%{public}d", fd, ret); @@ -74,7 +78,12 @@ napi_value Fdatasync::Async(napi_env env, napi_callback_info info) } auto cbExec = [fd = fd]() -> NError { - std::unique_ptr fdatasync_req = {new uv_fs_t, uv_fs_req_cleanup}; + std::unique_ptr fdatasync_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!fdatasync_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } int ret = uv_fs_fdatasync(nullptr, fdatasync_req.get(), fd, nullptr); if (ret < 0) { HILOGE("Failed to transfer data associated with file descriptor: %{public}d", fd); diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync.cpp b/interfaces/kits/js/src/mod_fs/properties/fsync.cpp index 85405dfe4a9b0041d776f621b4a4a4d591623530..422389c64e1cab3f7c12d5d6e159f38f3c49a20e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fsync.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fsync.cpp @@ -19,8 +19,8 @@ #include #include +#include "common_func.h" #include "filemgmt_libhilog.h" -#include "uv.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace std; @@ -42,7 +42,13 @@ napi_value Fsync::Sync(napi_env env, napi_callback_info info) return nullptr; } - std::unique_ptr fsync_req = {new uv_fs_t, uv_fs_req_cleanup}; + std::unique_ptr fsync_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!fsync_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } int ret = uv_fs_fsync(nullptr, fsync_req.get(), fd, nullptr); if (ret < 0) { HILOGE("Failed to transfer data associated with file descriptor: %{public}d", fd); @@ -69,7 +75,12 @@ napi_value Fsync::Async(napi_env env, napi_callback_info info) } auto cbExec = [fd = fd]() -> NError { - std::unique_ptr fsync_req = {new uv_fs_t, uv_fs_req_cleanup}; + std::unique_ptr fsync_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!fsync_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } int ret = uv_fs_fsync(nullptr, fsync_req.get(), fd, nullptr); if (ret < 0) { HILOGE("Failed to transfer data associated with file descriptor: %{public}d", fd); diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp index ddc209a5489b6519a2b548b0e9185491df1f2ffd..43ab54a2b316e8cb2b2b9309d38c9f3b86bc4da5 100755 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -15,8 +15,7 @@ #include "mkdtemp.h" -#include - +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -42,7 +41,13 @@ napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info) } string path = tmp.get(); - std::unique_ptr mkdtemp_req = { new uv_fs_t, uv_fs_req_cleanup }; + std::unique_ptr mkdtemp_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!mkdtemp_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), const_cast(path.c_str()), nullptr); if (ret < 0) { HILOGE("Failed to create a temporary directory with path: %{public}s", path.c_str()); @@ -71,7 +76,12 @@ napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) auto arg = make_shared(); auto cbExec = [path = tmp.get(), arg]() -> NError { - std::unique_ptr mkdtemp_req = { new uv_fs_t, uv_fs_req_cleanup }; + std::unique_ptr mkdtemp_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!mkdtemp_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), const_cast(path), nullptr); if (ret < 0) { HILOGE("Failed to create a temporary directory with path: %{public}s", path); diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index ff16921a08348dd2286a5baf9bd805a918457ac6..bcd429fc8f3b2a3dfdfc0c4751466849d3001d61 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include "../common_func.h" #include "ability.h" @@ -122,15 +121,21 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) NError(-1).ThrowErr(env); return nullptr; } - std::unique_ptr open_req = { new uv_fs_t, uv_fs_req_cleanup }; - int ret = uv_fs_open(uv_default_loop(), open_req.get(), path.get(), mode, S_IRUSR | - S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, NULL); + 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(), mode, S_IRUSR | + S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); if (ret < 0) { HILOGE("Failed to open file for libuv error %{public}d", ret); NError(errno).ThrowErr(env); return nullptr; } - auto file = InstantiateFile(env, open_req.get()->result, path.get(), false).val_; + auto file = InstantiateFile(env, ret, path.get(), false).val_; return file; } @@ -173,14 +178,19 @@ napi_value Open::Async(napi_env env, napi_callback_info info) HILOGE("Failed to open file by Datashare"); return NError(-1); } - std::unique_ptr open_req = { new uv_fs_t, uv_fs_req_cleanup }; - int ret = uv_fs_open(uv_default_loop(), open_req.get(), path.c_str(), mode, S_IRUSR | - S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, NULL); + std::unique_ptr open_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!open_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } + int ret = uv_fs_open(nullptr, open_req.get(), path.c_str(), mode, S_IRUSR | + S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); if (ret < 0) { HILOGE("Failed to open file for libuv error %{public}d", ret); return NError(errno); } - arg->fd = open_req.get()->result; + arg->fd = ret; arg->path = path; arg->uri = ""; return NError(ERRNO_NOERR); 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 bd207cb4b4d0516de829fbf9c7021bf3a820d68f..00606c0e9aee65c32123ad709ff2581b944c65ea 100755 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include "common_func.h" #include "class_file/file_entity.h" @@ -68,8 +67,9 @@ napi_value PropNExporter::AccessSync(napi_env env, napi_callback_info info) } bool isAccess = false; - std::unique_ptr access_req = { new uv_fs_t, uv_fs_req_cleanup }; - if (access_req == nullptr) { + std::unique_ptr access_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!access_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -109,8 +109,9 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) return nullptr; } auto cbExec = [path = string(tmp.get()), result]() -> NError { - std::unique_ptr access_req = {new uv_fs_t, uv_fs_req_cleanup}; - if (access_req == nullptr) { + std::unique_ptr access_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!access_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); } @@ -154,8 +155,9 @@ napi_value PropNExporter::Unlink(napi_env env, napi_callback_info info) } auto cbExec = [path = string(tmp.get())]() -> NError { - std::unique_ptr unlink_req = { new uv_fs_t, uv_fs_req_cleanup }; - if (unlink_req == nullptr) { + std::unique_ptr unlink_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!unlink_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); } @@ -199,8 +201,9 @@ napi_value PropNExporter::UnlinkSync(napi_env env, napi_callback_info info) return nullptr; } - std::unique_ptr unlink_req = { new uv_fs_t, uv_fs_req_cleanup }; - if (unlink_req == nullptr) { + std::unique_ptr unlink_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!unlink_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -232,8 +235,9 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) } auto cbExec = [path = string(tmp.get())]() -> NError { - std::unique_ptr mkdir_req = { new uv_fs_t, uv_fs_req_cleanup }; - if (mkdir_req == nullptr) { + 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); } @@ -277,8 +281,9 @@ napi_value PropNExporter::MkdirSync(napi_env env, napi_callback_info info) return nullptr; } - std::unique_ptr mkdir_req = { new uv_fs_t, uv_fs_req_cleanup }; - if (mkdir_req == 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; @@ -303,16 +308,22 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) return nullptr; } - auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + bool succ = false; + int fd = 0; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, buf, len, hasOffset, offset] = + void *buf = nullptr; + int64_t len = 0; + bool hasOffset = false; + int64_t offset = 0; + tie(succ, buf, len, hasOffset, offset) = CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); - if (!res) { + if (!succ) { HILOGE("Failed to resolve buf and options"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -320,8 +331,9 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) ssize_t actLen; uv_buf_t buffer = uv_buf_init(static_cast(buf), len); - std::unique_ptr read_req = { new uv_fs_t, uv_fs_req_cleanup }; - if (read_req == nullptr) { + 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; @@ -332,15 +344,16 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) NError(errno).ThrowErr(env); return nullptr; } - actLen = read_req.get()->result; + actLen = ret; return NVal::CreateInt64(env, actLen).val_; } static NError ReadExec(shared_ptr arg, void *buf, size_t len, int fd, size_t offset) { uv_buf_t buffer = uv_buf_init(static_cast(buf), len); - std::unique_ptr read_req = { new uv_fs_t, uv_fs_req_cleanup }; - if (read_req == nullptr) { + std::unique_ptr read_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!read_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); } @@ -349,7 +362,7 @@ static NError ReadExec(shared_ptr arg, void *buf, size_t len, in HILOGE("Failed to read file for %{public}d", ret); return NError(errno); } - arg->lenRead = read_req.get()->result; + arg->lenRead = ret; return NError(ERRNO_NOERR); } @@ -362,23 +375,29 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) return nullptr; } - auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + bool succ = false; + void *buf = nullptr; + int64_t len = 0; + int fd = 0; + bool hasOffset = false; + int64_t offset = 0; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, buf, len, hasOffset, offset] = + tie(succ, buf, len, hasOffset, offset) = CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); - if (!res) { + if (!succ) { HILOGE("Failed to resolve buf and options"); NError(EINVAL).ThrowErr(env); return nullptr; } auto arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); - if (arg == nullptr) { + if (!arg) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -414,8 +433,9 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) NError PropNExporter::WriteExec(shared_ptr arg, void *buf, size_t len, int fd, size_t offset) { uv_buf_t buffer = uv_buf_init(static_cast(buf), len); - std::unique_ptr write_req = { new uv_fs_t, uv_fs_req_cleanup }; - if (write_req == nullptr) { + std::unique_ptr write_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!write_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); } @@ -424,7 +444,7 @@ NError PropNExporter::WriteExec(shared_ptr arg, void *buf, size HILOGE("Failed to write file for %{public}d", ret); return NError(errno); } - arg->actLen = write_req.get()->result; + arg->actLen = ret; return NError(ERRNO_NOERR); } @@ -437,23 +457,30 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) return nullptr; } - auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + bool succ = false; + int fd; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, bufGuard, buf, len, hasOffset, offset] = + unique_ptr bufGuard; + void *buf = nullptr; + size_t len = 0; + size_t offset = 0; + bool hasOffset = false; + tie(succ, bufGuard, buf, len, hasOffset, offset) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); - if (!res) { + if (!succ) { HILOGE("Failed to resolve buf and options"); NError(EINVAL).ThrowErr(env); return nullptr; } auto arg = make_shared(move(bufGuard)); - if (arg == nullptr) { + if (!arg) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -497,24 +524,32 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) return nullptr; } - auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + bool succ = false; + int fd; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, bufGuard, buf, len, hasOffset, offset] = + void *buf = nullptr; + size_t len = 0; + size_t offset = 0; + unique_ptr bufGuard; + bool hasOffset = false; + tie(succ, bufGuard, buf, len, hasOffset, offset) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); - if (!res) { + if (!succ) { HILOGE("Failed to resolve buf and options"); return nullptr; } ssize_t writeLen; uv_buf_t buffer = uv_buf_init(static_cast(buf), len); - std::unique_ptr write_req = { new uv_fs_t, uv_fs_req_cleanup }; - if (write_req == nullptr) { + std::unique_ptr write_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!write_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -525,7 +560,7 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) NError(errno).ThrowErr(env); return nullptr; } - writeLen = write_req.get()->result; + writeLen = ret; return NVal::CreateInt64(env, writeLen).val_; } 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 c812876b75a7d209804bddbea6918932fad1ba4c..5ff8590e2dd0cc0096be7767c6e3732d4dee845d 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include "common_func.h" #include "filemgmt_libhilog.h" @@ -80,7 +79,12 @@ static NError ReadTextAsync(const std::string path, std::shared_ptr open_req = { new uv_fs_t, uv_fs_req_cleanup }; + std::unique_ptr open_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!open_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } int ret = uv_fs_open(nullptr, open_req.get(), path.c_str(), O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); if (ret < 0) { @@ -88,7 +92,7 @@ static NError ReadTextAsync(const std::string path, std::shared_ptrresult); + sfd.SetFD(ret); if (sfd.GetFD() < 0) { HILOGE("Failed to open file by path"); return NError(errno); @@ -106,7 +110,12 @@ static NError ReadTextAsync(const std::string path, std::shared_ptr statbf.st_size) ? statbf.st_size : len; string buffer(len, '\0'); uv_buf_t readbuf = uv_buf_init(const_cast(buffer.c_str()), len); - std::unique_ptr read_req = { new uv_fs_t, uv_fs_req_cleanup }; + std::unique_ptr read_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!read_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } arg->len = uv_fs_read(nullptr, read_req.get(), sfd.GetFD(), &readbuf, 1, offset, nullptr); if (arg->len < 0) { HILOGE("Failed to read file by fd: %{public}d", sfd.GetFD()); @@ -139,7 +148,13 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) } OHOS::DistributedFS::FDGuard sfd; - std::unique_ptr open_req = { new uv_fs_t, uv_fs_req_cleanup }; + 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 | S_IROTH, nullptr); if (ret < 0) { @@ -147,7 +162,7 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) NError(errno).ThrowErr(env); return nullptr; } - sfd.SetFD(open_req.get()->result); + 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()); @@ -156,7 +171,7 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) } if (offset > statbf.st_size) { - HILOGE("Invalid offset: %{public}d", offset); + HILOGE("Invalid offset: %{public}zd", offset); NError(EINVAL).ThrowErr(env); return nullptr; } @@ -164,7 +179,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()), len); - std::unique_ptr read_req = { new uv_fs_t, uv_fs_req_cleanup }; + 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()); diff --git a/interfaces/kits/js/src/mod_fs/properties/rename.cpp b/interfaces/kits/js/src/mod_fs/properties/rename.cpp index 157649e3df1716062951377663732a12395286e2..dd05e02f83c4e233b216ab691ad3c31240ba6c2c 100755 --- a/interfaces/kits/js/src/mod_fs/properties/rename.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/rename.cpp @@ -18,8 +18,8 @@ #include #include #include -#include +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -51,8 +51,9 @@ napi_value Rename::Sync(napi_env env, napi_callback_info info) return nullptr; } - std::unique_ptr rename_req = { new uv_fs_t, uv_fs_req_cleanup }; - if (rename_req == nullptr) { + std::unique_ptr rename_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!rename_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -91,8 +92,9 @@ napi_value Rename::Async(napi_env env, napi_callback_info info) } auto cbExec = [opath = string(src.get()), npath = string(dest.get())]() -> NError { - std::unique_ptr rename_req = { new uv_fs_t, uv_fs_req_cleanup }; - if (rename_req == nullptr) { + std::unique_ptr rename_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!rename_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); } diff --git a/interfaces/kits/js/src/mod_fs/properties/symlink.cpp b/interfaces/kits/js/src/mod_fs/properties/symlink.cpp index 8412893c39b29128613634cbf5ac708a7603e988..0c21cc9a6eee15f012c3660522d4ee5c24a56484 100755 --- a/interfaces/kits/js/src/mod_fs/properties/symlink.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/symlink.cpp @@ -19,8 +19,8 @@ #include #include #include -#include +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -60,7 +60,13 @@ napi_value Symlink::Sync(napi_env env, napi_callback_info info) return nullptr; } - std::unique_ptr symlink_req = {new uv_fs_t, uv_fs_req_cleanup}; + std::unique_ptr symlink_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!symlink_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } int ret = uv_fs_symlink(nullptr, symlink_req.get(), oldPath.c_str(), newPath.c_str(), 0, nullptr); if (ret < 0) { HILOGE("Failed to create a link for old path"); @@ -88,7 +94,12 @@ napi_value Symlink::Async(napi_env env, napi_callback_info info) } auto cbExec = [oldPath = move(oldPath), newPath = move(newPath)]() -> NError { - std::unique_ptr symlink_req = {new uv_fs_t, uv_fs_req_cleanup}; + std::unique_ptr symlink_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!symlink_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } int ret = uv_fs_symlink(nullptr, symlink_req.get(), oldPath.c_str(), newPath.c_str(), 0, nullptr); if (ret < 0) { HILOGE("Failed to create a link for old path"); diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index 0c0e9db88b19213b96c1ec9e78643484cd60fe3a..f6eb9e5600269176754e1ce03c77f4776015831f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -21,7 +21,6 @@ #include "../common_func.h" #include "filemgmt_libhilog.h" -#include "uv.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace std; @@ -65,22 +64,40 @@ napi_value Truncate::Sync(napi_env env, napi_callback_info info) } } if (fileInfo.isPath) { - std::unique_ptr open_req = { new uv_fs_t, uv_fs_req_cleanup }; + 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(), fileInfo.path.get(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); if (ret < 0) { NError(errno).ThrowErr(env); return nullptr; } - std::unique_ptr ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; - ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), open_req.get()->result, truncateLen, nullptr); + std::unique_ptr ftruncate_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!ftruncate_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), ret, truncateLen, nullptr); if (ret < 0) { HILOGE("Failed to truncate file by path"); NError(errno).ThrowErr(env); return nullptr; } } else { - std::unique_ptr ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; + std::unique_ptr ftruncate_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!ftruncate_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } int ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), fileInfo.fdg.GetFD(), truncateLen, nullptr); if (ret < 0) { HILOGE("Failed to truncate file by fd"); @@ -113,23 +130,35 @@ napi_value Truncate::Async(napi_env env, napi_callback_info info) } } auto cbExec = [fileInfo = make_shared(move(fileInfo)), truncateLen, env = env]() -> NError { - using uv_fs_unique_ptr_type = std::unique_ptr; + using uv_fs_unique_ptr_type = std::unique_ptr; if (fileInfo->isPath) { - uv_fs_unique_ptr_type open_req = { new uv_fs_t, uv_fs_req_cleanup }; - int ret = uv_fs_open(uv_default_loop(), open_req.get(), fileInfo->path.get(), O_RDWR, + uv_fs_unique_ptr_type open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!open_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } + int ret = uv_fs_open(nullptr, open_req.get(), fileInfo->path.get(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); if (ret < 0) { return NError(errno); } - uv_fs_unique_ptr_type ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; - ret = uv_fs_ftruncate(uv_default_loop(), ftruncate_req.get(), open_req.get()->result, truncateLen, nullptr); + uv_fs_unique_ptr_type ftruncate_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!ftruncate_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } + ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), ret, truncateLen, nullptr); if (ret < 0) { HILOGE("Failed to truncate file by path"); return NError(errno); } } else { - uv_fs_unique_ptr_type ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; - int ret = uv_fs_ftruncate(uv_default_loop(), ftruncate_req.get(), fileInfo->fdg.GetFD(), truncateLen, nullptr); + uv_fs_unique_ptr_type ftruncate_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!ftruncate_req) { + HILOGE("Failed to request heap memory."); + return NError(ERRNO_NOERR); + } + int ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), fileInfo->fdg.GetFD(), truncateLen, nullptr); if (ret < 0) { HILOGE("Failed to truncate file by fd"); return NError(errno); diff --git a/utils/filemgmt_libn/include/n_class.h b/utils/filemgmt_libn/include/n_class.h index b4ec8c75a5cd791821e1abf213328ce8fe80e466..e82bffbe819a21157047b4cdce8aa230fc0e1640 100644 --- a/utils/filemgmt_libn/include/n_class.h +++ b/utils/filemgmt_libn/include/n_class.h @@ -68,6 +68,21 @@ public: return status == napi_ok; } + template static T *RemoveEntityOfFinal(napi_env env, napi_value objStat) + { + if (!env || !objStat) { + HILOGD("Empty input: env %d,obj %d", env == nullptr, objStat == nullptr); + return nullptr; + } + T *t = nullptr; + napi_status status = napi_remove_wrap(env, objStat, (void **)&t); + if (status != napi_ok) { + HILOGD("Cannot umwrap for pointer: %d", status); + return nullptr; + } + return t; + } + private: NClass() = default; ~NClass() = default;