Skip to content

Commit

Permalink
Windows: improve file system mounting for customization
Browse files Browse the repository at this point in the history
- Check if config.txt exists on drive letter before proceeding to
  write changes. Wait up to 3 seconds if not.
- Force drive letter assignment for portable SSDs and other devices
  that pretend to be a fixed drive. (Windows only auto-assigns
  drive letters for removable media).

Ref #171
  • Loading branch information
maxnet committed Mar 21, 2021
1 parent c6129a1 commit 2bbb8bf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Expand Up @@ -8,8 +8,8 @@ endif()

project(rpi-imager LANGUAGES CXX C)
set(IMAGER_VERSION_MAJOR 1)
set(IMAGER_VERSION_MINOR 6)
set(IMAGER_VERSION_STR "${IMAGER_VERSION_MAJOR}.${IMAGER_VERSION_MINOR}")
set(IMAGER_VERSION_MINOR 7)
set(IMAGER_VERSION_STR "${IMAGER_VERSION_MAJOR}.${IMAGER_VERSION_MINOR}beta")
set(IMAGER_VERSION_CSV "${IMAGER_VERSION_MAJOR},${IMAGER_VERSION_MINOR},0,0")
add_definitions(-DIMAGER_VERSION_STR="${IMAGER_VERSION_STR}")
add_definitions(-DIMAGER_VERSION_CSV=${IMAGER_VERSION_CSV})
Expand Down
69 changes: 59 additions & 10 deletions downloadthread.cpp
Expand Up @@ -127,7 +127,7 @@ bool DownloadThread::_openAndPrepareDevice()

if (std::regex_match(_filename.constData(), m, windriveregex))
{
QByteArray _nr = QByteArray::fromStdString(m[1]);
_nr = QByteArray::fromStdString(m[1]);

if (!_nr.isEmpty()) {
qDebug() << "Removing partition table from Windows drive #" << _nr << "(" << _filename << ")";
Expand Down Expand Up @@ -811,6 +811,18 @@ bool DownloadThread::_customizeImage()

emit preparationStatusUpdate(tr("Waiting for FAT partition to be mounted"));

#ifdef Q_OS_WIN
qDebug() << "Running diskpart rescan";
QProcess proc;
proc.setProcessChannelMode(proc.MergedChannels);
proc.start("diskpart");
proc.waitForStarted();
proc.write("rescan\r\n");
proc.closeWriteChannel();
proc.waitForFinished();
qDebug() << proc.readAll();
#endif

/* See if OS auto-mounted the device */
for (int tries = 0; tries < 3; tries++)
{
Expand All @@ -826,6 +838,30 @@ bool DownloadThread::_customizeImage()
}
}

#ifdef Q_OS_WIN
if (folder.isEmpty() && !_nr.isEmpty()) {
qDebug() << "Windows did not assign drive letter automatically. Ask diskpart to do so manually.";
proc.start("diskpart");
proc.waitForStarted();
proc.write("select disk "+_nr+"\r\n"
"select partition 1\r\n"
"assign\r\n");
proc.closeWriteChannel();
proc.waitForFinished();
qDebug() << proc.readAll();

auto l = Drivelist::ListStorageDevices();
for (auto i : l)
{
if (QByteArray::fromStdString(i.device).toLower() == devlower && i.mountpoints.size())
{
folder = QByteArray::fromStdString(i.mountpoints.front());
break;
}
}
}
#endif

#ifdef Q_OS_LINUX
bool manualmount = false;

Expand Down Expand Up @@ -853,7 +889,7 @@ bool DownloadThread::_customizeImage()
QTemporaryDir td;
QStringList args;
folder = td.path();
args << fatpartition << folder;
args << "-t" << "vfat" << fatpartition << folder;

if (QProcess::execute("mount", args) != 0)
{
Expand Down Expand Up @@ -883,14 +919,29 @@ bool DownloadThread::_customizeImage()
return false;
}

/* 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";
for (int tries = 0; tries < 3; tries++)
{
if (QFile::exists(configFilename))
break;
QThread::sleep(1);
}

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

emit preparationStatusUpdate(tr("Customizing image"));

if (!_firstrun.isEmpty())
{
QFile f(folder+"/firstrun.sh");
if (f.open(f.WriteOnly))
if (f.open(f.WriteOnly) && f.write(_firstrun) == _firstrun.length())
{
f.write(_firstrun);
f.close();
}
else
Expand All @@ -906,8 +957,8 @@ bool DownloadThread::_customizeImage()
configItems.removeAll("");
QByteArray config;

QFile f(folder+"/config.txt");
if (f.exists() && f.open(f.ReadOnly))
QFile f(configFilename);
if (f.open(f.ReadOnly))
{
config = f.readAll();
f.close();
Expand All @@ -929,9 +980,8 @@ bool DownloadThread::_customizeImage()
}
}

if (f.open(f.WriteOnly))
if (f.open(f.WriteOnly) && f.write(config) == config.length())
{
f.write(config);
f.close();
}
else
Expand All @@ -953,9 +1003,8 @@ bool DownloadThread::_customizeImage()
}

cmdline += _cmdline;
if (f.open(f.WriteOnly))
if (f.open(f.WriteOnly) && f.write(cmdline) == cmdline.length())
{
f.write(cmdline);
f.close();
}
else
Expand Down
1 change: 1 addition & 0 deletions downloadthread.h
Expand Up @@ -176,6 +176,7 @@ class DownloadThread : public QThread

#ifdef Q_OS_WIN
WinFile _file, _volumeFile;
QByteArray _nr;
#elif defined(Q_OS_DARWIN)
MacFile _file;
#else
Expand Down

0 comments on commit 2bbb8bf

Please sign in to comment.