Improve autoscanner drive detection

This commit is contained in:
Mark Watkins 2014-04-29 04:28:10 +10:00
parent 83022db8cb
commit f0e523212e

View File

@ -314,6 +314,74 @@ void MainWindow::Startup()
}
#ifdef Q_OS_UNIX
#include <stdio.h>
#include <unistd.h>
#if defined(Q_OS_MAC) || defined(Q_OS_BSD4)
#include <sys/mount.h>
#else
#include <mntent.h>
#endif // Q_OS_MAC/BSD
#endif // Q_OS_UNIX
//! \brief Returns a list of drive mountpoints
QStringList getDriveList()
{
QStringList drivelist;
#if defined(Q_OS_MAC) || defined(Q_OS_BSD4)
struct statfs *mounts;
int num_mounts = getmntinfo(&mounts, MNT_WAIT);
if (num_mounts >= 0) {
for (int i = 0; i < num_mounts; i++) {
// TODO: need to check on what BSD calls FAT filesystems
QString name = mounts[i].f_mntonname;
if (name.toLower().startsWith("/volumes/")) {
drivelist.push_back(name);
// qDebug() << QString("Disk type '%1' mounted at: %2").arg(mounts[i].f_fstypename).arg(mounts[i].f_mntonname);
}
}
}
#elif defined(Q_OS_UNIX)
// Unix / Linux (except BSD)
FILE *mtab = setmntent("/etc/mtab", "r");
struct mntent *m;
struct mntent mnt;
char strings[4096];
while ((m = getmntent_r(mtab, &mnt, strings, sizeof(strings)))) {
struct statfs fs;
if ((mnt.mnt_dir != NULL) && (statfs(mnt.mnt_dir, &fs) == 0)) {
QString name = mnt.mnt_dir;
drivelist.push_back(name);
quint64 size = fs.f_blocks * fs.f_bsize;
quint64 free = fs.f_bfree * fs.f_bsize;
quint64 avail = fs.f_bavail * fs.f_bsize;
}
}
endmntent(mtab);
#elif defined(Q_OS_WIN)
QFileInfoList list = QDir::drives();
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
QString name = fileInfo.fileName();
if (name[0].toUpper() > 'C') { // Only bother looking after the C: drive
drivelist.push_back(name);
}
}
#endif
return drivelist;
}
void MainWindow::on_action_Import_Data_triggered()
{
if (m_inRecalculation) {
@ -321,61 +389,25 @@ void MainWindow::on_action_Import_Data_triggered()
return;
}
QList<QString> AutoScannerPaths =
#ifdef Q_OS_MAC
// Apple OSX mount points
{ "/Volumes" };
#elif Q_OS_WIN
// This is a dummy on windows, as we are scanning drive letters
{ QString() };
#else // UNIX
// Mountpoints for Linux, UNIX, BSD, etc.
{ "/media", "/mnt", "/Media" };
// TODO: Extra code will need to go here for user directory fuser mounts and similar
#endif
QHash<QString,QString> datacard;
QString datacard_path = QString();
MachineLoader * datacard_loader = nullptr;
QList<MachineLoader *>loaders = GetLoaders();
QStringList AutoScannerPaths = getDriveList();
Q_FOREACH(const QString &path, AutoScannerPaths) {
qDebug() << "Scanning" << path;
#ifdef Q_OS_WIN
// Get available drive letters
QFileInfoList list = QDir::drives();
#else
QDir dir(path);
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList list = dir.entryInfoList();
#endif
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
// Scan through available machine loaders and test if this folder contains valid folder structure
Q_FOREACH(MachineLoader * loader, loaders) {
QString scanpath;
#ifndef Q_OS_WIN
scanpath = path + "/";
#endif
scanpath += fileInfo.fileName();
if (loader->Detect(path)) {
datacard[loader->ClassName()] = path;
if (loader->Detect(scanpath)) {
datacard[loader->ClassName()]=scanpath;
qDebug() << "Found" << loader->ClassName() << "datacard at" << scanpath;
qDebug() << "Found" << loader->ClassName() << "datacard at" << path;
if (datacard_path.isEmpty()) {
datacard_loader = loader;
datacard_path = scanpath;
}
datacard_path = path;
}
}
}