Reworked code for eject/mount/unmount operations to support new TDEStorageOpResult return type.

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
pull/36/head
Michele Calgaro 6 years ago
parent 7214a7b6b5
commit bf68342793
Signed by: MicheleC
GPG Key ID: 2A75B7CA8ADED5CF

@ -18,8 +18,9 @@
*/ */
#include "disksHelper.h" #include "disksHelper.h"
#include "tdelocale.h"
#include "tdestoragedevice.h" #include "tdestoragedevice.h"
#include <tqvariant.h>
#include <tqdbusdata.h> #include <tqdbusdata.h>
#include <tqdbusmessage.h> #include <tqdbusmessage.h>
#include <tqdbusproxy.h> #include <tqdbusproxy.h>
@ -33,7 +34,10 @@
//------------------------------- //-------------------------------
// UDisks // UDisks
//------------------------------- //-------------------------------
bool ejectDriveUDisks(TDEStorageDevice* sdevice) { TDEStorageOpResult UDisksEjectDrive(TDEStorageDevice *sdevice) {
TDEStorageOpResult result;
result["result"] = false;
TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if (dbusConn.isConnected()) { if (dbusConn.isConnected()) {
TQString blockDeviceString = sdevice->deviceNode(); TQString blockDeviceString = sdevice->deviceNode();
@ -51,18 +55,23 @@ bool ejectDriveUDisks(TDEStorageDevice* sdevice) {
TQT_DBusMessage reply = driveControl.sendWithReply("DriveEject", params, &error); TQT_DBusMessage reply = driveControl.sendWithReply("DriveEject", params, &error);
if (error.isValid()) { if (error.isValid()) {
// Error! // Error!
printf("[ERROR][tdehwlib] ejectDriveUDisks: %s\n", error.name().ascii()); fflush(stdout); result["errStr"] = error.name() + ": " + error.message();
return false; return result;
} }
else { else {
return true; result["result"] = true;
return result;
} }
} }
} }
return false; return result;
} }
int mountDriveUDisks(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions, TQString* errStr) { TDEStorageOpResult UDisksMountDrive(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions) {
TDEStorageOpResult result;
result["result"] = false;
result["retcode"] = -2;
TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if (dbusConn.isConnected()) { if (dbusConn.isConnected()) {
TQString blockDeviceString = deviceNode; TQString blockDeviceString = deviceNode;
@ -78,32 +87,33 @@ int mountDriveUDisks(TQString deviceNode, TQString fileSystemType, TQStringList
params << TQT_DBusData::fromString(fileSystemType); params << TQT_DBusData::fromString(fileSystemType);
params << TQT_DBusData::fromList(TQT_DBusDataList(mountOptions)); params << TQT_DBusData::fromList(TQT_DBusDataList(mountOptions));
TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemMount", params, &error); TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemMount", params, &error);
if (error.isValid()) { if (!error.isValid()) {
// Error! // Success
if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") { result["retcode"] = 0;
// Service not installed or unavailable result["result"] = true;
return -2; return result;
}
if (errStr) {
*errStr = error.name() + ": " + error.message();
} }
else { else {
printf("[ERROR][tdehwlib] mountDriveUDisks: %s\n", error.name().ascii()); fflush(stdout); // Error!
} if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
return -1; return result; // Service not installed or unavailable
} }
else { else {
return 0; result["errStr"] = error.name() + ": " + error.message();
result["retcode"] = -1;
return result;
} }
} }
else {
return -2;
} }
} }
return -2; return result;
} }
int unMountDriveUDisks(TQString deviceNode, TQStringList unMountOptions, TQString* errStr) { TDEStorageOpResult UDisksUnmountDrive(TQString deviceNode, TQStringList unmountOptions) {
TDEStorageOpResult result;
result["result"] = false;
result["retcode"] = -2;
TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if (dbusConn.isConnected()) { if (dbusConn.isConnected()) {
TQString blockDeviceString = deviceNode; TQString blockDeviceString = deviceNode;
@ -116,37 +126,37 @@ int unMountDriveUDisks(TQString deviceNode, TQStringList unMountOptions, TQStrin
TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn); TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn);
if (driveControl.canSend()) { if (driveControl.canSend()) {
TQValueList<TQT_DBusData> params; TQValueList<TQT_DBusData> params;
params << TQT_DBusData::fromList(TQT_DBusDataList(unMountOptions)); params << TQT_DBusData::fromList(TQT_DBusDataList(unmountOptions));
TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemUnmount", params, &error); TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemUnmount", params, &error);
if (error.isValid()) { if (!error.isValid()) {
// Error! // Success
if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") { result["retcode"] = 0;
// Service not installed or unavailable result["result"] = true;
return -2; return result;
}
if (errStr) {
*errStr = error.name() + ": " + error.message();
} }
else { else {
printf("[ERROR][tdehwlib] unMountDriveUDisks: %s\n", error.name().ascii()); fflush(stdout); // Error!
} if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
return -1; return result; // Service not installed or unavailable
} }
else { else {
return 0; result["errStr"] = error.name() + ": " + error.message();
result["retcode"] = -1;
return result;
} }
} }
else {
return -2;
} }
} }
return -2; return result;
} }
//------------------------------- //-------------------------------
// UDisks2 // UDisks2
//------------------------------- //-------------------------------
bool ejectDriveUDisks2(TDEStorageDevice* sdevice) { TDEStorageOpResult UDisks2EjectDrive(TDEStorageDevice *sdevice) {
TDEStorageOpResult result;
result["result"] = false;
TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if (dbusConn.isConnected()) { if (dbusConn.isConnected()) {
TQString blockDeviceString = sdevice->deviceNode(); TQString blockDeviceString = sdevice->deviceNode();
@ -162,31 +172,33 @@ bool ejectDriveUDisks2(TDEStorageDevice* sdevice) {
TQT_DBusMessage reply = hardwareControl.sendWithReply("Get", params, &error); TQT_DBusMessage reply = hardwareControl.sendWithReply("Get", params, &error);
if (error.isValid()) { if (error.isValid()) {
// Error! // Error!
printf("[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout); result["errStr"] = error.name() + ": " + error.message();
return false; return result;
} }
else {
if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) { if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
TQT_DBusObjectPath driveObjectPath = reply[0].toVariant().value.toObjectPath(); TQT_DBusObjectPath driveObjectPath = reply[0].toVariant().value.toObjectPath();
if (!driveObjectPath.isValid()) { if (!driveObjectPath.isValid()) {
return false; return result;
} }
error = TQT_DBusError(); error = TQT_DBusError();
TQT_DBusProxy driveInformation("org.freedesktop.UDisks2", driveObjectPath, "org.freedesktop.DBus.Properties", dbusConn); TQT_DBusProxy driveInformation("org.freedesktop.UDisks2", driveObjectPath,
"org.freedesktop.DBus.Properties", dbusConn);
// can eject? // can eject?
TQValueList<TQT_DBusData> params; TQValueList<TQT_DBusData> params;
params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString("Ejectable"); params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString("Ejectable");
TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error); TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error);
if (error.isValid()) { if (error.isValid()) {
// Error! // Error!
printf("[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout); result["errStr"] = error.name() + ": " + error.message();
return false; return result;
} }
if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) { if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
bool ejectable = reply[0].toVariant().value.toBool(); bool ejectable = reply[0].toVariant().value.toBool();
if (!ejectable) { if (!ejectable) {
return false; result["errStr"] = i18n("Media not ejectable");
return result;
} }
// Eject the drive! // Eject the drive!
@ -197,21 +209,25 @@ bool ejectDriveUDisks2(TDEStorageDevice* sdevice) {
TQT_DBusMessage reply = driveControl.sendWithReply("Eject", params, &error); TQT_DBusMessage reply = driveControl.sendWithReply("Eject", params, &error);
if (error.isValid()) { if (error.isValid()) {
// Error! // Error!
printf("[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout); result["errStr"] = error.name() + ": " + error.message();
return false; return result;
} }
else { else {
return true; result["result"] = true;
return result;
} }
} }
} }
} }
} }
return result;
} }
return false;
}
int mountDriveUDisks2(TQString deviceNode, TQString fileSystemType, TQString mountOptions, TQString* errStr) { TDEStorageOpResult UDisks2MountDrive(TQString deviceNode, TQString fileSystemType, TQString mountOptions) {
TDEStorageOpResult result;
result["result"] = false;
result["retcode"] = -2;
TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if (dbusConn.isConnected()) { if (dbusConn.isConnected()) {
TQString blockDeviceString = deviceNode; TQString blockDeviceString = deviceNode;
@ -231,32 +247,33 @@ int mountDriveUDisks2(TQString deviceNode, TQString fileSystemType, TQString mou
optionsMap["options"] = (TQT_DBusData::fromString(mountOptions)).getAsVariantData(); optionsMap["options"] = (TQT_DBusData::fromString(mountOptions)).getAsVariantData();
params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap)); params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
TQT_DBusMessage reply = driveControl.sendWithReply("Mount", params, &error); TQT_DBusMessage reply = driveControl.sendWithReply("Mount", params, &error);
if (error.isValid()) { if (!error.isValid()) {
// Error! // Success
if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") { result["retcode"] = 0;
// Service not installed or unavailable result["result"] = true;
return -2; return result;
}
if (errStr) {
*errStr = error.name() + ": " + error.message();
} }
else { else {
printf("[ERROR][tdehwlib] mountDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout); // Error!
} if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
return -1; return result; // Service not installed or unavailable
} }
else { else {
return 0; result["errStr"] = error.name() + ": " + error.message();
result["retcode"] = -1;
return result;
} }
} }
else {
return -2;
} }
} }
return -2; return result;
} }
int unMountDriveUDisks2(TQString deviceNode, TQString unMountOptions, TQString* errStr) { TDEStorageOpResult UDisks2UnmountDrive(TQString deviceNode, TQString unmountOptions) {
TDEStorageOpResult result;
result["result"] = false;
result["retcode"] = -2;
TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if (dbusConn.isConnected()) { if (dbusConn.isConnected()) {
TQString blockDeviceString = deviceNode; TQString blockDeviceString = deviceNode;
@ -270,32 +287,29 @@ int unMountDriveUDisks2(TQString deviceNode, TQString unMountOptions, TQString*
if (driveControl.canSend()) { if (driveControl.canSend()) {
TQValueList<TQT_DBusData> params; TQValueList<TQT_DBusData> params;
TQMap<TQString, TQT_DBusData> optionsMap; TQMap<TQString, TQT_DBusData> optionsMap;
optionsMap["options"] = (TQT_DBusData::fromString(unMountOptions)).getAsVariantData(); optionsMap["options"] = (TQT_DBusData::fromString(unmountOptions)).getAsVariantData();
params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap)); params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
TQT_DBusMessage reply = driveControl.sendWithReply("Unmount", params, &error); TQT_DBusMessage reply = driveControl.sendWithReply("Unmount", params, &error);
if (error.isValid()) { if (!error.isValid()) {
// Error! // Success
if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") { result["retcode"] = 0;
// Service not installed or unavailable result["result"] = true;
return -2; return result;
}
if (errStr) {
*errStr = error.name() + ": " + error.message();
} }
else { else {
printf("[ERROR][tdehwlib] unMountDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout); // Error!
} if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
return -1; return result; // Service not installed or unavailable
} }
else { else {
return 0; result["errStr"] = error.name() + ": " + error.message();
result["retcode"] = -1;
return result;
} }
} }
else {
return -2;
} }
} }
return -2; return result;
} }

