[core] fix MBR/GPT sectors being zeroed more than once

* ClearMBRGPT() attempts to write WRITE_RETRIES times, even if all those times succeed.
* Instead, skip the remaining retries on success.
* Also improve code readability.
* Closes #1454
This commit is contained in:
Derek Foreman 2020-02-13 16:19:46 -06:00 committed by Pete Batard
parent 70419d31e9
commit 493d818cea
No known key found for this signature in database
GPG key ID: 38E0CF5E69EDD671
2 changed files with 23 additions and 24 deletions

View file

@ -717,33 +717,32 @@ static BOOL ClearMBRGPT(HANDLE hPhysicalDrive, LONGLONG DiskSize, DWORD SectorSi
num_sectors_to_clear = (DWORD)((add1MB ? 2048 : 0) + MAX_SECTORS_TO_CLEAR); num_sectors_to_clear = (DWORD)((add1MB ? 2048 : 0) + MAX_SECTORS_TO_CLEAR);
uprintf("Erasing %d sectors", num_sectors_to_clear); uprintf("Erasing %d sectors", num_sectors_to_clear);
for (i=0; i<num_sectors_to_clear; i++) { for (i = 0; i < num_sectors_to_clear; i++) {
for (j = 1; j <= WRITE_RETRIES; j++) { for (j = 1; j <= WRITE_RETRIES; j++) {
CHECK_FOR_USER_CANCEL; CHECK_FOR_USER_CANCEL;
if (write_sectors(hPhysicalDrive, SectorSize, i, 1, pBuf) != SectorSize) { if (write_sectors(hPhysicalDrive, SectorSize, i, 1, pBuf) == SectorSize)
if (j >= WRITE_RETRIES) break;
goto out; if (j >= WRITE_RETRIES)
uprintf("Retrying in %d seconds...", WRITE_TIMEOUT / 1000); goto out;
// Don't sit idly but use the downtime to check for conflicting processes... uprintf("Retrying in %d seconds...", WRITE_TIMEOUT / 1000);
Sleep(CheckDriveAccess(WRITE_TIMEOUT, FALSE)); // Don't sit idly but use the downtime to check for conflicting processes...
} Sleep(CheckDriveAccess(WRITE_TIMEOUT, FALSE));
} }
} }
for (i = last_sector - MAX_SECTORS_TO_CLEAR; i < last_sector; i++) { for (i = last_sector - MAX_SECTORS_TO_CLEAR; i < last_sector; i++) {
for (j = 1; j <= WRITE_RETRIES; j++) { for (j = 1; j <= WRITE_RETRIES; j++) {
CHECK_FOR_USER_CANCEL; CHECK_FOR_USER_CANCEL;
if (write_sectors(hPhysicalDrive, SectorSize, i, 1, pBuf) != SectorSize) { if (write_sectors(hPhysicalDrive, SectorSize, i, 1, pBuf) == SectorSize)
if (j < WRITE_RETRIES) { break;
uprintf("Retrying in %d seconds...", WRITE_TIMEOUT / 1000); if (j >= WRITE_RETRIES) {
Sleep(CheckDriveAccess(WRITE_TIMEOUT, FALSE)); // Windows seems to be an ass about keeping a lock on a backup GPT,
} else { // so we try to be lenient about not being able to clear it.
// Windows seems to be an ass about keeping a lock on a backup GPT, uprintf("Warning: Failed to clear backup GPT...");
// so we try to be lenient about not being able to clear it. r = TRUE;
uprintf("Warning: Failed to clear backup GPT..."); goto out;
r = TRUE;
goto out;
}
} }
uprintf("Retrying in %d seconds...", WRITE_TIMEOUT / 1000);
Sleep(CheckDriveAccess(WRITE_TIMEOUT, FALSE));
} }
} }
r = TRUE; r = TRUE;

View file

@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 232, 326 IDD_DIALOG DIALOGEX 12, 12, 232, 326
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_ACCEPTFILES EXSTYLE WS_EX_ACCEPTFILES
CAPTION "Rufus 3.9.1608" CAPTION "Rufus 3.9.1609"
FONT 9, "Segoe UI Symbol", 400, 0, 0x0 FONT 9, "Segoe UI Symbol", 400, 0, 0x0
BEGIN BEGIN
LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP
@ -394,8 +394,8 @@ END
// //
VS_VERSION_INFO VERSIONINFO VS_VERSION_INFO VERSIONINFO
FILEVERSION 3,9,1608,0 FILEVERSION 3,9,1609,0
PRODUCTVERSION 3,9,1608,0 PRODUCTVERSION 3,9,1609,0
FILEFLAGSMASK 0x3fL FILEFLAGSMASK 0x3fL
#ifdef _DEBUG #ifdef _DEBUG
FILEFLAGS 0x1L FILEFLAGS 0x1L
@ -413,13 +413,13 @@ BEGIN
VALUE "Comments", "https://rufus.ie" VALUE "Comments", "https://rufus.ie"
VALUE "CompanyName", "Akeo Consulting" VALUE "CompanyName", "Akeo Consulting"
VALUE "FileDescription", "Rufus" VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "3.9.1608" VALUE "FileVersion", "3.9.1609"
VALUE "InternalName", "Rufus" VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011-2020 Pete Batard (GPL v3)" VALUE "LegalCopyright", "© 2011-2020 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "https://www.gnu.org/licenses/gpl-3.0.html" VALUE "LegalTrademarks", "https://www.gnu.org/licenses/gpl-3.0.html"
VALUE "OriginalFilename", "rufus-3.9.exe" VALUE "OriginalFilename", "rufus-3.9.exe"
VALUE "ProductName", "Rufus" VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "3.9.1608" VALUE "ProductVersion", "3.9.1609"
END END
END END
BLOCK "VarFileInfo" BLOCK "VarFileInfo"