8#include "fstabhandling.h"
9#include "fstab_debug.h"
14#include <QRegularExpression>
15#include <QStandardPaths>
17#include <QThreadStorage>
19#include <solid/devices/soliddefs_p.h>
21#include <solid/config-solid.h>
39#define FSTAB "/etc/fstab"
47Solid::Backends::Fstab::FstabHandling::FstabHandling()
48 : m_fstabCacheValid(false)
49 , m_mtabCacheValid(false)
53bool _k_isFstabNetworkFileSystem(
const QString &fstype,
const QString &devName)
67bool _k_isFstabSupportedLocalFileSystem(
const QString &fstype)
81 return fstype + mountpoint;
88 auto _mountpoint = mountpoint;
98void Solid::Backends::Fstab::FstabHandling::_k_updateFstabMountPointsCache()
100 if (globalFstabCache->localData().m_fstabCacheValid) {
104 globalFstabCache->localData().m_fstabCache.clear();
105 globalFstabCache->localData().m_fstabOptionsCache.clear();
110 if ((fstab = setmntent(FSTAB,
"r")) ==
nullptr) {
115 while ((fe = getmntent(fstab)) !=
nullptr) {
118 if (_k_isFstabNetworkFileSystem(fstype, fsname) || _k_isFstabSupportedLocalFileSystem(fstype)) {
120 const QString device = _k_deviceNameForMountpoint(fsname, fstype, mountpoint);
123 globalFstabCache->localData().m_fstabCache.
insert(device, mountpoint);
124 globalFstabCache->localData().m_fstabFstypeCache.insert(device, fstype);
126 globalFstabCache->localData().m_fstabOptionsCache.insert(device, options.
takeFirst());
135 QFile fstab(QStringLiteral(FSTAB));
143 while (!stream.atEnd()) {
151 if (items.
count() < 4) {
156 if (_k_isFstabNetworkFileSystem(items.
at(2), items.
at(0)) || _k_isFstabSupportedLocalFileSystem(items.
at(2))) {
168 globalFstabCache->localData().m_fstabCache.insert(device, mountpoint);
174 globalFstabCache->localData().m_fstabCacheValid =
true;
177QStringList Solid::Backends::Fstab::FstabHandling::deviceList()
179 _k_updateFstabMountPointsCache();
180 _k_updateMtabMountPointsCache();
182 QStringList devices = globalFstabCache->localData().m_mtabCache.keys();
187 for (
auto it = globalFstabCache->localData().m_fstabCache.constBegin(), end = globalFstabCache->localData().m_fstabCache.constEnd(); it != end; ++it) {
188 auto device = it.key();
201 if (!devices.
contains(deviceName)) {
210 _k_updateFstabMountPointsCache();
211 _k_updateMtabMountPointsCache();
213 QStringList mountpoints = globalFstabCache->localData().m_fstabCache.values(device);
214 mountpoints += globalFstabCache->localData().m_mtabCache.values(device);
221 _k_updateFstabMountPointsCache();
223 QStringList options = globalFstabCache->localData().m_fstabOptionsCache.values(device);
227QString Solid::Backends::Fstab::FstabHandling::fstype(
const QString &device)
229 _k_updateFstabMountPointsCache();
231 return globalFstabCache->localData().m_fstabFstypeCache.value(device);
234bool Solid::Backends::Fstab::FstabHandling::callSystemCommand(
const QString &commandName,
237 std::function<
void(
QProcess *)> callback)
239 static const QStringList searchPaths{QStringLiteral(
"/sbin"), QStringLiteral(
"/bin"), QStringLiteral(
"/usr/sbin"), QStringLiteral(
"/usr/bin")};
243 qCWarning(FSTAB_LOG) <<
"Couldn't find executable" << commandName <<
"in" << joinedPaths;
254 Q_UNUSED(exitStatus);
263 process->
start(exec, args);
273void Solid::Backends::Fstab::FstabHandling::_k_updateMtabMountPointsCache()
275 if (globalFstabCache->localData().m_mtabCacheValid) {
279 globalFstabCache->localData().m_mtabCache.clear();
283#if GETMNTINFO_USES_STATVFS
284 struct statvfs *mounted;
286 struct statfs *mounted;
289 int num_fs = getmntinfo(&mounted, MNT_NOWAIT);
291 for (
int i = 0; i < num_fs; i++) {
293 if (_k_isFstabNetworkFileSystem(type,
QString()) || _k_isFstabSupportedLocalFileSystem(type)) {
296 const QString device = _k_deviceNameForMountpoint(fsname, type, mountpoint);
297 globalFstabCache->localData().m_mtabCache.insert(device, mountpoint);
298 globalFstabCache->localData().m_fstabFstypeCache.insert(device, type);
304 if ((mnttab = setmntent(
"/etc/mtab",
"r")) ==
nullptr) {
309 while ((fe = getmntent(mnttab)) !=
nullptr) {
311 if (_k_isFstabNetworkFileSystem(type,
QString()) || _k_isFstabSupportedLocalFileSystem(type)) {
314 const QString device = _k_deviceNameForMountpoint(fsname, type, mountpoint);
315 globalFstabCache->localData().m_mtabCache.insert(device, mountpoint);
316 globalFstabCache->localData().m_fstabFstypeCache.insert(device, type);
322 globalFstabCache->localData().m_mtabCacheValid =
true;
325QStringList Solid::Backends::Fstab::FstabHandling::currentMountPoints(
const QString &device)
327 _k_updateMtabMountPointsCache();
328 return globalFstabCache->localData().m_mtabCache.values(device);
331void Solid::Backends::Fstab::FstabHandling::flushMtabCache()
333 globalFstabCache->localData().m_mtabCacheValid =
false;
336void Solid::Backends::Fstab::FstabHandling::flushFstabCache()
338 globalFstabCache->localData().m_fstabCacheValid =
false;
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QString decodeName(const QByteArray &localFileName)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
qsizetype count() const const
iterator insert(const_iterator before, parameter_type value)
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setEnvironment(const QStringList &environment)
void finished(int exitCode, QProcess::ExitStatus exitStatus)
void start(OpenMode mode)
QStringList systemEnvironment()
bool waitForStarted(int msecs)
QString findExecutable(const QString &executableName, const QStringList &paths)
QString & append(QChar ch)
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString & prepend(QChar ch)
QString simplified() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
qsizetype removeDuplicates()
QStringList & replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs)