[core] reinstate locking of the logical drive when writing images

* When writing images such as tails, that contain a large ESP, Windows forcibly
  removes the media while we are writing it, unless we lock the logical drive.
* Also fix a Bled Coverity warning.
This commit is contained in:
Pete Batard 2020-10-28 21:41:43 +00:00
parent 69bf32dd33
commit 65a0c2c42d
No known key found for this signature in database
GPG key ID: 38E0CF5E69EDD671
4 changed files with 30 additions and 9 deletions

View file

@ -1746,6 +1746,23 @@ DWORD WINAPI FormatThread(void* param)
goto out;
}
RefreshDriveLayout(hPhysicalDrive);
// If we write an image that contains an ESP, Windows forcibly reassigns/removes the target
// drive, which causes a write error. To work around this, we must lock the logical drive.
if ((boot_type == BT_IMAGE) && write_as_image) {
// ...and get a lock to the logical drive so that we can actually write something
hLogicalVolume = GetLogicalHandle(DriveIndex, 0, TRUE, FALSE, !actual_lock_drive);
if (hLogicalVolume == INVALID_HANDLE_VALUE) {
uprintf("Could not lock volume");
FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_OPEN_FAILED;
goto out;
} else if (hLogicalVolume == NULL) {
// NULL is returned for cases where the drive is not yet partitioned
uprintf("Drive does not appear to be partitioned");
} else if (!UnmountVolume(hLogicalVolume)) {
uprintf("Trying to continue regardless...");
}
}
CHECK_FOR_USER_CANCEL;
if (!zero_drive && !write_as_image) {