From 0581fd2abc5733f8b434beb739bc7163b239e3d3 Mon Sep 17 00:00:00 2001 From: "haolan.sun" Date: Thu, 10 Nov 2022 13:01:16 +0800 Subject: [PATCH] [gstrace codepath] fix the residual temporary files when exception occurs. --- src/include/gstrace/gstrace_tool.h | 1 + src/lib/gstrace/tool/gstrace_tool.cpp | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/include/gstrace/gstrace_tool.h b/src/include/gstrace/gstrace_tool.h index f91d335ba..c1353c1ea 100644 --- a/src/include/gstrace/gstrace_tool.h +++ b/src/include/gstrace/gstrace_tool.h @@ -137,6 +137,7 @@ public: void outputStat(FILE* fp) override; void putIfUnexisted(pid_t pid, pid_t tid); void putFuncStatIfUnexisted(uint32_t func_id, func_stat* func_stat_map); + void removeAllTempFiles(map_flow::iterator it); void mergeFiles(const char* outPath, size_t len); void flushThreadFlows(); diff --git a/src/lib/gstrace/tool/gstrace_tool.cpp b/src/lib/gstrace/tool/gstrace_tool.cpp index fae974cab..2a50ac1c5 100644 --- a/src/lib/gstrace/tool/gstrace_tool.cpp +++ b/src/lib/gstrace/tool/gstrace_tool.cpp @@ -420,6 +420,24 @@ void DumpFileFlowVisitor::flushThreadFlows() } } +/** + * @brief If exception occures, remove all temp file before exit + * + * @param it map_flow::iterator + * @return trace_msg_code trace status code + */ +void DumpFileFlowVisitor::removeAllTempFiles(map_flow::iterator it){ + + //If exception occures, remove all temp file from where iterator begins + for ( ; it != mapFlows.end(); ++it) { + char tmpPath[MAX_PATH_LEN] = {0}; + int ret; + ret = snprintf_s(tmpPath, MAX_PATH_LEN, MAX_PATH_LEN - 1, "tid.%d", it->first); + securec_check_ss_c(ret, "\0", "\0"); + ret = remove(tmpPath); + } +} + void DumpFileFlowVisitor::mergeFiles(const char* outPath, size_t len) { FILE* fpOut = NULL; @@ -430,6 +448,7 @@ void DumpFileFlowVisitor::mergeFiles(const char* outPath, size_t len) fpOut = trace_fopen(outPath, "w+"); if (fpOut == NULL) { + this->removeAllTempFiles(mapFlows.begin()); printf("Cannot open file %s\n", outPath); goto exit; } @@ -446,6 +465,7 @@ void DumpFileFlowVisitor::mergeFiles(const char* outPath, size_t len) // Open the file with read mode FILE* fpIn = trace_fopen(tmpPath, "r"); if (NULL == fpIn) { + this->removeAllTempFiles(it); printf("Cannot open file %s\n", tmpPath); goto exit; }