| 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 | using namespace Solid::Backends::Fstab; |
| 21 | |
| 22 | FstabStorageAccess::FstabStorageAccess(Solid::Backends::Fstab::FstabDevice *device) |
| 23 | : QObject(device) |
| 24 | , m_fstabDevice(device) |
| 25 | { |
| 26 | QStringList currentMountPoints = FstabHandling::currentMountPoints(device: device->device()); |
| 27 | if (currentMountPoints.isEmpty()) { |
| 28 | QStringList mountPoints = FstabHandling::mountPoints(device: device->device()); |
| 29 | m_filePath = mountPoints.isEmpty() ? QString() : mountPoints.first(); |
| 30 | m_isAccessible = false; |
| 31 | } else { |
| 32 | m_filePath = currentMountPoints.first(); |
| 33 | m_isAccessible = true; |
| 34 | } |
| 35 | |
| 36 | const bool inUserPath = |
| 37 | m_filePath.startsWith(s: QLatin1String("/media/" )) || m_filePath.startsWith(s: QLatin1String("/run/media/" )) || m_filePath.startsWith(s: QDir::homePath()); |
| 38 | |
| 39 | const bool gvfsHidden = FstabHandling::options(device: device->device()).contains(key: QLatin1String("x-gvfs-hide" )); |
| 40 | const bool fsIsOverlay = FstabHandling::fstype(device: device->device()) == QLatin1String("overlay" ); |
| 41 | |
| 42 | m_isIgnored = gvfsHidden || |
| 43 | // ignore overlay fs not pointing to / or seemingly mounted by user |
| 44 | (fsIsOverlay && m_filePath != QLatin1String("/" ) && !inUserPath); |
| 45 | |
| 46 | connect(sender: device, signal: &FstabDevice::mtabChanged, context: this, slot: &FstabStorageAccess::onMtabChanged); |
| 47 | QTimer::singleShot(msec: 0, receiver: this, SLOT(connectDBusSignals())); |
| 48 | } |
| 49 | |
| 50 | FstabStorageAccess::~FstabStorageAccess() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void FstabStorageAccess::connectDBusSignals() |
| 55 | { |
| 56 | m_fstabDevice->registerAction(QStringLiteral("setup" ), dest: this, SLOT(slotSetupRequested()), SLOT(slotSetupDone(int, QString))); |
| 57 | |
| 58 | m_fstabDevice->registerAction(QStringLiteral("teardown" ), dest: this, SLOT(slotTeardownRequested()), SLOT(slotTeardownDone(int, QString))); |
| 59 | } |
| 60 | |
| 61 | const Solid::Backends::Fstab::FstabDevice *FstabStorageAccess::fstabDevice() const |
| 62 | { |
| 63 | return m_fstabDevice; |
| 64 | } |
| 65 | |
| 66 | bool FstabStorageAccess::isAccessible() const |
| 67 | { |
| 68 | return m_isAccessible; |
| 69 | } |
| 70 | |
| 71 | QString FstabStorageAccess::filePath() const |
| 72 | { |
| 73 | return m_filePath; |
| 74 | } |
| 75 | |
| 76 | bool FstabStorageAccess::isIgnored() const |
| 77 | { |
| 78 | return m_isIgnored; |
| 79 | } |
| 80 | |
| 81 | bool FstabStorageAccess::isEncrypted() const |
| 82 | { |
| 83 | return m_fstabDevice->isEncrypted(); |
| 84 | } |
| 85 | |
| 86 | bool FstabStorageAccess::setup() |
| 87 | { |
| 88 | if (filePath().isEmpty()) { |
| 89 | return false; |
| 90 | } |
| 91 | m_fstabDevice->broadcastActionRequested(QStringLiteral("setup" )); |
| 92 | return FstabHandling::callSystemCommand(QStringLiteral("mount" ), args: {filePath()}, recvr: this, callback: [this](QProcess *process) { |
| 93 | if (process->exitCode() == 0) { |
| 94 | m_fstabDevice->broadcastActionDone(QStringLiteral("setup" ), error: Solid::NoError, errorString: QString()); |
| 95 | } else { |
| 96 | m_fstabDevice->broadcastActionDone(QStringLiteral("setup" ), |
| 97 | error: Solid::UnauthorizedOperation, |
| 98 | errorString: QString::fromUtf8(ba: process->readAllStandardError().trimmed())); |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | void FstabStorageAccess::slotSetupRequested() |
| 104 | { |
| 105 | Q_EMIT setupRequested(m_fstabDevice->udi()); |
| 106 | } |
| 107 | |
| 108 | bool FstabStorageAccess::teardown() |
| 109 | { |
| 110 | if (filePath().isEmpty()) { |
| 111 | return false; |
| 112 | } |
| 113 | m_fstabDevice->broadcastActionRequested(QStringLiteral("teardown" )); |
| 114 | return FstabHandling::callSystemCommand(QStringLiteral("umount" ), args: {filePath()}, recvr: this, callback: [this](QProcess *process) { |
| 115 | if (process->exitCode() == 0) { |
| 116 | m_fstabDevice->broadcastActionDone(QStringLiteral("teardown" ), error: Solid::NoError); |
| 117 | } else if (process->exitCode() == EBUSY) { |
| 118 | m_fstabDevice->broadcastActionDone(QStringLiteral("teardown" ), error: Solid::DeviceBusy); |
| 119 | } else if (process->exitCode() == EPERM) { |
| 120 | m_fstabDevice->broadcastActionDone(QStringLiteral("teardown" ), |
| 121 | error: Solid::UnauthorizedOperation, |
| 122 | errorString: QString::fromUtf8(ba: process->readAllStandardError().trimmed())); |
| 123 | } else { |
| 124 | m_fstabDevice->broadcastActionDone(QStringLiteral("teardown" ), |
| 125 | error: Solid::OperationFailed, |
| 126 | errorString: QString::fromUtf8(ba: process->readAllStandardError().trimmed())); |
| 127 | } |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | void FstabStorageAccess::slotTeardownRequested() |
| 132 | { |
| 133 | Q_EMIT teardownRequested(udi: m_fstabDevice->udi()); |
| 134 | } |
| 135 | |
| 136 | void FstabStorageAccess::slotSetupDone(int error, const QString &errorString) |
| 137 | { |
| 138 | Q_EMIT setupDone(error: static_cast<Solid::ErrorType>(error), data: errorString, udi: m_fstabDevice->udi()); |
| 139 | } |
| 140 | |
| 141 | void FstabStorageAccess::slotTeardownDone(int error, const QString &errorString) |
| 142 | { |
| 143 | Q_EMIT teardownDone(error: static_cast<Solid::ErrorType>(error), data: errorString, udi: m_fstabDevice->udi()); |
| 144 | } |
| 145 | |
| 146 | void FstabStorageAccess::onMtabChanged(const QString &device) |
| 147 | { |
| 148 | QStringList currentMountPoints = FstabHandling::currentMountPoints(device); |
| 149 | if (currentMountPoints.isEmpty()) { |
| 150 | // device umounted |
| 151 | m_filePath = FstabHandling::mountPoints(device).first(); |
| 152 | if (m_isAccessible) { |
| 153 | m_isAccessible = false; |
| 154 | Q_EMIT accessibilityChanged(accessible: false, QStringLiteral(FSTAB_UDI_PREFIX "/%1" ).arg(a: device)); |
| 155 | } |
| 156 | } else { |
| 157 | // device added |
| 158 | m_filePath = currentMountPoints.first(); |
| 159 | if (!m_isAccessible) { |
| 160 | m_isAccessible = true; |
| 161 | Q_EMIT accessibilityChanged(accessible: true, QStringLiteral(FSTAB_UDI_PREFIX "/%1" ).arg(a: device)); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | #include "moc_fstabstorageaccess.cpp" |
| 167 | |