[core] improve write retry handling

* Add a proper delay before retrying a write operation and increase retry count to 4
* Add retries when clearing boot records or when zeroing a drive
* Also improve log output from USB device reset
This commit is contained in:
Pete Batard 2018-06-13 19:23:24 +01:00
parent 3ba7c9eab0
commit 5ab67c03d6
6 changed files with 70 additions and 37 deletions

View file

@ -339,19 +339,18 @@ BOOL WriteFileWithRetry(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWr
DWORD nTry;
BOOL readFilePointer;
LARGE_INTEGER liFilePointer, liZero = { { 0,0 } };
static char* retry_msg = " - retrying...";
// Need to get the current file pointer in case we need to retry
readFilePointer = SetFilePointerEx(hFile, liZero, &liFilePointer, FILE_CURRENT);
if (!readFilePointer)
uprintf(" Warning - Could not read file pointer: %s", WindowsErrorString());
uprintf("Warning: Could not read file pointer: %s", WindowsErrorString());
if (nNumRetries == 0)
nNumRetries = 1;
for (nTry = 1; nTry <= nNumRetries; nTry++) {
// Need to rewind our file position on retry - if we can't even do that, just give up
if ((nTry > 1) && (!SetFilePointerEx(hFile, liFilePointer, NULL, FILE_BEGIN))) {
uprintf(" Could not set file pointer - aborting");
uprintf("Could not set file pointer - Aborting");
break;
}
if (WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, NULL)) {
@ -362,15 +361,17 @@ BOOL WriteFileWithRetry(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWr
uprintf("Warning: Possible short write");
return TRUE;
}
uprintf(" Wrote %d bytes but requested %d%s", *lpNumberOfBytesWritten,
nNumberOfBytesToWrite, nTry < nNumRetries ? retry_msg : "");
uprintf("Wrote %d bytes but requested %d", *lpNumberOfBytesWritten, nNumberOfBytesToWrite);
} else {
uprintf(" Write error [0x%08X]%s", GetLastError(), nTry < nNumRetries ? retry_msg : "");
uprintf("Write error [0x%08X]", GetLastError());
}
// If we can't reposition for the next run, just abort
if (!readFilePointer)
break;
Sleep(200);
if (nTry < nNumRetries) {
uprintf("Retrying in %d seconds...", WRITE_TIMEOUT / 1000);
Sleep(WRITE_TIMEOUT);
}
}
if (SCODE_CODE(GetLastError()) == ERROR_SUCCESS)
SetLastError(ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT);