From 69576cb17d52ba5b2cbcc4b899ef580571b0771d Mon Sep 17 00:00:00 2001 From: Max Paperno Date: Sat, 20 May 2017 15:34:26 -0400 Subject: [PATCH] [Companion] Speed up/fix findMassstoragePath(): Use Qt methods when available (v5.4+) and only check for path on FAT[32] file systems; For older Qt, skip root (/), /net, /proc, & /run devices on Linux/macOS; Fixes long delay on eg. macOS due to network access. Remove unused #include "mountlist.h" in process_flash.cpp. (#4937) --- companion/src/process_flash.cpp | 3 +- companion/src/radiointerface.cpp | 88 +++++++++++++++++--------------- 2 files changed, 49 insertions(+), 42 deletions(-) diff --git a/companion/src/process_flash.cpp b/companion/src/process_flash.cpp index bd3727231..85b6abb9c 100644 --- a/companion/src/process_flash.cpp +++ b/companion/src/process_flash.cpp @@ -34,7 +34,6 @@ #define sleep(x) Sleep(x*1000) #else #include -#include "mountlist.h" #endif FlashProcess::FlashProcess(const QString &cmd, const QStringList &args, ProgressWidget *progress): @@ -240,7 +239,7 @@ void FlashProcess::onReadyReadStandardError() void FlashProcess::errorWizard() { QString output = progress->getText(); - + if (output.contains("avrdude: Expected signature for")) { // wrong signature int pos = output.indexOf("avrdude: Device signature = "); bool fwexist = false; diff --git a/companion/src/radiointerface.cpp b/companion/src/radiointerface.cpp index cf6a0e2c0..356d90e11 100644 --- a/companion/src/radiointerface.cpp +++ b/companion/src/radiointerface.cpp @@ -25,15 +25,10 @@ #include "radionotfound.h" #include "burnconfigdialog.h" #include "helpers.h" -#include "mountlist.h" #include "process_copy.h" #include "storage.h" #include "progresswidget.h" -#if defined WIN32 || !defined __GNUC__ - #include -#endif - QString getRadioInterfaceCmd() { burnConfigDialog bcd; @@ -401,7 +396,9 @@ bool writeEeprom(const QString & filename, ProgressWidget * progress) return false; } +#if (QT_VERSION < QT_VERSION_CHECK(5, 4, 0)) #if defined WIN32 || !defined __GNUC__ +#include bool isRemovableMedia(const QString & vol) { char szDosDeviceName[MAX_PATH]; @@ -415,61 +412,69 @@ bool isRemovableMedia(const QString & vol) } return true; } -#endif +#else + #include "mountlist.h" +#endif // defined WIN32 || !defined __GNUC__ +#endif // (QT_VERSION < QT_VERSION_CHECK(5, 4, 0)) QString findMassstoragePath(const QString & filename, bool onlyPath) { QString temppath; - QStringList drives; QString eepromfile; - QString fsname; + bool found = false; + QRegularExpression fstypeRe("^(v?fat|msdos)", QRegularExpression::CaseInsensitiveOption); // Linux: "vfat"; macOS: "msdos"; Win: "FAT32" +#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)) + foreach(const QStorageInfo & si, QStorageInfo::mountedVolumes()) { + //qDebug() << si.rootPath() << si.name() << si.device() << si.displayName() << si.fileSystemType() << si.isReady(); + if (!si.isReady() || !QString(si.fileSystemType()).contains(fstypeRe)) + continue; + temppath = si.rootPath(); + eepromfile = temppath % "/" % filename; + qDebug() << "Searching for" << eepromfile; + if (QFile::exists(eepromfile)) { + found = true; + break; + } + } +#elif defined WIN32 || !defined __GNUC__ static QStringList blacklist; - -#if defined WIN32 || !defined __GNUC__ - foreach(QFileInfo drive, QDir::drives()) { + foreach(const QFileInfo & drive, QDir::drives()) { + temppath = drive.absolutePath(); + if (blacklist.contains(temppath)) + continue; + if (!isRemovableMedia(temppath)) { + blacklist.append(temppath); + continue; + } WCHAR szVolumeName[256] ; WCHAR szFileSystemName[256]; DWORD dwSerialNumber = 0; DWORD dwMaxFileNameLength=256; DWORD dwFileSystemFlags=0; - if (!blacklist.contains(drive.absolutePath())) { - if (!isRemovableMedia( drive.absolutePath() )) { - blacklist.append(drive.absolutePath()); - } else { - bool ret = GetVolumeInformationW( (WCHAR *) drive.absolutePath().utf16(),szVolumeName,256,&dwSerialNumber,&dwMaxFileNameLength,&dwFileSystemFlags,szFileSystemName,256); - if (ret) { - QString vName = QString::fromUtf16 ( (const ushort *) szVolumeName) ; - temppath = drive.absolutePath(); - eepromfile = temppath; - eepromfile.append("/" + filename); - if (QFile::exists(eepromfile)) { - return onlyPath ? temppath : eepromfile; - } - } - } + if (!GetVolumeInformationW( (WCHAR *) drive.absolutePath().utf16(),szVolumeName,256,&dwSerialNumber,&dwMaxFileNameLength,&dwFileSystemFlags,szFileSystemName,256)) + continue; + eepromfile = temppath % "/" % filename; + qDebug() << "Searching for" << eepromfile; + if (QFile::exists(eepromfile)) { + found = true; + break; } } #else + const static QStringList blacklist = QStringList() << "/" << "/net" << "/proc" << "/run"; + QStringList drives; struct mount_entry *entry; struct mount_entry *firstEntry; firstEntry = entry = read_file_system_list(true); while (entry != NULL) { - if (!drives.contains(entry->me_devname)) { + temppath = entry->me_mountdir; + if (!drives.contains(entry->me_devname) && !blacklist.contains(temppath) && QString(entry->me_type).contains(fstypeRe)) { drives.append(entry->me_devname); - temppath = entry->me_mountdir; - eepromfile = temppath; - eepromfile.append("/" + filename); + eepromfile = temppath % "/" % filename; qDebug() << "Searching for" << eepromfile; -#if !defined __APPLE__ - QString fstype = entry->me_type; - // qDebug() << eepromfile; - - if (fstype.contains("fat") && QFile::exists(eepromfile)) { -#else if (QFile::exists(eepromfile)) { -#endif - free_file_system_list(firstEntry); - return onlyPath ? temppath : eepromfile; + found = true; + break; } } entry = entry->me_next; @@ -477,5 +482,8 @@ QString findMassstoragePath(const QString & filename, bool onlyPath) free_file_system_list(firstEntry); #endif - return QString(); + if (found) + return onlyPath ? temppath : eepromfile; + else + return QString(); }