Skip to content
This repository was archived by the owner on Apr 28, 2022. It is now read-only.

Commit e1d9073

Browse files
committed
Merge branch 'jb43396' into 'master'
Rescan block after formatting encrypted sd card See merge request mer-core/nemo-qml-plugin-systemsettings!90
2 parents b8d96da + 969443b commit e1d9073

File tree

7 files changed

+155
-59
lines changed

7 files changed

+155
-59
lines changed

src/udisks2block.cpp

Lines changed: 96 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,44 +76,8 @@ UDisks2::Block::Block(const QString &path, const UDisks2::InterfacePropertyMap &
7676
});
7777
}
7878

79-
// Use when morphing a block e.g. updating encrypted block to crypto backing block device (e.i. to a block that implements file system).
80-
UDisks2::Block &UDisks2::Block::operator=(const UDisks2::Block &other)
79+
UDisks2::Block &UDisks2::Block::operator=(const UDisks2::Block &)
8180
{
82-
if (&other == this)
83-
return *this;
84-
85-
if (!this->m_connection.disconnect(
86-
UDISKS2_SERVICE,
87-
m_path,
88-
DBUS_OBJECT_PROPERTIES_INTERFACE,
89-
UDisks2::propertiesChangedSignal,
90-
this,
91-
SLOT(updateProperties(QDBusMessage)))) {
92-
qCWarning(lcMemoryCardLog) << "Failed to disconnect to Block properties change interface" << m_path << m_connection.lastError().message();
93-
}
94-
95-
this->m_path = other.m_path;
96-
97-
if (!this->m_connection.connect(
98-
UDISKS2_SERVICE,
99-
this->m_path,
100-
DBUS_OBJECT_PROPERTIES_INTERFACE,
101-
UDisks2::propertiesChangedSignal,
102-
this,
103-
SLOT(updateProperties(QDBusMessage)))) {
104-
qCWarning(lcMemoryCardLog) << "Failed to connect to Block properties change interface" << m_path << m_connection.lastError().message();
105-
}
106-
107-
m_interfacePropertyMap = other.m_interfacePropertyMap;
108-
m_data = other.m_data;
109-
m_drive = other.m_drive;
110-
m_mountable = other.m_mountable;
111-
m_mountPath = other.m_mountPath;
112-
m_encrypted = other.m_encrypted;
113-
m_formatting = other.m_formatting;
114-
m_locking = other.m_locking;
115-
116-
return *this;
11781
}
11882

11983
UDisks2::Block::~Block()
@@ -184,7 +148,7 @@ bool UDisks2::Block::isCryptoBlock() const
184148
bool UDisks2::Block::hasCryptoBackingDevice() const
185149
{
186150
const QString cryptoBackingDev = cryptoBackingDeviceObjectPath();
187-
return cryptoBackingDev != QLatin1String("/");
151+
return !cryptoBackingDev.isEmpty() && cryptoBackingDev != QLatin1String("/");
188152
}
189153

190154
QString UDisks2::Block::cryptoBackingDevicePath() const
@@ -264,6 +228,11 @@ bool UDisks2::Block::isExternal() const
264228
return prefDevice != QStringLiteral("/dev/sailfish/home") && prefDevice != QStringLiteral("/dev/sailfish/root");
265229
}
266230

231+
bool UDisks2::Block::isValid() const
232+
{
233+
return m_interfacePropertyMap.contains(UDISKS2_BLOCK_INTERFACE);
234+
}
235+
267236
QString UDisks2::Block::idType() const
268237
{
269238
return value(QStringLiteral("IdType")).toString();
@@ -321,6 +290,72 @@ QString UDisks2::Block::cryptoBackingDevicePath(const QString &objectPath)
321290
}
322291
}
323292

