From 559e1b76337b9cbe9e3644f0f152323b0d04daaf Mon Sep 17 00:00:00 2001 From: Akseli Lahtinen Date: Thu, 13 Nov 2025 15:35:29 +0200 Subject: [PATCH] ViewProperties: Avoid tempfile flooding This patch sets the tempFile to be part of the ViewProperties scope, so that we do not leave any temporary files littered around whenever ViewProperties are closed. This fixes the issue with /tmp/ being flooded with Dolphin temporary files. It also utilizes the same tempfile until it does not need it anymore, avoiding creating new ones. We also check that we can actually open the temporary file before working on it, since open() is nodiscard. BUG: 508402 --- src/views/viewproperties.cpp | 42 ++++++++++++++++++++---------------- src/views/viewproperties.h | 1 + 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/views/viewproperties.cpp b/src/views/viewproperties.cpp index 8bf3b2531..2ec46ed11 100644 --- a/src/views/viewproperties.cpp +++ b/src/views/viewproperties.cpp @@ -42,18 +42,20 @@ ViewPropertySettings *ViewProperties::loadProperties(const QString &folderPath) return new ViewPropertySettings(KSharedConfig::openConfig(settingsFile, KConfig::SimpleConfig)); } - std::unique_ptr tempFile(new QTemporaryFile()); - tempFile->setAutoRemove(false); - if (!tempFile->open()) { + if (!m_tempFile->open()) { qCWarning(DolphinDebug) << "Could not open temp file"; return nullptr; } if (QFile::exists(settingsFile)) { // copy settings to tempfile to load them separately - QFile::remove(tempFile->fileName()); - QFile::copy(settingsFile, tempFile->fileName()); + QFile settings(settingsFile); + if (m_tempFile->open() && settings.open(QFile::ReadOnly)) { + m_tempFile->resize(0); + m_tempFile->write(settings.readAll()); + m_tempFile->close(); + } - auto config = KConfig(tempFile->fileName(), KConfig::SimpleConfig); + auto config = KConfig(m_tempFile->fileName(), KConfig::SimpleConfig); // ignore settings that are outside of dolphin scope if (config.hasGroup("Dolphin") || config.hasGroup("Settings")) { const auto groupList = config.groupList(); @@ -62,11 +64,11 @@ ViewPropertySettings *ViewProperties::loadProperties(const QString &folderPath) config.deleteGroup(group); } } - return new ViewPropertySettings(KSharedConfig::openConfig(tempFile->fileName(), KConfig::SimpleConfig)); + return new ViewPropertySettings(KSharedConfig::openConfig(m_tempFile->fileName(), KConfig::SimpleConfig)); } else if (!config.groupList().isEmpty()) { // clear temp file content - QFile::remove(tempFile->fileName()); + m_tempFile->remove(); } } @@ -76,11 +78,14 @@ ViewPropertySettings *ViewProperties::loadProperties(const QString &folderPath) return nullptr; } // load view properties from xattr to temp file then loads into ViewPropertySettings - QFile outputFile(tempFile->fileName()); - outputFile.open(QIODevice::WriteOnly); - outputFile.write(viewPropertiesString.toUtf8()); - outputFile.close(); - return new ViewPropertySettings(KSharedConfig::openConfig(tempFile->fileName(), KConfig::SimpleConfig)); + if (m_tempFile->open()) { + m_tempFile->resize(0); + m_tempFile->write(viewPropertiesString.toLatin1()); + m_tempFile->close(); + return new ViewPropertySettings(KSharedConfig::openConfig(m_tempFile->fileName(), KConfig::SimpleConfig)); + } else { + return new ViewPropertySettings(); + } } ViewPropertySettings *ViewProperties::defaultProperties() const @@ -88,13 +93,12 @@ ViewPropertySettings *ViewProperties::defaultProperties() const auto props = loadProperties(destinationDir(QStringLiteral("global"))); if (props == nullptr) { qCWarning(DolphinDebug) << "Could not load default global viewproperties"; - QTemporaryFile tempFile; - tempFile.setAutoRemove(false); - if (!tempFile.open()) { + if (!m_tempFile->open()) { qCWarning(DolphinDebug) << "Could not open temp file"; - props = new ViewPropertySettings; + m_tempFile->remove(); + props = new ViewPropertySettings(); } else { - props = new ViewPropertySettings(KSharedConfig::openConfig(tempFile.fileName(), KConfig::SimpleConfig)); + props = new ViewPropertySettings(KSharedConfig::openConfig(m_tempFile->fileName(), KConfig::SimpleConfig)); } } @@ -105,6 +109,7 @@ ViewProperties::ViewProperties(const QUrl &url) : m_changedProps(false) , m_autoSave(true) , m_node(nullptr) + , m_tempFile(new QTemporaryFile) { GeneralSettings *settings = GeneralSettings::self(); const bool useGlobalViewProps = settings->globalViewProps() || url.isEmpty(); @@ -112,6 +117,7 @@ ViewProperties::ViewProperties(const QUrl &url) bool useTrashView = false; bool useRecentDocumentsView = false; bool useDownloadsView = false; + m_tempFile->setAutoRemove(false); // We try and save it to the file .directory in the directory being viewed. // If the directory is not writable by the user or the directory is not local, diff --git a/src/views/viewproperties.h b/src/views/viewproperties.h index bee1e7330..5f2f8dfaf 100644 --- a/src/views/viewproperties.h +++ b/src/views/viewproperties.h @@ -168,6 +168,7 @@ class DOLPHIN_EXPORT ViewProperties bool m_autoSave; QString m_filePath; ViewPropertySettings *m_node; + std::unique_ptr m_tempFile; }; #endif