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
This commit is contained in:
Dominik Riebeling 2011-07-17 08:08:58 +00:00
parent b7fe90aa36
commit 9c9bc1216c
8 changed files with 231 additions and 241 deletions

View file

@ -25,32 +25,6 @@
#include "../ipodpatcher/ipodpatcher.h" #include "../ipodpatcher/ipodpatcher.h"
#include "../sansapatcher/sansapatcher.h" #include "../sansapatcher/sansapatcher.h"
#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
#include <stdio.h>
#endif
#if defined(Q_OS_LINUX)
#include <mntent.h>
#endif
#if defined(Q_OS_MACX)
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#endif
#if defined(Q_OS_WIN32)
#if defined(UNICODE)
#define _UNICODE
#endif
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <setupapi.h>
#include <winioctl.h>
#endif
#if defined(Q_OS_OPENBSD)
#include <sys/param.h>
#include <sys/mount.h>
#endif
#include "system.h" #include "system.h"
#include "utils.h" #include "utils.h"
@ -69,7 +43,7 @@ bool Autodetection::detect()
detectUsb(); detectUsb();
// Try detection via rockbox.info / rbutil.log // Try detection via rockbox.info / rbutil.log
QStringList mounts = mountpoints(); QStringList mounts = Utils::mountpoints();
for(int i=0; i< mounts.size();i++) for(int i=0; i< mounts.size();i++)
{ {
@ -183,7 +157,7 @@ bool Autodetection::detect()
#ifdef Q_OS_MACX #ifdef Q_OS_MACX
mp.append("s2"); mp.append("s2");
#endif #endif
m_mountpoint = resolveMountPoint(mp); m_mountpoint = Utils::resolveMountPoint(mp);
return true; return true;
} }
else { else {
@ -208,7 +182,7 @@ bool Autodetection::detect()
#ifdef Q_OS_MACX #ifdef Q_OS_MACX
mp.append("s1"); mp.append("s1");
#endif #endif
m_mountpoint = resolveMountPoint(mp); m_mountpoint = Utils::resolveMountPoint(mp);
return true; return true;
} }
else { 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; i<list.size();i++)
{
tempList << list.at(i).absolutePath();
qDebug() << "[Autodetection] Mounted on" << list.at(i).absolutePath();
}
#elif defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
int num;
struct statfs *mntinf;
num = getmntinfo(&mntinf, MNT_WAIT);
while(num--) {
tempList << QString(mntinf->f_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. /** @brief detect devices based on usb pid / vid.
* @return true upon success, false otherwise. * @return true upon success, false otherwise.
*/ */

View file

@ -38,8 +38,6 @@ public:
QString getMountPoint() {return m_mountpoint;} QString getMountPoint() {return m_mountpoint;}
QString errdev(void) { return m_errdev; } QString errdev(void) { return m_errdev; }
QString incompatdev(void) { return m_incompat; } QString incompatdev(void) { return m_incompat; }
static QStringList mountpoints(void);
static QString resolveDevicename(QString path);
private: private:
QString resolveMountPoint(QString); QString resolveMountPoint(QString);

View file

@ -22,7 +22,7 @@
#include "bootloaderinstallipod.h" #include "bootloaderinstallipod.h"
#include "../ipodpatcher/ipodpatcher.h" #include "../ipodpatcher/ipodpatcher.h"
#include "autodetection.h" #include "utils.h"
BootloaderInstallIpod::BootloaderInstallIpod(QObject *parent) BootloaderInstallIpod::BootloaderInstallIpod(QObject *parent)
@ -226,7 +226,7 @@ BootloaderInstallBase::Capabilities BootloaderInstallIpod::capabilities(void)
bool BootloaderInstallIpod::ipodInitialize(struct ipod_t *ipod) bool BootloaderInstallIpod::ipodInitialize(struct ipod_t *ipod)
{ {
if(!m_blfile.isEmpty()) { if(!m_blfile.isEmpty()) {
QString devicename = Autodetection::resolveDevicename(m_blfile); QString devicename = Utils::resolveDevicename(m_blfile);
if(devicename.isEmpty()) { if(devicename.isEmpty()) {
emit logItem(tr("Error: could not retrieve device name"), LOGERROR); emit logItem(tr("Error: could not retrieve device name"), LOGERROR);
return false; return false;

View file

@ -22,7 +22,7 @@
#include "bootloaderinstallsansa.h" #include "bootloaderinstallsansa.h"
#include "../sansapatcher/sansapatcher.h" #include "../sansapatcher/sansapatcher.h"
#include "autodetection.h" #include "utils.h"
BootloaderInstallSansa::BootloaderInstallSansa(QObject *parent) BootloaderInstallSansa::BootloaderInstallSansa(QObject *parent)
: BootloaderInstallBase(parent) : BootloaderInstallBase(parent)
@ -242,7 +242,7 @@ BootloaderInstallBase::BootloaderType BootloaderInstallSansa::installed(void)
bool BootloaderInstallSansa::sansaInitialize(struct sansa_t *sansa) bool BootloaderInstallSansa::sansaInitialize(struct sansa_t *sansa)
{ {
if(!m_blfile.isEmpty()) { if(!m_blfile.isEmpty()) {
QString devicename = Autodetection::resolveDevicename(m_blfile); QString devicename = Utils::resolveDevicename(m_blfile);
if(devicename.isEmpty()) { if(devicename.isEmpty()) {
emit logItem(tr("Error: could not retrieve device name"), LOGERROR); emit logItem(tr("Error: could not retrieve device name"), LOGERROR);
return false; return false;

View file

@ -32,14 +32,28 @@
#include <cstdlib> #include <cstdlib>
#include <stdio.h> #include <stdio.h>
#if defined(Q_OS_WIN32)
#include <windows.h>
#include <tchar.h>
#include <winioctl.h>
#endif
#if defined(Q_OS_LINUX) || defined(Q_OS_MACX) #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
#include <sys/statvfs.h> #include <sys/statvfs.h>
#endif #endif
#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
#include <stdio.h>
#endif
#if defined(Q_OS_LINUX)
#include <mntent.h>
#endif
#if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#endif
#if defined(Q_OS_WIN32)
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <setupapi.h>
#include <winioctl.h>
#endif
// recursive function to delete a dir with files // recursive function to delete a dir with files
bool Utils::recursiveRmdir( const QString &dirName ) bool Utils::recursiveRmdir( const QString &dirName )
@ -327,3 +341,203 @@ int Utils::compareVersionStrings(QString s1, QString s2)
return 0; 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; i<list.size();i++)
{
tempList << list.at(i).absolutePath();
qDebug() << "[Utils] Mounted on" << list.at(i).absolutePath();
}
#elif defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
int num;
struct statfs *mntinf;
num = getmntinfo(&mntinf, MNT_WAIT);
while(num--) {
tempList << QString(mntinf->f_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;
}

View file

@ -45,6 +45,9 @@ public:
static QString checkEnvironment(bool permission); static QString checkEnvironment(bool permission);
static int compareVersionStrings(QString s1, QString s2); static int compareVersionStrings(QString s1, QString s2);
static QString filesystemName(QString path); static QString filesystemName(QString path);
static QStringList mountpoints(void);
static QString resolveDevicename(QString path);
static QString resolveMountPoint(QString device);
}; };
#endif #endif

View file

@ -587,7 +587,7 @@ void Config::refreshMountpoint()
// unwanted item. // unwanted item.
ui.mountPoint->blockSignals(true); ui.mountPoint->blockSignals(true);
ui.mountPoint->clear(); ui.mountPoint->clear();
QStringList mps = Autodetection::mountpoints(); QStringList mps = Utils::mountpoints();
for(int i = 0; i < mps.size(); ++i) { for(int i = 0; i < mps.size(); ++i) {
// add mountpoint as user data so we can change the displayed string // add mountpoint as user data so we can change the displayed string
// later (to include volume label or similar) // later (to include volume label or similar)

View file

@ -61,7 +61,7 @@ QString Sysinfo::getInfo()
info += "<hr/>"; info += "<hr/>";
info += "<b>" + tr("Filesystem") + "</b><br/>"; info += "<b>" + tr("Filesystem") + "</b><br/>";
QStringList drives = Autodetection::mountpoints(); QStringList drives = Utils::mountpoints();
for(int i = 0; i < drives.size(); i++) { for(int i = 0; i < drives.size(); i++) {
info += tr("%1, %4 %2 GiB of %3 GiB available") info += tr("%1, %4 %2 GiB of %3 GiB available")
.arg(QDir::toNativeSeparators(drives.at(i))) .arg(QDir::toNativeSeparators(drives.at(i)))