1
0
mirror of https://github.com/pbatard/rufus.git synced 2026-10-24 02:28:58 +03:00

[core] improve WriteFileWithRetry()

* Cancelling now takes precedence over retries.
* Simplify the code and use an OVERLAPPED for re-positioning.
This commit is contained in:
Pete Batard
2026-06-03 16:45:39 +01:00
parent 74b1102814
commit ffab56f2ef
2 changed files with 18 additions and 25 deletions

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 4.15.2382"
CAPTION "Rufus 4.15.2383"
FONT 9, "Segoe UI Symbol", 400, 0, 0x0
BEGIN
LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP
@@ -409,8 +409,8 @@ END
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 4,15,2382,0
PRODUCTVERSION 4,15,2382,0
FILEVERSION 4,15,2383,0
PRODUCTVERSION 4,15,2383,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
@@ -428,13 +428,13 @@ BEGIN
VALUE "Comments", "https://rufus.ie"
VALUE "CompanyName", "Akeo Consulting"
VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "4.15.2382"
VALUE "FileVersion", "4.15.2383"
VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011-2026 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "https://www.gnu.org/licenses/gpl-3.0.html"
VALUE "OriginalFilename", "rufus-4.15.exe"
VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "4.15.2382"
VALUE "ProductVersion", "4.15.2383"
END
END
BLOCK "VarFileInfo"

View File

@@ -36,6 +36,7 @@
#include "rufus.h"
#include "ntdll.h"
#include "winio.h"
#include "missing.h"
#include "settings.h"
#include "resource.h"
@@ -614,28 +615,23 @@ HANDLE CreateFileWithTimeout(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwS
BOOL WriteFileWithRetry(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten, DWORD nNumRetries)
{
DWORD nTry;
BOOL readFilePointer;
LARGE_INTEGER liFilePointer, liZero = { { 0,0 } };
DWORD NumberOfBytesWritten;
static const LARGE_INTEGER liZero = { { 0,0 } };
DWORD nTry, NumberOfBytesWritten;
NOW_THATS_WHAT_I_CALL_AN_OVERLAPPED overlapped = { 0 };
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);
if (!readFilePointer)
uprintf("WARNING: Could not read file pointer %s", WindowsErrorString());
// Need to get the current file pointer for retry
if (!SetFilePointerEx(hFile, liZero, (PLARGE_INTEGER)&overlapped.Offset, FILE_CURRENT)) {
uprintf("ERROR: Could not set file offset %s", WindowsErrorString());
return FALSE;
}
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)) {
for (nTry = 1; nTry <= nNumRetries && (HRESULT_CODE(ErrorStatus) != ERROR_CANCELLED); nTry++) {
if (WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, (LPOVERLAPPED)&overlapped)) {
LastWriteError = 0;
if (nNumberOfBytesToWrite == *lpNumberOfBytesWritten)
return TRUE;
@@ -648,18 +644,15 @@ BOOL WriteFileWithRetry(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWr
} else {
uprintf("Write error %s", WindowsErrorString());
LastWriteError = RUFUS_ERROR(GetLastError());
if (LastWriteError == RUFUS_ERROR(ERROR_DISK_FULL))
if (LastWriteError == RUFUS_ERROR(ERROR_DISK_FULL) || HRESULT_CODE(ErrorStatus) == ERROR_CANCELLED)
break;
}
// If we can't reposition for the next run, just abort
if (!readFilePointer)
break;
if (nTry < nNumRetries) {
uprintf("Retrying in %d seconds...", WRITE_TIMEOUT / 1000);
Sleep(WRITE_TIMEOUT);
}
}
if (SCODE_CODE(GetLastError()) == ERROR_SUCCESS)
if (SCODE_CODE(GetLastError()) == ERROR_SUCCESS && HRESULT_CODE(ErrorStatus) != ERROR_CANCELLED)
SetLastError(RUFUS_ERROR(ERROR_WRITE_FAULT));
return FALSE;
}