diff options
-rw-r--r-- | tdecore/tdehardwaredevices.cpp | 100 | ||||
-rw-r--r-- | tdecore/tdehardwaredevices.h | 48 |
2 files changed, 141 insertions, 7 deletions
diff --git a/tdecore/tdehardwaredevices.cpp b/tdecore/tdehardwaredevices.cpp index 3c57ea6e9..cc1c90357 100644 --- a/tdecore/tdehardwaredevices.cpp +++ b/tdecore/tdehardwaredevices.cpp @@ -29,6 +29,9 @@ #include <fcntl.h> #include <poll.h> +#include <sys/stat.h> +#include <sys/ioctl.h> +#include <linux/fs.h> // NOTE TO DEVELOPERS // This command will greatly help when attempting to find properties to distinguish one device from another @@ -94,6 +97,11 @@ void TDEGenericDevice::setDeviceBus(TQString db) { m_deviceBus = db; } +TQString TDEGenericDevice::uniqueID() { + m_uniqueID = m_systemPath+m_deviceNode; + return m_uniqueID; +} + TDEStorageDevice::TDEStorageDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn), m_mediaInserted(true) { } @@ -108,6 +116,10 @@ void TDEStorageDevice::setDiskType(TDEDiskDeviceType::TDEDiskDeviceType dt) { m_diskType = dt; } +bool TDEStorageDevice::isDiskOfType(TDEDiskDeviceType::TDEDiskDeviceType tf) { + return ((m_diskType&tf)!=(TDEDiskDeviceType::TDEDiskDeviceType)0); +} + TDEDiskDeviceStatus::TDEDiskDeviceStatus TDEStorageDevice::diskStatus() { return m_diskStatus; } @@ -116,6 +128,10 @@ void TDEStorageDevice::setDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus st m_diskStatus = st; } +bool TDEStorageDevice::checkDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus sf) { + return ((m_diskStatus&sf)!=(TDEDiskDeviceStatus::TDEDiskDeviceStatus)0); +} + TQString &TDEStorageDevice::diskLabel() { return m_diskName; } @@ -172,6 +188,64 @@ void TDEStorageDevice::setSlaveDevices(TQStringList sd) { m_slaveDevices = sd; } +unsigned long TDEStorageDevice::deviceSize() { + TQString bsnodename = systemPath(); + bsnodename.append("/queue/physical_block_size"); + TQFile bsfile( bsnodename ); + TQString blocksize; + if ( bsfile.open( IO_ReadOnly ) ) { + TQTextStream stream( &bsfile ); + blocksize = stream.readLine(); + bsfile.close(); + } + + TQString dsnodename = systemPath(); + dsnodename.append("/size"); + TQFile dsfile( dsnodename ); + TQString devicesize; + if ( dsfile.open( IO_ReadOnly ) ) { + TQTextStream stream( &dsfile ); + devicesize = stream.readLine(); + dsfile.close(); + } + + return (blocksize.toULong()*devicesize.toULong()); +} + +TQString TDEStorageDevice::deviceFriendlySize() { + double bytes = deviceSize(); + TQString prettystring; + + prettystring = TQString("%1b").arg(bytes); + + if (bytes > 1024) { + bytes = bytes / 1024; + prettystring = TQString("%1Kb").arg(bytes, 0, 'f', 1); + } + + if (bytes > 1024) { + bytes = bytes / 1024; + prettystring = TQString("%1Mb").arg(bytes, 0, 'f', 1); + } + + if (bytes > 1024) { + bytes = bytes / 1024; + prettystring = TQString("%1Gb").arg(bytes, 0, 'f', 1); + } + + if (bytes > 1024) { + bytes = bytes / 1024; + prettystring = TQString("%1Tb").arg(bytes, 0, 'f', 1); + } + + if (bytes > 1024) { + bytes = bytes / 1024; + prettystring = TQString("%1Pb").arg(bytes, 0, 'f', 1); + } + + return prettystring; +} + TQString TDEStorageDevice::mountPath() { // See if this device node is mounted // This requires parsing /proc/mounts, looking for deviceNode() @@ -346,6 +420,20 @@ TDEGenericDevice* TDEHardwareDevices::findBySystemPath(TQString syspath) { return 0; } +TDEStorageDevice* TDEHardwareDevices::findDiskByUID(TQString uid) { + TDEGenericDevice *hwdevice; + for ( hwdevice = m_deviceList.first(); hwdevice; hwdevice = m_deviceList.next() ) { + if (hwdevice->type() == TDEGenericDeviceType::Disk) { + TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice); + if (sdevice->uniqueID() == uid) { + return sdevice; + } + } + } + + return 0; +} + void TDEHardwareDevices::checkForHotPluggedHardware() { udev_device* dev = udev_monitor_receive_device(m_udevMonitorStruct); if (dev) { @@ -547,11 +635,20 @@ TDEGenericDevice* TDEHardwareDevices::classifyUnknownDevice(udev_device* dev) { if (disktypestring.upper() == "DISK") { disktype = disktype | TDEDiskDeviceType::HDD; } + if (disktypestring.isNull()) { + // Fallback + // If we can't recognize the disk type then set it as a simple HDD volume + disktype = disktype | TDEDiskDeviceType::HDD; + } if (disktypestring.upper() == "CD") { if (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA")) == "1") { disktype = disktype | TDEDiskDeviceType::CDROM; } + if (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_CD_RW")) == "1") { + disktype = disktype | TDEDiskDeviceType::CDRW; + disktype = disktype & ~TDEDiskDeviceType::CDROM; + } if (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD")) == "1") { disktype = disktype | TDEDiskDeviceType::DVDROM; disktype = disktype & ~TDEDiskDeviceType::CDROM; @@ -607,6 +704,9 @@ TDEGenericDevice* TDEHardwareDevices::classifyUnknownDevice(udev_device* dev) { else if (filesystemtype.upper() == "CRYPTO") { disktype = disktype | TDEDiskDeviceType::OtherCrypted; } + else if (!filesystemtype.isNull()) { + diskstatus = diskstatus | TDEDiskDeviceStatus::ContainsFilesystem; + } // Detect RAM and Loop devices, since udev can't seem to... if (systempath.startsWith("/sys/devices/virtual/block/ram")) { diff --git a/tdecore/tdehardwaredevices.h b/tdecore/tdehardwaredevices.h index d309ff650..11844fa5c 100644 --- a/tdecore/tdehardwaredevices.h +++ b/tdecore/tdehardwaredevices.h @@ -71,6 +71,7 @@ enum TDEGenericDeviceType { namespace TDEDiskDeviceType { enum TDEDiskDeviceType { + MediaDevice = 0x00000001, Floppy = 0x00000002, CDROM = 0x00000004, CDRW = 0x00000008, @@ -121,13 +122,14 @@ inline TDEDiskDeviceType operator~(TDEDiskDeviceType a) namespace TDEDiskDeviceStatus { enum TDEDiskDeviceStatus { - Mountable = 0x00000001, - Removable = 0x00000002, - Inserted = 0x00000004, - Blank = 0x00000008, - UsedByDevice = 0x00000010, - UsesDevice = 0x00000020, - Other = 0x80000000 + Mountable = 0x00000001, + Removable = 0x00000002, + Inserted = 0x00000004, + Blank = 0x00000008, + UsedByDevice = 0x00000010, + UsesDevice = 0x00000020, + ContainsFilesystem = 0x00000040, + Other = 0x80000000 }; inline TDEDiskDeviceStatus operator|(TDEDiskDeviceStatus a, TDEDiskDeviceStatus b) @@ -233,6 +235,11 @@ class TDECORE_EXPORT TDEGenericDevice */ void setDeviceNode(TQString sn); + /** + * @return a TQString containing a unique identifier for this device + */ + TQString uniqueID(); + private: TDEGenericDeviceType::TDEGenericDeviceType m_deviceType; TQString m_deviceName; @@ -241,6 +248,7 @@ class TDECORE_EXPORT TDEGenericDevice TQString m_vendorName; TQString m_vendorModel; TQString m_deviceBus; + TQString m_uniqueID; }; class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice @@ -288,6 +296,11 @@ class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice void setDiskType(TDEDiskDeviceType::TDEDiskDeviceType tf); /** + * @param an OR-ed combination of TDEDiskDeviceType::TDEDiskDeviceType type flags + */ + bool isDiskOfType(TDEDiskDeviceType::TDEDiskDeviceType tf); + + /** * @return an OR-ed combination of TDEDiskDeviceStatus::TDEDiskDeviceStatus type flags */ TDEDiskDeviceStatus::TDEDiskDeviceStatus diskStatus(); @@ -298,6 +311,11 @@ class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice void setDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus st); /** + * @param an OR-ed combination of TDEDiskDeviceStatus::TDEDiskDeviceStatus type flags + */ + bool checkDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus sf); + + /** * @return true if media inserted, false if no media available */ bool mediaInserted(); @@ -376,6 +394,16 @@ class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice */ TQString mountPath(); + /** + * @return an unsigned long with the device size in bytes + */ + unsigned long deviceSize(); + + /** + * @return a TQString with the device size in human readable form + */ + TQString deviceFriendlySize(); + private: TDEDiskDeviceType::TDEDiskDeviceType m_diskType; TDEDiskDeviceStatus::TDEDiskDeviceStatus m_diskStatus; @@ -430,6 +458,12 @@ class TDECORE_EXPORT TDEHardwareDevices : TQObject */ TDEGenericDevice* findBySystemPath(TQString syspath); + /** + * Return the storage device with unique ID @arg uid, or 0 if no device exists for that uid + * @return TDEGenericDevice + */ + TDEStorageDevice* findDiskByUID(TQString uid); + signals: void hardwareAdded(TDEGenericDevice); void hardwareRemoved(TDEGenericDevice); |