From 9c9bc1216c78f2295ede77462d1dadf107b8d456 Mon Sep 17 00:00:00 2001 From: Dominik Riebeling Date: Sun, 17 Jul 2011 08:08:58 +0000 Subject: Move some helper functions out of Autodetection. Those functions are rather general, so put them into the Utils class instead. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30146 a1c6a512-1295-4272-9138-f99709370657 --- rbutil/rbutilqt/base/autodetection.cpp | 231 +----------------------- rbutil/rbutilqt/base/autodetection.h | 2 - rbutil/rbutilqt/base/bootloaderinstallipod.cpp | 4 +- rbutil/rbutilqt/base/bootloaderinstallsansa.cpp | 4 +- rbutil/rbutilqt/base/utils.cpp | 222 ++++++++++++++++++++++- rbutil/rbutilqt/base/utils.h | 3 + rbutil/rbutilqt/configure.cpp | 2 +- rbutil/rbutilqt/sysinfo.cpp | 2 +- 8 files changed, 230 insertions(+), 240 deletions(-) diff --git a/rbutil/rbutilqt/base/autodetection.cpp b/rbutil/rbutilqt/base/autodetection.cpp index 2bd61ff5ba..1a78ae54a8 100644 --- a/rbutil/rbutilqt/base/autodetection.cpp +++ b/rbutil/rbutilqt/base/autodetection.cpp @@ -25,32 +25,6 @@ #include "../ipodpatcher/ipodpatcher.h" #include "../sansapatcher/sansapatcher.h" -#if defined(Q_OS_LINUX) || defined(Q_OS_MACX) -#include -#endif -#if defined(Q_OS_LINUX) -#include -#endif -#if defined(Q_OS_MACX) -#include -#include -#include -#endif -#if defined(Q_OS_WIN32) -#if defined(UNICODE) -#define _UNICODE -#endif -#include -#include -#include -#include -#include -#endif - -#if defined(Q_OS_OPENBSD) -#include -#include -#endif #include "system.h" #include "utils.h" @@ -69,7 +43,7 @@ bool Autodetection::detect() detectUsb(); // Try detection via rockbox.info / rbutil.log - QStringList mounts = mountpoints(); + QStringList mounts = Utils::mountpoints(); for(int i=0; i< mounts.size();i++) { @@ -183,7 +157,7 @@ bool Autodetection::detect() #ifdef Q_OS_MACX mp.append("s2"); #endif - m_mountpoint = resolveMountPoint(mp); + m_mountpoint = Utils::resolveMountPoint(mp); return true; } else { @@ -208,7 +182,7 @@ bool Autodetection::detect() #ifdef Q_OS_MACX mp.append("s1"); #endif - m_mountpoint = resolveMountPoint(mp); + m_mountpoint = Utils::resolveMountPoint(mp); return true; } else { @@ -224,205 +198,6 @@ bool Autodetection::detect() } -QStringList Autodetection::mountpoints() -{ - QStringList tempList; -#if defined(Q_OS_WIN32) - QFileInfoList list = QDir::drives(); - for(int i=0; if_mntonname); - qDebug() << "[Autodetection] Mounted on" << mntinf->f_mntonname - << "is" << mntinf->f_mntfromname << "type" << mntinf->f_fstypename; - mntinf++; - } -#elif defined(Q_OS_LINUX) - - FILE *mn = setmntent("/etc/mtab", "r"); - if(!mn) - return QStringList(""); - - struct mntent *ent; - while((ent = getmntent(mn))) { - tempList << QString(ent->mnt_dir); - qDebug() << "[Autodetection] Mounted on" << ent->mnt_dir - << "is" << ent->mnt_fsname << "type" << ent->mnt_type; - } - endmntent(mn); - -#else -#error Unknown Platform -#endif - return tempList; -} - - -/** resolve device name to mount point / drive letter - * @param device device name / disk number - * @return mount point / drive letter - */ -QString Autodetection::resolveMountPoint(QString device) -{ - qDebug() << "[Autodetect] resolving mountpoint:" << device; - -#if defined(Q_OS_LINUX) - FILE *mn = setmntent("/etc/mtab", "r"); - if(!mn) - return QString(""); - - struct mntent *ent; - while((ent = getmntent(mn))) { - // Check for valid filesystem. Allow hfs too, as an Ipod might be a - // MacPod. - if(QString(ent->mnt_fsname) == device) { - QString result; - if(QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive) - || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive)) { - qDebug() << "[Autodetect] resolved mountpoint is:" << ent->mnt_dir; - result = QString(ent->mnt_dir); - } - else { - qDebug() << "[Autodetect] mountpoint is wrong filesystem!"; - } - endmntent(mn); - return result; - } - } - endmntent(mn); - -#endif - -#if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD) - int num; - struct statfs *mntinf; - - num = getmntinfo(&mntinf, MNT_WAIT); - while(num--) { - // Check for valid filesystem. Allow hfs too, as an Ipod might be a - // MacPod. - if(QString(mntinf->f_mntfromname) == device) { - if(QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive) - || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive)) { - qDebug() << "[Autodetect] resolved mountpoint is:" << mntinf->f_mntonname; - return QString(mntinf->f_mntonname); - } - else { - qDebug() << "[Autodetect] mountpoint is wrong filesystem!"; - return QString(); - } - } - mntinf++; - } -#endif - -#if defined(Q_OS_WIN32) - QString result; - unsigned int driveno = device.replace(QRegExp("^.*([0-9]+)"), "\\1").toInt(); - - int letter; - for(letter = 'A'; letter <= 'Z'; letter++) { - if(resolveDevicename(QString(letter)).toUInt() == driveno) { - result = letter; - qDebug() << "[Autodetect] resolved mountpoint is:" << result; - break; - } - } - if(!result.isEmpty()) - return result + ":/"; -#endif - qDebug() << "[Autodetect] resolving mountpoint failed!"; - return QString(""); -} - - -/** Resolve mountpoint to devicename / disk number - * @param path mountpoint path / drive letter - * @return devicename / disk number - */ -QString Autodetection::resolveDevicename(QString path) -{ - qDebug() << "[Autodetect] resolving device name" << path; -#if defined(Q_OS_LINUX) - FILE *mn = setmntent("/etc/mtab", "r"); - if(!mn) - return QString(""); - - struct mntent *ent; - while((ent = getmntent(mn))) { - // check for valid filesystem type. - // Linux can handle hfs (and hfsplus), so consider it a valid file - // system. Otherwise resolving the device name would fail, which in - // turn would make it impossible to warn about a MacPod. - if(QString(ent->mnt_dir) == path - && (QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive) - || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive))) { - endmntent(mn); - qDebug() << "[Autodetect] device name is" << ent->mnt_fsname; - return QString(ent->mnt_fsname); - } - } - endmntent(mn); - -#endif - -#if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD) - int num; - struct statfs *mntinf; - - num = getmntinfo(&mntinf, MNT_WAIT); - while(num--) { - // check for valid filesystem type. OS X can handle hfs (hfs+ is - // treated as hfs), BSD should be the same. - if(QString(mntinf->f_mntonname) == path - && (QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive) - || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive))) { - qDebug() << "[Autodetect] device name is" << mntinf->f_mntfromname; - return QString(mntinf->f_mntfromname); - } - mntinf++; - } -#endif - -#if defined(Q_OS_WIN32) - DWORD written; - HANDLE h; - TCHAR uncpath[MAX_PATH]; - UCHAR buffer[0x400]; - PVOLUME_DISK_EXTENTS extents = (PVOLUME_DISK_EXTENTS)buffer; - - _stprintf(uncpath, _TEXT("\\\\.\\%c:"), path.toAscii().at(0)); - h = CreateFile(uncpath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, OPEN_EXISTING, 0, NULL); - if(h == INVALID_HANDLE_VALUE) { - //qDebug() << "error getting extents for" << uncpath; - return ""; - } - // get the extents - if(DeviceIoControl(h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, - NULL, 0, extents, sizeof(buffer), &written, NULL)) { - if(extents->NumberOfDiskExtents > 1) { - qDebug() << "[Autodetect] resolving device name: volume spans multiple disks!"; - return ""; - } - qDebug() << "[Autodetect] device name is" << extents->Extents[0].DiskNumber; - return QString("%1").arg(extents->Extents[0].DiskNumber); - } -#endif - return QString(""); - -} - - /** @brief detect devices based on usb pid / vid. * @return true upon success, false otherwise. */ diff --git a/rbutil/rbutilqt/base/autodetection.h b/rbutil/rbutilqt/base/autodetection.h index c6b33ac313..6c98dc1998 100644 --- a/rbutil/rbutilqt/base/autodetection.h +++ b/rbutil/rbutilqt/base/autodetection.h @@ -38,8 +38,6 @@ public: QString getMountPoint() {return m_mountpoint;} QString errdev(void) { return m_errdev; } QString incompatdev(void) { return m_incompat; } - static QStringList mountpoints(void); - static QString resolveDevicename(QString path); private: QString resolveMountPoint(QString); diff --git a/rbutil/rbutilqt/base/bootloaderinstallipod.cpp b/rbutil/rbutilqt/base/bootloaderinstallipod.cpp index 14f3fa5ddf..af25de4e29 100644 --- a/rbutil/rbutilqt/base/bootloaderinstallipod.cpp +++ b/rbutil/rbutilqt/base/bootloaderinstallipod.cpp @@ -22,7 +22,7 @@ #include "bootloaderinstallipod.h" #include "../ipodpatcher/ipodpatcher.h" -#include "autodetection.h" +#include "utils.h" BootloaderInstallIpod::BootloaderInstallIpod(QObject *parent) @@ -226,7 +226,7 @@ BootloaderInstallBase::Capabilities BootloaderInstallIpod::capabilities(void) bool BootloaderInstallIpod::ipodInitialize(struct ipod_t *ipod) { if(!m_blfile.isEmpty()) { - QString devicename = Autodetection::resolveDevicename(m_blfile); + QString devicename = Utils::resolveDevicename(m_blfile); if(devicename.isEmpty()) { emit logItem(tr("Error: could not retrieve device name"), LOGERROR); return false; diff --git a/rbutil/rbutilqt/base/bootloaderinstallsansa.cpp b/rbutil/rbutilqt/base/bootloaderinstallsansa.cpp index 0dc94c553c..05e7c96eea 100644 --- a/rbutil/rbutilqt/base/bootloaderinstallsansa.cpp +++ b/rbutil/rbutilqt/base/bootloaderinstallsansa.cpp @@ -22,7 +22,7 @@ #include "bootloaderinstallsansa.h" #include "../sansapatcher/sansapatcher.h" -#include "autodetection.h" +#include "utils.h" BootloaderInstallSansa::BootloaderInstallSansa(QObject *parent) : BootloaderInstallBase(parent) @@ -242,7 +242,7 @@ BootloaderInstallBase::BootloaderType BootloaderInstallSansa::installed(void) bool BootloaderInstallSansa::sansaInitialize(struct sansa_t *sansa) { if(!m_blfile.isEmpty()) { - QString devicename = Autodetection::resolveDevicename(m_blfile); + QString devicename = Utils::resolveDevicename(m_blfile); if(devicename.isEmpty()) { emit logItem(tr("Error: could not retrieve device name"), LOGERROR); return false; 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 @@ #include #include +#if defined(Q_OS_LINUX) || defined(Q_OS_MACX) +#include +#endif +#if defined(Q_OS_LINUX) || defined(Q_OS_MACX) +#include +#endif +#if defined(Q_OS_LINUX) +#include +#endif +#if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD) +#include +#include +#include +#endif #if defined(Q_OS_WIN32) -#include +#include #include +#include +#include #include #endif -#if defined(Q_OS_LINUX) || defined(Q_OS_MACX) -#include -#endif + // recursive function to delete a dir with files bool Utils::recursiveRmdir( const QString &dirName ) @@ -327,3 +341,203 @@ int Utils::compareVersionStrings(QString s1, QString s2) return 0; } + +/** Resolve mountpoint to devicename / disk number + * @param path mountpoint path / drive letter + * @return devicename / disk number + */ +QString Utils::resolveDevicename(QString path) +{ + qDebug() << "[Utils] resolving device name" << path; +#if defined(Q_OS_LINUX) + FILE *mn = setmntent("/etc/mtab", "r"); + if(!mn) + return QString(""); + + struct mntent *ent; + while((ent = getmntent(mn))) { + // check for valid filesystem type. + // Linux can handle hfs (and hfsplus), so consider it a valid file + // system. Otherwise resolving the device name would fail, which in + // turn would make it impossible to warn about a MacPod. + if(QString(ent->mnt_dir) == path + && (QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive) + || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive))) { + endmntent(mn); + qDebug() << "[Utils] device name is" << ent->mnt_fsname; + return QString(ent->mnt_fsname); + } + } + endmntent(mn); + +#endif + +#if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD) + int num; + struct statfs *mntinf; + + num = getmntinfo(&mntinf, MNT_WAIT); + while(num--) { + // check for valid filesystem type. OS X can handle hfs (hfs+ is + // treated as hfs), BSD should be the same. + if(QString(mntinf->f_mntonname) == path + && (QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive) + || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive))) { + qDebug() << "[Utils] device name is" << mntinf->f_mntfromname; + return QString(mntinf->f_mntfromname); + } + mntinf++; + } +#endif + +#if defined(Q_OS_WIN32) + DWORD written; + HANDLE h; + TCHAR uncpath[MAX_PATH]; + UCHAR buffer[0x400]; + PVOLUME_DISK_EXTENTS extents = (PVOLUME_DISK_EXTENTS)buffer; + + _stprintf(uncpath, _TEXT("\\\\.\\%c:"), path.toAscii().at(0)); + h = CreateFile(uncpath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, 0, NULL); + if(h == INVALID_HANDLE_VALUE) { + //qDebug() << "error getting extents for" << uncpath; + return ""; + } + // get the extents + if(DeviceIoControl(h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, + NULL, 0, extents, sizeof(buffer), &written, NULL)) { + if(extents->NumberOfDiskExtents > 1) { + qDebug() << "[Utils] resolving device name: volume spans multiple disks!"; + return ""; + } + qDebug() << "[Utils] device name is" << extents->Extents[0].DiskNumber; + return QString("%1").arg(extents->Extents[0].DiskNumber); + } +#endif + return QString(""); + +} + + +/** resolve device name to mount point / drive letter + * @param device device name / disk number + * @return mount point / drive letter + */ +QString Utils::resolveMountPoint(QString device) +{ + qDebug() << "[Utils] resolving mountpoint:" << device; + +#if defined(Q_OS_LINUX) + FILE *mn = setmntent("/etc/mtab", "r"); + if(!mn) + return QString(""); + + struct mntent *ent; + while((ent = getmntent(mn))) { + // Check for valid filesystem. Allow hfs too, as an Ipod might be a + // MacPod. + if(QString(ent->mnt_fsname) == device) { + QString result; + if(QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive) + || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive)) { + qDebug() << "[Utils] resolved mountpoint is:" << ent->mnt_dir; + result = QString(ent->mnt_dir); + } + else { + qDebug() << "[Utils] mountpoint is wrong filesystem!"; + } + endmntent(mn); + return result; + } + } + endmntent(mn); + +#endif + +#if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD) + int num; + struct statfs *mntinf; + + num = getmntinfo(&mntinf, MNT_WAIT); + while(num--) { + // Check for valid filesystem. Allow hfs too, as an Ipod might be a + // MacPod. + if(QString(mntinf->f_mntfromname) == device) { + if(QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive) + || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive)) { + qDebug() << "[Utils] resolved mountpoint is:" << mntinf->f_mntonname; + return QString(mntinf->f_mntonname); + } + else { + qDebug() << "[Utils] mountpoint is wrong filesystem!"; + return QString(); + } + } + mntinf++; + } +#endif + +#if defined(Q_OS_WIN32) + QString result; + unsigned int driveno = device.replace(QRegExp("^.*([0-9]+)"), "\\1").toInt(); + + int letter; + for(letter = 'A'; letter <= 'Z'; letter++) { + if(resolveDevicename(QString(letter)).toUInt() == driveno) { + result = letter; + qDebug() << "[Utils] resolved mountpoint is:" << result; + break; + } + } + if(!result.isEmpty()) + return result + ":/"; +#endif + qDebug() << "[Utils] resolving mountpoint failed!"; + return QString(""); +} + + +QStringList Utils::mountpoints() +{ + QStringList tempList; +#if defined(Q_OS_WIN32) + QFileInfoList list = QDir::drives(); + for(int i=0; if_mntonname); + qDebug() << "[Utils] Mounted on" << mntinf->f_mntonname + << "is" << mntinf->f_mntfromname << "type" << mntinf->f_fstypename; + mntinf++; + } +#elif defined(Q_OS_LINUX) + + FILE *mn = setmntent("/etc/mtab", "r"); + if(!mn) + return QStringList(""); + + struct mntent *ent; + while((ent = getmntent(mn))) { + tempList << QString(ent->mnt_dir); + qDebug() << "[Utils] Mounted on" << ent->mnt_dir + << "is" << ent->mnt_fsname << "type" << ent->mnt_type; + } + endmntent(mn); + +#else +#error Unknown Platform +#endif + return tempList; +} + + diff --git a/rbutil/rbutilqt/base/utils.h b/rbutil/rbutilqt/base/utils.h index 20ac36eec1..659bbc4ed4 100644 --- a/rbutil/rbutilqt/base/utils.h +++ b/rbutil/rbutilqt/base/utils.h @@ -45,6 +45,9 @@ public: static QString checkEnvironment(bool permission); static int compareVersionStrings(QString s1, QString s2); static QString filesystemName(QString path); + static QStringList mountpoints(void); + static QString resolveDevicename(QString path); + static QString resolveMountPoint(QString device); }; #endif diff --git a/rbutil/rbutilqt/configure.cpp b/rbutil/rbutilqt/configure.cpp index 7dfd325255..9e1978974f 100644 --- a/rbutil/rbutilqt/configure.cpp +++ b/rbutil/rbutilqt/configure.cpp @@ -587,7 +587,7 @@ void Config::refreshMountpoint() // unwanted item. ui.mountPoint->blockSignals(true); ui.mountPoint->clear(); - QStringList mps = Autodetection::mountpoints(); + QStringList mps = Utils::mountpoints(); for(int i = 0; i < mps.size(); ++i) { // add mountpoint as user data so we can change the displayed string // later (to include volume label or similar) diff --git a/rbutil/rbutilqt/sysinfo.cpp b/rbutil/rbutilqt/sysinfo.cpp index 0217c22a3e..bd3f7b2e7f 100644 --- a/rbutil/rbutilqt/sysinfo.cpp +++ b/rbutil/rbutilqt/sysinfo.cpp @@ -61,7 +61,7 @@ QString Sysinfo::getInfo() info += "
"; info += "" + tr("Filesystem") + "
"; - QStringList drives = Autodetection::mountpoints(); + QStringList drives = Utils::mountpoints(); for(int i = 0; i < drives.size(); i++) { info += tr("%1, %4 %2 GiB of %3 GiB available") .arg(QDir::toNativeSeparators(drives.at(i))) -- cgit