Changeset View
Changeset View
Standalone View
Standalone View
src/solid/devices/backends/mounts/mounts.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by David Hallas <david@davidhallas.dk> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program; if not, write to the * | ||||
16 | * Free Software Foundation, Inc., * | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||||
18 | ***************************************************************************/ | ||||
19 | | ||||
20 | #include "mounts.h" | ||||
21 | #include <QDebug> | ||||
22 | #include <QFile> | ||||
23 | #include <QFileInfo> | ||||
24 | #include <QTextStream> | ||||
25 | | ||||
26 | Q_GLOBAL_STATIC(Solid::Backends::Mounts::Mounts, globalMounts) | ||||
27 | | ||||
28 | using namespace Solid::Backends::Mounts; | ||||
29 | | ||||
30 | Mounts::Mounts() : m_lastUpdated(0) | ||||
31 | { | ||||
32 | } | ||||
33 | | ||||
34 | Mounts::~Mounts() | ||||
35 | { | ||||
36 | } | ||||
37 | | ||||
38 | QString Mounts::readProcMounts() | ||||
39 | { | ||||
40 | QFile mountsFile("/proc/mounts"); | ||||
41 | if (!mountsFile.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||||
42 | qWarning() << "Failed to open" << mountsFile.fileName(); | ||||
43 | return QString(); | ||||
44 | } | ||||
45 | QTextStream mounts(&mountsFile); | ||||
46 | return mounts.readAll(); | ||||
47 | } | ||||
48 | | ||||
49 | void Mounts::updateFuseMounts() | ||||
50 | { | ||||
51 | QString data = readProcMounts(); | ||||
52 | QStringList mountEntries = data.split('\n', QString::SkipEmptyParts); | ||||
53 | QVector<FuseMount> fuseMounts; | ||||
54 | for (const auto& mount : mountEntries) { | ||||
55 | QStringList mountInfo = mount.split(' ', QString::SkipEmptyParts); | ||||
56 | if (mountInfo.size() < 6) { | ||||
57 | continue; | ||||
58 | } | ||||
59 | QString device(mountInfo[0]); | ||||
60 | QString mountPoint(mountInfo[1]); | ||||
61 | QString fuseType(mountInfo[2]); | ||||
62 | QString options(mountInfo[3]); | ||||
63 | if (fuseType.startsWith(QLatin1String("fuse."))) { | ||||
64 | fuseMounts << FuseMount{device, mountPoint, fuseType, options}; | ||||
65 | } | ||||
66 | } | ||||
67 | globalMounts->m_fuseMounts.swap(fuseMounts); | ||||
68 | globalMounts->m_lastUpdated = qHash(data); | ||||
69 | } | ||||
70 | | ||||
71 | QStringList Mounts::deviceList() | ||||
72 | { | ||||
73 | QStringList ret; | ||||
74 | for (const auto& mount : globalMounts->m_fuseMounts) { | ||||
75 | ret << QLatin1String(MOUNTS_UDI_PREFIX) + QLatin1Char('/') + mount.mountPoint; | ||||
76 | } | ||||
77 | return ret; | ||||
78 | } | ||||
79 | | ||||
80 | const FuseMount *Mounts::fuseMountFromUdi(QString udi) | ||||
81 | { | ||||
82 | QString mountPoint = udi.mid(QLatin1String(MOUNTS_UDI_PREFIX).size() + 1); | ||||
83 | for (const auto& mount : globalMounts->m_fuseMounts) { | ||||
84 | if (mount.mountPoint == mountPoint) { | ||||
85 | return &mount; | ||||
86 | } | ||||
87 | } | ||||
88 | return nullptr; | ||||
89 | } | ||||
90 | | ||||
91 | uint Mounts::lastUpdated() | ||||
92 | { | ||||
93 | return globalMounts->m_lastUpdated; | ||||
94 | } |