From e395b1eb3ff7a093e6c0de76b6cece6f8ff0ec44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=A4=C3=A4tt=C3=A4?= Date: Tue, 20 Jun 2023 13:26:52 +0300 Subject: [PATCH] CTF: Clear output location at startup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove old files from the output location before saving new ones. Change-Id: I75181126c6c920e13951e9a46a6be1c666d457fe Reviewed-by: Antti Määttä Reviewed-by: Janne Koskinen (cherry picked from commit c6ab516f71efeeaaf2a10dbc0368fb437f8b3a6c) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/tracing/qctflib.cpp | 30 +++++++++++++++++++++++++----- src/plugins/tracing/qctflib_p.h | 1 + 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/plugins/tracing/qctflib.cpp b/src/plugins/tracing/qctflib.cpp index 783be079f71..f57f1cf71f2 100644 --- a/src/plugins/tracing/qctflib.cpp +++ b/src/plugins/tracing/qctflib.cpp @@ -116,6 +116,7 @@ QCtfLibImpl::QCtfLibImpl() } m_location = location + QStringLiteral("/ust"); std::filesystem::create_directory(qPrintable(m_location), qPrintable(location)); + clearLocation(); } m_session.all = m_session.tracepoints.contains(QStringLiteral("all")); @@ -136,6 +137,30 @@ QCtfLibImpl::QCtfLibImpl() m_timer.start(); } +void QCtfLibImpl::clearLocation() +{ + const std::filesystem::path location{qUtf16Printable(m_location)}; + for (auto const& dirEntry : std::filesystem::directory_iterator{location}) + { + const auto path = dirEntry.path(); +#if __cplusplus > 201703L + if (dirEntry.is_regular_file() + && path.filename().wstring().starts_with(std::wstring_view(L"channel_")) + && !path.has_extension()) { +#else + const auto strview = std::wstring_view(L"channel_"); + const auto sub = path.filename().wstring().substr(0, strview.length()); + if (dirEntry.is_regular_file() && sub.compare(strview) == 0 + && !path.has_extension()) { +#endif + if (!std::filesystem::remove(path)) { + qCInfo(lcDebugTrace) << "Unable to clear output location."; + break; + } + } + } +} + void QCtfLibImpl::writeMetadata(const QString &metadata, bool overwrite) { FILE *file = nullptr; @@ -276,11 +301,6 @@ void QCtfLibImpl::doTracepoint(const QCtfTracePointEvent &point, const QByteArra if (ch.channelName[0] == 0) { m_threadIndices.insert(thread, m_threadIndices.size()); - sprintf(ch.channelName, "%s/channel_%d", qPrintable(m_location), m_threadIndices[thread]); - FILE *f = nullptr; - f = openFile(ch.channelName, "wb"_L1); - if (f) - fclose(f); ch.minTimestamp = ch.maxTimestamp = timestamp; ch.thread = thread; ch.threadIndex = m_threadIndices[thread]; diff --git a/src/plugins/tracing/qctflib_p.h b/src/plugins/tracing/qctflib_p.h index 98f34699bd0..8d88183b84f 100644 --- a/src/plugins/tracing/qctflib_p.h +++ b/src/plugins/tracing/qctflib_p.h @@ -89,6 +89,7 @@ private: QHash m_eventPrivs; void updateMetadata(const QCtfTracePointEvent &point); void writeMetadata(const QString &metadata, bool overwrite = false); + void clearLocation(); static void writeCtfPacket(Channel &ch); static constexpr QUuid s_TraceUuid = QUuid(0x3e589c95, 0xed11, 0xc159, 0x42, 0x02, 0x6a, 0x9b, 0x02, 0x00, 0x12, 0xac);