-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathPlatformUdisks2.cpp
371 lines (313 loc) · 10.5 KB
/
PlatformUdisks2.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
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <QtCore>
#include <QtGui>
#include <QRegExp>
#include <QDir>
#include <QProgressDialog>
#include <QMessageBox>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusPendingReply>
#include "DeviceItem.h"
#include "PlatformUdisks2.h"
#include "udisks2_interface.h"
#include "udisks2_mountpoints_interface.h"
PlatformUdisks2::PlatformUdisks2(bool kioskMode, bool unsafe)
: Platform(kioskMode, unsafe)
{
qRegisterMetaType<DBUSManagerStruct>("DBUSManagerStruct");
qDBusRegisterMetaType<DBUSManagerStruct>();
qRegisterMetaType<QVariantMapMap>("QVariantMapMap");
qDBusRegisterMetaType<QVariantMapMap>();
qRegisterMetaType<ByteArrayList>("ByteArrayList");
qDBusRegisterMetaType<ByteArrayList>();
}
bool
PlatformUdisks2::udisk2Enabled()
{
QDBusInterface remoteApp("org.freedesktop.UDisks2",
"/org/freedesktop/UDisks2/Manager",
"org.freedesktop.UDisks2.Manager",
QDBusConnection::systemBus());
QVariant reply = remoteApp.property("Version");
if (reply.isNull())
return false;
return true;
}
void
PlatformUdisks2::findDevices()
{
QRegExp reg("[0-9]+$");
if (!udisk2Enabled())
{
QMessageBox msgBox;
msgBox.setText(QObject::tr("You don't have UDisks2 support."));
msgBox.exec();
return;
}
org::freedesktop::DBus::ObjectManager manager("org.freedesktop.UDisks2", "/org/freedesktop/UDisks2", QDBusConnection::systemBus());
QDBusPendingReply<DBUSManagerStruct> reply = manager.GetManagedObjects();
reply.waitForFinished();
if (reply.isError()) {
qDebug() << "Failure: " << reply.error();
exit(0);
}
Q_FOREACH(const QDBusObjectPath &path, reply.value().keys()) {
const QString udi = path.path();
if (!udi.startsWith("/org/freedesktop/UDisks2/block_devices"))
continue;
// Skip disk slices
if (udi.contains(reg))
continue;
QVariantMap blockProperties = getBlockDeviceProperties(udi);
QString drivePath = blockProperties.value("drivePath").toString();
qlonglong size = blockProperties.value("size").toLongLong();
QString devicePath = blockProperties.value("path").toString();
if (drivePath.isEmpty() || size == 0 || devicePath.isEmpty())
continue;
QVariantMap driveProperties = getDriveProperties(drivePath);
bool isUSB = driveProperties.value("isUSB").toBool();
// "Safe mode" only lists USB devices
if (!mUnsafe)
{
if (!isUSB)
continue;
}
buildDevice(blockProperties, driveProperties);
}
}
QVariantMap
PlatformUdisks2::getBlockDeviceProperties(const QString &blockDevice)
{
QVariantMap properties;
QDBusInterface remoteApp("org.freedesktop.UDisks2",
blockDevice,
"org.freedesktop.UDisks2.Block",
QDBusConnection::systemBus());
QDBusObjectPath objectPath = qvariant_cast<QDBusObjectPath>(remoteApp.property("Drive"));
QString path = objectPath.path();
properties.insert("drivePath", path);
properties.insert("path", QString(remoteApp.property("Device").toByteArray()));
properties.insert("size", remoteApp.property("Size"));
return properties;
}
QVariantMap
PlatformUdisks2::getDriveProperties(const QString &drivePath)
{
QVariantMap properties;
QDBusInterface remoteApp("org.freedesktop.UDisks2",
drivePath,
"org.freedesktop.UDisks2.Drive",
QDBusConnection::systemBus());
properties.insert("removable", remoteApp.property("Removable"));
properties.insert("vendor", remoteApp.property("Vendor"));
properties.insert("serial", remoteApp.property("Serial"));
properties.insert("model", remoteApp.property("Model"));
if (remoteApp.property("ConnectionBus").toString().toLower() == "usb")
properties.insert("isUSB", true);
else
properties.insert("isUSB", false);
return properties;
}
DeviceItem *
PlatformUdisks2::buildDevice(QVariantMap &blockProperties, QVariantMap &driveProperties) {
DeviceItem *devItem = new DeviceItem;
QString path = blockProperties.value("path").toString();
devItem->setUDI(path.mid(path.lastIndexOf("/") + 1));
qDebug() << devItem->getUDI();
isMounted(devItem->getUDI());
devItem->setPath(path);
devItem->setIsRemovable(driveProperties.value("removable").toBool());
devItem->setSize(blockProperties.value("size").toLongLong());
devItem->setModelString(driveProperties.value("model").toString());
QString vendor = driveProperties.value("vendor").toString();
if (vendor == "")
{
if (mKioskMode)
devItem->setVendorString("SUSE Studio USB Key");
else
devItem->setVendorString("Unknown Device");
}
else
{
devItem->setVendorString(vendor);
}
QString newDisplayString = QString("%1 %2 - %3 (%4 MB)")
.arg(devItem->getVendorString())
.arg(devItem->getModelString())
.arg(devItem->getPath())
.arg(devItem->getSize() / 1048576);
devItem->setDisplayString(newDisplayString);
if (mKioskMode)
{
if((devItem->getSize() / 1048576) > 200000)
{
delete devItem;
return(NULL);
}
}
// If a device is 0 megs we might as well just not list it
if ((devItem->getSize() / 1048576) > 0)
{
itemList << devItem;
}
else
{
delete devItem;
devItem = NULL;
}
return(devItem);
}
DeviceItem *
PlatformUdisks2::getNewDevice(QString newPath)
{
QVariantMap blockProperties = getBlockDeviceProperties(newPath);
QString drivePath = blockProperties.value("drivePath").toString();
qlonglong size = blockProperties.value("size").toLongLong();
QString devicePath = blockProperties.value("path").toString();
if (drivePath.isEmpty() || size == 0 || devicePath.isEmpty())
return NULL;
QVariantMap driveProperties = getDriveProperties(drivePath);
bool isUSB = driveProperties.value("isUSB").toBool();
if (!mUnsafe)
{
if (!isUSB)
return NULL;
}
return buildDevice(blockProperties, driveProperties);
}
bool
PlatformUdisks2::isMounted(QString path)
{
bool mounted = false;
QStringList partitions = getPartitionList(path);
foreach(QString partition, partitions)
{
if (isPartitionMounted(partition))
{
mounted = true;
break;
}
}
if (!mounted) {
mounted = isPartitionMounted(QString("/org/freedesktop/UDisks2/block_devices/") + path);
}
return mounted;
}
QStringList
PlatformUdisks2::getPartitionList(const QString &devicePath)
{
QStringList partitionList;
org::freedesktop::DBus::ObjectManager manager("org.freedesktop.UDisks2", "/org/freedesktop/UDisks2", QDBusConnection::systemBus());
QDBusPendingReply<DBUSManagerStruct> reply = manager.GetManagedObjects();
reply.waitForFinished();
if (reply.isError()) {
qDebug() << "Failure: " << reply.error();
exit(0);
}
QRegExp reg(QString("%1[0-9]+$").arg(devicePath));
Q_FOREACH(const QDBusObjectPath &path, reply.value().keys()) {
const QString udi = path.path();
if (!udi.startsWith("/org/freedesktop/UDisks2/block_devices"))
continue;
if (!udi.contains(reg))
continue;
partitionList << udi;
}
return partitionList;
}
bool
PlatformUdisks2::isPartitionMounted(const QString &partitionPath)
{
org::freedesktop::UDisks2::Filesystem manager("org.freedesktop.UDisks2", partitionPath, QDBusConnection::systemBus());
ByteArrayList reply = manager.mountPoints();
if (reply.isEmpty())
{
qDebug() << partitionPath << "not mounted";
return false;
}
return true;
}
bool
PlatformUdisks2::unmountDevice(QString path)
{
bool res = true;
QStringList partitions = getPartitionList(path);
if (partitions.empty())
{
res = doUnmount(QString("/org/freedesktop/UDisks2/block_devices/") + path);
}
else
{
foreach(QString partition, partitions)
{
if (isPartitionMounted(partition) && !doUnmount(partition))
{
res = false;
break;
}
}
}
return(res);
}
bool
PlatformUdisks2::doUnmount(const QString &partitionPath)
{
bool ret = true;
QDBusConnection connection = QDBusConnection::systemBus();
QList<QVariant> args;
QVariantMap map;
args << map;
QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.UDisks2", partitionPath, "org.freedesktop.UDisks2.Filesystem", "Unmount");
message.setArguments(args);
QDBusMessage reply = connection.call(message);
if (reply.type() == QDBusMessage::ErrorMessage)
{
QMessageBox msgBox;
msgBox.setText(QString("DBUS error (%1): %2").arg(partitionPath).arg(reply.errorMessage()));
msgBox.exec();
ret = false;
}
return(ret);
}
int
PlatformUdisks2::open(DeviceItem* item)
{
int ret = -1;
QDBusConnection connection = QDBusConnection::systemBus();
QList<QVariant> args;
QVariantMap map;
args << map;
QString udi = QString("/org/freedesktop/UDisks2/block_devices/%1").arg(item->getUDI());
QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.UDisks2", udi, "org.freedesktop.UDisks2.Block", "OpenForRestore");
message.setArguments(args);
QDBusMessage reply = connection.call(message);
if (reply.type() == QDBusMessage::ErrorMessage)
{
QMessageBox msgBox;
msgBox.setText(QString("DBUS error (%1): %2").arg(item->getUDI()).arg(reply.errorMessage()));
msgBox.exec();
ret = -1;
}
else if (reply.arguments().empty() || reply.arguments()[0].userType() != qMetaTypeId<QDBusUnixFileDescriptor>())
{
QMessageBox msgBox;
msgBox.setText(QString("got empty or invalid reply!?"));
msgBox.exec();
errno = EINVAL;
}
else
{
QDBusUnixFileDescriptor fd(qvariant_cast<QDBusUnixFileDescriptor>(reply.arguments()[0]));
if (fd.isValid())
{
ret = ::dup(fd.fileDescriptor());
}
}
qDebug() << "ret" << ret;
return ret;
}