diff --git a/src/solid/devices/backends/fstab/fstabwatcher.cpp b/src/solid/devices/backends/fstab/fstabwatcher.cpp index 91e110d..e7f0411 100644 --- a/src/solid/devices/backends/fstab/fstabwatcher.cpp +++ b/src/solid/devices/backends/fstab/fstabwatcher.cpp @@ -1,104 +1,119 @@ /* SPDX-FileCopyrightText: 2010 Mario Bensi SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL */ #include "fstabwatcher.h" #include "soliddefs_p.h" +#include "fstab_debug.h" #include #include #include #include namespace Solid { namespace Backends { namespace Fstab { Q_GLOBAL_STATIC(FstabWatcher, globalFstabWatcher) static const QString s_mtabFile = QStringLiteral("/etc/mtab"); #ifdef Q_OS_SOLARIS static const QString s_fstabFile = QStringLiteral("/etc/vfstab"); #else static const QString s_fstabFile = QStringLiteral("/etc/fstab"); #endif +static const QString s_fstabPath = QStringLiteral("/etc"); FstabWatcher::FstabWatcher() : m_isRoutineInstalled(false) , m_fileSystemWatcher(new QFileSystemWatcher(this)) { if (qApp) { connect(qApp, &QCoreApplication::aboutToQuit, this, &FstabWatcher::orphanFileSystemWatcher); } m_mtabFile = new QFile(s_mtabFile, this); if (m_mtabFile && m_mtabFile->symLinkTarget().startsWith("/proc/") && m_mtabFile->open(QIODevice::ReadOnly)) { m_mtabSocketNotifier = new QSocketNotifier(m_mtabFile->handle(), QSocketNotifier::Exception, this); connect(m_mtabSocketNotifier, &QSocketNotifier::activated, this, &FstabWatcher::mtabChanged); } else { m_fileSystemWatcher->addPath(s_mtabFile); } - m_fileSystemWatcher->addPath(s_fstabFile); + m_fileSystemWatcher->addPath(s_fstabPath); + connect(m_fileSystemWatcher, &QFileSystemWatcher::directoryChanged, + this, [this] (const QString &) { + if (!m_isFstabWatched) { + m_isFstabWatched = m_fileSystemWatcher->addPath(s_fstabFile); + if (m_isFstabWatched) { + qCDebug(FSTAB) << "Readded" << s_fstabFile; + emit onFileChanged(s_fstabFile); + } + } + }); + + m_isFstabWatched = m_fileSystemWatcher->addPath(s_fstabFile); connect(m_fileSystemWatcher, &QFileSystemWatcher::fileChanged, this, &FstabWatcher::onFileChanged); } FstabWatcher::~FstabWatcher() { // The QFileSystemWatcher doesn't work correctly in a singleton // The solution so far was to destroy the QFileSystemWatcher when the application quits // But we have some crash with this solution. // For the moment to workaround the problem, we detach the QFileSystemWatcher from the parent // effectively leaking it on purpose. #if 0 //qRemovePostRoutine(globalFstabWatcher.destroy); #else m_fileSystemWatcher->setParent(nullptr); #endif } void FstabWatcher::orphanFileSystemWatcher() { m_fileSystemWatcher->setParent(nullptr); } FstabWatcher *FstabWatcher::instance() { #if 0 FstabWatcher *fstabWatcher = globalFstabWatcher; if (fstabWatcher && !fstabWatcher->m_isRoutineInstalled) { qAddPostRoutine(globalFstabWatcher.destroy); fstabWatcher->m_isRoutineInstalled = true; } return fstabWatcher; #else return globalFstabWatcher; #endif } void FstabWatcher::onFileChanged(const QString &path) { if (path == s_mtabFile) { emit mtabChanged(); if (!m_fileSystemWatcher->files().contains(s_mtabFile)) { m_fileSystemWatcher->addPath(s_mtabFile); } } if (path == s_fstabFile) { emit fstabChanged(); if (!m_fileSystemWatcher->files().contains(s_fstabFile)) { - m_fileSystemWatcher->addPath(s_fstabFile); + m_isFstabWatched = m_fileSystemWatcher->addPath(s_fstabFile); + qCDebug(FSTAB) << "Fstab removed, readded:" << m_isFstabWatched; } } } }}} // namespace Solid:Backends::Fstab diff --git a/src/solid/devices/backends/fstab/fstabwatcher.h b/src/solid/devices/backends/fstab/fstabwatcher.h index 8c54c7d..8e6e45a 100644 --- a/src/solid/devices/backends/fstab/fstabwatcher.h +++ b/src/solid/devices/backends/fstab/fstabwatcher.h @@ -1,50 +1,51 @@ /* SPDX-FileCopyrightText: 2010 Mario Bensi SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL */ #ifndef SOLID_BACKENDS_FSTAB_WATCHER_H #define SOLID_BACKENDS_FSTAB_WATCHER_H #include class QFileSystemWatcher; class QFile; class QSocketNotifier; namespace Solid { namespace Backends { namespace Fstab { class FstabWatcher : public QObject { Q_OBJECT public: FstabWatcher(); virtual ~FstabWatcher(); static FstabWatcher *instance(); Q_SIGNALS: void mtabChanged(); void fstabChanged(); private Q_SLOTS: void onFileChanged(const QString &path); void orphanFileSystemWatcher(); private: bool m_isRoutineInstalled; QFileSystemWatcher *m_fileSystemWatcher; QSocketNotifier *m_mtabSocketNotifier; QFile *m_mtabFile; + bool m_isFstabWatched; }; } } } #endif // SOLID_BACKENDS_FSTAB_WATCHER_H