mirror of
https://github.com/pbatard/rufus.git
synced 2025-06-06 17:41:36 -04:00
[core] improve WriteFileWithRetry() and move it to stdio.c
* Also fix uprintf() generating an error code if the log window is not instantiated yet.
This commit is contained in:
parent
f2a539a48c
commit
0fe0086c8f
4 changed files with 57 additions and 49 deletions
35
src/stdfn.c
35
src/stdfn.c
|
@ -810,38 +810,3 @@ BOOL SetLGP(BOOL bRestore, BOOL* bExistingKey, const char* szPath, const char* s
|
|||
return FALSE;
|
||||
return (BOOL) r;
|
||||
}
|
||||
|
||||
BOOL WriteFileWithRetry(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
|
||||
LPDWORD lpNumberOfBytesWritten, DWORD nNumRetries)
|
||||
{
|
||||
DWORD nTry = 1;
|
||||
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());
|
||||
|
||||
do {
|
||||
// Need to rewind our file position on retry
|
||||
if ((nTry > 1) && (!SetFilePointerEx(hFile, liFilePointer, NULL, FILE_BEGIN))) {
|
||||
uprintf(" Could not set file pointer%s", retry_msg);
|
||||
goto next_try;
|
||||
}
|
||||
if (WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, NULL)) {
|
||||
if (nNumberOfBytesToWrite == *lpNumberOfBytesWritten)
|
||||
return TRUE;
|
||||
uprintf(" Wrote %d bytes but requested %d%s", *lpNumberOfBytesWritten,
|
||||
nNumberOfBytesToWrite, nTry < nNumRetries ? retry_msg : "");
|
||||
SetLastError(ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_INCORRECT_SIZE);
|
||||
} else {
|
||||
uprintf(" Write error%s", nTry < nNumRetries ? retry_msg : "");
|
||||
}
|
||||
next_try:
|
||||
Sleep(200);
|
||||
nTry++;
|
||||
} while((readFilePointer) && (nTry < nNumRetries));
|
||||
return FALSE;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue