bool LibPartedDevice::createPartitionTable(Report& report, const PartitionTable& ptable) { PedDiskType* pedDiskType = ped_disk_type_get(ptable.typeName().toLatin1().constData()); if (pedDiskType == nullptr) { report.line() << xi18nc("@info:progress", "Creating partition table failed: Could not retrieve partition table type \"%1\" for <filename>%2</filename>.", ptable.typeName(), deviceNode()); return false; } PedDevice* dev = ped_device_get(deviceNode().toLatin1().constData()); if (dev == nullptr) { report.line() << xi18nc("@info:progress", "Creating partition table failed: Could not open backend device <filename>%1</filename>.", deviceNode()); return false; } PedDisk* disk = ped_disk_new_fresh(dev, pedDiskType); if (disk == nullptr) { report.line() << xi18nc("@info:progress", "Creating partition table failed: Could not create a new partition table in the backend for device <filename>%1</filename>.", deviceNode()); return false; } return LibPartedPartitionTable::commit(disk); }
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/<something>, // where <something> is listed in <system path>/dm/name // First, ensure that all device information (mainly holders/slaves) is accurate TDEGlobal::hardwareDevices()->rescanDeviceInformation(this); 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 ) ) { TQTextStream stream( &file ); TQString line; while ( !stream.atEnd() ) { line = stream.readLine(); TQStringList mountInfo = TQStringList::split(" ", line, true); TQString testNode = *mountInfo.at(0); // Check for match if ((testNode == deviceNode()) || (testNode == dmaltname) || (testNode == ("/dev/disk/by-uuid/" + diskUUID()))) { TQString ret = *mountInfo.at(1); ret.replace("\\040", " "); return ret; } lines += line; } 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 = holdingDevices(); for ( TQStringList::Iterator slavedevit = slaveDeviceList.begin(); slavedevit != slaveDeviceList.end(); ++slavedevit ) { // Try to locate this device path in the TDE device tree TDEHardwareDevices *hwdevices = TDEGlobal::hardwareDevices(); TDEGenericDevice *hwdevice = hwdevices->findBySystemPath(*slavedevit); if ((hwdevice) && (hwdevice->type() == TDEGenericDeviceType::Disk)) { TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice); return sdevice->mountPath(); } } return TQString::null; }
bool TDEStorageDevice::ejectDrive() { #ifdef WITH_UDISKS2 if (!(TDEGlobal::dirs()->findExe("udisksctl").isEmpty())) { if (ejectDriveUDisks2(this)) { return TRUE; } else { printf("[tdehwlib] Failed to eject drive '%s' via udisks2, falling back to alternate mechanism\n", deviceNode().ascii()); fflush(stdout); } } #endif // WITH_UDISKS2 #ifdef WITH_UDISKS if (!(TDEGlobal::dirs()->findExe("udisks").isEmpty())) { if (ejectDriveUDisks(this)) { return TRUE; } else { printf("[tdehwlib] Failed to eject drive '%s' via udisks, falling back to alternate mechanism\n", deviceNode().ascii()); fflush(stdout); } } #endif // WITH_UDISKS if (!(TDEGlobal::dirs()->findExe("eject").isEmpty())) { TQString command = TQString("eject -v '%1' 2>&1").arg(deviceNode()); FILE *exepipe = popen(command.ascii(), "r"); if (exepipe) { TQString eject_output; TQTextStream ts(exepipe, IO_ReadOnly); eject_output = ts.read(); int retcode = pclose(exepipe); if (retcode == 0) { return TRUE; } } printf("[tdehwlib] Failed to eject drive '%s' via 'eject' command\n", deviceNode().ascii()); fflush(stdout); } return FALSE; }
bool LibPartedDevice::open() { Q_ASSERT(pedDevice() == nullptr); if (pedDevice()) return false; m_PedDevice = ped_device_get(deviceNode().toLatin1().constData()); return m_PedDevice != nullptr; }
bool TDEStorageDevice::lockDriveMedia(bool lock) { int fd = open(deviceNode().ascii(), O_RDWR | O_NONBLOCK); if (fd < 0) { return false; } if (ioctl(fd, CDROM_LOCKDOOR, (lock)?1:0) != 0) { close(fd); return false; } else { close(fd); return true; } }
bool TDEStorageDevice::ejectDriveMedia() { int fd = open(deviceNode().ascii(), O_RDWR | O_NONBLOCK); if (fd < 0) { return false; } if (ioctl(fd, CDROMEJECT) != 0) { close(fd); return false; } else { close(fd); return true; } }
bool Partition::operator==(const Partition& other) const { return other.deviceNode() == deviceNode(); }
TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet, int* retcode) { int internal_retcode; if (!retcode) { retcode = &internal_retcode; } 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 optionString; if (mountOptions["ro"] == "true") { optionString.append(" -r"); } if (mountOptions["atime"] != "true") { optionString.append(" -A"); } if (mountOptions["utf8"] == "true") { optionString.append(" -c utf8"); } if (mountOptions["sync"] == "true") { optionString.append(" -s"); } if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) { optionString.append(TQString(" -t %1").arg(mountOptions["filesystem"])); } if (mountOptions.contains("locale")) { optionString.append(TQString(" -c %1").arg(mountOptions["locale"])); } TQString passFileName = passwordFile.name(); TQString devNode = deviceNode(); 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); FILE *exepipe = popen(command.local8Bit(), "r"); if (exepipe) { TQString mount_output; TQTextStream ts(exepipe, IO_ReadOnly); mount_output = ts.read(); *retcode = pclose(exepipe); if (errRet) { *errRet = mount_output; } } // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); ret = mountPath(); return ret; }
TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet, int* retcode) { int internal_retcode; if (!retcode) { retcode = &internal_retcode; } TQString ret = mountPath(); // Device is already mounted if (!ret.isNull()) { return ret; } TQString command; TQString devNode = deviceNode(); devNode.replace("'", "'\\''"); mediaName.replace("'", "'\\''"); #if defined(WITH_UDISKS2) || defined(WITH_UDISKS) // Prepare filesystem options for mount TQStringList udisksOptions; TQString optionString; if (mountOptions["ro"] == "true") { udisksOptions.append("ro"); } if (mountOptions["atime"] != "true") { udisksOptions.append("noatime"); } if (mountOptions["sync"] == "true") { udisksOptions.append("sync"); } 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["utf8"] == "true") { // udisks/udisks2 for now does not support option iocharset= for jfs // udisksOptions.append("iocharset=utf8"); } } 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.contains("journaling")) { // udisks/udisks2 for now does not support option data= for ext3/ext4 // udisksOptions.append(TQString("data=%1").arg(mountOptions["journaling"])); } } for (TQStringList::Iterator it = udisksOptions.begin(); it != udisksOptions.end(); ++it) { optionString.append(","); optionString.append(*it); } if (!optionString.isEmpty()) { optionString.remove(0, 1); } #endif // defined(WITH_UDISKS2) || defined(WITH_UDISKS) #ifdef WITH_UDISKS2 if(command.isEmpty()) { // 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) { // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); ret = mountPath(); return ret; } else if (uDisks2Ret == -1) { if (errRet) { *errRet = errorString; } // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); ret = mountPath(); return ret; } else { // The UDISKS v2 DBUS service was either not available or was unusable; try another method... command = TQString::null; } } #endif // WITH_UDISKS2 #ifdef WITH_UDISKS if(command.isEmpty()) { // Try to use UDISKS v1 via DBUS, if available TQString errorString; TQString fileSystemType; if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) { fileSystemType = mountOptions["filesystem"]; } int uDisksRet = mountDriveUDisks(devNode, fileSystemType, udisksOptions, &errorString); if (uDisksRet == 0) { // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); ret = mountPath(); return ret; } else if (uDisksRet == -1) { if (errRet) { *errRet = errorString; } // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); ret = mountPath(); return ret; } else { // The UDISKS v1 DBUS service was either not available or was unusable; try another method... command = TQString::null; } } #endif // WITH_UDISKS if(command.isEmpty()) { // Use 'pmount' command, if available TQString pmountProg = TDEGlobal::dirs()->findExe("pmount"); if (!pmountProg.isEmpty()) { // Create dummy password file KTempFile passwordFile(TQString::null, "tmp", 0600); passwordFile.setAutoDelete(true); TQString optionString; if (mountOptions["ro"] == "true") { optionString.append(" -r"); } if (mountOptions["atime"] != "true") { optionString.append(" -A"); } if (mountOptions["utf8"] == "true") { optionString.append(" -c utf8"); } if (mountOptions["sync"] == "true") { optionString.append(" -s"); } if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) { optionString.append(TQString(" -t %1").arg(mountOptions["filesystem"])); } if (mountOptions.contains("locale")) { optionString.append(TQString(" -c %1").arg(mountOptions["locale"])); } TQString passFileName = passwordFile.name(); passFileName.replace("'", "'\\''"); command = TQString("pmount -p '%1' %2 '%3' '%4' 2>&1").arg(passFileName).arg(optionString).arg(devNode).arg(mediaName); } } if(command.isEmpty()) { if (errRet) { *errRet = i18n("No supported mounting methods were detected on your system"); } return ret; } FILE *exepipe = popen(command.local8Bit(), "r"); if (exepipe) { TQString mount_output; TQTextStream ts(exepipe, IO_ReadOnly); mount_output = ts.read(); *retcode = pclose(exepipe); if (errRet) { *errRet = mount_output; } } // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); ret = mountPath(); return ret; }
bool TDEStorageDevice::unmountDevice(TQString* errRet, int* retcode) { int internal_retcode; if (!retcode) { retcode = &internal_retcode; } TQString mountpoint = mountPath(); TQString devNode = deviceNode(); if (mountpoint.isNull()) { return true; } mountpoint.replace("'", "'\\''"); TQString command; #ifdef WITH_UDISKS2 if(command.isEmpty()) { // Try to use UDISKS v2 via DBUS, if available TQString errorString; int unMountUDisks2Ret = unMountDriveUDisks2(devNode, TQString::null, &errorString); if (unMountUDisks2Ret == 0) { // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); return true; } else if (unMountUDisks2Ret == -1) { if (errRet) { *errRet = errorString; } // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); return false; } else { // The UDISKS v2 DBUS service was either not available or was unusable; try another method... command = TQString::null; } } #endif // WITH_UDISKS2 #ifdef WITH_UDISKS if(command.isEmpty()) { // Try to use UDISKS v1 via DBUS, if available TQString errorString; int unMountUDisksRet = unMountDriveUDisks(devNode, TQStringList(), &errorString); if (unMountUDisksRet == 0) { // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); return true; } else if (unMountUDisksRet == -1) { if (errRet) { *errRet = errorString; } // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); return false; } else { // The UDISKS v1 DBUS service was either not available or was unusable; try another method... command = TQString::null; } } #endif // WITH_UDISKS 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 true; } FILE *exepipe = popen(command.local8Bit(), "r"); if (exepipe) { TQString umount_output; TQTextStream ts(exepipe, IO_ReadOnly); umount_output = ts.read(); *retcode = pclose(exepipe); if (*retcode == 0) { // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); return true; } else { if (errRet) { *errRet = umount_output; } } } // Update internal mount data TDEGlobal::hardwareDevices()->processModifiedMounts(); return false; }