CTF: Clear output location at startup

Remove old files from the output location before saving new ones.

Change-Id: I75181126c6c920e13951e9a46a6be1c666d457fe
Reviewed-by: Antti Määttä <antti.maatta@qt.io>
Reviewed-by: Janne Koskinen <janne.p.koskinen@qt.io>
(cherry picked from commit c6ab516f71efeeaaf2a10dbc0368fb437f8b3a6c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Antti Määttä 2023-06-20 13:26:52 +03:00 committed by Qt Cherry-pick Bot
parent 068e0cbb2f
commit e395b1eb3f
2 changed files with 26 additions and 5 deletions

View File

@ -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];

View File

@ -89,6 +89,7 @@ private:
QHash<QString, QCtfTracePointPrivate *> 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);