[core] speed up the clearing of MBR/GPT sectors

* write_sector() should really only be used when writing single sectors as it
  is way to slow for anything else => Switch to using WriteFileWithRetry().
* Also revert an unwarranted change from f0047986e7.
This commit is contained in:
Pete Batard 2021-09-19 14:29:30 +01:00
parent f0047986e7
commit 9dc045a701
No known key found for this signature in database
GPG Key ID: 38E0CF5E69EDD671
5 changed files with 27 additions and 26 deletions

View File

@ -11,7 +11,7 @@
<Identity
Name="19453.net.Rufus"
Publisher="CN=7AC86D13-3E5A-491A-ADD5-80095C212740"
Version="3.16.1819.0" />
Version="3.16.1820.0" />
<Properties>
<DisplayName>Rufus</DisplayName>

View File

@ -710,19 +710,16 @@ out:
static BOOL ClearMBRGPT(HANDLE hPhysicalDrive, LONGLONG DiskSize, DWORD SectorSize, BOOL add1MB)
{
BOOL r = FALSE;
uint64_t i, last_sector = DiskSize/SectorSize, num_sectors_to_clear;
unsigned char* pBuf = (unsigned char*) calloc(SectorSize, 1);
LARGE_INTEGER liFilePointer;
uint64_t num_sectors_to_clear;
unsigned char* pZeroBuf = NULL;
PrintInfoDebug(0, MSG_224);
if (pBuf == NULL) {
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_NOT_ENOUGH_MEMORY;
goto out;
}
// http://en.wikipedia.org/wiki/GUID_Partition_Table tells us we should clear 34 sectors at the
// beginning and 33 at the end. We bump these values to MAX_SECTORS_TO_CLEAR each end to help
// with reluctant access to large drive.
// We try to clear at least 1MB + the PBR when Large FAT32 is selected (add1MB), but
// We try to clear at least 1MB + the VBR when Large FAT32 is selected (add1MB), but
// don't do it otherwise, as it seems unnecessary and may take time for slow drives.
// Also, for various reasons (one of which being that Windows seems to have issues
// with GPT drives that contain a lot of small partitions) we try not not to clear
@ -733,22 +730,23 @@ static BOOL ClearMBRGPT(HANDLE hPhysicalDrive, LONGLONG DiskSize, DWORD SectorSi
num_sectors_to_clear = (DWORD)((add1MB ? 2048 : 0) + MAX_SECTORS_TO_CLEAR);
uprintf("Erasing %d sectors", num_sectors_to_clear);
for (i = 0; i < num_sectors_to_clear; i++) {
CHECK_FOR_USER_CANCEL;
if (write_sectors(hPhysicalDrive, SectorSize, i, 1, pBuf) != SectorSize)
goto out;
}
for (i = last_sector - MAX_SECTORS_TO_CLEAR; i < last_sector; i++) {
CHECK_FOR_USER_CANCEL;
// Windows seems to be an ass about keeping a lock on a backup GPT,
// so we try to be lenient about not being able to clear it.
if (write_sectors(hPhysicalDrive, SectorSize, i, 1, pBuf) != SectorSize)
break;
pZeroBuf = calloc(SectorSize, num_sectors_to_clear);
if (pZeroBuf == NULL) {
FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_NOT_ENOUGH_MEMORY;
goto out;
}
if (!WriteFileWithRetry(hPhysicalDrive, pZeroBuf, (DWORD)(SectorSize * num_sectors_to_clear), NULL, WRITE_RETRIES))
goto out;
CHECK_FOR_USER_CANCEL;
liFilePointer.QuadPart = DiskSize - (LONGLONG)SectorSize * MAX_SECTORS_TO_CLEAR;
SetFilePointerEx(hPhysicalDrive, liFilePointer, &liFilePointer, FILE_BEGIN);
// Windows seems to be an ass about keeping a lock on a backup GPT,
// so we try to be lenient about not being able to clear it.
WriteFileWithRetry(hPhysicalDrive, pZeroBuf, SectorSize * MAX_SECTORS_TO_CLEAR, NULL, WRITE_RETRIES);
r = TRUE;
out:
safe_free(pBuf);
safe_free(pZeroBuf);
return r;
}

View File

@ -91,7 +91,6 @@ static str_score_t str_adjust[] = {
{ "Gadget", -10 },
{ "Flash", -10 },
{ "HDD", +20 },
{ "SDXC", +10 },
{ "SSD", +20 }
};

View File

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

View File

@ -855,6 +855,10 @@ BOOL WriteFileWithRetry(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWr
DWORD nTry;
BOOL readFilePointer;
LARGE_INTEGER liFilePointer, liZero = { { 0,0 } };
DWORD NumberOfBytesWritten;
if (lpNumberOfBytesWritten == NULL)
lpNumberOfBytesWritten = &NumberOfBytesWritten;
// Need to get the current file pointer in case we need to retry
readFilePointer = SetFilePointerEx(hFile, liZero, &liFilePointer, FILE_CURRENT);