[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

@ -2948,11 +2948,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
hFile = CreateFileU(loc_file, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if ((hFile == INVALID_HANDLE_VALUE) || (!WriteFileWithRetry(hFile, loc_data, loc_size, &size, WRITE_RETRIES))) {
uprintf("localization: unable to extract '%s': %s.\n", loc_file, WindowsErrorString());
uprintf("localization: unable to extract '%s': %s", loc_file, WindowsErrorString());
safe_closehandle(hFile);
goto out;
}
uprintf("localization: extracted data to '%s'\n", loc_file);
uprintf("localization: extracted data to '%s'", loc_file);
safe_closehandle(hFile);
} else {
safe_sprintf(loc_file, sizeof(loc_file), "%s\\%s", app_dir, rufus_loc);
@ -2962,7 +2962,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
if ( (!get_supported_locales(loc_file))
|| ((selected_locale = ((locale_name == NULL)?get_locale_from_lcid(lcid, TRUE):get_locale_from_name(locale_name, TRUE))) == NULL) ) {
uprintf("FATAL: Could not access locale!\n");
uprintf("FATAL: Could not access locale!");
MessageBoxU(NULL, "The locale data is missing or invalid. This application will now exit.",
"Fatal error", MB_ICONSTOP|MB_SYSTEMMODAL);
goto out;

View File

@ -32,7 +32,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 242, 376
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Rufus 2.7.831"
CAPTION "Rufus 2.7.832"
FONT 8, "Segoe UI Symbol", 400, 0, 0x0
BEGIN
LTEXT "Device",IDS_DEVICE_TXT,9,6,200,8
@ -319,8 +319,8 @@ END
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 2,7,831,0
PRODUCTVERSION 2,7,831,0
FILEVERSION 2,7,832,0
PRODUCTVERSION 2,7,832,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
@ -337,13 +337,13 @@ BEGIN
BEGIN
VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)"
VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "2.7.831"
VALUE "FileVersion", "2.7.832"
VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011-2016 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html"
VALUE "OriginalFilename", "rufus.exe"
VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "2.7.831"
VALUE "ProductVersion", "2.7.832"
END
END
BLOCK "VarFileInfo"

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

View File

@ -62,12 +62,14 @@ void _uprintf(const char *format, ...)
// Send output to Windows debug facility
OutputDebugStringA(buf);
// Send output to our log Window
Edit_SetSel(hLog, MAX_LOG_SIZE, MAX_LOG_SIZE);
Edit_ReplaceSelU(hLog, buf);
// Make sure the message scrolls into view
// (Or see code commented in LogProc:WM_SHOWWINDOW for a less forceful scroll)
SendMessage(hLog, EM_LINESCROLL, 0, SendMessage(hLog, EM_GETLINECOUNT, 0, 0));
if ((hLog != NULL) && (hLog != INVALID_HANDLE_VALUE)) {
// Send output to our log Window
Edit_SetSel(hLog, MAX_LOG_SIZE, MAX_LOG_SIZE);
Edit_ReplaceSelU(hLog, buf);
// Make sure the message scrolls into view
// (Or see code commented in LogProc:WM_SHOWWINDOW for a less forceful scroll)
SendMessage(hLog, EM_LINESCROLL, 0, SendMessage(hLog, EM_GETLINECOUNT, 0, 0));
}
}
#endif
@ -280,3 +282,44 @@ const char* StrError(DWORD error_code, BOOL use_default_locale)
toggle_default_locale();
return ret;
}
BOOL WriteFileWithRetry(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten, DWORD nNumRetries)
{
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());
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");
break;
}
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 : "");
}
else {
uprintf(" Write error [0x%8X]%s", GetLastError(), nTry < nNumRetries ? retry_msg : "");
}
// If we can't reposition for the next run, just abort
if (!readFilePointer)
break;
Sleep(200);
}
if (SCODE_CODE(GetLastError()) == ERROR_SUCCESS)
SetLastError(ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT);
return FALSE;
}