From 371690dfc18585f4ba41e886798c5ddb8857f20b Mon Sep 17 00:00:00 2001 From: hzzhouzebin Date: Thu, 29 Jun 2023 22:16:42 +0800 Subject: [PATCH] Rename ap file when save Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I7H5R2 Signed-off-by: hzzhouzebin Change-Id: I9c7e3a69fb9817481fd9a2168e354184a067babb --- ecmascript/pgo_profiler/pgo_profiler_encoder.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ecmascript/pgo_profiler/pgo_profiler_encoder.cpp b/ecmascript/pgo_profiler/pgo_profiler_encoder.cpp index 2f7659b3c7..1da159a16f 100644 --- a/ecmascript/pgo_profiler/pgo_profiler_encoder.cpp +++ b/ecmascript/pgo_profiler/pgo_profiler_encoder.cpp @@ -13,6 +13,9 @@ * limitations under the License. */ +#include +#include + #include "ecmascript/pgo_profiler/pgo_profiler_encoder.h" #include "ecmascript/platform/file.h" @@ -79,15 +82,21 @@ bool PGOProfilerEncoder::Save() bool PGOProfilerEncoder::SaveProfiler(const SaveTask *task) { - std::ofstream fileStream(realOutPath_.c_str()); + static const char *tempSuffix = ".tmp"; + auto tmpOutPath = realOutPath_ + tempSuffix; + std::ofstream fileStream(tmpOutPath.c_str()); if (!fileStream.is_open()) { - LOG_ECMA(ERROR) << "The file path(" << realOutPath_ << ") open failure!"; + LOG_ECMA(ERROR) << "The file path(" << tmpOutPath << ") open failure!"; return false; } pandaFileInfos_->ProcessToBinary(fileStream, header_->GetPandaInfoSection()); globalRecordInfos_->ProcessToBinary(task, fileStream, header_); header_->ProcessToBinary(fileStream); fileStream.close(); + if (rename(tmpOutPath.c_str(), realOutPath_.c_str())) { + LOG_ECMA(ERROR) << "Rename " << tmpOutPath << " --> " << realOutPath_ << " failure!, errno: " << errno; + return false; + } return true; }