[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:
Pete Batard 2016-01-15 12:26:31 +00:00
parent f2a539a48c
commit 0fe0086c8f
4 changed files with 57 additions and 49 deletions

View file

@ -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;
}