293+
void UDisks2::Block::addInterface(const QString &interface, QVariantMap propertyMap)
294+
{
295+
m_interfacePropertyMap.insert(interface, propertyMap);
296+
if (interface == UDISKS2_FILESYSTEM_INTERFACE) {
297+
setMountable(true);
298+
} else if (interface == UDISKS2_ENCRYPTED_INTERFACE) {
299+
setEncrypted(true);
300+
}
301+
}
302+
303+
void UDisks2::Block::removeInterface(const QString &interface)
304+
{
305+
m_interfacePropertyMap.remove(interface);
306+
if (interface == UDISKS2_BLOCK_INTERFACE) {
307+
m_data.clear();
308+
} else if (interface == UDISKS2_DRIVE_INTERFACE) {
309+
m_drive.clear();
310+
} else if (interface == UDISKS2_FILESYSTEM_INTERFACE) {
311+
setMountable(false);
312+
}else if (interface == UDISKS2_ENCRYPTED_INTERFACE) {
313+
setEncrypted(false);
314+
}
315+
}
316+
317+
void UDisks2::Block::morph(const UDisks2::Block &other)
318+
{
319+
if (&other == this)
320+
return;
321+
322+
if (!this->m_connection.disconnect(
323+
UDISKS2_SERVICE,
324+
m_path,
325+
DBUS_OBJECT_PROPERTIES_INTERFACE,
326+
UDisks2::propertiesChangedSignal,
327+
this,
328+
SLOT(updateProperties(QDBusMessage)))) {
329+
qCWarning(lcMemoryCardLog) << "Failed to disconnect to Block properties change interface" << m_path << m_connection.lastError().message();
330+
}
331+
332+
this->m_path = other.m_path;
333+
334+
if (!this->m_connection.connect(
335+
UDISKS2_SERVICE,
336+
this->m_path,
337+
DBUS_OBJECT_PROPERTIES_INTERFACE,
338+
UDisks2::propertiesChangedSignal,
339+
this,
340+
SLOT(updateProperties(QDBusMessage)))) {
341+
qCWarning(lcMemoryCardLog) << "Failed to connect to Block properties change interface" << m_path << m_connection.lastError().message();
342+
}
343+
344+
m_interfacePropertyMap = other.m_interfacePropertyMap;
345+
m_data = other.m_data;
346+
m_drive = other.m_drive;
347+
m_mountPath = other.m_mountPath;
348+
m_mountable = other.m_mountable;
349+
m_encrypted = other.m_encrypted;
350+
bool wasFormatting = m_formatting;
351+
m_formatting = other.m_formatting;
352+
m_locking = other.m_locking;
353+
354+
if (wasFormatting && hasCryptoBackingDevice()) {
355+
rescan(cryptoBackingDeviceObjectPath());
356+
}
357+
}
358+
324359
void UDisks2::Block::updateProperties(const QDBusMessage &message)
325360
{
326361
QList<QVariant> arguments = message.arguments();
@@ -458,3 +493,26 @@ void UDisks2::Block::getDriveProperties()
458493
complete();
459494
});
460495
}
496+
497+
void UDisks2::Block::rescan(const QString &dbusObjectPath)
498+
{
499+
QVariantList arguments;
500+
QVariantMap options;
501+
arguments << options;
502+
503+
QDBusInterface blockDeviceInterface(UDISKS2_SERVICE,
504+
dbusObjectPath,
505+
UDISKS2_BLOCK_INTERFACE,
506+
m_connection);
507+
508+
QDBusPendingCall pendingCall = blockDeviceInterface.asyncCallWithArgumentList(UDISKS2_BLOCK_RESCAN, arguments);
509+
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this);
510+
connect(watcher, &QDBusPendingCallWatcher::finished,
511+
this, [dbusObjectPath](QDBusPendingCallWatcher *watcher) {
512+
if (watcher->isError()) {
513+
QDBusError error = watcher->error();
514+
qCDebug(lcMemoryCardLog) << "UDisks failed to rescan object path" << dbusObjectPath << ", error type:" << error.type() << ",name:" << error.name() << ", message:" << error.message();
515+
}
516+
watcher->deleteLater();
517+
});
518+
}

src/udisks2block_p.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ class Block : public QObject
4949

5050
public:
5151
Block(const QString &path, const UDisks2::InterfacePropertyMap &interfacePropertyMap, QObject *parent = nullptr);
52-
Block& operator=(const Block& other);
5352

54-
~Block();
53+
virtual ~Block();
5554

5655
QString path() const;
5756

@@ -72,10 +71,7 @@ class Block : public QObject
7271
QString cryptoBackingDeviceObjectPath() const;
7372

7473
bool isEncrypted() const;
75-
bool setEncrypted(bool encrypted);
76-
7774
bool isMountable() const;
78-
bool setMountable(bool mountable);
7975

8076
bool isFormatting() const;
8177
bool setFormatting(bool formatting);
@@ -86,6 +82,8 @@ class Block : public QObject
8682
bool isReadOnly() const;
8783
bool isExternal() const;
8884

85+
bool isValid() const;
86+
8987
QString idType() const;
9088
QString idVersion() const;
9189
QString idLabel() const;
@@ -101,6 +99,11 @@ class Block : public QObject
10199

102100
static QString cryptoBackingDevicePath(const QString &objectPath);
103101

102+
void addInterface(const QString &interface, QVariantMap propertyMap);
103+
void removeInterface(const QString &interface);
104+
105+
void morph(const Block& other);
106+
104107
signals:
105108
void completed();
106109
void updated();
@@ -111,6 +114,11 @@ private slots:
111114
void updateProperties(const QDBusMessage &message);
112115

