diff --git a/interfaces/kits/hyperaio/include/hyperaio.h b/interfaces/kits/hyperaio/include/hyperaio.h index f7f64104cec9fa78cd531fc15862354d3572c36a..d13b9ef9b4add5cc2486f76e11ebfbbf9b10967f 100644 --- a/interfaces/kits/hyperaio/include/hyperaio.h +++ b/interfaces/kits/hyperaio/include/hyperaio.h @@ -97,6 +97,9 @@ private: std::atomic stopThread_ = true; std::atomic initialized_ = false; void HarvestRes(); + void HandleRequestError(std::vector &errorVec, int32_t errorcode); + void HandleSqeError(uint32_t count, std::vector &infoVec); + int32_t CheckParameter(uint32_t reqNum); }; } } diff --git a/interfaces/kits/hyperaio/src/hyperaio.cpp b/interfaces/kits/hyperaio/src/hyperaio.cpp index 780b2179831afeba5764a7b33150ddaa34e0ae9a..112649c5ff67e48123ee8f85bc2a3e7113fd976d 100644 --- a/interfaces/kits/hyperaio/src/hyperaio.cpp +++ b/interfaces/kits/hyperaio/src/hyperaio.cpp @@ -114,45 +114,92 @@ int32_t HyperAio::CtxInit(ProcessIoResultCallBack *callBack) return EOK; } -int32_t HyperAio::StartOpenReqs(OpenReqs *req) +void HyperAio::HandleRequestError(std::vector &errorVec, int32_t errorcode) { - if (pImpl_ == nullptr) { - return -EINVAL; + if (errorVec.empty()) { + HILOGE("errorVec is empty"); + return; } - if (req == nullptr || req->reqs == nullptr) { + for (auto &userdata : errorVec) { + HILOGE("HandleRequestError: userData = %{private}lu", userdata); + auto response = std::make_unique(userdata, errorcode, 0); + ioResultCallBack_(std::move(response)); + } + errorVec.clear(); +} + +void HyperAio::HandleSqeError(uint32_t count, std::vector &infoVec) +{ + if (count > 0) { + int32_t ret = io_uring_submit(&pImpl_->uring_); + if (ret < 0) { + HILOGE("submit read reqs failed, ret = %{public}d", ret); + HandleRequestError(infoVec, ret); + } + readReqCount_ += count; + } +} + +int32_t HyperAio::CheckParameter(uint32_t reqNum) +{ + if (pImpl_ == nullptr) { + HILOGE("pImpl is not initialized"); return -EINVAL; } if (!initialized_.load()) { HILOGE("HyperAio is not initialized"); return -EPERM; } - if (!ValidateReqNum(req->reqNum)) { - HILOGE("reqNum is out of range: %{public}u", req->reqNum); + if (!ValidateReqNum(reqNum)) { + HILOGE("reqNum is out of range: %{public}u", reqNum); return -EINVAL; } + return EOK; +} + +int32_t HyperAio::StartOpenReqs(OpenReqs *req) +{ + if (req == nullptr || req->reqs == nullptr) { + HILOGE("the request is empty"); + return -EINVAL; + } + + int32_t ret = CheckParameter(req->reqNum); + if (ret < 0) { + return ret; + } + HyperaioTrace trace("StartOpenReqs" + std::to_string(req->reqNum)); uint32_t totalReqs = req->reqNum; uint32_t count = 0; + std::vector errorVec; + std::vector openInfoVec; for (uint32_t i = 0; i < totalReqs; i++) { struct io_uring_sqe *sqe = GetSqeWithRetry(&pImpl_->uring_); if (sqe == nullptr) { HILOGE("get sqe failed"); - return -ENOMEM; + for (; i < totalReqs; ++i) { + errorVec.push_back(req->reqs[i].userData); + } + HandleSqeError(count, openInfoVec); + HandleRequestError(errorVec, -EBUSY); + break; } struct OpenInfo *openInfo = &req->reqs[i]; io_uring_sqe_set_data(sqe, reinterpret_cast(openInfo->userData)); io_uring_prep_openat(sqe, openInfo->dfd, static_cast(openInfo->path), openInfo->flags, openInfo->mode); - HILOGD("open flags = %{public}d, mode = %{public}u, userData = %{public}lu", + HILOGD("open flags = %{public}d, mode = %{public}u, userData = %{private}lu", openInfo->flags, openInfo->mode, openInfo->userData); HyperaioTrace trace("open flags:" + std::to_string(openInfo->flags) + "mode:" + std::to_string(openInfo->mode) + "userData:" + std::to_string(openInfo->userData)); count++; + openInfoVec.push_back(openInfo->userData); if (count >= BATCH_SIZE || i == totalReqs - 1) { int32_t ret = io_uring_submit(&pImpl_->uring_); if (ret < 0) { HILOGE("submit open reqs failed, ret = %{public}d", ret); - return ret; + HandleRequestError(openInfoVec, -EBUSY); } openReqCount_ += count; count = 0; @@ -163,42 +210,45 @@ int32_t HyperAio::StartOpenReqs(OpenReqs *req) int32_t HyperAio::StartReadReqs(ReadReqs *req) { - if (pImpl_ == nullptr) { - return -EINVAL; - } if (req == nullptr || req->reqs == nullptr) { + HILOGE("the request is empty"); return -EINVAL; } - if (!initialized_.load()) { - HILOGE("HyperAio is not initialized"); - return -EPERM; - } - if (!ValidateReqNum(req->reqNum)) { - HILOGE("reqNum is out of range: %{public}u", req->reqNum); - return -EINVAL; + + int32_t ret = CheckParameter(req->reqNum); + if (ret < 0) { + return ret; } HyperaioTrace trace("StartReadReqs" + std::to_string(req->reqNum)); uint32_t totalReqs = req->reqNum; uint32_t count = 0; + std::vector errorVec; + std::vector readInfoVec; for (uint32_t i = 0; i < totalReqs; i++) { struct io_uring_sqe *sqe = GetSqeWithRetry(&pImpl_->uring_); if (sqe == nullptr) { HILOGE("get sqe failed"); - return -ENOMEM; + for (; i < totalReqs; ++i) { + errorVec.push_back(req->reqs[i].userData); + } + HandleSqeError(count, readInfoVec); + HandleRequestError(errorVec, -EBUSY); + break; } struct ReadInfo *readInfo = &req->reqs[i]; io_uring_sqe_set_data(sqe, reinterpret_cast(readInfo->userData)); io_uring_prep_read(sqe, readInfo->fd, readInfo->buf, readInfo->len, readInfo->offset); - HILOGD("read len = %{public}u, offset = %{public}lu, userData = %{public}lu", + HILOGD("read len = %{public}u, offset = %{public}lu, userData = %{private}lu", readInfo->len, readInfo->offset, readInfo->userData); HyperaioTrace trace("read len:" + std::to_string(readInfo->len) + "offset:" + std::to_string(readInfo->offset) + "userData:" + std::to_string(readInfo->userData)); count++; + readInfoVec.push_back(readInfo->userData); if (count >= BATCH_SIZE || i == totalReqs - 1) { int32_t ret = io_uring_submit(&pImpl_->uring_); if (ret < 0) { HILOGE("submit read reqs failed, ret = %{public}d", ret); - return ret; + HandleRequestError(readInfoVec, -EBUSY); } readReqCount_ += count; count = 0; @@ -209,42 +259,45 @@ int32_t HyperAio::StartReadReqs(ReadReqs *req) int32_t HyperAio::StartCancelReqs(CancelReqs *req) { - if (pImpl_ == nullptr) { - return -EINVAL; - } if (req == nullptr || req->reqs == nullptr) { + HILOGE("the request is empty"); return -EINVAL; } - if (!initialized_.load()) { - HILOGE("HyperAio is not initialized"); - return -EPERM; - } - if (!ValidateReqNum(req->reqNum)) { - HILOGE("reqNum is out of range: %{public}u", req->reqNum); - return -EINVAL; + + int32_t ret = CheckParameter(req->reqNum); + if (ret < 0) { + return ret; } HyperaioTrace trace("StartCancelReqs" + std::to_string(req->reqNum)); uint32_t totalReqs = req->reqNum; uint32_t count = 0; + std::vector errorVec; + std::vector cancelInfoVec; for (uint32_t i = 0; i < totalReqs; i++) { struct io_uring_sqe *sqe = GetSqeWithRetry(&pImpl_->uring_); if (sqe == nullptr) { HILOGE("get sqe failed"); - return -ENOMEM; + for (; i < totalReqs; ++i) { + errorVec.push_back(req->reqs[i].userData); + } + HandleSqeError(count, cancelInfoVec); + HandleRequestError(errorVec, -EBUSY); + break; } struct CancelInfo *cancelInfo = &req->reqs[i]; io_uring_sqe_set_data(sqe, reinterpret_cast(cancelInfo->userData)); io_uring_prep_cancel(sqe, reinterpret_cast(cancelInfo->targetUserData), 0); - HILOGD("cancel userData = %{public}lu, targetUserData = %{public}lu", + HILOGD("cancel userData = %{private}lu, targetUserData = %{private}lu", cancelInfo->userData, cancelInfo->targetUserData); HyperaioTrace trace("cancel userData:" + std::to_string(cancelInfo->userData) + "targetUserData:" + std::to_string(cancelInfo->targetUserData)); count++; + cancelInfoVec.push_back(cancelInfo->userData); if (count >= BATCH_SIZE || i == totalReqs - 1) { int32_t ret = io_uring_submit(&pImpl_->uring_); if (ret < 0) { HILOGE("submit cancel reqs failed, ret = %{public}d", ret); - return ret; + HandleRequestError(cancelInfoVec, -EBUSY); } cancelReqCount_ += count; count = 0; diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp index 4a2350c680a66d92614a4fdafd33bd535f2153f2..b13d1a2edbe019b662e1e10d801ac271e7bb715f 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp @@ -357,7 +357,7 @@ napi_value RandomAccessFileNExporter::Write(napi_env env, napi_callback_info inf static NError CloseFd(int fd) { std::unique_ptr close_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!close_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp index 8642ae512c49e0ee34a4ab8183302ecdcd01ed09..d8dc3811fd5d2a8b27829a797252db050c197b46 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp @@ -91,7 +91,7 @@ ani_object ReaderIteratorAni::Next(ani_env *env, [[maybe_unused]] ani_object obj } auto nextRet = ret.GetData().value(); - auto result = ReaderIteratorResultAni::Wrap(env, &nextRet); + auto result = ReaderIteratorResultAni::Wrap(env, nextRet); if (result == nullptr) { ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp index b8efe806e026de0b7c54f2b29aab3d76007120ac..299832ea92def89aedea3589024ed78d40cfafa3 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp @@ -28,13 +28,8 @@ using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; -ani_object ReaderIteratorResultAni::Wrap(ani_env *env, const ReaderIteratorResult *result) +ani_object ReaderIteratorResultAni::Wrap(ani_env *env, const ReaderIteratorResult &result) { - if (result == nullptr) { - HILOGE("ReaderIteratorResult pointer is null!"); - return nullptr; - } - auto classDesc = FS::ReaderIteratorResultInner::classDesc.c_str(); ani_class cls; if (ANI_OK != env->FindClass(classDesc, &cls)) { @@ -50,21 +45,18 @@ ani_object ReaderIteratorResultAni::Wrap(ani_env *env, const ReaderIteratorResul return nullptr; } - ani_long ptr = static_cast(reinterpret_cast(result)); ani_object obj; - if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) { + if (ANI_OK != env->Object_New(cls, ctor, &obj)) { HILOGE("New %s obj Failed!", classDesc); return nullptr; } - const auto &done = result->done; - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "done", static_cast(done))) { + if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "done", static_cast(result.done))) { HILOGE("Set 'done' field value failed!"); return nullptr; } - const auto &value = result->value; - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "value", value)) { + if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "value", result.value)) { HILOGE("Set 'value' field value failed!"); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.h b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.h index 31bf73e9cef76868f054bdc0b5d3158ef2cb7606..81af29c16976afd069de27e7e4943c7d1e352096 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.h +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.h @@ -27,7 +27,7 @@ namespace ANI { class ReaderIteratorResultAni final { public: - static ani_object Wrap(ani_env *env, const ReaderIteratorResult *result); + static ani_object Wrap(ani_env *env, const ReaderIteratorResult &result); }; } // namespace ANI } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/properties/close.cpp b/interfaces/kits/js/src/mod_fs/properties/close.cpp index 0ca2fe8e5ffc57ff0b3f53cc72717c7e13d01c8a..d0aea72a5681e83def7af8d42597ea16a7602bff 100755 --- a/interfaces/kits/js/src/mod_fs/properties/close.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/close.cpp @@ -45,7 +45,7 @@ static FileEntity *GetFileEntity(napi_env env, napi_value objFile) static NError CloseFd(int fd) { std::unique_ptr close_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!close_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); diff --git a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp index 3d7672b47d62f6aa73c10294bca188b7beec4709..2c04155dd56125bf5a6992bb0b487305639016d2 100644 --- a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp @@ -185,7 +185,7 @@ napi_value CreateRandomAccessFile::Sync(napi_env env, napi_callback_info info) return nullptr; } std::unique_ptr open_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); @@ -218,7 +218,11 @@ NError AsyncExec(shared_ptr arg, { if (fileInfo->isPath) { std::unique_ptr open_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!open_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } int ret = uv_fs_open(nullptr, open_req.get(), fileInfo->path.get(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, NULL); 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 94ace2d8e8b390aafed757acf966bcdaf0bc3849..05eed610e047e9e02cac9df1e540a988edad2494 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp @@ -45,7 +45,7 @@ napi_value Fdatasync::Sync(napi_env env, napi_callback_info info) } std::unique_ptr fdatasync_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!fdatasync_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); @@ -79,7 +79,7 @@ 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, CommonFunc::fs_req_cleanup }; + new (nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!fdatasync_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync.cpp b/interfaces/kits/js/src/mod_fs/properties/fsync.cpp index a475546e0f3d7017cac02f69454eafedc652f911..a5bccb922e3ac28a8406a945cd31e2cb27450c5a 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fsync.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fsync.cpp @@ -43,7 +43,7 @@ napi_value Fsync::Sync(napi_env env, napi_callback_info info) } std::unique_ptr fsync_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!fsync_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); @@ -76,7 +76,7 @@ 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, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!fsync_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp index c304e4a6724b9dc96e85f268b289a4ded0a36d25..a61d9e78ec5e1b50ec52450dadf9f1b55f3d9bfa 100755 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -43,7 +43,7 @@ napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info) string path = tmp.get(); std::unique_ptr mkdtemp_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!mkdtemp_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); @@ -62,7 +62,7 @@ napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info) static NError MkdTempExec(shared_ptr arg, const string &path) { std::unique_ptr mkdtemp_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!mkdtemp_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 31eee911cb138102da4573430895eb83e6c39769..e68a9594d66d0aab2df96b4cf270eb0b969cc736 100644 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -210,7 +210,7 @@ static int RecurMoveDir(const string &srcPath, const string &destPath, const int return ERRNO_NOERR; } - unique_ptr ptr = {new struct NameListArg, Deleter}; + unique_ptr ptr = { new (std::nothrow) struct NameListArg, Deleter }; if (!ptr) { HILOGE("Failed to request heap memory."); return ENOMEM; diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 7b5280059ddbd6bea3b4cdcd5608e45565439d64..c9b3d2c993fcebd692752abc613702dc23427700 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -136,7 +136,7 @@ static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri, static int OpenFileByPath(const string &path, unsigned int mode) { unique_ptr open_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); return -ENOMEM; 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 74502ebf6b9b13559fd9458b33f37176ab9cd15b..cb0f8c52e23b482cd4c3d01dcc3d3a356d65ea28 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 @@ -104,8 +104,8 @@ struct AccessArgs { static int UvAccess(const string &path, int mode) { - std::unique_ptr access_req = {new uv_fs_t, - CommonFunc::fs_req_cleanup}; + std::unique_ptr access_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup}; if (!access_req) { HILOGE("Failed to request heap memory."); return ENOMEM; @@ -338,7 +338,7 @@ 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, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!unlink_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); @@ -384,7 +384,7 @@ napi_value PropNExporter::UnlinkSync(napi_env env, napi_callback_info info) } std::unique_ptr unlink_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!unlink_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); @@ -403,7 +403,7 @@ napi_value PropNExporter::UnlinkSync(napi_env env, napi_callback_info info) static int MkdirCore(const string &path) { std::unique_ptr mkdir_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!mkdir_req) { HILOGE("Failed to request heap memory."); return ENOMEM; @@ -562,7 +562,7 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) uv_buf_t buffer = uv_buf_init(static_cast(buf), static_cast(len)); std::unique_ptr read_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!read_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); @@ -582,7 +582,7 @@ static NError ReadExec(shared_ptr arg, char *buf, size_t len, in { uv_buf_t buffer = uv_buf_init(buf, len); std::unique_ptr read_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!read_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); @@ -660,7 +660,7 @@ static NError WriteExec(shared_ptr arg, char *buf, size_t len, { uv_buf_t buffer = uv_buf_init(buf, static_cast(len)); std::unique_ptr write_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!write_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); @@ -762,7 +762,7 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) uv_buf_t buffer = uv_buf_init(static_cast(buf), static_cast(len)); std::unique_ptr write_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!write_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); 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 d77d5601a6f5ca44edc1fd177a4ce04b8d99fb84..dcb46febfd6c236e6fa938d701b7d0009911dd8d 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,11 @@ static tuple> GetReadTextArg(na int64_t len = 0; bool succ = false; bool hasLen = false; - unique_ptr encoding { new char[]{ "utf-8" } }; + unique_ptr encoding { new (std::nothrow) char[]{ "utf-8" } }; + if (!encoding) { + HILOGE("Failed to request heap memory."); + return { false, offset, hasLen, len, nullptr }; + } if (op.HasProp("offset") && !op.GetProp("offset").TypeIs(napi_undefined)) { tie(succ, offset) = op.GetProp("offset").ToInt64(); @@ -74,7 +78,7 @@ static NError ReadTextAsync(const std::string &path, std::shared_ptr open_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); @@ -105,7 +109,7 @@ static NError ReadTextAsync(const std::string &path, std::shared_ptr(buffer.c_str()), static_cast(len)); std::unique_ptr read_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!read_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); @@ -122,7 +126,7 @@ static NError ReadTextAsync(const std::string &path, std::shared_ptr open_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (open_req == nullptr) { HILOGE("Failed to request heap memory."); @@ -137,7 +141,7 @@ 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 }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (read_req == nullptr) { HILOGE("Failed to request heap memory."); return -ENOMEM; diff --git a/interfaces/kits/js/src/mod_fs/properties/rename.cpp b/interfaces/kits/js/src/mod_fs/properties/rename.cpp index c71258c943288635d398b23c42c4ac1bf57af68f..620f032a74e67201f2263e2e22156ad67fdc47d8 100755 --- a/interfaces/kits/js/src/mod_fs/properties/rename.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/rename.cpp @@ -52,7 +52,7 @@ napi_value Rename::Sync(napi_env env, napi_callback_info info) } std::unique_ptr rename_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!rename_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); @@ -93,7 +93,7 @@ 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, CommonFunc::fs_req_cleanup }; + new (std::nothrow) 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 a83e8e18796a6670e5c5209a06ad43b0d48a6bd0..49e73d31a42d47218bfa82f01e5eb4b6b750031a 100755 --- a/interfaces/kits/js/src/mod_fs/properties/symlink.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/symlink.cpp @@ -61,7 +61,7 @@ napi_value Symlink::Sync(napi_env env, napi_callback_info info) } std::unique_ptr symlink_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!symlink_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); @@ -95,7 +95,7 @@ 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, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!symlink_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index 1fafa81d055b15b861472abf5dfdfac1eaf835fb..acefbf6d54eb88266f9b590a78edaa8ad5d3991e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -53,7 +53,7 @@ static NError TruncateCore(napi_env env, FileInfo &fileInfo, int64_t truncateLen { if (fileInfo.isPath) { std::unique_ptr open_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); @@ -66,7 +66,7 @@ static NError TruncateCore(napi_env env, FileInfo &fileInfo, int64_t truncateLen } FDGuard fd(ret); std::unique_ptr ftruncate_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!ftruncate_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); @@ -78,7 +78,7 @@ static NError TruncateCore(napi_env env, FileInfo &fileInfo, int64_t truncateLen } } else { std::unique_ptr ftruncate_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!ftruncate_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); diff --git a/interfaces/kits/js/src/mod_fs/properties/utimes.cpp b/interfaces/kits/js/src/mod_fs/properties/utimes.cpp index 53de8c7cdfcd505d8a4675e8a5f1628afc2b6031..de819baeb0ad198587f08155edc15c0a3af7b4d5 100644 --- a/interfaces/kits/js/src/mod_fs/properties/utimes.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/utimes.cpp @@ -65,7 +65,7 @@ napi_value Utimes::Sync(napi_env env, napi_callback_info info) } std::unique_ptr utimes_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!utimes_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); diff --git a/interfaces/test/unittest/hyperaio/hyperaio_test.cpp b/interfaces/test/unittest/hyperaio/hyperaio_test.cpp index b1916adbd090218beea36cd19246248bff1b1a05..0aa3de24bcffec82d714cebba8bd0ee877b0642a 100644 --- a/interfaces/test/unittest/hyperaio/hyperaio_test.cpp +++ b/interfaces/test/unittest/hyperaio/hyperaio_test.cpp @@ -240,8 +240,12 @@ namespace OHOS::HyperAio { EXPECT_EQ(result, 0); sqe_flag = false; result = hyperAio_->StartOpenReqs(&openReqs); - EXPECT_EQ(result, -ENOMEM); + EXPECT_EQ(result, 0); sqe_flag = true; + submit_flag = false; + result = hyperAio_->StartOpenReqs(&openReqs); + EXPECT_EQ(result, 0); + submit_flag = true; result = hyperAio_->DestroyCtx(); EXPECT_EQ(result, 0); GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_StartOpenReqs_0003"; @@ -381,8 +385,12 @@ namespace OHOS::HyperAio { EXPECT_EQ(result, 0); sqe_flag = false; result = hyperAio_->StartReadReqs(&readReqs); - EXPECT_EQ(result, -ENOMEM); + EXPECT_EQ(result, 0); sqe_flag = true; + submit_flag = false; + result = hyperAio_->StartReadReqs(&readReqs); + EXPECT_EQ(result, 0); + submit_flag = true; result = hyperAio_->DestroyCtx(); EXPECT_EQ(result, 0); GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_StartReadReqs_0003"; @@ -518,8 +526,12 @@ namespace OHOS::HyperAio { EXPECT_EQ(result, 0); sqe_flag = false; result = hyperAio_->StartCancelReqs(&cancelReqs); - EXPECT_EQ(result, -ENOMEM); + EXPECT_EQ(result, 0); sqe_flag = true; + submit_flag = false; + result = hyperAio_->StartCancelReqs(&cancelReqs); + EXPECT_EQ(result, 0); + submit_flag = true; result = hyperAio_->DestroyCtx(); EXPECT_EQ(result, 0); GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_StartCancelReqs_0003"; diff --git a/interfaces/test/unittest/hyperaio/include/liburing.h b/interfaces/test/unittest/hyperaio/include/liburing.h index b00112be3c3b823357047843934b4b6ad5451c7e..e5b53ba3544fdee818751eb2ce6332ad8405c61b 100644 --- a/interfaces/test/unittest/hyperaio/include/liburing.h +++ b/interfaces/test/unittest/hyperaio/include/liburing.h @@ -25,6 +25,7 @@ inline bool sqe_flag = true; inline bool init_flag = true; inline bool wait_flag = true; inline bool cqe_res_flag = true; +inline bool submit_flag = true; struct io_uring_sqe { int32_t data; }; @@ -56,6 +57,7 @@ struct io_uring { inline struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring) { if (sqe_flag) { + sqe_flag = !sqe_flag; return ring->io_uring_get_sqe(); } return nullptr; @@ -63,11 +65,15 @@ inline struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring) inline int io_uring_submit(struct io_uring *ring) { - return 1; + if (submit_flag) { + return 1; + } + return -1; } inline int io_uring_queue_init(unsigned entries, struct io_uring *ring, unsigned flags) { + sqe_flag = true; if (init_flag) { return 1; } diff --git a/interfaces/test/unittest/js/mod_fs/class_tasksignal/fs_task_signal_test.cpp b/interfaces/test/unittest/js/mod_fs/class_tasksignal/fs_task_signal_test.cpp index 9232047719e98ae16cfdc85d49986ca6fbe28e9c..a75567d755fa53bb94da491ed9061e3ac59cf443 100644 --- a/interfaces/test/unittest/js/mod_fs/class_tasksignal/fs_task_signal_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_tasksignal/fs_task_signal_test.cpp @@ -136,7 +136,7 @@ HWTEST_F(FsTaskSignalTest, FsTaskSignalTest_Cancel_001, testing::ext::TestSize.L auto res = fsTaskSignal.Cancel(); - EXPECT_EQ(fsTaskSignal.taskSignal_, nullptr); + EXPECT_NE(fsTaskSignal.taskSignal_, nullptr); GTEST_LOG_(INFO) << "NClassTest-end FsTaskSignalTest_Cancel_001"; } @@ -175,7 +175,7 @@ HWTEST_F(FsTaskSignalTest, FsTaskSignalTest_OnCancel_001, testing::ext::TestSize auto res = fsTaskSignal.OnCancel(); - EXPECT_EQ(fsTaskSignal.taskSignal_, nullptr); + EXPECT_NE(fsTaskSignal.taskSignal_, nullptr); GTEST_LOG_(INFO) << "NClassTest-end FsTaskSignalTest_OnCancel_001"; }