20 #include "tdestoragedevice.h" 25 #include <sys/ioctl.h> 26 #include <linux/cdrom.h> 34 #include "tdeglobal.h" 35 #include "kiconloader.h" 36 #include "tdetempfile.h" 37 #include "kstandarddirs.h" 39 #include "tdehardwaredevices.h" 43 #if defined(WITH_CRYPTSETUP) 44 #ifdef CRYPTSETUP_OLD_API 45 #define class cryptsetup_class 46 #define CRYPT_SLOT_INVALID INVALID 47 #define CRYPT_SLOT_INACTIVE INACTIVE 48 #define CRYPT_SLOT_ACTIVE ACTIVE 49 #define CRYPT_SLOT_BUSY BUSY 50 #define CRYPT_SLOT_ACTIVE_LAST ACTIVE 51 #include <libcryptsetup.h> 54 #include <libcryptsetup.h> 59 #if defined(WITH_UDISKS) || defined(WITH_UDISKS2) 60 #include <tqdbusdata.h> 61 #include <tqdbusmessage.h> 62 #include <tqdbusproxy.h> 63 #include <tqdbusvariant.h> 64 #include <tqdbusconnection.h> 65 #include <tqdbuserror.h> 66 #include <tqdbusdatamap.h> 67 #include <tqdbusobjectpath.h> 68 #endif // defined(WITH_UDISKS) || defined(WITH_UDISKS2) 69 #if defined(WITH_UDISKS) 70 #include "tqdbusdatalist.h" 71 #endif // ddefined(WITH_UDISKS) 73 #if defined(WITH_UDISKS) || defined(WITH_UDISKS2) 75 TQT_DBusData convertDBUSDataToVariantData(TQT_DBusData);
76 #endif // defined(WITH_UDISKS) || defined(WITH_UDISKS2) 78 TDEStorageDevice::TDEStorageDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn), m_mediaInserted(true), m_cryptDevice(NULL) {
79 m_diskType = TDEDiskDeviceType::Null;
80 m_diskStatus = TDEDiskDeviceStatus::Null;
83 TDEStorageDevice::~TDEStorageDevice() {
84 #if defined(WITH_CRYPTSETUP) 86 crypt_free(m_cryptDevice);
92 TDEDiskDeviceType::TDEDiskDeviceType TDEStorageDevice::diskType() {
96 void TDEStorageDevice::internalGetLUKSKeySlotStatus() {
97 #if defined(WITH_CRYPTSETUP) 99 crypt_keyslot_info keyslot_status;
100 TDELUKSKeySlotStatus::TDELUKSKeySlotStatus tde_keyslot_status;
102 m_cryptKeyslotStatus.clear();
103 for (i = 0; i < m_cryptKeySlotCount; i++) {
104 keyslot_status = crypt_keyslot_status(m_cryptDevice, i);
105 tde_keyslot_status = TDELUKSKeySlotStatus::Invalid;
106 if (keyslot_status == CRYPT_SLOT_INACTIVE) {
107 tde_keyslot_status = TDELUKSKeySlotStatus::Inactive;
109 else if (keyslot_status == CRYPT_SLOT_ACTIVE) {
110 tde_keyslot_status = TDELUKSKeySlotStatus::Active;
112 else if (keyslot_status == CRYPT_SLOT_ACTIVE_LAST) {
113 tde_keyslot_status = TDELUKSKeySlotStatus::Active | TDELUKSKeySlotStatus::Last;
115 m_cryptKeyslotStatus.append(tde_keyslot_status);
120 void TDEStorageDevice::internalInitializeLUKSIfNeeded() {
121 #if defined(WITH_CRYPTSETUP) 124 if (m_diskType & TDEDiskDeviceType::LUKS) {
125 if (!m_cryptDevice) {
126 TQString node = deviceNode();
128 ret = crypt_init(&m_cryptDevice, node.ascii());
130 ret = crypt_load(m_cryptDevice, NULL, NULL);
133 #if defined(CRYPTSETUP_OLD_API) || !defined(HAVE_CRYPTSETUP_GET_TYPE) 134 kdWarning() <<
"TDEStorageDevice: The version of libcryptsetup that TDE was compiled against was too old! Most LUKS features will not function" <<
endl;
135 m_cryptDeviceType = TQString::null;
138 m_cryptDeviceType = crypt_get_type(m_cryptDevice);
139 keyslot_count = crypt_keyslot_max(m_cryptDeviceType.ascii());
141 if (keyslot_count < 0) {
142 m_cryptKeySlotCount = 0;
145 m_cryptKeySlotCount = keyslot_count;
147 internalGetLUKSKeySlotStatus();
151 m_cryptDevice = NULL;
158 crypt_free(m_cryptDevice);
159 m_cryptDevice = NULL;
165 void TDEStorageDevice::cryptSetOperationsUnlockPassword(TQByteArray password) {
166 #if defined(WITH_CRYPTSETUP) 167 crypt_memory_lock(NULL, 1);
168 m_cryptDevicePassword = password;
172 void TDEStorageDevice::cryptClearOperationsUnlockPassword() {
173 m_cryptDevicePassword.fill(0);
174 m_cryptDevicePassword.resize(0);
175 #if defined(WITH_CRYPTSETUP) 176 crypt_memory_lock(NULL, 0);
180 bool TDEStorageDevice::cryptOperationsUnlockPasswordSet() {
181 if (m_cryptDevicePassword.size() > 0) {
189 TDELUKSResult::TDELUKSResult TDEStorageDevice::cryptCheckKey(
unsigned int keyslot) {
190 #if defined(WITH_CRYPTSETUP) 194 if (keyslot < m_cryptKeySlotCount) {
195 ret = crypt_activate_by_passphrase(m_cryptDevice, NULL, keyslot, m_cryptDevicePassword.data(), m_cryptDevicePassword.size(), 0);
197 return TDELUKSResult::KeyslotOpFailed;
200 return TDELUKSResult::Success;
204 return TDELUKSResult::InvalidKeyslot;
208 return TDELUKSResult::LUKSNotFound;
211 return TDELUKSResult::LUKSNotSupported;
215 TDELUKSResult::TDELUKSResult TDEStorageDevice::cryptAddKey(
unsigned int keyslot, TQByteArray password) {
216 #if defined(WITH_CRYPTSETUP) 220 if (keyslot < m_cryptKeySlotCount) {
221 ret = crypt_keyslot_add_by_passphrase(m_cryptDevice, keyslot, m_cryptDevicePassword.data(), m_cryptDevicePassword.size(), password.data(), password.size());
223 return TDELUKSResult::KeyslotOpFailed;
226 internalGetLUKSKeySlotStatus();
227 return TDELUKSResult::Success;
231 return TDELUKSResult::InvalidKeyslot;
235 return TDELUKSResult::LUKSNotFound;
238 return TDELUKSResult::LUKSNotSupported;
242 TDELUKSResult::TDELUKSResult TDEStorageDevice::cryptDelKey(
unsigned int keyslot) {
243 #if defined(WITH_CRYPTSETUP) 247 if (keyslot < m_cryptKeySlotCount) {
248 ret = crypt_keyslot_destroy(m_cryptDevice, keyslot);
250 return TDELUKSResult::KeyslotOpFailed;
253 internalGetLUKSKeySlotStatus();
254 return TDELUKSResult::Success;
258 return TDELUKSResult::InvalidKeyslot;
262 return TDELUKSResult::LUKSNotFound;
265 return TDELUKSResult::LUKSNotSupported;
269 unsigned int TDEStorageDevice::cryptKeySlotCount() {
270 return m_cryptKeySlotCount;
273 TDELUKSKeySlotStatusList TDEStorageDevice::cryptKeySlotStatus() {
274 return m_cryptKeyslotStatus;
277 TQString TDEStorageDevice::cryptKeySlotFriendlyName(TDELUKSKeySlotStatus::TDELUKSKeySlotStatus status) {
278 if (status & TDELUKSKeySlotStatus::Inactive) {
279 return i18n(
"Inactive");
281 else if (status & TDELUKSKeySlotStatus::Active) {
282 return i18n(
"Active");
285 return i18n(
"Unknown");
289 void TDEStorageDevice::internalSetDeviceNode(TQString sn) {
290 TDEGenericDevice::internalSetDeviceNode(sn);
291 internalInitializeLUKSIfNeeded();
294 void TDEStorageDevice::internalSetDiskType(TDEDiskDeviceType::TDEDiskDeviceType dt) {
296 internalInitializeLUKSIfNeeded();
299 bool TDEStorageDevice::isDiskOfType(TDEDiskDeviceType::TDEDiskDeviceType tf) {
300 return ((m_diskType&tf)!=TDEDiskDeviceType::Null);
303 TDEDiskDeviceStatus::TDEDiskDeviceStatus TDEStorageDevice::diskStatus() {
307 void TDEStorageDevice::internalSetDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus st) {
311 bool TDEStorageDevice::checkDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus sf) {
312 return ((m_diskStatus&sf)!=(TDEDiskDeviceStatus::TDEDiskDeviceStatus)0);
315 bool TDEStorageDevice::lockDriveMedia(
bool lock) {
316 int fd =
open(deviceNode().ascii(), O_RDWR | O_NONBLOCK);
320 if (ioctl(fd, CDROM_LOCKDOOR, (lock)?1:0) != 0) {
330 bool ejectDriveUDisks(TDEStorageDevice* sdevice) {
332 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
333 if (dbusConn.isConnected()) {
334 TQString blockDeviceString = sdevice->deviceNode();
335 blockDeviceString.replace(
"/dev/",
"");
336 blockDeviceString =
"/org/freedesktop/UDisks/devices/" + blockDeviceString;
340 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks", blockDeviceString,
"org.freedesktop.UDisks.Device", dbusConn);
341 if (driveControl.canSend()) {
342 TQValueList<TQT_DBusData> params;
343 TQT_DBusDataList options;
344 params << TQT_DBusData::fromList(options);
345 TQT_DBusMessage reply = driveControl.sendWithReply(
"DriveEject", params, &error);
346 if (error.isValid()) {
348 printf(
"[ERROR][tdehwlib] ejectDriveUDisks: %s\n", error.name().ascii()); fflush(stdout);
356 #endif // WITH_UDISKS 360 bool ejectDriveUDisks2(TDEStorageDevice* sdevice) {
362 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
363 if (dbusConn.isConnected()) {
364 TQString blockDeviceString = sdevice->deviceNode();
365 blockDeviceString.replace(
"/dev/",
"");
366 blockDeviceString =
"/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
367 TQT_DBusProxy hardwareControl(
"org.freedesktop.UDisks2", blockDeviceString,
"org.freedesktop.DBus.Properties", dbusConn);
368 if (hardwareControl.canSend()) {
371 TQValueList<TQT_DBusData> params;
372 params << TQT_DBusData::fromString(
"org.freedesktop.UDisks2.Block") << TQT_DBusData::fromString(
"Drive");
373 TQT_DBusMessage reply = hardwareControl.sendWithReply(
"Get", params, &error);
374 if (error.isValid()) {
376 printf(
"[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
380 if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
381 TQT_DBusObjectPath driveObjectPath = reply[0].toVariant().value.toObjectPath();
382 if (!driveObjectPath.isValid()) {
386 error = TQT_DBusError();
387 TQT_DBusProxy driveInformation(
"org.freedesktop.UDisks2", driveObjectPath,
"org.freedesktop.DBus.Properties", dbusConn);
389 TQValueList<TQT_DBusData> params;
390 params << TQT_DBusData::fromString(
"org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString(
"Ejectable");
391 TQT_DBusMessage reply = driveInformation.sendWithReply(
"Get", params, &error);
392 if (error.isValid()) {
394 printf(
"[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
397 if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
398 bool ejectable = reply[0].toVariant().value.toBool();
404 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks2", driveObjectPath,
"org.freedesktop.UDisks2.Drive", dbusConn);
405 TQValueList<TQT_DBusData> params;
406 TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant);
407 params << TQT_DBusData::fromStringKeyMap(options);
408 TQT_DBusMessage reply = driveControl.sendWithReply(
"Eject", params, &error);
409 if (error.isValid()) {
411 printf(
"[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
422 #endif // WITH_UDISKS2 426 int mountDriveUDisks(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions, TQString* errStr = NULL) {
428 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
429 if (dbusConn.isConnected()) {
430 TQString blockDeviceString = deviceNode;
431 blockDeviceString.replace(
"/dev/",
"");
432 blockDeviceString =
"/org/freedesktop/UDisks/devices/" + blockDeviceString;
436 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks", blockDeviceString,
"org.freedesktop.UDisks.Device", dbusConn);
437 if (driveControl.canSend()) {
438 TQValueList<TQT_DBusData> params;
439 params << TQT_DBusData::fromString(fileSystemType);
440 params << TQT_DBusData::fromList(TQT_DBusDataList(mountOptions));
441 TQT_DBusMessage reply = driveControl.sendWithReply(
"FilesystemMount", params, &error);
442 if (error.isValid()) {
444 if (error.name() ==
"org.freedesktop.DBus.Error.ServiceUnknown") {
449 *errStr = error.name() +
": " + error.message();
452 printf(
"[ERROR][tdehwlib] mountDriveUDisks: %s\n", error.name().ascii()); fflush(stdout);
464 #endif // WITH_UDISKS 468 int mountDriveUDisks2(TQString deviceNode, TQString fileSystemType, TQString mountOptions, TQString* errStr = NULL) {
470 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
471 if (dbusConn.isConnected()) {
472 TQString blockDeviceString = deviceNode;
473 blockDeviceString.replace(
"/dev/",
"");
474 blockDeviceString =
"/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
478 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks2", blockDeviceString,
"org.freedesktop.UDisks2.Filesystem", dbusConn);
479 if (driveControl.canSend()) {
480 TQValueList<TQT_DBusData> params;
481 TQMap<TQString, TQT_DBusData> optionsMap;
482 if (fileSystemType !=
"") {
483 optionsMap[
"fstype"] = convertDBUSDataToVariantData(TQT_DBusData::fromString(fileSystemType));
485 optionsMap[
"options"] = convertDBUSDataToVariantData(TQT_DBusData::fromString(mountOptions));
486 params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
487 TQT_DBusMessage reply = driveControl.sendWithReply(
"Mount", params, &error);
488 if (error.isValid()) {
490 if (error.name() ==
"org.freedesktop.DBus.Error.ServiceUnknown") {
495 *errStr = error.name() +
": " + error.message();
498 printf(
"[ERROR][tdehwlib] mountDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
510 #endif // WITH_UDISKS2 514 int unMountDriveUDisks(TQString deviceNode, TQStringList unMountOptions, TQString* errStr = NULL) {
516 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
517 if (dbusConn.isConnected()) {
518 TQString blockDeviceString = deviceNode;
519 blockDeviceString.replace(
"/dev/",
"");
520 blockDeviceString =
"/org/freedesktop/UDisks/devices/" + blockDeviceString;
524 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks", blockDeviceString,
"org.freedesktop.UDisks.Device", dbusConn);
525 if (driveControl.canSend()) {
526 TQValueList<TQT_DBusData> params;
527 params << TQT_DBusData::fromList(TQT_DBusDataList(unMountOptions));
528 TQT_DBusMessage reply = driveControl.sendWithReply(
"FilesystemUnmount", params, &error);
529 if (error.isValid()) {
531 if (error.name() ==
"org.freedesktop.DBus.Error.ServiceUnknown") {
536 *errStr = error.name() +
": " + error.message();
539 printf(
"[ERROR][tdehwlib] unMountDriveUDisks: %s\n", error.name().ascii()); fflush(stdout);
551 #endif // WITH_UDISKS 555 int unMountDriveUDisks2(TQString deviceNode, TQString unMountOptions, TQString* errStr = NULL) {
557 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
558 if (dbusConn.isConnected()) {
559 TQString blockDeviceString = deviceNode;
560 blockDeviceString.replace(
"/dev/",
"");
561 blockDeviceString =
"/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
565 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks2", blockDeviceString,
"org.freedesktop.UDisks2.Filesystem", dbusConn);
566 if (driveControl.canSend()) {
567 TQValueList<TQT_DBusData> params;
568 TQMap<TQString, TQT_DBusData> optionsMap;
569 optionsMap[
"options"] = convertDBUSDataToVariantData(TQT_DBusData::fromString(unMountOptions));
570 params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
571 TQT_DBusMessage reply = driveControl.sendWithReply(
"Unmount", params, &error);
572 if (error.isValid()) {
574 if (error.name() ==
"org.freedesktop.DBus.Error.ServiceUnknown") {
579 *errStr = error.name() +
": " + error.message();
582 printf(
"[ERROR][tdehwlib] unMountDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
594 #endif // WITH_UDISKS2 598 bool TDEStorageDevice::ejectDrive() {
601 if (ejectDriveUDisks2(
this)) {
605 printf(
"[tdehwlib] Failed to eject drive '%s' via udisks2, falling back to alternate mechanism\n", deviceNode().ascii());
609 #endif // WITH_UDISKS2 613 if (ejectDriveUDisks(
this)) {
617 printf(
"[tdehwlib] Failed to eject drive '%s' via udisks, falling back to alternate mechanism\n", deviceNode().ascii());
621 #endif // WITH_UDISKS 624 TQString command = TQString(
"eject -v '%1' 2>&1").arg(deviceNode());
626 FILE *exepipe = popen(command.ascii(),
"r");
628 TQString eject_output;
629 TQTextStream ts(exepipe, IO_ReadOnly);
630 eject_output = ts.read();
631 int retcode = pclose(exepipe);
636 printf(
"[tdehwlib] Failed to eject drive '%s' via 'eject' command\n", deviceNode().ascii());
643 bool TDEStorageDevice::ejectDriveMedia() {
644 int fd =
open(deviceNode().ascii(), O_RDWR | O_NONBLOCK);
648 if (ioctl(fd, CDROMEJECT) != 0) {
658 TQString TDEStorageDevice::diskLabel() {
662 void TDEStorageDevice::internalSetDiskLabel(TQString dn) {
666 bool TDEStorageDevice::mediaInserted() {
667 return m_mediaInserted;
670 void TDEStorageDevice::internalSetMediaInserted(
bool inserted) {
671 m_mediaInserted = inserted;
674 TQString TDEStorageDevice::fileSystemName() {
675 return m_fileSystemName;
678 void TDEStorageDevice::internalSetFileSystemName(TQString fn) {
679 m_fileSystemName = fn;
682 TQString TDEStorageDevice::fileSystemUsage() {
683 return m_fileSystemUsage;
686 void TDEStorageDevice::internalSetFileSystemUsage(TQString fu) {
687 m_fileSystemUsage = fu;
690 TQString TDEStorageDevice::diskUUID() {
694 void TDEStorageDevice::internalSetDiskUUID(TQString
id) {
698 TQStringList TDEStorageDevice::holdingDevices() {
699 return m_holdingDevices;
702 void TDEStorageDevice::internalSetHoldingDevices(TQStringList hd) {
703 m_holdingDevices = hd;
706 TQStringList TDEStorageDevice::slaveDevices() {
707 return m_slaveDevices;
710 void TDEStorageDevice::internalSetSlaveDevices(TQStringList sd) {
714 TQString decodeHexEncoding(TQString str) {
715 TQRegExp hexEncRegExp(
"\\\\x[0-9A-Fa-f]{1,2}");
716 hexEncRegExp.setMinimal(
false);
717 hexEncRegExp.setCaseSensitive(
true);
720 while((s = hexEncRegExp.search(str, s+1))>=0){
721 str.replace(s, hexEncRegExp.cap(0).length(), TQChar((
char)strtol(hexEncRegExp.cap(0).mid(2).ascii(), NULL, 16)));
727 TQString TDEStorageDevice::friendlyName() {
729 TQString devicevendorid = vendorEncoded();
730 TQString devicemodelid = modelEncoded();
732 devicevendorid = decodeHexEncoding(devicevendorid);
733 devicemodelid = decodeHexEncoding(devicemodelid);
735 devicevendorid = devicevendorid.stripWhiteSpace();
736 devicemodelid = devicemodelid.stripWhiteSpace();
737 devicevendorid = devicevendorid.simplifyWhiteSpace();
738 devicemodelid = devicemodelid.simplifyWhiteSpace();
740 TQString devicename = devicevendorid +
" " + devicemodelid;
742 devicename = devicename.stripWhiteSpace();
743 devicename = devicename.simplifyWhiteSpace();
745 if (devicename !=
"") {
749 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
750 return TDEGenericDevice::friendlyName();
753 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
754 return friendlyDeviceType();
757 TQString
label = diskLabel();
758 if (label.isNull()) {
759 if (deviceSize() > 0) {
760 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
761 label = i18n(
"%1 Removable Device").arg(deviceFriendlySize());
764 label = i18n(
"%1 Fixed Storage Device").arg(deviceFriendlySize());
769 if (!label.isNull()) {
773 return friendlyDeviceType();
776 TQString TDEStorageDevice::detailedFriendlyName() {
777 return TQString(
"%1 [%2]").arg(friendlyName()).arg(deviceNode());
780 TQString TDEStorageDevice::friendlyDeviceType() {
781 TQString ret = i18n(
"Hard Disk Drive");
784 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
785 ret = i18n(
"Floppy Drive");
787 if (isDiskOfType(TDEDiskDeviceType::Optical)) {
788 ret = i18n(
"Optical Drive");
790 if (isDiskOfType(TDEDiskDeviceType::CDROM)) {
791 ret = i18n(
"CDROM Drive");
793 if (isDiskOfType(TDEDiskDeviceType::CDRW)) {
794 ret = i18n(
"CDRW Drive");
796 if (isDiskOfType(TDEDiskDeviceType::DVDROM)) {
797 ret = i18n(
"DVD Drive");
799 if (isDiskOfType(TDEDiskDeviceType::DVDRW)) {
800 ret = i18n(
"DVDRW Drive");
802 if (isDiskOfType(TDEDiskDeviceType::DVDRAM)) {
803 ret = i18n(
"DVDRAM Drive");
805 if (isDiskOfType(TDEDiskDeviceType::Zip)) {
806 ret = i18n(
"Zip Drive");
808 if (isDiskOfType(TDEDiskDeviceType::Tape)) {
809 ret = i18n(
"Tape Drive");
811 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
812 ret = i18n(
"Digital Camera");
815 if (isDiskOfType(TDEDiskDeviceType::HDD)) {
816 ret = i18n(
"Hard Disk Drive");
817 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
818 ret = i18n(
"Removable Storage");
820 if (isDiskOfType(TDEDiskDeviceType::CompactFlash)) {
821 ret = i18n(
"Compact Flash");
823 if (isDiskOfType(TDEDiskDeviceType::MemoryStick)) {
824 ret = i18n(
"Memory Stick");
826 if (isDiskOfType(TDEDiskDeviceType::SmartMedia)) {
827 ret = i18n(
"Smart Media");
829 if (isDiskOfType(TDEDiskDeviceType::SDMMC)) {
830 ret = i18n(
"Secure Digital");
834 if (isDiskOfType(TDEDiskDeviceType::RAM)) {
835 ret = i18n(
"Random Access Memory");
837 if (isDiskOfType(TDEDiskDeviceType::Loop)) {
838 ret = i18n(
"Loop Device");
845 TQString mountString;
846 if (mountPath() != TQString::null) {
847 mountString =
"-mounted";
850 TQPixmap ret = DesktopIcon(
"drive-harddisk" + mountString, size);
852 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
853 ret = DesktopIcon(
"media-floppy-3_5" + mountString, size);
855 if (isDiskOfType(TDEDiskDeviceType::Optical)) {
856 ret = DesktopIcon(
"media-optical-cdrom" + mountString, size);
858 if (isDiskOfType(TDEDiskDeviceType::CDROM)) {
859 ret = DesktopIcon(
"media-optical-cdrom" + mountString, size);
861 if (isDiskOfType(TDEDiskDeviceType::CDRW)) {
862 ret = DesktopIcon(
"media-optical-cdwriter" + mountString, size);
864 if (isDiskOfType(TDEDiskDeviceType::DVDROM)) {
865 ret = DesktopIcon(
"media-optical-dvd" + mountString, size);
867 if (isDiskOfType(TDEDiskDeviceType::DVDRW)) {
868 ret = DesktopIcon(
"media-optical-dvd" + mountString, size);
870 if (isDiskOfType(TDEDiskDeviceType::DVDRAM)) {
871 ret = DesktopIcon(
"media-optical-dvd" + mountString, size);
873 if (isDiskOfType(TDEDiskDeviceType::Zip)) {
874 ret = DesktopIcon(
"media-floppy-zip" + mountString, size);
876 if (isDiskOfType(TDEDiskDeviceType::Tape)) {
877 ret = DesktopIcon(
"media-tape" + mountString, size);
879 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
880 ret = DesktopIcon(
"camera" + TQString((mountPath() != TQString::null) ?
"_mount" :
"_umount"), size);
883 if (isDiskOfType(TDEDiskDeviceType::HDD)) {
884 ret = DesktopIcon(
"drive-harddisk" + mountString, size);
885 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
886 ret = DesktopIcon(
"media-flash-usb" + mountString, size);
888 if (isDiskOfType(TDEDiskDeviceType::CompactFlash)) {
889 ret = DesktopIcon(
"media-flash-compact_flash" + mountString, size);
891 if (isDiskOfType(TDEDiskDeviceType::MemoryStick)) {
892 ret = DesktopIcon(
"media-flash-memory_stick" + mountString, size);
894 if (isDiskOfType(TDEDiskDeviceType::SmartMedia)) {
895 ret = DesktopIcon(
"media-flash-smart_media" + mountString, size);
897 if (isDiskOfType(TDEDiskDeviceType::SDMMC)) {
898 ret = DesktopIcon(
"media-flash-sd_mmc" + mountString, size);
902 if (isDiskOfType(TDEDiskDeviceType::RAM)) {
903 ret = DesktopIcon(
"memory" + mountString, size);
905 if (isDiskOfType(TDEDiskDeviceType::Loop)) {
906 ret = DesktopIcon(
"blockdevice" + mountString, size);
912 unsigned long long TDEStorageDevice::deviceSize() {
913 TQString bsnodename = systemPath();
916 TQString blocksize =
"512";
918 TQString dsnodename = systemPath();
919 dsnodename.append(
"/size");
920 TQFile dsfile( dsnodename );
922 if ( dsfile.open( IO_ReadOnly ) ) {
923 TQTextStream stream( &dsfile );
924 devicesize = stream.readLine();
928 return ((
unsigned long long)blocksize.toULong()*(
unsigned long long)devicesize.toULong());
931 TQString TDEStorageDevice::deviceFriendlySize() {
932 return TDEHardwareDevices::bytesToFriendlySizeString(deviceSize());
935 TQString TDEStorageDevice::mountPath() {
946 TQString dmnodename = systemPath();
947 dmnodename.append(
"/dm/name");
948 TQFile namefile( dmnodename );
950 if ( namefile.open( IO_ReadOnly ) ) {
951 TQTextStream stream( &namefile );
952 dmaltname = stream.readLine();
955 if (!dmaltname.isNull()) {
956 dmaltname.prepend(
"/dev/mapper/");
960 TQFile file(
"/proc/mounts" );
961 if ( file.open( IO_ReadOnly ) ) {
962 TQTextStream stream( &file );
964 while ( !stream.atEnd() ) {
965 line = stream.readLine();
966 TQStringList mountInfo = TQStringList::split(
" ", line,
true);
967 TQString testNode = *mountInfo.at(0);
969 if ((testNode == deviceNode()) || (testNode == dmaltname) || (testNode == (
"/dev/disk/by-uuid/" + diskUUID()))) {
970 TQString ret = *mountInfo.at(1);
971 ret.replace(
"\\040",
" ");
981 TQStringList slaveDeviceList = holdingDevices();
982 for ( TQStringList::Iterator slavedevit = slaveDeviceList.begin(); slavedevit != slaveDeviceList.end(); ++slavedevit ) {
985 TDEGenericDevice *hwdevice = hwdevices->findBySystemPath(*slavedevit);
986 if ((hwdevice) && (hwdevice->type() == TDEGenericDeviceType::Disk)) {
987 TDEStorageDevice* sdevice =
static_cast<TDEStorageDevice*
>(hwdevice);
988 return sdevice->mountPath();
992 return TQString::null;
995 TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet,
int* retcode) {
996 int internal_retcode;
998 retcode = &internal_retcode;
1001 TQString ret = mountPath();
1004 if (!ret.isNull()) {
1009 TQString devNode = deviceNode();
1010 devNode.replace(
"'",
"'\\''");
1011 mediaName.replace(
"'",
"'\\''");
1013 #if defined(WITH_UDEVIL) || defined(WITH_UDISKS2) || defined(WITH_UDISKS) 1015 TQStringList udisksOptions;
1016 TQString optionString;
1018 if (mountOptions[
"ro"] ==
"true") {
1019 udisksOptions.append(
"ro");
1022 if (mountOptions[
"atime"] !=
"true") {
1023 udisksOptions.append(
"noatime");
1026 if (mountOptions[
"sync"] ==
"true") {
1027 udisksOptions.append(
"sync");
1030 if( (mountOptions[
"filesystem"] ==
"fat")
1031 || (mountOptions[
"filesystem"] ==
"vfat")
1032 || (mountOptions[
"filesystem"] ==
"msdos")
1033 || (mountOptions[
"filesystem"] ==
"umsdos")
1035 if (mountOptions.contains(
"shortname")) {
1036 udisksOptions.append(TQString(
"shortname=%1").arg(mountOptions[
"shortname"]));
1040 if( (mountOptions[
"filesystem"] ==
"jfs")) {
1041 if (mountOptions[
"utf8"] ==
"true") {
1047 if( (mountOptions[
"filesystem"] ==
"ntfs-3g") ) {
1048 if (mountOptions.contains(
"locale")) {
1049 udisksOptions.append(TQString(
"locale=%1").arg(mountOptions[
"locale"]));
1053 if( (mountOptions[
"filesystem"] ==
"ext3")
1054 || (mountOptions[
"filesystem"] ==
"ext4")
1056 if (mountOptions.contains(
"journaling")) {
1062 for (TQStringList::Iterator it = udisksOptions.begin(); it != udisksOptions.end(); ++it) {
1063 optionString.append(
",");
1064 optionString.append(*it);
1067 if (!optionString.isEmpty()) {
1068 optionString.remove(0, 1);
1070 #endif // defined(WITH_UDEVIL) || defined(WITH_UDISKS2) || defined(WITH_UDISKS) 1073 if(command.isEmpty()) {
1076 if (!udevilProg.isEmpty()) {
1078 TQString fileSystemType;
1079 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
1080 fileSystemType = TQString(
"-t %1").arg(mountOptions[
"filesystem"]);
1083 TQString mountpoint;
1084 if (mountOptions.contains(
"mountpoint")
1085 && !mountOptions[
"mountpoint"].isEmpty()
1086 && (mountOptions[
"mountpoint"] !=
"/media/")) {
1087 mountpoint = mountOptions[
"mountpoint"];
1088 mountpoint.replace(
"'",
"'\\''");
1091 mountpoint = TQString(
"/media/%1").arg(mediaName);
1094 command = TQString(
"udevil mount %1 -o '%2' '%3' '%4' 2>&1").arg(fileSystemType).arg(optionString).arg(devNode).arg(mountpoint);
1097 #endif // WITH_UDEVIL 1100 if(command.isEmpty()) {
1102 TQString errorString;
1103 TQString fileSystemType;
1105 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
1106 fileSystemType = mountOptions[
"filesystem"];
1109 int uDisks2Ret = mountDriveUDisks2(devNode, fileSystemType, optionString, &errorString);
1110 if (uDisks2Ret == 0) {
1117 else if (uDisks2Ret == -1) {
1119 *errRet = errorString;
1130 command = TQString::null;
1133 #endif // WITH_UDISKS2 1136 if(command.isEmpty()) {
1138 TQString errorString;
1139 TQString fileSystemType;
1141 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
1142 fileSystemType = mountOptions[
"filesystem"];
1145 int uDisksRet = mountDriveUDisks(devNode, fileSystemType, udisksOptions, &errorString);
1146 if (uDisksRet == 0) {
1153 else if (uDisksRet == -1) {
1155 *errRet = errorString;
1166 command = TQString::null;
1169 #endif // WITH_UDISKS 1171 if(command.isEmpty()) {
1174 if (!pmountProg.isEmpty()) {
1176 KTempFile passwordFile(TQString::null,
"tmp", 0600);
1177 passwordFile.setAutoDelete(
true);
1179 TQString optionString;
1180 if (mountOptions[
"ro"] ==
"true") {
1181 optionString.append(
" -r");
1184 if (mountOptions[
"atime"] !=
"true") {
1185 optionString.append(
" -A");
1188 if (mountOptions[
"utf8"] ==
"true") {
1189 optionString.append(
" -c utf8");
1192 if (mountOptions[
"sync"] ==
"true") {
1193 optionString.append(
" -s");
1196 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
1197 optionString.append(TQString(
" -t %1").arg(mountOptions[
"filesystem"]));
1200 if (mountOptions.contains(
"locale")) {
1201 optionString.append(TQString(
" -c %1").arg(mountOptions[
"locale"]));
1204 TQString mountpoint;
1205 if (mountOptions.contains(
"mountpoint")
1206 && !mountOptions[
"mountpoint"].isEmpty()
1207 && (mountOptions[
"mountpoint"] !=
"/media/")) {
1208 mountpoint = mountOptions[
"mountpoint"];
1209 mountpoint.replace(
"'",
"'\\''");
1212 mountpoint = mediaName;
1215 TQString passFileName = passwordFile.name();
1216 passFileName.replace(
"'",
"'\\''");
1218 command = TQString(
"pmount -p '%1' %2 '%3' '%4' 2>&1").arg(passFileName).arg(optionString).arg(devNode).arg(mountpoint);
1222 if(command.isEmpty()) {
1224 *errRet = i18n(
"No supported mounting methods were detected on your system");
1229 FILE *exepipe = popen(command.local8Bit(),
"r");
1231 TQString mount_output;
1232 TQTextStream* ts =
new TQTextStream(exepipe, IO_ReadOnly);
1233 mount_output = ts->read();
1235 *retcode = pclose(exepipe);
1237 *errRet = mount_output;
1249 TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet,
int* retcode) {
1250 int internal_retcode;
1252 retcode = &internal_retcode;
1255 TQString ret = mountPath();
1257 if (!ret.isNull()) {
1262 KTempFile passwordFile(TQString::null,
"tmp", 0600);
1263 passwordFile.setAutoDelete(
true);
1264 TQFile* pwFile = passwordFile.file();
1266 return TQString::null;
1269 pwFile->writeBlock(passphrase.ascii(), passphrase.length());
1272 TQString optionString;
1273 if (mountOptions[
"ro"] ==
"true") {
1274 optionString.append(
" -r");
1277 if (mountOptions[
"atime"] !=
"true") {
1278 optionString.append(
" -A");
1281 if (mountOptions[
"utf8"] ==
"true") {
1282 optionString.append(
" -c utf8");
1285 if (mountOptions[
"sync"] ==
"true") {
1286 optionString.append(
" -s");
1289 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
1290 optionString.append(TQString(
" -t %1").arg(mountOptions[
"filesystem"]));
1293 if (mountOptions.contains(
"locale")) {
1294 optionString.append(TQString(
" -c %1").arg(mountOptions[
"locale"]));
1297 TQString passFileName = passwordFile.name();
1298 TQString devNode = deviceNode();
1299 passFileName.replace(
"'",
"'\\''");
1300 devNode.replace(
"'",
"'\\''");
1301 mediaName.replace(
"'",
"'\\''");
1302 TQString command = TQString(
"pmount -p '%1' %2 '%3' '%4' 2>&1").arg(passFileName).arg(optionString).arg(devNode).arg(mediaName);
1304 FILE *exepipe = popen(command.local8Bit(),
"r");
1306 TQString mount_output;
1307 TQTextStream* ts =
new TQTextStream(exepipe, IO_ReadOnly);
1308 mount_output = ts->read();
1310 *retcode = pclose(exepipe);
1312 *errRet = mount_output;
1324 bool TDEStorageDevice::unmountDevice(TQString* errRet,
int* retcode) {
1325 int internal_retcode;
1327 retcode = &internal_retcode;
1330 TQString mountpoint = mountPath();
1331 TQString devNode = deviceNode();
1333 if (mountpoint.isNull()) {
1337 mountpoint.replace(
"'",
"'\\''");
1342 if(command.isEmpty() &&
1344 command = TQString(
"udevil umount '%1' 2>&1").arg(mountpoint);
1346 #endif // WITH_UDEVIL 1349 if(command.isEmpty()) {
1351 TQString errorString;
1352 int unMountUDisks2Ret = unMountDriveUDisks2(devNode, TQString::null, &errorString);
1353 if (unMountUDisks2Ret == 0) {
1359 else if (unMountUDisks2Ret == -1) {
1361 *errRet = errorString;
1371 command = TQString::null;
1374 #endif // WITH_UDISKS2 1377 if(command.isEmpty()) {
1379 TQString errorString;
1380 int unMountUDisksRet = unMountDriveUDisks(devNode, TQStringList(), &errorString);
1381 if (unMountUDisksRet == 0) {
1387 else if (unMountUDisksRet == -1) {
1389 *errRet = errorString;
1399 command = TQString::null;
1402 #endif // WITH_UDISKS 1404 if(command.isEmpty() &&
1406 command = TQString(
"pumount '%1' 2>&1").arg(mountpoint);
1409 if(command.isEmpty()) {
1411 *errRet = i18n(
"No supported unmounting methods were detected on your system");
1416 FILE *exepipe = popen(command.local8Bit(),
"r");
1418 TQString umount_output;
1419 TQTextStream* ts =
new TQTextStream(exepipe, IO_ReadOnly);
1420 umount_output = ts->read();
1422 *retcode = pclose(exepipe);
1423 if (*retcode == 0) {
1431 *errRet = umount_output;
1442 TQString TDEStorageDevice::determineFileSystemType(TQString path) {
1443 TQStringList mountTable;
1444 TQString prevPath = path;
1447 struct stat directory_info;
1448 if (path.startsWith(
"/")) {
1449 stat(path.local8Bit(), &directory_info);
1450 prevDev = directory_info.st_dev;
1453 while (path !=
"/") {
1454 pos = path.findRev(
"/", -1, TRUE);
1458 path = path.mid(0, pos);
1462 stat(path.local8Bit(), &directory_info);
1463 if (directory_info.st_dev != prevDev) {
1467 prevDev = directory_info.st_dev;
1473 TQFile file(
"/proc/mounts" );
1474 if ( file.open( IO_ReadOnly ) ) {
1475 TQTextStream stream( &file );
1476 while ( !stream.atEnd() ) {
1477 mountTable.append(stream.readLine());
1483 TQStringList::Iterator it;
1484 for ( it = mountTable.begin(); it != mountTable.end(); ++it ) {
1485 TQStringList mountInfo = TQStringList::split(
" ", (*it),
true);
1486 if ((*mountInfo.at(1)) == prevPath) {
1487 return (*mountInfo.at(2));
1492 return TQString::null;
1495 #include "tdestoragedevice.moc" const TDEShortcut & open()
Open file.
static TDEHardwareDevices * hardwareDevices()
Returns a TDEHardwareDevices object.
static TDEStandardDirs * dirs()
Returns the application standard dirs object.
StdSizes
These are the standard sizes for icons.
static TQString findExe(const TQString &appname, const TQString &pathstr=TQString::null, bool ignoreExecBit=false)
Finds the executable in the system path.
const TDEShortcut & close()
Close current document.
The KTempFile class creates and opens a unique file for temporary use.
kndbgstream & endl(kndbgstream &s)
Does nothing.
TQString label(StdAccel id)
Returns a localized label for user-visible display.