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

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

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

@ -166,7 +166,9 @@ enum TDELUKSResult {
};
};
typedef TQMap<TQString,TQString> TDEStorageMountOptions;
class TQVariant;
typedef TQMap<TQString, TQString> TDEStorageMountOptions;
typedef TQMap<TQString, TQVariant> TDEStorageOpResult;
class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice
{
@ -231,36 +233,31 @@ class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice
* Mounts the device if not encrypted
*
* @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 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
* @param a TDEStorageMountOptions containing any mount options for 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
*
* @param a TQString containing the passphrase
* @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 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
* @param a TDEStorageMountOptions containing any mount options for 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
*
* @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 true if unmount was successful
* @return a TDEStorageOpResult object containing information about the operation outcome
*/
bool unmountDevice(TQString* errRet, int* retcode=0);
TDEStorageOpResult unmountDevice();
/**
* @return a TQString with the mount path, if mounted

Loading…
Cancel
Save