/
udisks2block.cpp
460 lines (399 loc) · 15.1 KB
/
udisks2block.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
109
110
111
112
113
114
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "udisks2block_p.h"
#include "udisks2defines.h"
#include "logging_p.h"
#include <nemo-dbus/dbus.h>
#include <QDBusInterface>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
UDisks2::Block::Block(const QString &path, const UDisks2::InterfacePropertyMap &interfacePropertyMap, QObject *parent)
: QObject(parent)
, m_path(path)
, m_interfacePropertyMap(interfacePropertyMap)
, m_data(interfacePropertyMap.value(UDISKS2_BLOCK_INTERFACE))
, m_connection(QDBusConnection::systemBus())
, m_mountable(interfacePropertyMap.contains(UDISKS2_FILESYSTEM_INTERFACE))
, m_encrypted(interfacePropertyMap.contains(UDISKS2_ENCRYPTED_INTERFACE))
, m_formatting(false)
, m_locking(false)
, m_pendingFileSystem(nullptr)
, m_pendingBlock(nullptr)
, m_pendingEncrypted(nullptr)
, m_pendingDrive(nullptr)
{
if (!m_connection.connect(
UDISKS2_SERVICE,
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();
}
QDBusInterface dbusPropertyInterface(UDISKS2_SERVICE,
m_path,
DBUS_OBJECT_PROPERTIES_INTERFACE,
m_connection);
qCInfo(lcMemoryCardLog) << "Creating a new block. Mountable:" << m_mountable << ", encrypted:" << m_encrypted << "object path:" << m_path << "data is empty:" << m_data.isEmpty();
if (m_data.isEmpty()) {
getFileSystemInterface();
getEncryptedInterface();
QDBusPendingCall pendingCall = dbusPropertyInterface.asyncCall(DBUS_GET_ALL, UDISKS2_BLOCK_INTERFACE);
m_pendingBlock = new QDBusPendingCallWatcher(pendingCall, this);
connect(m_pendingBlock, &QDBusPendingCallWatcher::finished, this, [this, path](QDBusPendingCallWatcher *watcher) {
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;
getDriveProperties();
} else {
QDBusError error = watcher->error();
qCWarning(lcMemoryCardLog) << "Error reading block properties:" << error.name() << error.message();
}
m_pendingBlock->deleteLater();
m_pendingBlock = nullptr;
complete();
});
} else {
if (m_mountable) {
QVariantMap map = interfacePropertyMap.value(UDISKS2_FILESYSTEM_INTERFACE);
updateMountPoint(map);
}
getDriveProperties();
// We have either org.freedesktop.UDisks2.Filesystem or org.freedesktop.UDisks2.Encrypted interface.
complete();
}
connect(this, &Block::completed, this, [this]() {
clearFormattingState();
});
}
// 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_drive = other.m_drive;
m_mountable = other.m_mountable;
m_mountPath = other.m_mountPath;
m_encrypted = other.m_encrypted;
m_formatting = other.m_formatting;
m_locking = other.m_locking;
return *this;
}
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();
}
QString UDisks2::Block::connectionBus() const
{
QString bus = NemoDBus::demarshallDBusArgument(m_drive.value(QStringLiteral("ConnectionBus"))).toString();
// Do a bit of guesswork as we're missing connection between unlocked crypto block to crypto backing block device
// from where we could see the drive where this block belongs to.
if (bus != QLatin1String("/") && hasCryptoBackingDevice()) {
QString cryptoBackingPath = cryptoBackingDevicePath();
if (cryptoBackingPath.contains(QLatin1String("mmcblk"))) {
return QStringLiteral("sdio");
} else if (cryptoBackingPath.startsWith(QLatin1String("/dev/sd"))) {
return QStringLiteral("usb");
}
return QStringLiteral("ieee1394");
}
return bus;
}
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();
}
bool UDisks2::Block::isCryptoBlock() const
{
return isEncrypted() || hasCryptoBackingDevice();
}
bool UDisks2::Block::hasCryptoBackingDevice() const
{
const QString cryptoBackingDev = cryptoBackingDeviceObjectPath();
return cryptoBackingDev != QLatin1String("/");
}
QString UDisks2::Block::cryptoBackingDevicePath() const
{
const QString object = cryptoBackingDeviceObjectPath();
return Block::cryptoBackingDevicePath(object);
}
QString UDisks2::Block::cryptoBackingDeviceObjectPath() const
{
return value(UDisks2::cryptoBackingDeviceKey).toString();
}
bool UDisks2::Block::isEncrypted() const
{
return m_encrypted;
}
bool UDisks2::Block::setEncrypted(bool encrypted)
{
if (m_encrypted != encrypted) {
m_encrypted = encrypted;
emit updated();
return true;
}
return false;
}
bool UDisks2::Block::isMountable() const
{
return m_mountable;
}
bool UDisks2::Block::setMountable(bool mountable)
{
if (m_mountable != mountable) {
m_mountable = mountable;
emit updated();
return true;
}
return false;
}
bool UDisks2::Block::isFormatting() const
{
return m_formatting;
}
bool UDisks2::Block::setFormatting(bool formatting)
{
if (m_formatting != formatting) {
m_formatting = formatting;
emit updated();
return true;
}
return false;
}
bool UDisks2::Block::isLocking() const
{
return m_locking;
}
void UDisks2::Block::setLocking()
{
m_locking = true;
}
bool UDisks2::Block::isReadOnly() const
{
return value(QStringLiteral("ReadOnly")).toBool();
}
bool UDisks2::Block::isExternal() const
{
const QString prefDevice = preferredDevice();
return prefDevice != QStringLiteral("/dev/sailfish/home") && prefDevice != QStringLiteral("/dev/sailfish/root");
}
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();
}
void UDisks2::Block::dumpInfo() const
{
qCInfo(lcMemoryCardLog) << "Block device:" << device() << "Preferred device:" << preferredDevice();
qCInfo(lcMemoryCardLog) << "- drive:" << drive() << "device number:" << deviceNumber() << "connection bus:" << connectionBus();
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();
qCInfo(lcMemoryCardLog) << "- isencrypted:" << isEncrypted() << "crypto backing device:" << cryptoBackingDevicePath();
}
QString UDisks2::Block::cryptoBackingDevicePath(const QString &objectPath)
{
if (objectPath == QLatin1String("/") || objectPath.isEmpty()) {
return QString();
} else {
QString deviceName = objectPath.section(QChar('/'), 5);
return QString("/dev/%1").arg(deviceName);
}
}
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));
for (QMap<QString, QVariant>::const_iterator i = changedProperties.constBegin(); i != changedProperties.constEnd(); ++i) {
m_data.insert(i.key(), i.value());
}
if (!clearFormattingState()) {
emit updated();
}
} else if (interface == UDISKS2_FILESYSTEM_INTERFACE) {
updateMountPoint(arguments.value(1));
}
}
bool UDisks2::Block::isCompleted() const
{
return !m_pendingFileSystem && !m_pendingBlock && !m_pendingEncrypted && !m_pendingDrive;
}
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;
}
}
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;
emit mountPathChanged();
}
void UDisks2::Block::complete()
{
if (isCompleted()) {
QMetaObject::invokeMethod(this, "completed", Qt::QueuedConnection);
}
}
bool UDisks2::Block::clearFormattingState()
{
if (isCompleted() && isMountable() && isFormatting()) {
return setFormatting(false);
}
return false;
}
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();
});
}
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();
});
}
void UDisks2::Block::getDriveProperties()
{
QDBusInterface drivePropertyInterface(UDISKS2_SERVICE,
drive(),
DBUS_OBJECT_PROPERTIES_INTERFACE,
m_connection);
QDBusPendingCall pendingCall = drivePropertyInterface.asyncCall(DBUS_GET_ALL, UDISKS2_DRIVE_INTERFACE);
m_pendingDrive = new QDBusPendingCallWatcher(pendingCall, this);
connect(m_pendingDrive, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
if (watcher->isValid() && watcher->isFinished()) {
QDBusPendingReply<> reply = *watcher;
QDBusMessage message = reply.reply();
QVariantMap driveProperties = NemoDBus::demarshallArgument<QVariantMap>(message.arguments().at(0));
qCInfo(lcMemoryCardLog) << "Drive properties:" << driveProperties;
m_drive = driveProperties;
} else {
QDBusError error = watcher->error();
qCWarning(lcMemoryCardLog) << "Error reading drive properties:" << error.name() << error.message();
m_drive.clear();
}
m_pendingDrive->deleteLater();
m_pendingDrive = nullptr;
complete();
});
}