@ -21,16 +21,21 @@
#define _DISKS_HELPER_H #define _DISKS_HELPER_H
#include <stdlib.h> #include <stdlib.h>
#include "tdestoragedevice.h"
class TDEStorageDevice;
class TQString;
class TQStringList; //-------------------------------
// UDisks
bool ejectDriveUDisks(TDEStorageDevice* sdevice); //-------------------------------
bool ejectDriveUDisks2(TDEStorageDevice* sdevice); TDEStorageOpResult UDisksEjectDrive(TDEStorageDevice *sdevice);
int mountDriveUDisks(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions, TQString* errStr = NULL); TDEStorageOpResult UDisksMountDrive(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions);
int mountDriveUDisks2(TQString deviceNode, TQString fileSystemType, TQString mountOptions, TQString* errStr = NULL); TDEStorageOpResult UDisksUnmountDrive(TQString deviceNode, TQStringList unmountOptions);
int unMountDriveUDisks(TQString deviceNode, TQStringList unMountOptions, TQString* errStr = NULL);
int unMountDriveUDisks2(TQString deviceNode, TQString unMountOptions, TQString* errStr = NULL); //-------------------------------
// UDisks2
//-------------------------------
TDEStorageOpResult UDisks2EjectDrive(TDEStorageDevice *sdevice);
TDEStorageOpResult UDisks2MountDrive(TQString deviceNode, TQString fileSystemType, TQString mountOptions);
TDEStorageOpResult UDisks2UnmountDrive(TQString deviceNode, TQString unmountOptions);
#endif #endif

