Solid

fstabstorageaccess.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 "fstabstorageaccess.h"
8#include "fstabwatcher.h"
9#include <QStringList>
10#include <solid/devices/backends/fstab/fstabdevice.h>
11#include <solid/devices/backends/fstab/fstabhandling.h>
12#include <solid/devices/backends/fstab/fstabservice.h>
13
14#include <QDir>
15#include <QProcess>
16#include <QTimer>
17
18#include <errno.h>
19
20#define MTAB "/etc/mtab"
21
22using namespace Solid::Backends::Fstab;
23
24FstabStorageAccess::FstabStorageAccess(Solid::Backends::Fstab::FstabDevice *device)
25 : QObject(device)
26 , m_fstabDevice(device)
27{
28 QStringList currentMountPoints = FstabHandling::currentMountPoints(device->device());
29 if (currentMountPoints.isEmpty()) {
30 QStringList mountPoints = FstabHandling::mountPoints(device->device());
31 m_filePath = mountPoints.isEmpty() ? QString() : mountPoints.first();
32 m_isAccessible = false;
33 } else {
34 m_filePath = currentMountPoints.first();
35 m_isAccessible = true;
36 }
37
38 const bool inUserPath =
39 m_filePath.startsWith(QLatin1String("/media/")) || m_filePath.startsWith(QLatin1String("/run/media/")) || m_filePath.startsWith(QDir::homePath());
40
41 const bool gvfsHidden = FstabHandling::options(device->device()).contains(QLatin1String("x-gvfs-hide"));
42 const bool fsIsOverlay = FstabHandling::fstype(device->device()) == QLatin1String("overlay");
43
44 m_isIgnored = gvfsHidden ||
45 // ignore overlay fs not pointing to / or seemingly mounted by user
46 (fsIsOverlay && m_filePath != QLatin1String("/") && !inUserPath);
47
48 connect(device, SIGNAL(mtabChanged(QString)), this, SLOT(onMtabChanged(QString)));
49 QTimer::singleShot(0, this, SLOT(connectDBusSignals()));
50}
51
52FstabStorageAccess::~FstabStorageAccess()
53{
54}
55
56void FstabStorageAccess::connectDBusSignals()
57{
58 m_fstabDevice->registerAction("setup", this, SLOT(slotSetupRequested()), SLOT(slotSetupDone(int, QString)));
59
60 m_fstabDevice->registerAction("teardown", this, SLOT(slotTeardownRequested()), SLOT(slotTeardownDone(int, QString)));
61}
62
63const Solid::Backends::Fstab::FstabDevice *FstabStorageAccess::fstabDevice() const
64{
65 return m_fstabDevice;
66}
67
68bool FstabStorageAccess::isAccessible() const
69{
70 return m_isAccessible;
71}
72
73QString FstabStorageAccess::filePath() const
74{
75 return m_filePath;
76}
77
78bool FstabStorageAccess::isIgnored() const
79{
80 return m_isIgnored;
81}
82
83bool FstabStorageAccess::isEncrypted() const
84{
85 return m_fstabDevice->isEncrypted();
86}
87
88bool FstabStorageAccess::setup()
89{
90 if (filePath().isEmpty()) {
91 return false;
92 }
93 m_fstabDevice->broadcastActionRequested("setup");
94 return FstabHandling::callSystemCommand("mount", {filePath()}, this, [this](QProcess *process) {
95 if (process->exitCode() == 0) {
96 m_fstabDevice->broadcastActionDone("setup", Solid::NoError, QString());
97 } else {
98 m_fstabDevice->broadcastActionDone("setup", Solid::UnauthorizedOperation, process->readAllStandardError().trimmed());
99 }
100 });
101}
102
103void FstabStorageAccess::slotSetupRequested()
104{
105 Q_EMIT setupRequested(m_fstabDevice->udi());
106}
107
108bool FstabStorageAccess::teardown()
109{
110 if (filePath().isEmpty()) {
111 return false;
112 }
113 m_fstabDevice->broadcastActionRequested("teardown");
114 return FstabHandling::callSystemCommand("umount", {filePath()}, this, [this](QProcess *process) {
115 if (process->exitCode() == 0) {
116 m_fstabDevice->broadcastActionDone("teardown", Solid::NoError);
117 } else if (process->exitCode() == EBUSY) {
118 m_fstabDevice->broadcastActionDone("teardown", Solid::DeviceBusy);
119 } else if (process->exitCode() == EPERM) {
120 m_fstabDevice->broadcastActionDone("teardown", Solid::UnauthorizedOperation, process->readAllStandardError().trimmed());
121 } else {
122 m_fstabDevice->broadcastActionDone("teardown", Solid::OperationFailed, process->readAllStandardError().trimmed());
123 }
124 });
125}
126
127void FstabStorageAccess::slotTeardownRequested()
128{
129 Q_EMIT teardownRequested(m_fstabDevice->udi());
130}
131
132void FstabStorageAccess::slotSetupDone(int error, const QString &errorString)
133{
134 Q_EMIT setupDone(static_cast<Solid::ErrorType>(error), errorString, m_fstabDevice->udi());
135}
136
137void FstabStorageAccess::slotTeardownDone(int error, const QString &errorString)
138{
139 Q_EMIT teardownDone(static_cast<Solid::ErrorType>(error), errorString, m_fstabDevice->udi());
140}
141
142void FstabStorageAccess::onMtabChanged(const QString &device)
143{
144 QStringList currentMountPoints = FstabHandling::currentMountPoints(device);
145 if (currentMountPoints.isEmpty()) {
146 // device umounted
147 m_filePath = FstabHandling::mountPoints(device).first();
148 m_isAccessible = false;
149 Q_EMIT accessibilityChanged(false, QString(FSTAB_UDI_PREFIX) + "/" + device);
150 } else {
151 // device added
152 m_filePath = currentMountPoints.first();
153 m_isAccessible = true;
154 Q_EMIT accessibilityChanged(true, QString(FSTAB_UDI_PREFIX) + "/" + device);
155 }
156}
157
158#include "moc_fstabstorageaccess.cpp"
void broadcastActionRequested(const QString &actionName) const
Allows to broadcast that an action just got requested on a device to all the corresponding devices in...
void broadcastActionDone(const QString &actionName, int error=Solid::NoError, const QString &errorString=QString()) const
Allows to broadcast that an action just completed in a device to all the corresponding devices in oth...
void registerAction(const QString &actionName, QObject *dest, const char *requestSlot, const char *doneSlot) const
Register an action for the given device.
QString homePath()
T & first()
bool isEmpty() const const
Q_EMITQ_EMIT
QString first(qsizetype n) const const
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.