113116
private:
117+
Block& operator=(const Block& other);
118+
119+
bool setEncrypted(bool encrypted);
120+
bool setMountable(bool mountable);
121+
114122
bool isCompleted() const;
115123
void updateMountPoint(const QVariant &mountPoints);
116124
void complete();
@@ -120,6 +128,8 @@ private slots:
120128
void getEncryptedInterface();
121129
void getDriveProperties();
122130

131+
void rescan(const QString &dbusObjectPath);
132+
123133
QString m_path;
124134
UDisks2::InterfacePropertyMap m_interfacePropertyMap;
125135
QVariantMap m_data;

src/udisks2defines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Q_DECLARE_METATYPE(UDisks2::InterfacePropertyMap)
8181
#define UDISKS2_ENCRYPTED_UNLOCK QLatin1String("Unlock")
8282
#define UDISKS2_FILESYSTEM_MOUNT QLatin1String("Mount")
8383
#define UDISKS2_FILESYSTEM_UNMOUNT QLatin1String("Unmount")
84+
#define UDISKS2_BLOCK_RESCAN QLatin1String("Rescan")
8485

8586
// Errors
8687
#define UDISKS2_ERROR_DEVICE_BUSY QLatin1String("org.freedesktop.UDisks2.Error.DeviceBusy")

src/udisks2job.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ UDisks2::Job::Job(const QString &path, const QVariantMap &data, QObject *parent)
5858
}
5959

