From 0aed61de3581997e04c6d5fb8de2333b7e115452 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Fri, 30 Mar 2012 18:02:28 -0500 Subject: Add mount and unmount functionality to TDE disk hardware class --- tdecore/tdehardwaredevices.cpp | 113 ++++++++++++++++++++++++++++++++++++++++- tdecore/tdehardwaredevices.h | 30 +++++++++++ 2 files changed, 142 insertions(+), 1 deletion(-) diff --git a/tdecore/tdehardwaredevices.cpp b/tdecore/tdehardwaredevices.cpp index 6124f0173..3c57ea6e9 100644 --- a/tdecore/tdehardwaredevices.cpp +++ b/tdecore/tdehardwaredevices.cpp @@ -22,6 +22,9 @@ #include #include +#include +#include + #include #include @@ -173,6 +176,22 @@ TQString TDEStorageDevice::mountPath() { // See if this device node is mounted // This requires parsing /proc/mounts, looking for deviceNode() + // The Device Mapper throws a monkey wrench into this + // It likes to advertise mounts as /dev/mapper/, + // where is listed in /dm/name + TQString dmnodename = systemPath(); + dmnodename.append("/dm/name"); + TQFile namefile( dmnodename ); + TQString dmaltname; + if ( namefile.open( IO_ReadOnly ) ) { + TQTextStream stream( &namefile ); + dmaltname = stream.readLine(); + namefile.close(); + } + if (!dmaltname.isNull()) { + dmaltname.prepend("/dev/mapper/"); + } + TQStringList lines; TQFile file( "/proc/mounts" ); if ( file.open( IO_ReadOnly ) ) { @@ -181,7 +200,7 @@ TQString TDEStorageDevice::mountPath() { while ( !stream.atEnd() ) { line = stream.readLine(); TQStringList mountInfo = TQStringList::split(" ", line, true); - if (*mountInfo.at(0) == deviceNode()) { + if ((*mountInfo.at(0) == deviceNode()) || (*mountInfo.at(0) == dmaltname)) { return *mountInfo.at(1); } lines += line; @@ -189,9 +208,90 @@ TQString TDEStorageDevice::mountPath() { file.close(); } + // While this device is not directly mounted, it could concievably be mounted via the Device Mapper + // If so, try to retrieve the mount path... + TQStringList slaveDeviceList = slaveDevices(); + for ( TQStringList::Iterator slavedevit = slaveDeviceList.begin(); slavedevit != slaveDeviceList.end(); ++slavedevit ) { + // Try to locate this device path in the TDE device tree + TDEHardwareDevices *hwdevices = KGlobal::hardwareDevices(); + TDEGenericDevice *hwdevice = hwdevices->findBySystemPath(*slavedevit); + if ((hwdevice) && (hwdevice->type() == TDEGenericDeviceType::Disk)) { + TDEStorageDevice* sdevice = static_cast(hwdevice); + return sdevice->mountPath(); + } + } + return TQString::null; } +TQString TDEStorageDevice::mountDevice(TQString mediaName) { + TQString ret = mountPath(); + + if (!ret.isNull()) { + return ret; + } + + // Create dummy password file + KTempFile passwordFile(TQString::null, "tmp", 0600); + passwordFile.setAutoDelete(true); + + TQString command = TQString("pmount -p %1 %2").arg(passwordFile.name()).arg(deviceNode()); + if (!mediaName.isNull()) { + command.append(mediaName); + } + + if (system(command.ascii()) == 0) { + ret = mountPath(); + } + + return ret; +} + +TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString mediaName) { + TQString ret = mountPath(); + + if (!ret.isNull()) { + return ret; + } + + // Create dummy password file + KTempFile passwordFile(TQString::null, "tmp", 0600); + passwordFile.setAutoDelete(true); + TQFile* pwFile = passwordFile.file(); + if (!pwFile) { + return TQString::null; + } + + pwFile->writeBlock(passphrase.ascii(), passphrase.length()); + pwFile->flush(); + + TQString command = TQString("pmount -p %1 %2").arg(passwordFile.name()).arg(deviceNode()); + if (!mediaName.isNull()) { + command.append(mediaName); + } + + if (system(command.ascii()) == 0) { + ret = mountPath(); + } + + return ret; +} + +bool TDEStorageDevice::unmountDevice() { + TQString mountpoint = mountPath(); + + if (mountpoint.isNull()) { + return true; + } + + TQString command = TQString("pumount %1").arg(mountpoint); + if (system(command.ascii()) == 0) { + return true; + } + + return false; +} + TDEHardwareDevices::TDEHardwareDevices() { // Set up device list m_deviceList.setAutoDelete( TRUE ); // the list owns the objects @@ -235,6 +335,17 @@ TDEHardwareDevices::~TDEHardwareDevices() { udev_unref(m_udevStruct); } +TDEGenericDevice* TDEHardwareDevices::findBySystemPath(TQString syspath) { + TDEGenericDevice *hwdevice; + for ( hwdevice = m_deviceList.first(); hwdevice; hwdevice = m_deviceList.next() ) { + if (hwdevice->systemPath() == syspath) { + return hwdevice; + } + } + + return 0; +} + void TDEHardwareDevices::checkForHotPluggedHardware() { udev_device* dev = udev_monitor_receive_device(m_udevMonitorStruct); if (dev) { diff --git a/tdecore/tdehardwaredevices.h b/tdecore/tdehardwaredevices.h index 531106c36..d309ff650 100644 --- a/tdecore/tdehardwaredevices.h +++ b/tdecore/tdehardwaredevices.h @@ -347,6 +347,30 @@ class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice */ void setSlaveDevices(TQStringList sd); + /** + * Mounts the device if not encrypted + * + * @param a TQString containing a requested mount name under /media, if desired + * @return a TQString with the mount path, if successful + */ + TQString mountDevice(TQString mediaName=TQString::null); + + /** + * 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 + * @return a TQString with the mount path, if successful + */ + TQString mountEncryptedDevice(TQString passphrase, TQString mediaName=TQString::null); + + /** + * Unmounts the device + * + * @return TRUE if unmount was successful + */ + bool unmountDevice(); + /** * @return a TQString with the mount path, if mounted */ @@ -400,6 +424,12 @@ class TDECORE_EXPORT TDEHardwareDevices : TQObject */ TQPtrList &listAllPhysicalDevices(); + /** + * Return the device with system path @arg syspath, or 0 if no device exists for that path + * @return TDEGenericDevice + */ + TDEGenericDevice* findBySystemPath(TQString syspath); + signals: void hardwareAdded(TDEGenericDevice); void hardwareRemoved(TDEGenericDevice); -- cgit v1.2.1