diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index e1b4ec79a7837bb2e19c2be01a7509a00bf41500..d3138ae37344f4ec5c28d125297947cf7443da86 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -110,6 +110,7 @@ ohos_shared_library("fs") { "${src_path}/common", "${src_path}/common/file_helper", "${src_path}/mod_fs", + "${utils_path}/common/include", "//third_party/libuv/include", ] @@ -163,6 +164,8 @@ ohos_shared_library("fs") { "ipc:ipc_core", "samgr:samgr_proxy", ] + + use_exceptions = true } ohos_shared_library("hash") { diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index 4ab935a2418ce1acc5a6889aaa42380bb5d5f758..7623d84ad430e75407150e93491daa1f9c817718 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -22,6 +22,7 @@ #include #include +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "filemgmt_libn.h" #include "../common_func.h" @@ -195,7 +196,12 @@ napi_value FileNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - auto rafEntity = make_unique(); + auto rafEntity = CreateUniquePtr(); + if (rafEntity == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(rafEntity))) { HILOGE("Failed to set file entity"); NError(EIO).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp index 0b2baa572bc9466f4a4c6c997dd1f080bf576537..30ae48f94bc0358d6499d9d6d65bd32186641f0c 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp @@ -21,6 +21,7 @@ #include #include +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -234,7 +235,12 @@ napi_value StatNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr statEntity = make_unique(); + auto statEntity = CreateUniquePtr(); + if (statEntity == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(statEntity))) { HILOGE("Failed to set stat entity"); NError(EINVAL).ThrowErr(env); 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 5bfd00d7a1f34fe5639b9ac55be2b7119bd787a9..6ecdf56b28d10cbbf66dab9af01ad6683b615bf2 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 @@ -25,6 +25,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "flush.h" #include "stream_entity.h" @@ -169,8 +170,8 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) return nullptr; } - auto arg = make_shared(move(bufGuard)); - if (!arg) { + auto arg = CreateSharedPtr(move(bufGuard)); + if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -235,8 +236,8 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) return nullptr; } - auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); - if (!arg) { + auto arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::FIRST])); + if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -329,7 +330,12 @@ napi_value StreamNExporter::Constructor(napi_env env, napi_callback_info cbInfo) return nullptr; } - unique_ptr streamEntity = make_unique(); + auto streamEntity = CreateUniquePtr(); + if (streamEntity == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(streamEntity))) { HILOGE("INNER BUG. Failed to wrap entity for obj stream"); NError(EIO).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index abc57862652955bd52a9766f2c9cf4a797dbaa8e..a1988cbebe6849b9088c21bfde63a443937417b1 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -21,6 +21,7 @@ #include #include "../common_func.h" +#include "file_utils.h" #include "filemgmt_libn.h" #include "filemgmt_libhilog.h" #include "securec.h" @@ -37,7 +38,12 @@ napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr watcherEntity = make_unique(); + auto watcherEntity = CreateUniquePtr(); + if (watcherEntity == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(watcherEntity))) { NError(EIO).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index f5f999312217c2fd17b2b8ab99b94a128a91c2ff..b3c28919618a7a41bc0f05a6f97ec1d746aebf6b 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -87,7 +87,6 @@ static tuple GetActualLen(napi_env env, size_t bufLen, size_t bufO unsigned int CommonFunc::ConvertJsFlags(unsigned int &flags) { - static constexpr unsigned int usrReadOnly = 00; static constexpr unsigned int usrWriteOnly = 01; static constexpr unsigned int usrReadWrite = 02; static constexpr unsigned int usrCreate = 0100; @@ -99,8 +98,8 @@ unsigned int CommonFunc::ConvertJsFlags(unsigned int &flags) static constexpr unsigned int usrNoFollowed = 0400000; static constexpr unsigned int usrSynchronous = 04010000; + // default value is usrReadOnly 00 unsigned int flagsABI = 0; - flagsABI |= ((flags & usrReadOnly) == usrReadOnly) ? O_RDONLY : 0; flagsABI |= ((flags & usrWriteOnly) == usrWriteOnly) ? O_WRONLY : 0; flagsABI |= ((flags & usrReadWrite) == usrReadWrite) ? O_RDWR : 0; flagsABI |= ((flags & usrCreate) == usrCreate) ? O_CREAT : 0; 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 51744a99174b323e4b0d828559e6fe052424765e..77ecd5ea533d0875c47bda0cbfc9537999a626d0 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -23,6 +23,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -80,9 +81,9 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) HILOGE("Failed to open srcFile with ret: %{public}d", ret); return NError(errno); } - srcFile.fdg = make_unique(ret, true); - if (!srcFile.fdg) { - HILOGE("Failed to request heap memory for src file descriptor."); + srcFile.fdg = CreateUniquePtr(ret, true); + if (srcFile.fdg == nullptr) { + HILOGE("Failed to request heap memory."); return NError(ENOMEM); } } @@ -105,9 +106,9 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) HILOGE("Failed to open destFile with ret: %{public}d", ret); return NError(errno); } - destFile.fdg = make_unique(ret, true); - if (!destFile.fdg) { - HILOGE("Failed to request heap memory for dest file descriptor."); + destFile.fdg = CreateUniquePtr(ret, true); + if (destFile.fdg == nullptr) { + HILOGE("Failed to request heap memory."); return NError(ENOMEM); } } @@ -136,7 +137,12 @@ static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg auto [isFd, fd] = pathOrFdFromJsArg.ToInt32(); if (isFd) { - auto fdg = make_unique(fd, false); + auto fdg = CreateUniquePtr(fd, false); + if (fdg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; + } return { true, FileInfo { false, {}, move(fdg) } }; } @@ -207,7 +213,13 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) return nullptr; } - auto cbExec = [para = make_shared(move(src), move(dest))]() -> NError { + auto para = CreateSharedPtr(move(src), move(dest)); + if (para == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + auto cbExec = [para]() -> NError { if (para->src_.isPath && para->dest_.isPath) { return IsAllPath(para->src_, para->dest_); } diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 4925c50edf006f49f1a930e00c95b821095b2330..16329aff9e099dc82c28e3f8ef6ccbb02231d277 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -23,6 +23,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "uv.h" @@ -60,8 +61,8 @@ static tuple, unique_ptr, int> ParseAndCheckJsO return { false, nullptr, nullptr, 0 }; } int mode = 0; - bool resGetThirdArg = false; if (funcArg.GetArgc() >= NARG_CNT::THREE) { + bool resGetThirdArg = false; tie(resGetThirdArg, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(mode); if (!resGetThirdArg || (mode < COPYMODE_MIN || mode > COPYMODE_MAX)) { HILOGE("Invalid mode"); @@ -278,7 +279,7 @@ napi_value CopyDir::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + auto arg = CreateSharedPtr(); if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp index 5ea0beb50ae40bddbf8a9e74309e1259fa516582..b2ac7d695ff4aa58eb5bd451e4b8857a34f01283 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -21,6 +21,7 @@ #include "class_stream/stream_entity.h" #include "class_stream/stream_n_exporter.h" #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -85,8 +86,8 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); - if (!arg) { + auto arg = CreateSharedPtr(); + if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp index eee88be73082119802619b83ae1d78e17725b36f..4029464edb7c624e33ddab79d506628f7671b2bb 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp @@ -21,6 +21,7 @@ #include "class_stream/stream_entity.h" #include "class_stream/stream_n_exporter.h" #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -85,8 +86,8 @@ napi_value FdopenStream::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); - if (!arg) { + shared_ptr arg = CreateSharedPtr(); + if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index c3af337f7fee810b4912af23f542a9bc6e863c18..3311f6d97c6ed7f6793166f811d4fb214e624885 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -23,6 +23,7 @@ #include #include +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -364,13 +365,12 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); - if (!arg) { + auto arg = CreateSharedPtr(); + if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [arg, optionArgsTmp]() -> NError { g_optionArgs = optionArgsTmp; int ret = 0; diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index 1391bba8a2ab4dadb90ccaa74810e51110dc9ddc..407b59515ada01059b0ba5a69e392f4e66e9c05b 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -20,6 +20,7 @@ #include "class_stat/stat_entity.h" #include "class_stat/stat_n_exporter.h" #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -60,6 +61,24 @@ napi_value Lstat::Sync(napi_env env, napi_callback_info info) return stat; } +static NError LstatExec(shared_ptr arg, string path) +{ + std::unique_ptr lstat_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!lstat_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_lstat(nullptr, lstat_req.get(), path.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to get stat of file by path: %{public}s, ret: %{public}d", path.c_str(), ret); + return NError(errno); + } else { + arg->stat_ = lstat_req->statbuf; + return NError(ERRNO_NOERR); + } +} + napi_value Lstat::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -77,22 +96,14 @@ napi_value Lstat::Async(napi_env env, napi_callback_info info) } string path = tmp.get(); - auto arg = make_shared(); + auto arg = CreateSharedPtr(); + if (arg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, path]() -> NError { - std::unique_ptr lstat_req = { - new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!lstat_req) { - HILOGE("Failed to request heap memory."); - return NError(ENOMEM); - } - int ret = uv_fs_lstat(nullptr, lstat_req.get(), path.c_str(), nullptr); - if (ret < 0) { - HILOGE("Failed to get stat of file by path: %{public}s, ret: %{public}d", path.c_str(), ret); - return NError(errno); - } else { - arg->stat_ = lstat_req->statbuf; - return NError(ERRNO_NOERR); - } + return LstatExec(arg, path); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp index 532e7818095a0f0ff77a786aea6d6ab2bfc84961..13c22b6d45ed2193695077a6633b079000ffbb33 100755 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -16,6 +16,7 @@ #include "mkdtemp.h" #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -58,6 +59,24 @@ napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info) return NVal::CreateUTF8String(env, mkdtemp_req->path).val_; } +static NError MkdTempExec(shared_ptr arg, string path) +{ + std::unique_ptr mkdtemp_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!mkdtemp_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), path.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to create a temporary directory with path"); + return NError(errno); + } else { + *arg = mkdtemp_req->path; + return NError(ERRNO_NOERR); + } +} + napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -74,24 +93,15 @@ napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + auto arg = CreateSharedPtr(); + if (arg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [path = string(tmp.get()), arg]() -> NError { - std::unique_ptr mkdtemp_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!mkdtemp_req) { - HILOGE("Failed to request heap memory."); - return NError(ENOMEM); - } - int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), path.c_str(), nullptr); - if (ret < 0) { - HILOGE("Failed to create a temporary directory with path"); - return NError(errno); - } else { - *arg = mkdtemp_req->path; - return NError(ERRNO_NOERR); - } + return MkdTempExec(arg, path); }; - auto cbComplete = [arg](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 01c0afb07ad65382319abfdb6fd091537ff4bb18..aeb71a8d9ca42534c547c5a55f0ab12212d2355c 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -24,6 +24,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "uv.h" @@ -319,8 +320,7 @@ napi_value MoveDir::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - - auto arg = make_shared(); + auto arg = CreateSharedPtr(); if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index b99b361bed15560e43da798b8b922e3fff5e88c4..ea5f82bc17b3f9e676481fce3173438669f0a2dd 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -26,6 +26,7 @@ #include "class_file/file_n_exporter.h" #include "common_func.h" #include "datashare_helper.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "ipc_skeleton.h" #include "iservice_registry.h" @@ -100,7 +101,12 @@ static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) } return NVal(); } - auto fdg = make_unique(fd, false); + auto fdg = CreateUniquePtr(fd, false); + if (fdg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return NVal(); + } fileEntity->fd_.swap(fdg); if (isUri) { fileEntity->path_ = ""; @@ -264,7 +270,12 @@ napi_value Open::Async(napi_env env, napi_callback_info info) if (!succMode) { return nullptr; } - auto arg = make_shared(); + auto arg = CreateSharedPtr(); + if (arg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto argv = funcArg[NARG_POS::FIRST]; auto cbExec = [arg, argv, path = string(path.get()), mode = mode, env = env]() -> NError { string pathStr = path; 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 f9c145ac92c392ea704c99b94aae788d5b5fa87f..b7573c89e465c9dc86a27f44e0a60dbd485f9063 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 @@ -31,6 +31,7 @@ #include "create_stream.h" #include "fdatasync.h" #include "fdopen_stream.h" +#include "file_utils.h" #include "filemgmt_libn.h" #include "fsync.h" #include "js_native_api.h" @@ -107,8 +108,8 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) return nullptr; } - auto result = make_shared(); - if (!result) { + auto result = CreateSharedPtr(); + if (result == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -398,13 +399,12 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); - if (!arg) { + auto arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::SECOND])); + if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [arg, buf, len, fd, offset]() -> NError { return ReadExec(arg, static_cast(buf), len, fd, offset); }; @@ -475,13 +475,12 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(move(bufGuard)); - if (!arg) { + auto arg = CreateSharedPtr(move(bufGuard)); + if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [arg, buf, len, 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 0436ae4bd99df9b729c991a2d9836efadc905d86..262f52da0dbf8a3cdd5f82e7e6ccc268dc93cc41 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -21,6 +21,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -210,8 +211,12 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - - auto arg = make_shared(NVal(env, funcArg.GetThisVar())); + auto arg = CreateSharedPtr(NVal(env, funcArg.GetThisVar())); + if (arg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [path = string(path.get()), arg, offset = offset, hasLen = hasLen, len = len]() -> NError { return ReadTextAsync(path, arg, offset, hasLen, len); }; diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index d68e7cfd299f6b71d7c9058f2d8cc104f6793046..2d60b89c2dcb1a5ec39d3f61b12cbc3b115d3d38 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -20,6 +20,7 @@ #include "class_stat/stat_entity.h" #include "class_stat/stat_n_exporter.h" #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -39,8 +40,8 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - auto fdg = make_unique(fd, false); - if (!fdg) { + auto fdg = CreateUniquePtr(fd, false); + if (fdg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; @@ -52,6 +53,24 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs return { false, FileInfo { false, {}, {} } }; }; +static NError CheckFsStat(const FileInfo &fileInfo, uv_fs_t* req) +{ + if (fileInfo.isPath) { + int ret = uv_fs_stat(nullptr, req, fileInfo.path.get(), nullptr); + if (ret < 0) { + HILOGE("Failed to stat file with path"); + return NError(errno); + } + } else { + int ret = uv_fs_fstat(nullptr, req, fileInfo.fdg->GetFD(), nullptr); + if (ret < 0) { + HILOGE("Failed to stat file with fd"); + return NError(errno); + } + } + return NError(ERRNO_NOERR); +} + napi_value Stat::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -72,20 +91,10 @@ napi_value Stat::Sync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - if (fileInfo.isPath) { - int ret = uv_fs_stat(nullptr, stat_req.get(), fileInfo.path.get(), nullptr); - if (ret < 0) { - HILOGE("Failed to stat file with path"); - NError(errno).ThrowErr(env); - return nullptr; - } - } else { - int ret = uv_fs_fstat(nullptr, stat_req.get(), fileInfo.fdg->GetFD(), nullptr); - if (ret < 0) { - HILOGE("Failed to stat file with fd"); - NError(errno).ThrowErr(env); - return nullptr; - } + auto err = CheckFsStat(fileInfo, stat_req.get()); + if (err) { + err.ThrowErr(env); + return nullptr; } auto stat = CommonFunc::InstantiateStat(env, stat_req->statbuf).val_; @@ -104,8 +113,12 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) if (!succ) { return nullptr; } - - auto arg = make_shared(); + auto arg = CreateSharedPtr(); + if (arg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, fileInfo = make_shared(move(fileInfo))]() -> NError { std::unique_ptr stat_req = { new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -113,18 +126,9 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) HILOGE("Failed to request heap memory."); return NError(ENOMEM); } - if (fileInfo->isPath) { - int ret = uv_fs_stat(nullptr, stat_req.get(), fileInfo->path.get(), nullptr); - if (ret < 0) { - HILOGE("Failed to stat file with path"); - return NError(errno); - } - } else { - int ret = uv_fs_fstat(nullptr, stat_req.get(), fileInfo->fdg->GetFD(), nullptr); - if (ret < 0) { - HILOGE("Failed to stat file with fd"); - return NError(errno); - } + auto err = CheckFsStat(*fileInfo, stat_req.get()); + if (err) { + return err; } arg->stat_ = stat_req->statbuf; return NError(ERRNO_NOERR); diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index a67ce8911b5db6d57aa911944701c161171c1893..695b6a45c327685fcd5f31da09b3b58d69be6cf2 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -20,6 +20,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -38,8 +39,8 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - auto fdg = make_unique(fd, false); - if (!fdg) { + auto fdg = CreateUniquePtr(fd, false); + if (fdg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 45e72c26484236bcce03978698f346201ec53dc4..abb167ec757f2031f9da189002878fa3cf3872af 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -21,6 +21,7 @@ #include #include #include "ipc_skeleton.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "tokenid_kit.h" #include "../class_watcher/watcher_entity.h" @@ -38,6 +39,23 @@ namespace { } } +static tuple, napi_value, NError> CreateAndCheckForWatcherEntity(napi_env env, int fd) +{ + auto watcherPtr = CreateSharedPtr(); + if (watcherPtr == nullptr) { + HILOGE("Failed to request heap memory."); + return {nullptr, nullptr, NError(ENOMEM)}; + } + if (!watcherPtr->InitNotify(fd)) { + return {watcherPtr, nullptr, NError(errno)}; + } + napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {}); + if (!objWatcher) { + return {watcherPtr, nullptr, NError(EINVAL)}; + } + return {watcherPtr, objWatcher, NError(ERRNO_NOERR)}; +} + napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) { if (!IsSystemApp()) { @@ -64,15 +82,9 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) } int fd = -1; - shared_ptr watcherPtr = make_shared(); - if (!watcherPtr->InitNotify(fd)) { - NError(errno).ThrowErr(env); - return nullptr; - } - - napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {}); - if (!objWatcher) { - NError(EINVAL).ThrowErr(env); + auto [watcherPtr, objWatcher, err] = CreateAndCheckForWatcherEntity(env, fd); + if (watcherPtr == nullptr || !objWatcher) { + err.ThrowErr(env); return nullptr; } @@ -86,8 +98,12 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - - watcherEntity->data_ = std::make_unique(NVal(env, funcArg[NARG_POS::THIRD])); + watcherEntity->data_ = CreateUniquePtr(NVal(env, funcArg[NARG_POS::THIRD])); + if (watcherEntity->data_ == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } watcherEntity->data_->events = events; watcherEntity->data_->env = env; watcherEntity->data_->filename = string(filename.get()); diff --git a/utils/common/include/file_utils.h b/utils/common/include/file_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..c6c4b73dbece3a38cd04eef15ecec96aa9af2256 --- /dev/null +++ b/utils/common/include/file_utils.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILEMGMT_COMMON_FILE_UTILS_H +#define FILEMGMT_COMMON_FILE_UTILS_H + +#include + +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement { +template +std::shared_ptr CreateSharedPtr(Args&&... args) +{ + std::shared_ptr sPtr = nullptr; + try { + sPtr = std::make_shared(std::forward(args)...); + } catch (const std::bad_alloc&) { + return nullptr; + } + return sPtr; +}; + +template +std::unique_ptr CreateUniquePtr(Args&&... args) +{ + std::unique_ptr uPtr = nullptr; + try { + uPtr = std::make_unique(std::forward(args)...); + } catch (const std::bad_alloc&) { + return nullptr; + } + return uPtr; +} + +} // namespace OHOS::FileManagement +#endif // FILEMGMT_COMMON_FILE_UTILS_H diff --git a/utils/filemgmt_libn/BUILD.gn b/utils/filemgmt_libn/BUILD.gn index f7dbe0706dbc5da9d7d84ce1e4fdf8d520c7b24b..09cff7b559d4739fab3794e4f8ac8c3d6c0b9226 100644 --- a/utils/filemgmt_libn/BUILD.gn +++ b/utils/filemgmt_libn/BUILD.gn @@ -20,6 +20,7 @@ config("libn_public_config") { include_dirs = [ "include", "include/n_async", + "${utils_path}/common/include", ] } @@ -40,6 +41,8 @@ ohos_shared_library("filemgmt_libn") { "${file_api_path}/utils/filemgmt_libhilog", ] + use_exceptions = true + subsystem_name = "filemanagement" part_name = "file_api" } diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 5e29440068f5bc824d961d4386eeb60034ed8f7e..6cc0ffb0bd7148d6e3acda5bd8deb95dfda89803 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -17,8 +17,9 @@ #include -#include "n_error.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" +#include "n_error.h" namespace OHOS { namespace FileManagement { @@ -63,11 +64,14 @@ tuple, size_t> NVal::ToUTF8String() const size_t strLen = 0; napi_status status = napi_get_value_string_utf8(env_, val_, nullptr, -1, &strLen); if (status != napi_ok) { - return {false, nullptr, 0}; + return { false, nullptr, 0 }; } size_t bufLen = strLen + 1; - unique_ptr str = make_unique(bufLen); + auto str = CreateUniquePtr(bufLen); + if (str == nullptr) { + return { false, nullptr, 0 }; + } status = napi_get_value_string_utf8(env_, val_, str.get(), bufLen, &strLen); return make_tuple(status == napi_ok, move(str), strLen); } @@ -75,9 +79,8 @@ tuple, size_t> NVal::ToUTF8String() const tuple, size_t> NVal::ToUTF8String(string defaultValue) const { if (TypeIs(napi_undefined) || TypeIs(napi_function)) { - unique_ptr str = make_unique(defaultValue.size() + 1); + auto str = CreateUniquePtr(defaultValue.size() + 1); if (str == nullptr) { - HILOGE("Failed to request heap memory"); return { false, nullptr, 0 }; } copy(defaultValue.begin(), defaultValue.end(), str.get()); @@ -93,13 +96,16 @@ tuple, size_t> NVal::ToUTF16String() const size_t strLen = 0; napi_status status = napi_get_value_string_utf16(env_, val_, nullptr, -1, &strLen); if (status != napi_ok) { - return {false, nullptr, 0}; + return { false, nullptr, 0 }; } - auto str = make_unique(++strLen); + auto str = CreateUniquePtr(++strLen); + if (str == nullptr) { + return { false, nullptr, 0 }; + } status = napi_get_value_string_utf16(env_, val_, str.get(), strLen, nullptr); if (status != napi_ok) { - return {false, nullptr, 0}; + return { false, nullptr, 0 }; } strLen = reinterpret_cast(str.get() + strLen) - reinterpret_cast(str.get());