6060
connect(Monitor::instance(), &Monitor::errorMessage, this, [this](const QString &objectPath, const QString &errorName) {
61-
QStringList objects = value(UDISKS2_JOB_KEY_OBJECTS).toStringList();
62-
if (objects.contains(objectPath) && errorName == UDISKS2_ERROR_DEVICE_BUSY) {
61+
if (objects().contains(objectPath) && errorName == UDISKS2_ERROR_DEVICE_BUSY) {
6362
m_message = errorName;
6463
if (!isCompleted() && deviceBusy()) {
6564
updateCompleted(false, m_message);
@@ -100,6 +99,11 @@ bool UDisks2::Job::deviceBusy() const
10099
return m_message == UDISKS2_ERROR_TARGET_BUSY || m_message == UDISKS2_ERROR_DEVICE_BUSY;
101100
}
102101

102+
QStringList UDisks2::Job::objects() const
103+
{
104+
return value(UDISKS2_JOB_KEY_OBJECTS).toStringList();
105+
}
106+
103107
QString UDisks2::Job::path() const
104108
{
105109
return m_path;

src/udisks2job_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class Job : public QObject
6868
QString message() const;
6969
bool deviceBusy() const;
7070

71+
QStringList objects() const;
72+
7173
QString path() const;
7274
QVariant value(const QString &key) const;
7375

src/udisks2monitor.cpp

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,18 @@ void UDisks2::Monitor::interfacesAdded(const QDBusObjectPath &objectPath, const
226226
// External device must have file system or partition so that it can added to the model.
227227
// Devices without partition table have filesystem interface.
228228

229-
if ((interfaces.contains(UDISKS2_FILESYSTEM_INTERFACE) ||
230-
interfaces.contains(UDISKS2_ENCRYPTED_INTERFACE)) && externalBlockDevice(path)) {
231-
229+
if (path.startsWith(QStringLiteral("/org/freedesktop/UDisks2/block_devices/")) && externalBlockDevice(path)) {
232230
if (m_blockDevices.contains(path)) {
233231
UDisks2::Block *block = m_blockDevices.value(path);
234232
if (interfaces.contains(UDISKS2_FILESYSTEM_INTERFACE)) {
235-
block->setMountable(true);
233+
block->addInterface(UDISKS2_FILESYSTEM_INTERFACE, interfaces.value(UDISKS2_FILESYSTEM_INTERFACE));
236234
}
237235
if (interfaces.contains(UDISKS2_ENCRYPTED_INTERFACE)) {
238-
block->setEncrypted(true);
236+
block->addInterface(UDISKS2_ENCRYPTED_INTERFACE, interfaces.value(UDISKS2_ENCRYPTED_INTERFACE));
239237
}
240238
} else {
241-
QVariantMap dict = interfaces.value(UDISKS2_BLOCK_INTERFACE);
242-
createBlockDevice(path, interfaces);
239+
UDisks2::Block *block = createBlockDevice(path, interfaces);
240+
updateFormattingState(block);
243241
}
244242
} else if (path.startsWith(QStringLiteral("/org/freedesktop/UDisks2/jobs"))) {
245243
QVariantMap dict = interfaces.value(UDISKS2_JOB_INTERFACE);
@@ -257,6 +255,17 @@ void UDisks2::Monitor::interfacesAdded(const QDBusObjectPath &objectPath, const
257255
UDisks2::Job *job = qobject_cast<UDisks2::Job *>(sender());
258256
updatePartitionStatus(job, success);
259257
});
258+
259+
if (job->operation() == Job::Format) {
260+
for (const QString &objectPath : job->objects()) {
261+
if (UDisks2::Block *block = m_blockDevices.value(objectPath, nullptr)) {
262+
block->blockSignals(true);
263+
block->setFormatting(true);
264+
block->blockSignals(false);
265+
}
266+
}
267+
}
268+
260269
m_jobsToWait.insert(path, job);
261270
}
262271
}
@@ -296,10 +305,10 @@ void UDisks2::Monitor::interfacesRemoved(const QDBusObjectPath &objectPath, cons
296305
} else if (m_blockDevices.contains(path)) {
297306
UDisks2::Block *block = m_blockDevices.value(path);
298307
if (interfaces.contains(UDISKS2_FILESYSTEM_INTERFACE)) {
299-
block->setMountable(false);
308+
block->removeInterface(UDISKS2_FILESYSTEM_INTERFACE);
300309
}
301310
if (interfaces.contains(UDISKS2_ENCRYPTED_INTERFACE)) {
302-
block->setEncrypted(false);
311+
block->removeInterface(UDISKS2_ENCRYPTED_INTERFACE);
303312
}
304313
}
305314
}
@@ -369,7 +378,7 @@ void UDisks2::Monitor::updatePartitionStatus(const UDisks2::Job *job, bool succe
369378
{
370379
UDisks2::Job::Operation operation = job->operation();
371380
PartitionManagerPrivate::Partitions affectedPartitions;
372-
lookupPartitions(affectedPartitions, job->value(UDISKS2_JOB_KEY_OBJECTS).toStringList());
381+
lookupPartitions(affectedPartitions, job->objects());
373382
if (operation == UDisks2::Job::Lock || operation == UDisks2::Job::Unlock) {
374383
for (auto partition : affectedPartitions) {
375384
Partition::Status oldStatus = partition->status;
@@ -648,13 +657,10 @@ UDisks2::Block *UDisks2::Monitor::createBlockDevice(const QString &dbusObjectPat
648657
if (completedBlock->hasCryptoBackingDevice() && m_blockDevices.contains(cryptoBackingDeviceObjectPath)) {
649658
// Update crypto backing device to file system device.
650659
UDisks2::Block *cryptoBackingDev = m_blockDevices.value(cryptoBackingDeviceObjectPath);
651-
*cryptoBackingDev = *completedBlock;
652-
660+
cryptoBackingDev->morph(*completedBlock);
653661
m_blockDevices.remove(cryptoBackingDeviceObjectPath);
654662
m_blockDevices.insert(cryptoBackingDev->path(), cryptoBackingDev);
655-
656663
updatePartitionProperties(cryptoBackingDev);
657-
658664
completedBlock->deleteLater();
659665
} else if (!m_blockDevices.contains(completedBlock->path())) {
660666
m_blockDevices.insert(completedBlock->path(), completedBlock);
@@ -677,6 +683,7 @@ UDisks2::Block *UDisks2::Monitor::createBlockDevice(const QString &dbusObjectPat
677683
} else {
678684
// This is garbage block device that should not be exposed
679685
// from the partition model.
686+
completedBlock->removeInterface(UDISKS2_BLOCK_INTERFACE);
680687
completedBlock->deleteLater();
681688
}
682689
});
@@ -809,6 +816,19 @@ UDisks2::Block *UDisks2::Monitor::findBlock(const QString &devicePath) const
809816
return nullptr;
810817
}
811818

819+
void UDisks2::Monitor::updateFormattingState(UDisks2::Block *block)
820+
{
821+
UDisks2::Block *cryptoBackingDevice = nullptr;
822+
QString cryptoBackingDevicePath = block->cryptoBackingDeviceObjectPath();
823+
824+
// If we have crypto backing device, copy over formatting state.
825+
if (cryptoBackingDevicePath != QLatin1String("/") && (cryptoBackingDevice = m_blockDevices.value(cryptoBackingDevicePath, nullptr))) {
826+
block->blockSignals(true);
827+
block->setFormatting(cryptoBackingDevice->isFormatting());
828+
block->blockSignals(false);
829+
}
830+
}
831+
812832
QString UDisks2::Monitor::objectPath(const QString &devicePath) const
813833
{
814834
for (QMap<QString, Block *>::const_iterator i = m_blockDevices.begin(); i != m_blockDevices.end(); ++i) {

src/udisks2monitor_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ private slots:
118118
void getBlockDevices();
119119

120120
Block *findBlock(const QString &devicePath) const;
121+
void updateFormattingState(UDisks2::Block *block);
121122

122123
private:
123124
static Monitor *sharedInstance;

0 commit comments

Comments
 (0)