@ -28,6 +28,7 @@
#include <tqregexp.h> #include <tqregexp.h>
#include <tqpixmap.h> #include <tqpixmap.h>
#include <tqfile.h> #include <tqfile.h>
#include <tqvariant.h>
#include "kdebug.h" #include "kdebug.h"
#include "tdelocale.h" #include "tdelocale.h"
@ -309,7 +310,8 @@ bool TDEStorageDevice::lockDriveMedia(bool lock) {
bool TDEStorageDevice::ejectDrive() { bool TDEStorageDevice::ejectDrive() {
if (!(TDEGlobal::dirs()->findExe("udisksctl").isEmpty())) { if (!(TDEGlobal::dirs()->findExe("udisksctl").isEmpty())) {
if (ejectDriveUDisks2(this)) { TDEStorageOpResult ejectResult = UDisks2EjectDrive(this);
if (ejectResult["result"].toBool()) {
return true; return true;
} }
else { else {
@ -318,7 +320,8 @@ bool TDEStorageDevice::ejectDrive() {
} }
} }
if (!(TDEGlobal::dirs()->findExe("udisks").isEmpty())) { if (!(TDEGlobal::dirs()->findExe("udisks").isEmpty())) {
if (ejectDriveUDisks(this)) { TDEStorageOpResult ejectResult = UDisksEjectDrive(this);
if (ejectResult["result"].toBool()) {
return true; return true;
} }
else { else {
@ -699,15 +702,15 @@ TQString TDEStorageDevice::mountPath() {
return TQString::null; return TQString::null;
} }
TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet, int* retcode) { TDEStorageOpResult TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOptions mountOptions) {
// Device is already mounted TDEStorageOpResult result;
if (!mountPath().isNull()) {
return mountPath();
}
int internal_retcode; // Check if device is already mounted
if (!retcode) { TQString mountpath = mountPath();
retcode = &internal_retcode; if (!mountpath.isEmpty()) {
result["mountPath"] = mountpath;
result["result"] = true;
return result;
} }
TQString devNode = deviceNode(); TQString devNode = deviceNode();
@ -716,8 +719,6 @@ TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOption
// Prepare filesystem options for mount // Prepare filesystem options for mount
TQStringList udisksOptions; TQStringList udisksOptions;
TQString optionString;
if (mountOptions["ro"] == "true") { if (mountOptions["ro"] == "true") {
udisksOptions.append("ro"); udisksOptions.append("ro");
} }
@ -730,11 +731,8 @@ TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOption
udisksOptions.append("sync"); udisksOptions.append("sync");
} }
if( (mountOptions["filesystem"] == "fat") if ((mountOptions["filesystem"] == "fat") || (mountOptions["filesystem"] == "vfat") ||
|| (mountOptions["filesystem"] == "vfat") (mountOptions["filesystem"] == "msdos") || (mountOptions["filesystem"] == "umsdos")) {
|| (mountOptions["filesystem"] == "msdos")
|| (mountOptions["filesystem"] == "umsdos")
) {
if (mountOptions.contains("shortname")) { if (mountOptions.contains("shortname")) {
udisksOptions.append(TQString("shortname=%1").arg(mountOptions["shortname"])); udisksOptions.append(TQString("shortname=%1").arg(mountOptions["shortname"]));
} }
@ -753,15 +751,14 @@ TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOption
} }
} }
if( (mountOptions["filesystem"] == "ext3") if ((mountOptions["filesystem"] == "ext3") || (mountOptions["filesystem"] == "ext4")) {
|| (mountOptions["filesystem"] == "ext4")
) {
if (mountOptions.contains("journaling")) { if (mountOptions.contains("journaling")) {
// udisks/udisks2 for now does not support option data= for ext3/ext4 // udisks/udisks2 for now does not support option data= for ext3/ext4
// udisksOptions.append(TQString("data=%1").arg(mountOptions["journaling"])); // udisksOptions.append(TQString("data=%1").arg(mountOptions["journaling"]));
} }
} }
TQString optionString;
for (TQStringList::Iterator it = udisksOptions.begin(); it != udisksOptions.end(); ++it) { for (TQStringList::Iterator it = udisksOptions.begin(); it != udisksOptions.end(); ++it) {
optionString.append(","); optionString.append(",");
optionString.append(*it); optionString.append(*it);
@ -772,50 +769,49 @@ TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOption
} }
// Try to use UDISKS v2 via DBUS, if available // Try to use UDISKS v2 via DBUS, if available
TQString errorString;
TQString fileSystemType; TQString fileSystemType;
if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) { if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) {
fileSystemType = mountOptions["filesystem"]; fileSystemType = mountOptions["filesystem"];
} }
int uDisks2Ret = mountDriveUDisks2(devNode, fileSystemType, optionString, &errorString); TDEStorageOpResult mountResult = UDisks2MountDrive(devNode, fileSystemType, optionString);
if (uDisks2Ret == 0) { if (mountResult["result"].toBool()) {
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
return mountPath(); result["mountPath"] = mountPath();
} result["result"] = true;
else if (uDisks2Ret == -1) { return result;
if (errRet) {
*errRet = errorString;
} }
else if (mountResult["retcode"].toInt() == -1) {
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
return mountPath(); result["errStr"] = mountResult["errStr"];
result["result"] = false;
return result;
} }
// The UDISKS v2 DBUS service was either not available or was unusable // The UDISKS v2 DBUS service was either not available or was unusable
// Try to use UDISKS v1 via DBUS, if available // Try to use UDISKS v1 via DBUS, if available
int uDisksRet = mountDriveUDisks(devNode, fileSystemType, udisksOptions, &errorString); mountResult = UDisksMountDrive(devNode, fileSystemType, udisksOptions);
if (uDisksRet == 0) { if (mountResult["result"].toBool()) {
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
return mountPath(); result["mountPath"] = mountPath();
} result["result"] = true;
else if (uDisksRet == -1) { return result;
if (errRet) {
*errRet = errorString;
} }
else if (mountResult["retcode"].toInt() == -1) {
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
return mountPath(); result["errStr"] = mountResult["errStr"];
result["result"] = false;
return result;
} }
// The UDISKS v1 DBUS service was either not available or was unusable // The UDISKS v1 DBUS service was either not available or was unusable
// Use 'udevil' command, if available // Use 'udevil' command, if available
TQString command = TQString::null; TQString command = TQString::null;
TQString udevilProg = TDEGlobal::dirs()->findExe("udevil"); if (!TDEGlobal::dirs()->findExe("udevil").isEmpty()) {
if (!udevilProg.isEmpty()) {
if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) { if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) {
fileSystemType = TQString("-t %1").arg(mountOptions["filesystem"]); fileSystemType = TQString("-t %1").arg(mountOptions["filesystem"]);
} }
@ -834,8 +830,7 @@ TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOption
// If 'udevil' was not found, use 'pmount' command if available // If 'udevil' was not found, use 'pmount' command if available
if(command.isEmpty()) { if(command.isEmpty()) {
TQString pmountProg = TDEGlobal::dirs()->findExe("pmount"); if (!TDEGlobal::dirs()->findExe("pmount").isEmpty()) {
if (!pmountProg.isEmpty()) {
// Create dummy password file // Create dummy password file
KTempFile passwordFile(TQString::null, "tmp", 0600); KTempFile passwordFile(TQString::null, "tmp", 0600);
passwordFile.setAutoDelete(true); passwordFile.setAutoDelete(true);
@ -884,39 +879,38 @@ TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOption
} }
if(command.isEmpty()) { if(command.isEmpty()) {
if (errRet) { result["errStr"] = i18n("No supported mounting methods were detected on your system");
*errRet = i18n("No supported mounting methods were detected on your system"); result["result"] = false;
} return result;
return mountPath();
} }
FILE *exepipe = popen(command.local8Bit(), "r"); FILE *exepipe = popen(command.local8Bit(), "r");
if (exepipe) { if (exepipe) {
TQString mount_output;
TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly); TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly);
mount_output = ts->read(); TQString mount_output = ts->read();
delete ts; delete ts;
*retcode = pclose(exepipe); int retcode = pclose(exepipe);
if (errRet) { result["errStr"] = mount_output;
*errRet = mount_output; result["retCode"] = retcode;
}
} }
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
return mountPath(); result["mountPath"] = mountPath();
result["result"] = !mountPath().isEmpty();
return result;
} }
TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet, int* retcode) { TDEStorageOpResult TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString mediaName,
int internal_retcode; TDEStorageMountOptions mountOptions) {
if (!retcode) { TDEStorageOpResult result;
retcode = &internal_retcode;
}
TQString ret = mountPath(); // Check if device is already mounted
TQString mountpath = mountPath();
if (!ret.isNull()) { if (!mountpath.isEmpty()) {
return ret; result["mountPath"] = mountpath;
result["result"] = true;
return result;
} }
// Create dummy password file // Create dummy password file
@ -924,7 +918,9 @@ TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString me
passwordFile.setAutoDelete(true); passwordFile.setAutoDelete(true);
TQFile* pwFile = passwordFile.file(); TQFile* pwFile = passwordFile.file();
if (!pwFile) { if (!pwFile) {
return TQString::null; result["errStr"] = i18n("Cannot create temporary password file");
result["result"] = false;
return result;
} }
pwFile->writeBlock(passphrase.ascii(), passphrase.length()); pwFile->writeBlock(passphrase.ascii(), passphrase.length());
@ -960,119 +956,112 @@ TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString me
passFileName.replace("'", "'\\''"); passFileName.replace("'", "'\\''");
devNode.replace("'", "'\\''"); devNode.replace("'", "'\\''");
mediaName.replace("'", "'\\''"); mediaName.replace("'", "'\\''");
TQString command = TQString("pmount -p '%1' %2 '%3' '%4' 2>&1").arg(passFileName).arg(optionString).arg(devNode).arg(mediaName); TQString command = TQString("pmount -p '%1' %2 '%3' '%4' 2>&1")
.arg(passFileName).arg(optionString).arg(devNode).arg(mediaName);
FILE *exepipe = popen(command.local8Bit(), "r"); FILE *exepipe = popen(command.local8Bit(), "r");
if (exepipe) { if (exepipe) {
TQString mount_output;
TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly); TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly);
mount_output = ts->read(); TQString mount_output = ts->read();
delete ts; delete ts;
*retcode = pclose(exepipe); int retcode = pclose(exepipe);
if (errRet) { result["errStr"] = mount_output;
*errRet = mount_output; result["retCode"] = retcode;
}
} }
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
result["mountPath"] = mountPath();
ret = mountPath(); result["result"] = !mountPath().isEmpty();
return result;
return ret;
} }
bool TDEStorageDevice::unmountDevice(TQString* errRet, int* retcode) { TDEStorageOpResult TDEStorageDevice::unmountDevice() {
if (mountPath().isNull()) { TDEStorageOpResult result;
return true;
}
int internal_retcode;
if (!retcode) {
retcode = &internal_retcode;
}
// Check if device is already unmounted
TQString mountpoint = mountPath(); TQString mountpoint = mountPath();
TQString devNode = deviceNode(); if (mountpoint.isEmpty()) {
result["result"] = true;
return result;
}
mountpoint.replace("'", "'\\''"); mountpoint.replace("'", "'\\''");
TQString devNode = deviceNode();
// Try to use UDISKS v2 via DBUS, if available // Try to use UDISKS v2 via DBUS, if available
TQString errorString; TDEStorageOpResult unmountResult = UDisks2UnmountDrive(devNode, TQString::null);
int unMountUDisks2Ret = unMountDriveUDisks2(devNode, TQString::null, &errorString); if (unmountResult["result"].toBool()) {
if (unMountUDisks2Ret == 0) {
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
return true; result["result"] = true;
} return result;
else if (unMountUDisks2Ret == -1) {
if (errRet) {
*errRet = errorString;
} }
else if (unmountResult["retcode"].toInt() == -1) {
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
return false; result["errStr"] = unmountResult["errStr"];
result["result"] = false;
return result;
} }
// The UDISKS v2 DBUS service was either not available or was unusable // The UDISKS v2 DBUS service was either not available or was unusable
// Try to use UDISKS v1 via DBUS, if available // Try to use UDISKS v1 via DBUS, if available
int unMountUDisksRet = unMountDriveUDisks(devNode, TQStringList(), &errorString); unmountResult = UDisksUnmountDrive(devNode, TQStringList());
if (unMountUDisksRet == 0) { if (unmountResult["result"].toBool()) {
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
return true; result["result"] = true;
} return result;
else if (unMountUDisksRet == -1) {
if (errRet) {
*errRet = errorString;
} }
else if (unmountResult["retcode"].toInt() == -1) {
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
return false; result["errStr"] = unmountResult["errStr"];
result["result"] = false;
return result;
} }
// The UDISKS v1 DBUS service was either not available or was unusable // The UDISKS v1 DBUS service was either not available or was unusable
// Try to use udevil, if available // Use 'udevil' command, if available
TQString command; TQString command = TQString::null;
if(!(TDEGlobal::dirs()->findExe("udevil").isEmpty())) { if (!TDEGlobal::dirs()->findExe("udevil").isEmpty()) {
command = TQString("udevil umount '%1' 2>&1").arg(mountpoint); command = TQString("udevil umount '%1' 2>&1").arg(mountpoint);
} }
// If 'udevil' was not found, use 'pmount' command if available // If 'udevil' was not found, use 'pmount' command if available
if(command.isEmpty() && if(command.isEmpty() && !TDEGlobal::dirs()->findExe("pumount").isEmpty()) {
!(TDEGlobal::dirs()->findExe("pumount").isEmpty())) {
command = TQString("pumount '%1' 2>&1").arg(mountpoint); command = TQString("pumount '%1' 2>&1").arg(mountpoint);
} }
if(command.isEmpty()) { if(command.isEmpty()) {
if (errRet) { result["errStr"] = i18n("No supported unmounting methods were detected on your system");
*errRet = i18n("No supported unmounting methods were detected on your system"); result["result"] = false;
} return result;
return false;
} }
FILE *exepipe = popen(command.local8Bit(), "r"); FILE *exepipe = popen(command.local8Bit(), "r");
if (exepipe) { if (exepipe) {
TQString umount_output;
TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly); TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly);
umount_output = ts->read(); TQString umount_output = ts->read();
delete ts; delete ts;
*retcode = pclose(exepipe); int retcode = pclose(exepipe);
if (*retcode == 0) { if (retcode == 0) {
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
return true; result["result"] = true;
return result;
} }
else { else {
if (errRet) { result["errStr"] = umount_output;
*errRet = umount_output; result["retCode"] = retcode;
}
} }
} }
// Update internal mount data // Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts(); TDEGlobal::hardwareDevices()->processModifiedMounts();
return false; result["result"] = false;
return result;
} }
TQString TDEStorageDevice::determineFileSystemType(TQString path) { TQString TDEStorageDevice::determineFileSystemType(TQString path) {

@ -166,7 +166,9 @@ enum TDELUKSResult {
}; };
}; };
class TQVariant;
typedef TQMap<TQString, TQString> TDEStorageMountOptions; typedef TQMap<TQString, TQString> TDEStorageMountOptions;
typedef TQMap<TQString, TQVariant> TDEStorageOpResult;
class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice
{ {
@ -231,36 +233,31 @@ class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice
* Mounts the device if not encrypted * Mounts the device if not encrypted
* *
* @param a TQString containing a requested mount name under /media, if desired * @param a TQString containing a requested mount name under /media, if desired
* @param a TQString containing any mount options for pmount, if desired * @param a TDEStorageMountOptions containing any mount options for pmount, if desired
* @param a pointer to a TQString which will be populated with any error messages from pmount, if desired
* @param a pointer to an integer which will be populated with the return code from pmount, if desired
* *
* @return a TQString with the mount path, if successful * @return a TDEStorageOpResult object containing information about the operation outcome
*/ */
TQString mountDevice(TQString mediaName=TQString::null, TDEStorageMountOptions mountOptions=TDEStorageMountOptions(), TQString* errRet=0, int* retcode=0); TDEStorageOpResult mountDevice(TQString mediaName = TQString::null,
TDEStorageMountOptions mountOptions = TDEStorageMountOptions());
/** /**
* Mounts the encrypted device if the correct passphrase is given * Mounts the encrypted device if the correct passphrase is given
* *
* @param a TQString containing the passphrase * @param a TQString containing the passphrase
* @param a TQString containing a requested mount name under /media, if desired * @param a TQString containing a requested mount name under /media, if desired
* @param a TQString containing any mount options for pmount, if desired * @param a TDEStorageMountOptions containing any mount options for pmount, if desired
* @param a pointer to a TQString which will be populated with any error messages from pmount, if desired
* @param a pointer to an integer which will be populated with the return code from pmount, if desired
* *
* @return a TQString with the mount path, if successful * @return a TDEStorageOpResult object containing information about the operation outcome
*/ */
TQString mountEncryptedDevice(TQString passphrase, TQString mediaName=TQString::null, TDEStorageMountOptions mountOptions=TDEStorageMountOptions(), TQString* errRet=0, int* retcode=0); TDEStorageOpResult mountEncryptedDevice(TQString passphrase, TQString mediaName = TQString::null,
TDEStorageMountOptions mountOptions = TDEStorageMountOptions());
/** /**
* Unmounts the device * Unmounts the device
* *
* @param a pointer to a TQString which will be populated with any error messages from pmount, if desired * @return a TDEStorageOpResult object containing information about the operation outcome
* @param a pointer to an integer which will be populated with the return code from pmount, if desired
*
* @return true if unmount was successful
*/ */
bool unmountDevice(TQString* errRet, int* retcode=0); TDEStorageOpResult unmountDevice();
/** /**
* @return a TQString with the mount path, if mounted * @return a TQString with the mount path, if mounted

Loading…
Cancel
Save