diff --git a/src/include/gstrace/gstrace_tool.h b/src/include/gstrace/gstrace_tool.h index ef9739b77..2e759d5fa 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); trace_msg_code 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 e022663d1..8f30c87eb 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); + } +} + trace_msg_code DumpFileFlowVisitor::mergeFiles(const char* outPath, size_t len) { FILE* fpOut = NULL; @@ -430,6 +448,7 @@ trace_msg_code DumpFileFlowVisitor::mergeFiles(const char* outPath, size_t len) fpOut = trace_fopen(outPath, "w+"); if (fpOut == NULL) { + this->removeAllTempFiles(mapFlows.begin()); return TRACE_OPEN_OUTPUT_FILE_ERR; } @@ -445,6 +464,7 @@ trace_msg_code 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); (void)trace_fclose(fpOut); free(buffer); return TRACE_OPEN_TMP_FILE_ERR;