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"
38 #include "tdehardwaredevices.h"
39 #include "disksHelper.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>
58 TDEStorageDevice::TDEStorageDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn), m_mediaInserted(true), m_cryptDevice(NULL) {
59 m_diskType = TDEDiskDeviceType::Null;
60 m_diskStatus = TDEDiskDeviceStatus::Null;
63 TDEStorageDevice::~TDEStorageDevice() {
64 #if defined(WITH_CRYPTSETUP)
66 crypt_free(m_cryptDevice);
72 TDEDiskDeviceType::TDEDiskDeviceType TDEStorageDevice::diskType() {
76 void TDEStorageDevice::internalGetLUKSKeySlotStatus() {
77 #if defined(WITH_CRYPTSETUP)
79 crypt_keyslot_info keyslot_status;
80 TDELUKSKeySlotStatus::TDELUKSKeySlotStatus tde_keyslot_status;
82 m_cryptKeyslotStatus.clear();
83 for (i = 0; i < m_cryptKeySlotCount; i++) {
84 keyslot_status = crypt_keyslot_status(m_cryptDevice, i);
85 tde_keyslot_status = TDELUKSKeySlotStatus::Invalid;
86 if (keyslot_status == CRYPT_SLOT_INACTIVE) {
87 tde_keyslot_status = TDELUKSKeySlotStatus::Inactive;
89 else if (keyslot_status == CRYPT_SLOT_ACTIVE) {
90 tde_keyslot_status = TDELUKSKeySlotStatus::Active;
92 else if (keyslot_status == CRYPT_SLOT_ACTIVE_LAST) {
93 tde_keyslot_status = TDELUKSKeySlotStatus::Active | TDELUKSKeySlotStatus::Last;
95 m_cryptKeyslotStatus.append(tde_keyslot_status);
100 void TDEStorageDevice::internalInitializeLUKSIfNeeded() {
101 #if defined(WITH_CRYPTSETUP)
104 if (m_diskType & TDEDiskDeviceType::LUKS) {
105 if (!m_cryptDevice) {
106 TQString node = deviceNode();
108 ret = crypt_init(&m_cryptDevice, node.ascii());
110 ret = crypt_load(m_cryptDevice, NULL, NULL);
113 #if defined(CRYPTSETUP_OLD_API) || !defined(HAVE_CRYPTSETUP_GET_TYPE)
114 kdWarning() <<
"TDEStorageDevice: The version of libcryptsetup that TDE was compiled against was too old! Most LUKS features will not function" <<
endl;
115 m_cryptDeviceType = TQString::null;
118 m_cryptDeviceType = crypt_get_type(m_cryptDevice);
119 keyslot_count = crypt_keyslot_max(m_cryptDeviceType.ascii());
121 if (keyslot_count < 0) {
122 m_cryptKeySlotCount = 0;
125 m_cryptKeySlotCount = keyslot_count;
127 internalGetLUKSKeySlotStatus();
131 m_cryptDevice = NULL;
138 crypt_free(m_cryptDevice);
139 m_cryptDevice = NULL;
145 void TDEStorageDevice::cryptSetOperationsUnlockPassword(TQByteArray password) {
146 #if defined(WITH_CRYPTSETUP)
147 crypt_memory_lock(NULL, 1);
148 m_cryptDevicePassword = password;
152 void TDEStorageDevice::cryptClearOperationsUnlockPassword() {
153 m_cryptDevicePassword.fill(0);
154 m_cryptDevicePassword.resize(0);
155 #if defined(WITH_CRYPTSETUP)
156 crypt_memory_lock(NULL, 0);
160 bool TDEStorageDevice::cryptOperationsUnlockPasswordSet() {
161 if (m_cryptDevicePassword.size() > 0) {
169 TDELUKSResult::TDELUKSResult TDEStorageDevice::cryptCheckKey(
unsigned int keyslot) {
170 #if defined(WITH_CRYPTSETUP)
174 if (keyslot < m_cryptKeySlotCount) {
175 ret = crypt_activate_by_passphrase(m_cryptDevice, NULL, keyslot, m_cryptDevicePassword.data(), m_cryptDevicePassword.size(), 0);
177 return TDELUKSResult::KeyslotOpFailed;
180 return TDELUKSResult::Success;
184 return TDELUKSResult::InvalidKeyslot;
188 return TDELUKSResult::LUKSNotFound;
191 return TDELUKSResult::LUKSNotSupported;
195 TDELUKSResult::TDELUKSResult TDEStorageDevice::cryptAddKey(
unsigned int keyslot, TQByteArray password) {
196 #if defined(WITH_CRYPTSETUP)
200 if (keyslot < m_cryptKeySlotCount) {
201 ret = crypt_keyslot_add_by_passphrase(m_cryptDevice, keyslot, m_cryptDevicePassword.data(), m_cryptDevicePassword.size(), password.data(), password.size());
203 return TDELUKSResult::KeyslotOpFailed;
206 internalGetLUKSKeySlotStatus();
207 return TDELUKSResult::Success;
211 return TDELUKSResult::InvalidKeyslot;
215 return TDELUKSResult::LUKSNotFound;
218 return TDELUKSResult::LUKSNotSupported;
222 TDELUKSResult::TDELUKSResult TDEStorageDevice::cryptDelKey(
unsigned int keyslot) {
223 #if defined(WITH_CRYPTSETUP)
227 if (keyslot < m_cryptKeySlotCount) {
228 ret = crypt_keyslot_destroy(m_cryptDevice, keyslot);
230 return TDELUKSResult::KeyslotOpFailed;
233 internalGetLUKSKeySlotStatus();
234 return TDELUKSResult::Success;
238 return TDELUKSResult::InvalidKeyslot;
242 return TDELUKSResult::LUKSNotFound;
245 return TDELUKSResult::LUKSNotSupported;
249 unsigned int TDEStorageDevice::cryptKeySlotCount() {
250 return m_cryptKeySlotCount;
253 TDELUKSKeySlotStatusList TDEStorageDevice::cryptKeySlotStatus() {
254 return m_cryptKeyslotStatus;
257 TQString TDEStorageDevice::cryptKeySlotFriendlyName(TDELUKSKeySlotStatus::TDELUKSKeySlotStatus status) {
258 if (status & TDELUKSKeySlotStatus::Inactive) {
259 return i18n(
"Inactive");
261 else if (status & TDELUKSKeySlotStatus::Active) {
262 return i18n(
"Active");
265 return i18n(
"Unknown");
269 void TDEStorageDevice::internalSetDeviceNode(TQString sn) {
270 TDEGenericDevice::internalSetDeviceNode(sn);
271 internalInitializeLUKSIfNeeded();
274 void TDEStorageDevice::internalSetDiskType(TDEDiskDeviceType::TDEDiskDeviceType dt) {
276 internalInitializeLUKSIfNeeded();
279 bool TDEStorageDevice::isDiskOfType(TDEDiskDeviceType::TDEDiskDeviceType tf) {
280 return ((m_diskType&tf)!=TDEDiskDeviceType::Null);
283 TDEDiskDeviceStatus::TDEDiskDeviceStatus TDEStorageDevice::diskStatus() {
287 void TDEStorageDevice::internalSetDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus st) {
291 bool TDEStorageDevice::checkDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus sf) {
292 return ((m_diskStatus&sf)!=(TDEDiskDeviceStatus::TDEDiskDeviceStatus)0);
295 bool TDEStorageDevice::lockDriveMedia(
bool lock) {
296 int fd =
open(deviceNode().ascii(), O_RDWR | O_NONBLOCK);
300 if (ioctl(fd, CDROM_LOCKDOOR, (lock)?1:0) != 0) {
310 bool TDEStorageDevice::ejectDrive() {
312 TQStringVariantMap ejectResult = UDisks2EjectDrive(
this);
313 if (ejectResult[
"result"].toBool()) {
317 printf(
"[tdehwlib] Failed to eject drive '%s' via udisks2, falling back to alternate mechanism\n", deviceNode().ascii());
322 TQStringVariantMap ejectResult = UDisksEjectDrive(
this);
323 if (ejectResult[
"result"].toBool()) {
327 printf(
"[tdehwlib] Failed to eject drive '%s' via udisks, falling back to alternate mechanism\n", deviceNode().ascii());
333 TQString command = TQString(
"eject -v '%1' 2>&1").arg(deviceNode());
335 FILE *exepipe = popen(command.ascii(),
"r");
337 TQString eject_output;
338 TQTextStream ts(exepipe, IO_ReadOnly);
339 eject_output = ts.read();
340 int retcode = pclose(exepipe);
345 printf(
"[tdehwlib] Failed to eject drive '%s' via 'eject' command\n", deviceNode().ascii());
352 bool TDEStorageDevice::ejectDriveMedia() {
353 int fd =
open(deviceNode().ascii(), O_RDWR | O_NONBLOCK);
357 if (ioctl(fd, CDROMEJECT) != 0) {
367 TQString TDEStorageDevice::diskLabel() {
371 void TDEStorageDevice::internalSetDiskLabel(TQString dn) {
375 bool TDEStorageDevice::mediaInserted() {
376 return m_mediaInserted;
379 void TDEStorageDevice::internalSetMediaInserted(
bool inserted) {
380 m_mediaInserted = inserted;
383 TQString TDEStorageDevice::fileSystemName() {
384 return m_fileSystemName;
387 void TDEStorageDevice::internalSetFileSystemName(TQString fn) {
388 m_fileSystemName = fn;
391 TQString TDEStorageDevice::fileSystemUsage() {
392 return m_fileSystemUsage;
395 void TDEStorageDevice::internalSetFileSystemUsage(TQString fu) {
396 m_fileSystemUsage = fu;
399 TQString TDEStorageDevice::diskUUID() {
403 void TDEStorageDevice::internalSetDiskUUID(TQString
id) {
407 TQStringList TDEStorageDevice::holdingDevices() {
408 return m_holdingDevices;
411 void TDEStorageDevice::internalSetHoldingDevices(TQStringList hd) {
412 m_holdingDevices = hd;
415 TQStringList TDEStorageDevice::slaveDevices() {
416 return m_slaveDevices;
419 void TDEStorageDevice::internalSetSlaveDevices(TQStringList sd) {
423 TQString decodeHexEncoding(TQString str) {
424 TQRegExp hexEncRegExp(
"\\\\x[0-9A-Fa-f]{1,2}");
425 hexEncRegExp.setMinimal(
false);
426 hexEncRegExp.setCaseSensitive(
true);
429 while((s = hexEncRegExp.search(str, s+1))>=0){
430 str.replace(s, hexEncRegExp.cap(0).length(), TQChar((
char)strtol(hexEncRegExp.cap(0).mid(2).ascii(), NULL, 16)));
436 TQString TDEStorageDevice::friendlyName() {
438 TQString devicevendorid = vendorEncoded();
439 TQString devicemodelid = modelEncoded();
441 devicevendorid = decodeHexEncoding(devicevendorid);
442 devicemodelid = decodeHexEncoding(devicemodelid);
444 devicevendorid = devicevendorid.stripWhiteSpace();
445 devicemodelid = devicemodelid.stripWhiteSpace();
446 devicevendorid = devicevendorid.simplifyWhiteSpace();
447 devicemodelid = devicemodelid.simplifyWhiteSpace();
449 TQString devicename = devicevendorid +
" " + devicemodelid;
451 devicename = devicename.stripWhiteSpace();
452 devicename = devicename.simplifyWhiteSpace();
454 if (devicename !=
"") {
458 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
459 return TDEGenericDevice::friendlyName();
462 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
463 return friendlyDeviceType();
466 TQString
label = diskLabel();
467 if (
label.isNull()) {
468 if (deviceSize() > 0) {
469 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
470 label =
i18n(
"%1 Removable Device").arg(deviceFriendlySize());
473 label =
i18n(
"%1 Fixed Storage Device").arg(deviceFriendlySize());
478 if (!
label.isNull()) {
482 return friendlyDeviceType();
485 TQString TDEStorageDevice::detailedFriendlyName() {
486 return TQString(
"%1 [%2]").arg(friendlyName()).arg(deviceNode());
489 TQString TDEStorageDevice::friendlyDeviceType() {
490 TQString ret =
i18n(
"Hard Disk Drive");
493 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
494 ret =
i18n(
"Floppy Drive");
496 if (isDiskOfType(TDEDiskDeviceType::Optical)) {
497 ret =
i18n(
"Optical Drive");
499 if (isDiskOfType(TDEDiskDeviceType::CDROM)) {
500 ret =
i18n(
"CDROM Drive");
502 if (isDiskOfType(TDEDiskDeviceType::CDRW)) {
503 ret =
i18n(
"CDRW Drive");
505 if (isDiskOfType(TDEDiskDeviceType::DVDROM)) {
506 ret =
i18n(
"DVD Drive");
508 if (isDiskOfType(TDEDiskDeviceType::DVDRW)) {
509 ret =
i18n(
"DVDRW Drive");
511 if (isDiskOfType(TDEDiskDeviceType::DVDRAM)) {
512 ret =
i18n(
"DVDRAM Drive");
514 if (isDiskOfType(TDEDiskDeviceType::Zip)) {
515 ret =
i18n(
"Zip Drive");
517 if (isDiskOfType(TDEDiskDeviceType::Tape)) {
518 ret =
i18n(
"Tape Drive");
520 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
521 ret =
i18n(
"Digital Camera");
524 if (isDiskOfType(TDEDiskDeviceType::HDD)) {
525 ret =
i18n(
"Hard Disk Drive");
526 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
527 ret =
i18n(
"Removable Storage");
529 if (isDiskOfType(TDEDiskDeviceType::CompactFlash)) {
530 ret =
i18n(
"Compact Flash");
532 if (isDiskOfType(TDEDiskDeviceType::MemoryStick)) {
533 ret =
i18n(
"Memory Stick");
535 if (isDiskOfType(TDEDiskDeviceType::SmartMedia)) {
536 ret =
i18n(
"Smart Media");
538 if (isDiskOfType(TDEDiskDeviceType::SDMMC)) {
539 ret =
i18n(
"Secure Digital");
543 if (isDiskOfType(TDEDiskDeviceType::RAM)) {
544 ret =
i18n(
"Random Access Memory");
546 if (isDiskOfType(TDEDiskDeviceType::Loop)) {
547 ret =
i18n(
"Loop Device");
554 TQString mountString;
555 if (mountPath() != TQString::null) {
556 mountString =
"-mounted";
559 mountString =
"-unmounted";
562 TQPixmap ret =
DesktopIcon(
"drive-harddisk" + mountString, size);
564 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
565 ret =
DesktopIcon(
"media-floppy-3_5" + mountString, size);
567 if (isDiskOfType(TDEDiskDeviceType::Optical)) {
568 ret =
DesktopIcon(
"media-optical-cdrom" + mountString, size);
570 if (isDiskOfType(TDEDiskDeviceType::CDROM)) {
571 ret =
DesktopIcon(
"media-optical-cdrom" + mountString, size);
573 if (isDiskOfType(TDEDiskDeviceType::CDRW)) {
576 if (isDiskOfType(TDEDiskDeviceType::DVDROM)) {
577 ret =
DesktopIcon(
"media-optical-dvd" + mountString, size);
579 if (isDiskOfType(TDEDiskDeviceType::DVDRW)) {
580 ret =
DesktopIcon(
"media-optical-dvd" + mountString, size);
582 if (isDiskOfType(TDEDiskDeviceType::DVDRAM)) {
583 ret =
DesktopIcon(
"media-optical-dvd" + mountString, size);
585 if (isDiskOfType(TDEDiskDeviceType::Zip)) {
586 ret =
DesktopIcon(
"media-floppy-zip" + mountString, size);
588 if (isDiskOfType(TDEDiskDeviceType::Tape)) {
589 ret =
DesktopIcon(
"media-tape" + mountString, size);
591 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
595 if (isDiskOfType(TDEDiskDeviceType::HDD)) {
596 ret =
DesktopIcon(
"drive-harddisk" + mountString, size);
597 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
598 ret =
DesktopIcon(
"media-flash-usb" + mountString, size);
600 if (isDiskOfType(TDEDiskDeviceType::CompactFlash)) {
601 ret =
DesktopIcon(
"media-flash-compact_flash" + mountString, size);
603 if (isDiskOfType(TDEDiskDeviceType::MemoryStick)) {
604 ret =
DesktopIcon(
"media-flash-memory_stick" + mountString, size);
606 if (isDiskOfType(TDEDiskDeviceType::SmartMedia)) {
607 ret =
DesktopIcon(
"media-flash-smart_media" + mountString, size);
609 if (isDiskOfType(TDEDiskDeviceType::SDMMC)) {
610 ret =
DesktopIcon(
"media-flash-sd_mmc" + mountString, size);
614 if (isDiskOfType(TDEDiskDeviceType::RAM)) {
617 if (isDiskOfType(TDEDiskDeviceType::Loop)) {
624 unsigned long long TDEStorageDevice::deviceSize() {
625 TQString bsnodename = systemPath();
628 TQString blocksize =
"512";
630 TQString dsnodename = systemPath();
631 dsnodename.append(
"/size");
632 TQFile dsfile( dsnodename );
634 if ( dsfile.open( IO_ReadOnly ) ) {
635 TQTextStream stream( &dsfile );
636 devicesize = stream.readLine();
640 return ((
unsigned long long)blocksize.toULong()*(
unsigned long long)devicesize.toULong());
643 TQString TDEStorageDevice::deviceFriendlySize() {
644 return TDEHardwareDevices::bytesToFriendlySizeString(deviceSize());
647 TQString TDEStorageDevice::mountPath() {
656 TDEGlobal::hardwareDevices()->rescanDeviceInformation(
this);
658 TQString dmnodename = systemPath();
659 dmnodename.append(
"/dm/name");
660 TQFile namefile( dmnodename );
662 if ( namefile.open( IO_ReadOnly ) ) {
663 TQTextStream stream( &namefile );
664 dmaltname = stream.readLine();
667 if (!dmaltname.isNull()) {
668 dmaltname.prepend(
"/dev/mapper/");
672 TQFile file(
"/proc/mounts" );
673 if ( file.open( IO_ReadOnly ) ) {
674 TQTextStream stream( &file );
676 while ( !stream.atEnd() ) {
677 line = stream.readLine();
678 TQStringList mountInfo = TQStringList::split(
" ", line,
true);
679 TQString testNode = *mountInfo.at(0);
681 if ((testNode == deviceNode()) || (testNode == dmaltname) || (testNode == (
"/dev/disk/by-uuid/" + diskUUID()))) {
682 TQString ret = *mountInfo.at(1);
683 ret.replace(
"\\040",
" ");
693 TQStringList slaveDeviceList = holdingDevices();
694 for ( TQStringList::Iterator slavedevit = slaveDeviceList.begin(); slavedevit != slaveDeviceList.end(); ++slavedevit ) {
696 TDEHardwareDevices *hwdevices = TDEGlobal::hardwareDevices();
697 TDEGenericDevice *hwdevice = hwdevices->findBySystemPath(*slavedevit);
698 if ((hwdevice) && (hwdevice->type() == TDEGenericDeviceType::Disk)) {
699 TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
700 return sdevice->mountPath();
704 return TQString::null;
707 TQStringVariantMap TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOptions mountOptions) {
708 TQStringVariantMap result;
711 TQString mountpath = mountPath();
712 if (!mountpath.isEmpty()) {
713 result[
"mountPath"] = mountpath;
714 result[
"result"] =
true;
718 TQString devNode = deviceNode();
719 devNode.replace(
"'",
"'\\''");
720 mediaName.replace(
"'",
"'\\''");
723 TQStringList udisksOptions;
724 if (mountOptions[
"ro"] ==
"true") {
725 udisksOptions.append(
"ro");
728 if (mountOptions[
"atime"] !=
"true") {
729 udisksOptions.append(
"noatime");
732 if (mountOptions[
"sync"] ==
"true") {
733 udisksOptions.append(
"sync");
736 if ((mountOptions[
"filesystem"] ==
"fat") || (mountOptions[
"filesystem"] ==
"vfat") ||
737 (mountOptions[
"filesystem"] ==
"msdos") || (mountOptions[
"filesystem"] ==
"umsdos")) {
738 if (mountOptions.contains(
"shortname")) {
739 udisksOptions.append(TQString(
"shortname=%1").arg(mountOptions[
"shortname"]));
743 if ((mountOptions[
"filesystem"] ==
"jfs")) {
744 if (mountOptions[
"utf8"] ==
"true") {
750 if ((mountOptions[
"filesystem"] ==
"ntfs-3g")) {
751 if (mountOptions.contains(
"locale")) {
752 udisksOptions.append(TQString(
"locale=%1").arg(mountOptions[
"locale"]));
756 if ((mountOptions[
"filesystem"] ==
"ext3") || (mountOptions[
"filesystem"] ==
"ext4")) {
757 if (mountOptions.contains(
"journaling")) {
763 TQString optionString;
764 for (TQStringList::Iterator it = udisksOptions.begin(); it != udisksOptions.end(); ++it) {
765 optionString.append(
",");
766 optionString.append(*it);
769 if (!optionString.isEmpty()) {
770 optionString.remove(0, 1);
774 TQString fileSystemType;
775 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
776 fileSystemType = mountOptions[
"filesystem"];
779 TQStringVariantMap mountResult = UDisks2MountDrive(devNode, fileSystemType, optionString);
780 if (mountResult[
"result"].toBool()) {
782 TDEGlobal::hardwareDevices()->processModifiedMounts();
783 result[
"mountPath"] = mountPath();
784 result[
"result"] =
true;
787 else if (mountResult[
"retcode"].toInt() == -1) {
789 TDEGlobal::hardwareDevices()->processModifiedMounts();
790 result[
"errStr"] = mountResult[
"errStr"];
791 result[
"result"] =
false;
797 mountResult = UDisksMountDrive(devNode, fileSystemType, udisksOptions);
798 if (mountResult[
"result"].toBool()) {
800 TDEGlobal::hardwareDevices()->processModifiedMounts();
801 result[
"mountPath"] = mountPath();
802 result[
"result"] =
true;
805 else if (mountResult[
"retcode"].toInt() == -1) {
807 TDEGlobal::hardwareDevices()->processModifiedMounts();
808 result[
"errStr"] = mountResult[
"errStr"];
809 result[
"result"] =
false;
815 TQString command = TQString::null;
817 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
818 fileSystemType = TQString(
"-t %1").arg(mountOptions[
"filesystem"]);
821 if (mountOptions.contains(
"mountpoint") && !mountOptions[
"mountpoint"].isEmpty() &&
822 (mountOptions[
"mountpoint"] !=
"/media/")) {
823 mountpoint = mountOptions[
"mountpoint"];
824 mountpoint.replace(
"'",
"'\\''");
827 mountpoint = TQString(
"/media/%1").arg(mediaName);
829 command = TQString(
"udevil mount %1 -o '%2' '%3' '%4' 2>&1")
830 .arg(fileSystemType).arg(optionString).arg(devNode).arg(mountpoint);
834 if(command.isEmpty()) {
837 KTempFile passwordFile(TQString::null,
"tmp", 0600);
838 passwordFile.setAutoDelete(
true);
840 TQString optionString;
841 if (mountOptions[
"ro"] ==
"true") {
842 optionString.append(
" -r");
845 if (mountOptions[
"atime"] !=
"true") {
846 optionString.append(
" -A");
849 if (mountOptions[
"utf8"] ==
"true") {
850 optionString.append(
" -c utf8");
853 if (mountOptions[
"sync"] ==
"true") {
854 optionString.append(
" -s");
857 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
858 optionString.append(TQString(
" -t %1").arg(mountOptions[
"filesystem"]));
861 if (mountOptions.contains(
"locale")) {
862 optionString.append(TQString(
" -c %1").arg(mountOptions[
"locale"]));
866 if (mountOptions.contains(
"mountpoint") && !mountOptions[
"mountpoint"].isEmpty() &&
867 (mountOptions[
"mountpoint"] !=
"/media/")) {
868 mountpoint = mountOptions[
"mountpoint"];
869 mountpoint.replace(
"'",
"'\\''");
872 mountpoint = mediaName;
875 TQString passFileName = passwordFile.name();
876 passFileName.replace(
"'",
"'\\''");
878 command = TQString(
"pmount -p '%1' %2 '%3' '%4' 2>&1")
879 .arg(passFileName).arg(optionString).arg(devNode).arg(mountpoint);
883 if(command.isEmpty()) {
884 result[
"errStr"] =
i18n(
"No supported mounting methods were detected on your system");
885 result[
"result"] =
false;
889 FILE *exepipe = popen(command.local8Bit(),
"r");
891 TQTextStream* ts =
new TQTextStream(exepipe, IO_ReadOnly);
892 TQString mount_output = ts->read();
894 int retcode = pclose(exepipe);
895 result[
"errStr"] = mount_output;
896 result[
"retCode"] = retcode;
900 TDEGlobal::hardwareDevices()->processModifiedMounts();
901 result[
"mountPath"] = mountPath();
902 result[
"result"] = !mountPath().isEmpty();
906 TQStringVariantMap TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString mediaName,
907 TDEStorageMountOptions mountOptions) {
908 TQStringVariantMap result;
911 TQString mountpath = mountPath();
912 if (!mountpath.isEmpty()) {
913 result[
"mountPath"] = mountpath;
914 result[
"result"] =
true;
919 KTempFile passwordFile(TQString::null,
"tmp", 0600);
920 passwordFile.setAutoDelete(
true);
921 TQFile* pwFile = passwordFile.file();
923 result[
"errStr"] =
i18n(
"Cannot create temporary password file");
924 result[
"result"] =
false;
928 pwFile->writeBlock(passphrase.ascii(), passphrase.length());
931 TQString optionString;
932 if (mountOptions[
"ro"] ==
"true") {
933 optionString.append(
" -r");
936 if (mountOptions[
"atime"] !=
"true") {
937 optionString.append(
" -A");
940 if (mountOptions[
"utf8"] ==
"true") {
941 optionString.append(
" -c utf8");
944 if (mountOptions[
"sync"] ==
"true") {
945 optionString.append(
" -s");
948 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
949 optionString.append(TQString(
" -t %1").arg(mountOptions[
"filesystem"]));
952 if (mountOptions.contains(
"locale")) {
953 optionString.append(TQString(
" -c %1").arg(mountOptions[
"locale"]));
956 TQString passFileName = passwordFile.name();
957 TQString devNode = deviceNode();
958 passFileName.replace(
"'",
"'\\''");
959 devNode.replace(
"'",
"'\\''");
960 mediaName.replace(
"'",
"'\\''");
961 TQString command = TQString(
"pmount -p '%1' %2 '%3' '%4' 2>&1")
962 .arg(passFileName).arg(optionString).arg(devNode).arg(mediaName);
964 FILE *exepipe = popen(command.local8Bit(),
"r");
966 TQTextStream* ts =
new TQTextStream(exepipe, IO_ReadOnly);
967 TQString mount_output = ts->read();
969 int retcode = pclose(exepipe);
970 result[
"errStr"] = mount_output;
971 result[
"retCode"] = retcode;
975 TDEGlobal::hardwareDevices()->processModifiedMounts();
976 result[
"mountPath"] = mountPath();
977 result[
"result"] = !mountPath().isEmpty();
981 TQStringVariantMap TDEStorageDevice::unmountDevice() {
982 TQStringVariantMap result;
985 TQString mountpoint = mountPath();
986 if (mountpoint.isEmpty()) {
987 result[
"result"] =
true;
991 mountpoint.replace(
"'",
"'\\''");
992 TQString devNode = deviceNode();
995 TQStringVariantMap unmountResult = UDisks2UnmountDrive(devNode, TQString::null);
996 if (unmountResult[
"result"].toBool()) {
998 TDEGlobal::hardwareDevices()->processModifiedMounts();
999 result[
"result"] =
true;
1002 else if (unmountResult[
"retcode"].toInt() == -1) {
1004 TDEGlobal::hardwareDevices()->processModifiedMounts();
1005 result[
"errStr"] = unmountResult[
"errStr"];
1006 result[
"result"] =
false;
1012 unmountResult = UDisksUnmountDrive(devNode, TQStringList());
1013 if (unmountResult[
"result"].toBool()) {
1015 TDEGlobal::hardwareDevices()->processModifiedMounts();
1016 result[
"result"] =
true;
1019 else if (unmountResult[
"retcode"].toInt() == -1) {
1021 TDEGlobal::hardwareDevices()->processModifiedMounts();
1022 result[
"errStr"] = unmountResult[
"errStr"];
1023 result[
"result"] =
false;
1029 TQString command = TQString::null;
1031 command = TQString(
"udevil umount '%1' 2>&1").arg(mountpoint);
1036 command = TQString(
"pumount '%1' 2>&1").arg(mountpoint);
1039 if(command.isEmpty()) {
1040 result[
"errStr"] =
i18n(
"No supported unmounting methods were detected on your system");
1041 result[
"result"] =
false;
1045 FILE *exepipe = popen(command.local8Bit(),
"r");
1047 TQTextStream* ts =
new TQTextStream(exepipe, IO_ReadOnly);
1048 TQString umount_output = ts->read();
1050 int retcode = pclose(exepipe);
1053 TDEGlobal::hardwareDevices()->processModifiedMounts();
1054 result[
"result"] =
true;
1058 result[
"errStr"] = umount_output;
1059 result[
"retCode"] = retcode;
1064 TDEGlobal::hardwareDevices()->processModifiedMounts();
1065 result[
"result"] =
false;
1069 TQString TDEStorageDevice::determineFileSystemType(TQString path) {
1070 TQStringList mountTable;
1071 TQString prevPath = path;
1074 struct stat directory_info;
1075 if (path.startsWith(
"/")) {
1076 stat(path.local8Bit(), &directory_info);
1077 prevDev = directory_info.st_dev;
1080 while (path !=
"/") {
1081 pos = path.findRev(
"/", -1,
true);
1085 path = path.mid(0, pos);
1089 stat(path.local8Bit(), &directory_info);
1090 if (directory_info.st_dev != prevDev) {
1094 prevDev = directory_info.st_dev;
1100 TQFile file(
"/proc/mounts" );
1101 if ( file.open( IO_ReadOnly ) ) {
1102 TQTextStream stream( &file );
1103 while ( !stream.atEnd() ) {
1104 mountTable.append(stream.readLine());
1110 TQStringList::Iterator it;
1111 for ( it = mountTable.begin(); it != mountTable.end(); ++it ) {
1112 TQStringList mountInfo = TQStringList::split(
" ", (*it),
true);
1113 if ((*mountInfo.at(1)) == prevPath) {
1114 return (*mountInfo.at(2));
1119 return TQString::null;
1122 #include "tdestoragedevice.moc"