Solid

fstabwatcher.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Mario Bensi <mbensi@ipsquad.net>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "fstabwatcher.h"
8#include "fstab_debug.h"
9#include "soliddefs_p.h"
10
11#include <QCoreApplication>
12#include <QFile>
13#include <QFileSystemWatcher>
14#include <QSocketNotifier>
15
16namespace Solid
17{
18namespace Backends
19{
20namespace Fstab
21{
22Q_GLOBAL_STATIC(FstabWatcher, globalFstabWatcher)
23
24static const QString s_mtabFile = QStringLiteral("/etc/mtab");
25#ifdef Q_OS_SOLARIS
26static const QString s_fstabFile = QStringLiteral("/etc/vfstab");
27#else
28static const QString s_fstabFile = QStringLiteral("/etc/fstab");
29#endif
30static const QString s_fstabPath = QStringLiteral("/etc");
31
32FstabWatcher::FstabWatcher()
33 : m_isRoutineInstalled(false)
34 , m_fileSystemWatcher(new QFileSystemWatcher(this))
35{
36 if (qApp) {
37 connect(qApp, &QCoreApplication::aboutToQuit, this, &FstabWatcher::orphanFileSystemWatcher);
38 }
39
40 m_mtabFile = new QFile(s_mtabFile, this);
41 if (m_mtabFile && m_mtabFile->symLinkTarget().startsWith("/proc/") && m_mtabFile->open(QIODevice::ReadOnly)) {
42 m_mtabSocketNotifier = new QSocketNotifier(m_mtabFile->handle(), QSocketNotifier::Exception, this);
43 connect(m_mtabSocketNotifier, &QSocketNotifier::activated, this, &FstabWatcher::mtabChanged);
44 } else {
45 m_fileSystemWatcher->addPath(s_mtabFile);
46 }
47
48 m_fileSystemWatcher->addPath(s_fstabPath);
49 connect(m_fileSystemWatcher, &QFileSystemWatcher::directoryChanged, this, [this](const QString &) {
50 if (!m_isFstabWatched) {
51 m_isFstabWatched = m_fileSystemWatcher->addPath(s_fstabFile);
52 if (m_isFstabWatched) {
53 qCDebug(FSTAB_LOG) << "Re-added" << s_fstabFile;
54 Q_EMIT onFileChanged(s_fstabFile);
55 }
56 }
57 });
58
59 m_isFstabWatched = m_fileSystemWatcher->addPath(s_fstabFile);
60 connect(m_fileSystemWatcher, &QFileSystemWatcher::fileChanged, this, &FstabWatcher::onFileChanged);
61}
62
63FstabWatcher::~FstabWatcher()
64{
65 // The QFileSystemWatcher doesn't work correctly in a singleton
66 // The solution so far was to destroy the QFileSystemWatcher when the application quits
67 // But we have some crash with this solution.
68 // For the moment to workaround the problem, we detach the QFileSystemWatcher from the parent
69 // effectively leaking it on purpose.
70
71#if 0
72 //qRemovePostRoutine(globalFstabWatcher.destroy);
73#else
74 m_fileSystemWatcher->setParent(nullptr);
75#endif
76}
77
78void FstabWatcher::orphanFileSystemWatcher()
79{
80 m_fileSystemWatcher->setParent(nullptr);
81}
82
83FstabWatcher *FstabWatcher::instance()
84{
85#if 0
86 FstabWatcher *fstabWatcher = globalFstabWatcher;
87
88 if (fstabWatcher && !fstabWatcher->m_isRoutineInstalled) {
89 qAddPostRoutine(globalFstabWatcher.destroy);
90 fstabWatcher->m_isRoutineInstalled = true;
91 }
92 return fstabWatcher;
93#else
94 return globalFstabWatcher;
95#endif
96}
97
98void FstabWatcher::onFileChanged(const QString &path)
99{
100 if (path == s_mtabFile) {
101 Q_EMIT mtabChanged();
102 if (!m_fileSystemWatcher->files().contains(s_mtabFile)) {
103 m_fileSystemWatcher->addPath(s_mtabFile);
104 }
105 }
106 if (path == s_fstabFile) {
107 Q_EMIT fstabChanged();
108 if (!m_fileSystemWatcher->files().contains(s_fstabFile)) {
109 m_isFstabWatched = m_fileSystemWatcher->addPath(s_fstabFile);
110 qCDebug(FSTAB_LOG) << "Fstab removed, re-added:" << m_isFstabWatched;
111 }
112 }
113}
114
115}
116}
117} // namespace Solid:Backends::Fstab
118
119#include "moc_fstabwatcher.cpp"
The single responsibility of this class is to create arguments valid for logind Inhibit call.
Definition fakebattery.h:16
void directoryChanged(const QString &path)
void fileChanged(const QString &path)
void activated(QSocketDescriptor socket, QSocketNotifier::Type type)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:12 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.