From 2ef9d4bb306e864433d6409e0461d6fc8c23a27a Mon Sep 17 00:00:00 2001 From: changleipeng Date: Fri, 24 Mar 2023 09:41:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9RemoveEntityOfFinal=E6=94=BE?= =?UTF-8?q?=E5=9C=A8=E4=B8=BB=E7=BA=BF=E7=A8=8B=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: changleipeng --- .../mod_fs/class_stream/stream_n_exporter.cpp | 19 +++--- .../kits/js/src/mod_fs/properties/close.cpp | 65 +++++++++++-------- 2 files changed, 46 insertions(+), 38 deletions(-) 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 5d2fc15ec..76822f940 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 @@ -300,18 +300,17 @@ napi_value StreamNExporter::Close(napi_env env, napi_callback_info cbInfo) return nullptr; } - auto cbExec = [streamEntity]() -> NError { - auto filp = streamEntity->fp.release(); - if (!fclose(filp)) { - HILOGE("Failed to close file with filp"); - return NError(ERRNO_NOERR); - } else { - return NError(errno); - } + auto fp = NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); + if (!fp) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = []() -> NError { + return NError(ERRNO_NOERR); }; - auto cbCompl = [arg = funcArg.GetThisVar()](napi_env env, NError err) -> NVal { - (void)NClass::RemoveEntityOfFinal(env, arg); + auto cbCompl = [](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } else { diff --git a/interfaces/kits/js/src/mod_fs/properties/close.cpp b/interfaces/kits/js/src/mod_fs/properties/close.cpp index 195feb69d..05cf915c4 100755 --- a/interfaces/kits/js/src/mod_fs/properties/close.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/close.cpp @@ -42,12 +42,31 @@ static FileEntity *GetFileEntity(napi_env env, napi_value objFile) return fileEntity; } +static NError CloseFd(int fd) +{ + std::unique_ptr close_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!close_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_close(nullptr, close_req.get(), fd, nullptr); + if (ret < 0) { + HILOGE("Failed to close file with ret: %{public}d", ret); + return NError(errno); + } + return NError(ERRNO_NOERR); +} + static tuple ParseJsOperand(napi_env env, napi_value fdOrFileFromJsArg) { auto [isFd, fd] = NVal(env, fdOrFileFromJsArg).ToInt32(); - if (isFd) { + if (isFd && fd >= 0) { return { true, FileStruct { true, fd, nullptr } }; } + if (isFd && fd < 0) { + return { false, FileStruct { false, -1, nullptr } }; + } auto file = GetFileEntity(env, fdOrFileFromJsArg); if (file) { return { true, FileStruct { false, -1, file } }; @@ -73,23 +92,18 @@ napi_value Close::Sync(napi_env env, napi_callback_info info) } if (fileStruct.isFd) { - 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); + auto err = CloseFd(fileStruct.fd); + if (err) { + err.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); - NError(errno).ThrowErr(env); + } else { + auto fp = NClass::RemoveEntityOfFinal(env, funcArg[NARG_POS::FIRST]); + if (!fp) { + NError(EINVAL).ThrowErr(env); return nullptr; } - } else { - fileStruct.fileEntity->fd_.reset(); } - (void)NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); return NVal::CreateUndefined(env).val_; } @@ -110,27 +124,22 @@ napi_value Close::Async(napi_env env, napi_callback_info info) return nullptr; } + if (!fileStruct.isFd) { + auto fp = NClass::RemoveEntityOfFinal(env, funcArg[NARG_POS::FIRST]); + if (!fp) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + } + auto cbExec = [fileStruct = fileStruct]() -> NError { if (fileStruct.isFd) { - 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); - return NError(errno); - } - } else { - fileStruct.fileEntity->fd_.reset(); + return CloseFd(fileStruct.fd); } return NError(ERRNO_NOERR); }; - auto cbComplete = [arg = funcArg.GetThisVar()](napi_env env, NError err) -> NVal { - (void)NClass::RemoveEntityOfFinal(env, arg); + auto cbComplete = [](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } else { -- Gitee