From 0328416c34468749b0f05ca1bf4c5cb380e970e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E9=99=B6=E9=87=91=E6=B2=9B=E2=80=9D?= Date: Tue, 15 Apr 2025 20:29:00 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=1B[200~AI=20Review=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=9A=E6=B7=BB=E5=8A=A0rename()=E5=92=8Cr?= =?UTF-8?q?emove()=E8=BF=94=E5=9B=9E=E5=80=BC=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: “陶金沛” --- .../mod_fs/class_atomicfile/atomicfile_n_exporter.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp index 456a2f8f0..bae1c1d4b 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp @@ -388,7 +388,11 @@ napi_value AtomicFileNExporter::FinishWrite(napi_env env, napi_callback_info inf CallFunctionByName(env, writeStream, "closeSync"); - std::rename(rafEntity->newFileName.c_str(), rafEntity->baseFileName.c_str()); + if (!std::rename(rafEntity->newFileName.c_str(), rafEntity->baseFileName.c_str())) { + HILOGE("Failed to rename file"); + NError(UNKROWN_ERR).ThrowErr(env, "Internal error"); + return nullptr; + } std::string tmpNewFileName = rafEntity->baseFileName; rafEntity->newFileName = tmpNewFileName.append(TEMP_FILE_SUFFIX); status = napi_delete_reference(env, rafEntity->writeStreamObj); @@ -423,7 +427,8 @@ napi_value AtomicFileNExporter::FailWrite(napi_env env, napi_callback_info info) CallFunctionByName(env, writeStream, "closeSync"); if (!fs::remove(rafEntity->newFileName)) { - HILOGW("Failed to remove file"); + HILOGE("Failed to remove file"); + return nullptr; } std::string tmpNewFileName = rafEntity->baseFileName; rafEntity->newFileName = tmpNewFileName.append(TEMP_FILE_SUFFIX); -- Gitee From 9f75d9f3d2b7747df9ec781fc3795be0a8715a03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E9=99=B6=E9=87=91=E6=B2=9B=E2=80=9D?= Date: Sun, 27 Apr 2025 17:02:57 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ref=E5=86=85=E5=AD=98?= =?UTF-8?q?=E9=87=8A=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: “陶金沛” --- .../class_atomicfile/atomicfile_n_exporter.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp index bae1c1d4b..bdcd4b58d 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp @@ -390,6 +390,12 @@ napi_value AtomicFileNExporter::FinishWrite(napi_env env, napi_callback_info inf if (!std::rename(rafEntity->newFileName.c_str(), rafEntity->baseFileName.c_str())) { HILOGE("Failed to rename file"); + status = napi_delete_reference(env, rafEntity->writeStreamObj); + if (status != napi_ok) { + HILOGE("Failed to delete reference"); + NError(UNKROWN_ERR).ThrowErr(env, "Internal error"); + return nullptr; + } NError(UNKROWN_ERR).ThrowErr(env, "Internal error"); return nullptr; } @@ -427,8 +433,15 @@ napi_value AtomicFileNExporter::FailWrite(napi_env env, napi_callback_info info) CallFunctionByName(env, writeStream, "closeSync"); if (!fs::remove(rafEntity->newFileName)) { - HILOGE("Failed to remove file"); - return nullptr; + HILOGW("Failed to remove file"); + status = napi_delete_reference(env, rafEntity->writeStreamObj); + if (status != napi_ok) { + HILOGE("Failed to delete reference"); + NError(UNKROWN_ERR).ThrowErr(env, "Internal error"); + return nullptr; + } + NError(UNKROWN_ERR).ThrowErr(env, "Internal error"); + return nullptr; } std::string tmpNewFileName = rafEntity->baseFileName; rafEntity->newFileName = tmpNewFileName.append(TEMP_FILE_SUFFIX); -- Gitee From 82d14c29fea8dd25c21382146496b20a4b3115cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E9=99=B6=E9=87=91=E6=B2=9B=E2=80=9D?= Date: Mon, 28 Apr 2025 14:10:02 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E8=B0=83=E6=95=B4message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: “陶金沛” --- .../src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp index bdcd4b58d..5721a9044 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp @@ -393,10 +393,10 @@ napi_value AtomicFileNExporter::FinishWrite(napi_env env, napi_callback_info inf status = napi_delete_reference(env, rafEntity->writeStreamObj); if (status != napi_ok) { HILOGE("Failed to delete reference"); - NError(UNKROWN_ERR).ThrowErr(env, "Internal error"); + NError(UNKROWN_ERR).ThrowErr(env, "Failed to delete reference"); return nullptr; } - NError(UNKROWN_ERR).ThrowErr(env, "Internal error"); + NError(UNKROWN_ERR).ThrowErr(env, "Failed to rename file"); return nullptr; } std::string tmpNewFileName = rafEntity->baseFileName; @@ -437,10 +437,10 @@ napi_value AtomicFileNExporter::FailWrite(napi_env env, napi_callback_info info) status = napi_delete_reference(env, rafEntity->writeStreamObj); if (status != napi_ok) { HILOGE("Failed to delete reference"); - NError(UNKROWN_ERR).ThrowErr(env, "Internal error"); + NError(UNKROWN_ERR).ThrowErr(env, "Failed to delete reference"); return nullptr; } - NError(UNKROWN_ERR).ThrowErr(env, "Internal error"); + NError(UNKROWN_ERR).ThrowErr(env, "Failed to remove file"); return nullptr; } std::string tmpNewFileName = rafEntity->baseFileName; -- Gitee From c963cb6b81f2b0a6c157b76c3ae4f49ad6ddfa01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E9=99=B6=E9=87=91=E6=B2=9B=E2=80=9D?= Date: Wed, 30 Apr 2025 09:32:01 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8Drename=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: “陶金沛” --- .../js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp index 5721a9044..aa8902e50 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/atomicfile_n_exporter.cpp @@ -388,8 +388,9 @@ napi_value AtomicFileNExporter::FinishWrite(napi_env env, napi_callback_info inf CallFunctionByName(env, writeStream, "closeSync"); - if (!std::rename(rafEntity->newFileName.c_str(), rafEntity->baseFileName.c_str())) { - HILOGE("Failed to rename file"); + int32_t result = std::rename(rafEntity->newFileName.c_str(), rafEntity->baseFileName.c_str()); + if (result != 0) { + HILOGE("Failed to rename file, ret:%{public}d", result); status = napi_delete_reference(env, rafEntity->writeStreamObj); if (status != napi_ok) { HILOGE("Failed to delete reference"); -- Gitee