Skip to content

Commit

Permalink
Image customization: search all mountpoints for config.txt
Browse files Browse the repository at this point in the history
Search all mountpoints associated with the target drive for
FAT partition that has config.txt

Ref: #171
  • Loading branch information
maxnet committed Mar 22, 2021
1 parent 2bbb8bf commit f682c3a
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions downloadthread.cpp
Expand Up @@ -807,6 +807,7 @@ void DownloadThread::setImageCustomization(const QByteArray &config, const QByte
bool DownloadThread::_customizeImage()
{
QString folder;
std::vector<std::string> mountpoints;
QByteArray devlower = _filename.toLower();

emit preparationStatusUpdate(tr("Waiting for FAT partition to be mounted"));
Expand All @@ -832,14 +833,14 @@ bool DownloadThread::_customizeImage()
{
if (QByteArray::fromStdString(i.device).toLower() == devlower && i.mountpoints.size())
{
folder = QByteArray::fromStdString(i.mountpoints.front());
mountpoints = i.mountpoints;
break;
}
}
}

#ifdef Q_OS_WIN
if (folder.isEmpty() && !_nr.isEmpty()) {
if (mountpoints.empty() && !_nr.isEmpty()) {
qDebug() << "Windows did not assign drive letter automatically. Ask diskpart to do so manually.";
proc.start("diskpart");
proc.waitForStarted();
Expand All @@ -855,7 +856,7 @@ bool DownloadThread::_customizeImage()
{
if (QByteArray::fromStdString(i.device).toLower() == devlower && i.mountpoints.size())
{
folder = QByteArray::fromStdString(i.mountpoints.front());
mountpoints = i.mountpoints;
break;
}
}
Expand All @@ -865,7 +866,7 @@ bool DownloadThread::_customizeImage()
#ifdef Q_OS_LINUX
bool manualmount = false;

if (folder.isEmpty())
if (mountpoints.empty())
{
/* Manually mount folder */
manualmount = true;
Expand All @@ -880,16 +881,16 @@ bool DownloadThread::_customizeImage()
/* Not running as root, try to outsource mounting to udisks2 */
#ifndef QT_NO_DBUS
UDisks2Api udisks2;
folder = udisks2.mountDevice(fatpartition);
mountpoints.push_back(udisks2.mountDevice(fatpartition).toStdString());
#endif
}
else
{
/* Running as root, attempt running mount directly */
QTemporaryDir td;
QStringList args;
folder = td.path();
args << "-t" << "vfat" << fatpartition << folder;
mountpoints.push_back(td.path().toStdString());
args << "-t" << "vfat" << fatpartition << td.path();

if (QProcess::execute("mount", args) != 0)
{
Expand All @@ -901,7 +902,7 @@ bool DownloadThread::_customizeImage()
}
#endif

if (folder.isEmpty())
if (mountpoints.empty())
{
//
qDebug() << "drive info. searching for:" << devlower;
Expand All @@ -921,15 +922,32 @@ bool DownloadThread::_customizeImage()

/* Some operating system take longer to complete mounting FAT32
wait up to 3 seconds for config.txt file to appear */
QString configFilename = folder+"/config.txt";
QString configFilename;
bool foundFile = false;

for (int tries = 0; tries < 3; tries++)
{
if (QFile::exists(configFilename))
/* Search all mountpoints, as on some systems FAT partition
may not be first volume */
for (auto mp : mountpoints)
{
folder = QString::fromStdString(mp);
if (!folder.isEmpty() && folder.back() == '\\')
folder.chop(1);
configFilename = folder+"/config.txt";

if (QFile::exists(configFilename))
{
foundFile = true;
break;

This comment has been minimized.

Copy link
@lurch

lurch Mar 22, 2021

Contributor

It may be too much of a rare edge-case to worry about, but what if the user has two drives plugged in that both contain a config.txt file?

This comment has been minimized.

Copy link
@maxnet

maxnet Mar 22, 2021

Author Collaborator

@lurch

"mountpoints" variable only contain the mountpoints associated with our target drive.
See:

            if (QByteArray::fromStdString(i.device).toLower() == devlower && i.mountpoints.size())
            {
                mountpoints = i.mountpoints;
                break;
            }

Only corner case when there would be more partitions ON THE SAME DRIVE containing config.txt, would be NOOBS style setups.
And I don't think anybody ships that as Image.

This comment has been minimized.

Copy link
@lurch

lurch Mar 22, 2021

Contributor

Sorry, I hope you didn't mind me asking.

}
}
if (foundFile)
break;
QThread::sleep(1);
}

if (!QFile::exists(configFilename))
if (!foundFile)
{
emit error(tr("Unable to customize. File '%1' does not exist.").arg(configFilename));
return false;
Expand Down

0 comments on commit f682c3a

Please sign in to comment.