summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/base/utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/base/utils.cpp')
-rw-r--r--rbutil/rbutilqt/base/utils.cpp222
1 files changed, 218 insertions, 4 deletions
diff --git a/rbutil/rbutilqt/base/utils.cpp b/rbutil/rbutilqt/base/utils.cpp
index d52eba4250..721aecc044 100644
--- a/rbutil/rbutilqt/base/utils.cpp
+++ b/rbutil/rbutilqt/base/utils.cpp
@@ -32,14 +32,28 @@
32#include <cstdlib> 32#include <cstdlib>
33#include <stdio.h> 33#include <stdio.h>
34 34
35#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
36#include <sys/statvfs.h>
37#endif
38#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
39#include <stdio.h>
40#endif
41#if defined(Q_OS_LINUX)
42#include <mntent.h>
43#endif
44#if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
45#include <sys/param.h>
46#include <sys/ucred.h>
47#include <sys/mount.h>
48#endif
35#if defined(Q_OS_WIN32) 49#if defined(Q_OS_WIN32)
36#include <windows.h> 50#include <stdio.h>
37#include <tchar.h> 51#include <tchar.h>
52#include <windows.h>
53#include <setupapi.h>
38#include <winioctl.h> 54#include <winioctl.h>
39#endif 55#endif
40#if defined(Q_OS_LINUX) || defined(Q_OS_MACX) 56
41#include <sys/statvfs.h>
42#endif
43 57
44// recursive function to delete a dir with files 58// recursive function to delete a dir with files
45bool Utils::recursiveRmdir( const QString &dirName ) 59bool Utils::recursiveRmdir( const QString &dirName )
@@ -327,3 +341,203 @@ int Utils::compareVersionStrings(QString s1, QString s2)
327 return 0; 341 return 0;
328} 342}
329 343
344
345/** Resolve mountpoint to devicename / disk number
346 * @param path mountpoint path / drive letter
347 * @return devicename / disk number
348 */
349QString Utils::resolveDevicename(QString path)
350{
351 qDebug() << "[Utils] resolving device name" << path;
352#if defined(Q_OS_LINUX)
353 FILE *mn = setmntent("/etc/mtab", "r");
354 if(!mn)
355 return QString("");
356
357 struct mntent *ent;
358 while((ent = getmntent(mn))) {
359 // check for valid filesystem type.
360 // Linux can handle hfs (and hfsplus), so consider it a valid file
361 // system. Otherwise resolving the device name would fail, which in
362 // turn would make it impossible to warn about a MacPod.
363 if(QString(ent->mnt_dir) == path
364 && (QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive)
365 || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive))) {
366 endmntent(mn);
367 qDebug() << "[Utils] device name is" << ent->mnt_fsname;
368 return QString(ent->mnt_fsname);
369 }
370 }
371 endmntent(mn);
372
373#endif
374
375#if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
376 int num;
377 struct statfs *mntinf;
378
379 num = getmntinfo(&mntinf, MNT_WAIT);
380 while(num--) {
381 // check for valid filesystem type. OS X can handle hfs (hfs+ is
382 // treated as hfs), BSD should be the same.
383 if(QString(mntinf->f_mntonname) == path
384 && (QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive)
385 || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive))) {
386 qDebug() << "[Utils] device name is" << mntinf->f_mntfromname;
387 return QString(mntinf->f_mntfromname);
388 }
389 mntinf++;
390 }
391#endif
392
393#if defined(Q_OS_WIN32)
394 DWORD written;
395 HANDLE h;
396 TCHAR uncpath[MAX_PATH];
397 UCHAR buffer[0x400];
398 PVOLUME_DISK_EXTENTS extents = (PVOLUME_DISK_EXTENTS)buffer;
399
400 _stprintf(uncpath, _TEXT("\\\\.\\%c:"), path.toAscii().at(0));
401 h = CreateFile(uncpath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
402 NULL, OPEN_EXISTING, 0, NULL);
403 if(h == INVALID_HANDLE_VALUE) {
404 //qDebug() << "error getting extents for" << uncpath;
405 return "";
406 }
407 // get the extents
408 if(DeviceIoControl(h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
409 NULL, 0, extents, sizeof(buffer), &written, NULL)) {
410 if(extents->NumberOfDiskExtents > 1) {
411 qDebug() << "[Utils] resolving device name: volume spans multiple disks!";
412 return "";
413 }
414 qDebug() << "[Utils] device name is" << extents->Extents[0].DiskNumber;
415 return QString("%1").arg(extents->Extents[0].DiskNumber);
416 }
417#endif
418 return QString("");
419
420}
421
422
423/** resolve device name to mount point / drive letter
424 * @param device device name / disk number
425 * @return mount point / drive letter
426 */
427QString Utils::resolveMountPoint(QString device)
428{
429 qDebug() << "[Utils] resolving mountpoint:" << device;
430
431#if defined(Q_OS_LINUX)
432 FILE *mn = setmntent("/etc/mtab", "r");
433 if(!mn)
434 return QString("");
435
436 struct mntent *ent;
437 while((ent = getmntent(mn))) {
438 // Check for valid filesystem. Allow hfs too, as an Ipod might be a
439 // MacPod.
440 if(QString(ent->mnt_fsname) == device) {
441 QString result;
442 if(QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive)
443 || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive)) {
444 qDebug() << "[Utils] resolved mountpoint is:" << ent->mnt_dir;
445 result = QString(ent->mnt_dir);
446 }
447 else {
448 qDebug() << "[Utils] mountpoint is wrong filesystem!";
449 }
450 endmntent(mn);
451 return result;
452 }
453 }
454 endmntent(mn);
455
456#endif
457
458#if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
459 int num;
460 struct statfs *mntinf;
461
462 num = getmntinfo(&mntinf, MNT_WAIT);
463 while(num--) {
464 // Check for valid filesystem. Allow hfs too, as an Ipod might be a
465 // MacPod.
466 if(QString(mntinf->f_mntfromname) == device) {
467 if(QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive)
468 || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive)) {
469 qDebug() << "[Utils] resolved mountpoint is:" << mntinf->f_mntonname;
470 return QString(mntinf->f_mntonname);
471 }
472 else {
473 qDebug() << "[Utils] mountpoint is wrong filesystem!";
474 return QString();
475 }
476 }
477 mntinf++;
478 }
479#endif
480
481#if defined(Q_OS_WIN32)
482 QString result;
483 unsigned int driveno = device.replace(QRegExp("^.*([0-9]+)"), "\\1").toInt();
484
485 int letter;
486 for(letter = 'A'; letter <= 'Z'; letter++) {
487 if(resolveDevicename(QString(letter)).toUInt() == driveno) {
488 result = letter;
489 qDebug() << "[Utils] resolved mountpoint is:" << result;
490 break;
491 }
492 }
493 if(!result.isEmpty())
494 return result + ":/";
495#endif
496 qDebug() << "[Utils] resolving mountpoint failed!";
497 return QString("");
498}
499
500
501QStringList Utils::mountpoints()
502{
503 QStringList tempList;
504#if defined(Q_OS_WIN32)
505 QFileInfoList list = QDir::drives();
506 for(int i=0; i<list.size();i++)
507 {
508 tempList << list.at(i).absolutePath();
509 qDebug() << "[Utils] Mounted on" << list.at(i).absolutePath();
510 }
511
512#elif defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
513 int num;
514 struct statfs *mntinf;
515
516 num = getmntinfo(&mntinf, MNT_WAIT);
517 while(num--) {
518 tempList << QString(mntinf->f_mntonname);
519 qDebug() << "[Utils] Mounted on" << mntinf->f_mntonname
520 << "is" << mntinf->f_mntfromname << "type" << mntinf->f_fstypename;
521 mntinf++;
522 }
523#elif defined(Q_OS_LINUX)
524
525 FILE *mn = setmntent("/etc/mtab", "r");
526 if(!mn)
527 return QStringList("");
528
529 struct mntent *ent;
530 while((ent = getmntent(mn))) {
531 tempList << QString(ent->mnt_dir);
532 qDebug() << "[Utils] Mounted on" << ent->mnt_dir
533 << "is" << ent->mnt_fsname << "type" << ent->mnt_type;
534 }
535 endmntent(mn);
536
537#else
538#error Unknown Platform
539#endif
540 return tempList;
541}
542
543