udisks2block.cpp 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
#include "udisks2block_p.h"
#include "udisks2defines.h"
#include "logging_p.h"

#include <nemo-dbus/dbus.h>
#include <QDBusInterface>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>

10
UDisks2::Block::Block(const QString &path, const UDisks2::InterfacePropertyMap &interfacePropertyMap, QObject *parent)
11 12
    : QObject(parent)
    , m_path(path)
13 14
    , m_interfacePropertyMap(interfacePropertyMap)
    , m_data(interfacePropertyMap.value(UDISKS2_BLOCK_INTERFACE))
15
    , m_connection(QDBusConnection::systemBus())
16 17
    , m_mountable(interfacePropertyMap.contains(UDISKS2_FILESYSTEM_INTERFACE))
    , m_encrypted(interfacePropertyMap.contains(UDISKS2_ENCRYPTED_INTERFACE))
18
    , m_formatting(false)
19
    , m_locking(false)
20 21
    , m_pendingFileSystem(nullptr)
    , m_pendingBlock(nullptr)
22
    , m_pendingEncrypted(nullptr)
23 24 25 26 27
{
    if (!m_connection.connect(
                UDISKS2_SERVICE,
                m_path,
                DBUS_OBJECT_PROPERTIES_INTERFACE,
28
                UDisks2::propertiesChangedSignal,
29 30 31 32 33 34 35 36 37 38
                this,
                SLOT(updateProperties(QDBusMessage)))) {
        qCWarning(lcMemoryCardLog) << "Failed to connect to Block properties change interface" << m_path << m_connection.lastError().message();
    }

    QDBusInterface dbusPropertyInterface(UDISKS2_SERVICE,
                                    m_path,
                                    DBUS_OBJECT_PROPERTIES_INTERFACE,
                                    m_connection);

39
    qCInfo(lcMemoryCardLog) << "Creating a new block. Mountable:" << m_mountable << ", encrypted:" << m_encrypted << "object path:" << m_path << "data is empty:" << m_data.isEmpty();
40 41

    if (m_data.isEmpty()) {
42 43
        getFileSystemInterface();
        getEncryptedInterface();
44
        QDBusPendingCall pendingCall = dbusPropertyInterface.asyncCall(DBUS_GET_ALL, UDISKS2_BLOCK_INTERFACE);
45 46
        m_pendingBlock = new QDBusPendingCallWatcher(pendingCall, this);
        connect(m_pendingBlock, &QDBusPendingCallWatcher::finished, this, [this, path](QDBusPendingCallWatcher *watcher) {
47 48 49 50 51 52 53 54 55 56
            if (watcher->isValid() && watcher->isFinished()) {
                QDBusPendingReply<> reply =  *watcher;
                QDBusMessage message = reply.reply();
                QVariantMap blockProperties = NemoDBus::demarshallArgument<QVariantMap>(message.arguments().at(0));
                qCInfo(lcMemoryCardLog) << "Block properties:" << blockProperties;
                m_data = blockProperties;
            } else {
                QDBusError error = watcher->error();
                qCWarning(lcMemoryCardLog) << "Error reading block properties:" << error.name() << error.message();
            }
57
            m_pendingBlock->deleteLater();
58 59
            m_pendingBlock = nullptr;
            complete();
60
        });
61
    } else {
62 63 64 65 66 67
        if (m_mountable) {
            QVariantMap map = interfacePropertyMap.value(UDISKS2_FILESYSTEM_INTERFACE);
            updateMountPoint(map);
        }

        // We have either org.freedesktop.UDisks2.Filesystem or org.freedesktop.UDisks2.Encrypted interface.
68
        complete();
69
    }
70 71 72 73

    connect(this, &Block::completed, this, [this]() {
        clearFormattingState();
    });
74 75
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
// Use when morphing a block e.g. updating encrypted block to crypto backing block device (e.i. to a block that implements file system).
UDisks2::Block &UDisks2::Block::operator=(const UDisks2::Block &other)
{
    if (&other == this)
        return *this;

    if (!this->m_connection.disconnect(
                UDISKS2_SERVICE,
                m_path,
                DBUS_OBJECT_PROPERTIES_INTERFACE,
                UDisks2::propertiesChangedSignal,
                this,
                SLOT(updateProperties(QDBusMessage)))) {
        qCWarning(lcMemoryCardLog) << "Failed to disconnect to Block properties change interface" << m_path << m_connection.lastError().message();
    }

    this->m_path = other.m_path;

    if (!this->m_connection.connect(
                UDISKS2_SERVICE,
                this->m_path,
                DBUS_OBJECT_PROPERTIES_INTERFACE,
                UDisks2::propertiesChangedSignal,
                this,
                SLOT(updateProperties(QDBusMessage)))) {
        qCWarning(lcMemoryCardLog) << "Failed to connect to Block properties change interface" << m_path << m_connection.lastError().message();
    }

    m_interfacePropertyMap = other.m_interfacePropertyMap;
    m_data = other.m_data;
    m_mountable = other.m_mountable;
    m_mountPath = other.m_mountPath;
    m_encrypted = other.m_encrypted;
109 110
    m_formatting = other.m_formatting;
    m_locking = other.m_locking;
111 112 113 114

    return *this;
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
UDisks2::Block::~Block()
{
}

QString UDisks2::Block::path() const
{
    return m_path;
}

QString UDisks2::Block::device() const
{
    QByteArray d = m_data.value(QStringLiteral("Device")).toByteArray();
    return QString::fromLocal8Bit(d);
}

QString UDisks2::Block::preferredDevice() const
{
    QByteArray d = m_data.value(QStringLiteral("PreferredDevice")).toByteArray();
    return QString::fromLocal8Bit(d);
}

QString UDisks2::Block::drive() const
{
    return value(QStringLiteral("Drive")).toString();
}

qint64 UDisks2::Block::deviceNumber() const
{
    return value(QStringLiteral("DeviceNumber")).toLongLong();
}

QString UDisks2::Block::id() const
{
    return value(QStringLiteral("Id")).toString();
}

qint64 UDisks2::Block::size() const
{
    return value(QStringLiteral("Size")).toLongLong();
}

156 157 158 159 160 161
bool UDisks2::Block::hasCryptoBackingDevice() const
{
    const QString cryptoBackingDev = cryptoBackingDeviceObjectPath();
    return cryptoBackingDev != QLatin1String("/");
}

162
QString UDisks2::Block::cryptoBackingDevicePath() const
163 164
{
    const QString object = cryptoBackingDeviceObjectPath();
165
    return Block::cryptoBackingDevicePath(object);
166 167 168 169 170 171 172 173 174 175 176 177
}

QString UDisks2::Block::cryptoBackingDeviceObjectPath() const
{
    return value(UDisks2::cryptoBackingDeviceKey).toString();
}

bool UDisks2::Block::isEncrypted() const
{
    return m_encrypted;
}

178
bool UDisks2::Block::setEncrypted(bool encrypted)
179 180 181 182
{
    if (m_encrypted != encrypted) {
        m_encrypted = encrypted;
        emit updated();
183
        return true;
184
    }
185
    return false;
186 187
}

188 189
bool UDisks2::Block::isMountable() const
{
190
    return m_mountable;
191 192
}

193
bool UDisks2::Block::setMountable(bool mountable)
194 195 196 197
{
    if (m_mountable != mountable) {
        m_mountable = mountable;
        emit updated();
198
        return true;
199
    }
200
    return false;
201 202
}

203
bool UDisks2::Block::isFormatting() const
204
{
205 206 207 208 209 210 211 212 213 214 215
    return m_formatting;
}

bool UDisks2::Block::setFormatting(bool formatting)
{
    if (m_formatting != formatting) {
        m_formatting = formatting;
        emit updated();
        return true;
    }
    return false;
216 217
}

218 219 220 221 222 223 224 225 226 227
bool UDisks2::Block::isLocking() const
{
    return m_locking;
}

void UDisks2::Block::setLocking()
{
    m_locking = true;
}

228 229 230 231 232
bool UDisks2::Block::isReadOnly() const
{
    return value(QStringLiteral("ReadOnly")).toBool();
}

233 234 235 236 237 238
bool UDisks2::Block::isExternal() const
{
    const QString prefDevice = preferredDevice();
    return prefDevice != QStringLiteral("/dev/sailfish/home") && prefDevice != QStringLiteral("/dev/sailfish/root");
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
QString UDisks2::Block::idType() const
{
    return value(QStringLiteral("IdType")).toString();
}

QString UDisks2::Block::idVersion() const
{
    return value(QStringLiteral("IdVersion")).toString();
}

QString UDisks2::Block::idLabel() const
{
    return value(QStringLiteral("IdLabel")).toString();
}

QString UDisks2::Block::idUUID() const
{
    return value(QStringLiteral("IdUUID")).toString();
}

QString UDisks2::Block::mountPath() const
{
    return m_mountPath;
}

QVariant UDisks2::Block::value(const QString &key) const
{
    return NemoDBus::demarshallDBusArgument(m_data.value(key));
}

bool UDisks2::Block::hasData() const
{
    return !m_data.isEmpty();
}

274 275 276 277 278 279 280 281 282
void UDisks2::Block::dumpInfo() const
{
    qCInfo(lcMemoryCardLog) << "Block device:" << device() << "Preferred device:" << preferredDevice();
    qCInfo(lcMemoryCardLog) << "- drive:" << drive() << "dNumber:" << deviceNumber();
    qCInfo(lcMemoryCardLog) << "- id:" << id() << "size:" << size();
    qCInfo(lcMemoryCardLog) << "- isreadonly:" << isReadOnly() << "idtype:" << idType();
    qCInfo(lcMemoryCardLog) << "- idversion:" << idVersion() << "idlabel:" << idLabel();
    qCInfo(lcMemoryCardLog) << "- iduuid:" << idUUID();
    qCInfo(lcMemoryCardLog) << "- ismountable:" << isMountable() << "mount path:" << mountPath();
283
    qCInfo(lcMemoryCardLog) << "- isencrypted:" << isEncrypted() << "crypto backing device:" << cryptoBackingDevicePath();
284 285
}

286
QString UDisks2::Block::cryptoBackingDevicePath(const QString &objectPath)
287 288 289 290 291 292 293 294 295
{
    if (objectPath == QLatin1String("/") || objectPath.isEmpty()) {
        return QString();
    } else {
        QString deviceName = objectPath.section(QChar('/'), 5);
        return QString("/dev/%1").arg(deviceName);
    }
}

296 297 298 299 300 301
void UDisks2::Block::updateProperties(const QDBusMessage &message)
{
    QList<QVariant> arguments = message.arguments();
    QString interface = arguments.value(0).toString();
    if (interface == UDISKS2_BLOCK_INTERFACE) {
        QVariantMap changedProperties = NemoDBus::demarshallArgument<QVariantMap>(arguments.value(1));
302
        for (QMap<QString, QVariant>::const_iterator i = changedProperties.constBegin(); i != changedProperties.constEnd(); ++i) {
303 304
            m_data.insert(i.key(), i.value());
        }
305 306 307 308

        if (!clearFormattingState()) {
            emit updated();
        }
309 310 311 312 313
    } else if (interface == UDISKS2_FILESYSTEM_INTERFACE) {
        updateMountPoint(arguments.value(1));
    }
}

314 315 316 317 318
bool UDisks2::Block::isCompleted() const
{
    return !m_pendingFileSystem && !m_pendingBlock && !m_pendingEncrypted;
}

319 320 321 322 323 324 325 326 327 328 329 330 331
void UDisks2::Block::updateMountPoint(const QVariant &mountPoints)
{
    QVariantMap mountPointsMap = NemoDBus::demarshallArgument<QVariantMap>(mountPoints);
    QList<QByteArray> mountPointList = NemoDBus::demarshallArgument<QList<QByteArray> >(mountPointsMap.value(QStringLiteral("MountPoints")));
    m_mountPath.clear();

    for (const QByteArray &bytes : mountPointList) {
        if (bytes.startsWith("/run")) {
            m_mountPath = QString::fromLocal8Bit(bytes);
            break;
        }
    }

332 333 334 335 336 337 338 339 340 341 342
    bool triggerUpdate = false;
    blockSignals(true);
    triggerUpdate = setMountable(true);
    triggerUpdate |= clearFormattingState();
    blockSignals(false);

    if (triggerUpdate) {
        emit updated();
    }

    qCInfo(lcMemoryCardLog) << "New file system mount points:" << mountPoints << "resolved mount path: " << m_mountPath << "trigger update:" << triggerUpdate;
343 344
    emit mountPathChanged();
}
345 346 347

void UDisks2::Block::complete()
{
348
    if (isCompleted()) {
349 350 351
        QMetaObject::invokeMethod(this, "completed", Qt::QueuedConnection);
    }
}
352

353 354 355 356 357 358 359 360
bool UDisks2::Block::clearFormattingState()
{
    if (isCompleted() && isMountable() && isFormatting()) {
        return setFormatting(false);
    }
    return false;
}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
void UDisks2::Block::getFileSystemInterface()
{
    QDBusInterface dbusPropertyInterface(UDISKS2_SERVICE,
                                    m_path,
                                    DBUS_OBJECT_PROPERTIES_INTERFACE,
                                    m_connection);
    QDBusPendingCall pendingCall = dbusPropertyInterface.asyncCall(DBUS_GET_ALL, UDISKS2_FILESYSTEM_INTERFACE);
    m_pendingFileSystem = new QDBusPendingCallWatcher(pendingCall, this);
    connect(m_pendingFileSystem, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
        if (watcher->isValid() && watcher->isFinished()) {
            QDBusPendingReply<> reply =  *watcher;
            QDBusMessage message = reply.reply();
            updateMountPoint(message.arguments().at(0));
        } else {
            QDBusError error = watcher->error();
            qCWarning(lcMemoryCardLog) << "Error reading filesystem properties:" << error.name() << error.message() << m_path;
            m_mountable = false;
        }
        m_pendingFileSystem->deleteLater();
        m_pendingFileSystem = nullptr;
        complete();
    });
}
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

void UDisks2::Block::getEncryptedInterface()
{
    QDBusInterface dbusPropertyInterface(UDISKS2_SERVICE,
                                    m_path,
                                    DBUS_OBJECT_PROPERTIES_INTERFACE,
                                    m_connection);
    QDBusPendingCall pendingCall = dbusPropertyInterface.asyncCall(DBUS_GET_ALL, UDISKS2_ENCRYPTED_INTERFACE);
    m_pendingEncrypted = new QDBusPendingCallWatcher(pendingCall, this);
    connect(m_pendingEncrypted, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
        if (watcher->isValid() && watcher->isFinished()) {
            m_encrypted = true;
        } else {
            QDBusError error = watcher->error();
            qCWarning(lcMemoryCardLog) << "Error reading encrypted properties:" << error.name() << error.message() << m_path;
            m_encrypted = false;
        }
        m_pendingEncrypted->deleteLater();
        m_pendingEncrypted = nullptr;
        complete();
    });
}