From f2a539a48cdbae2c44347d5fdd10b4b6041abe3e Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Thu, 14 Jan 2016 17:43:02 +0000 Subject: [PATCH] [core] add and use WriteFileWithRetry() where possible --- src/dos.c | 12 ++++---- src/drive.c | 10 ++----- src/format.c | 2 +- src/icon.c | 31 ++++++++++----------- src/iso.c | 41 +++++++++------------------ src/rufus.c | 2 +- src/rufus.h | 4 ++- src/rufus.rc | 10 +++---- src/stdfn.c | 37 ++++++++++++++++++++++++- src/syslinux.c | 75 ++++++++++++++++++++++++-------------------------- src/vhd.c | 8 +++--- 11 files changed, 123 insertions(+), 109 deletions(-) diff --git a/src/dos.c b/src/dos.c index a08aa741..69cc467d 100644 --- a/src/dos.c +++ b/src/dos.c @@ -2,8 +2,8 @@ * Rufus: The Reliable USB Formatting Utility * DOS boot file extraction, from the FAT12 floppy image in diskcopy.dll * (MS WinME DOS) or from the embedded FreeDOS resource files - * Copyright © 2011-2015 Pete Batard - * + * Copyright © 2011-2016 Pete Batard + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -257,8 +257,8 @@ static BOOL ExtractFAT(int entry, const char* path) return FALSE; } - if ((!WriteFile(hFile, &DiskImage[filestart], (DWORD)filesize, &Size, 0)) || (filesize != Size)) { - uprintf("Couldn't write file '%s': %s.\n", filename, WindowsErrorString()); + if (!WriteFileWithRetry(hFile, &DiskImage[filestart], (DWORD)filesize, &Size, WRITE_RETRIES)) { + uprintf("Could not write file '%s': %s.\n", filename, WindowsErrorString()); safe_closehandle(hFile); return FALSE; } @@ -394,8 +394,8 @@ BOOL ExtractFreeDOS(const char* path) return FALSE; } - if ((!WriteFile(hFile, res_data, res_size, &Size, 0)) || (res_size != Size)) { - uprintf("Couldn't write file '%s': %s.\n", filename, WindowsErrorString()); + if (!WriteFileWithRetry(hFile, res_data, res_size, &Size, WRITE_RETRIES)) { + uprintf("Could not write file '%s': %s.\n", filename, WindowsErrorString()); safe_closehandle(hFile); return FALSE; } diff --git a/src/drive.c b/src/drive.c index ff464ca7..0e6c3711 100644 --- a/src/drive.c +++ b/src/drive.c @@ -1065,7 +1065,7 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m bufsize = 65536; // 64K should be enough for everyone buffer = calloc(bufsize, 1); if (buffer != NULL) { - if ((!WriteFile(hDrive, buffer, bufsize, &size, NULL)) || (size != bufsize)) + if (!WriteFileWithRetry(hDrive, buffer, bufsize, &size, WRITE_RETRIES)) uprintf(" Could not zero MSR: %s", WindowsErrorString()); free(buffer); } @@ -1165,12 +1165,8 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m uprintf("Could not access uefi-ntfs.img"); return FALSE; } - r = WriteFile(hDrive, buffer, bufsize, &size, NULL); - if ((!r) || (size != bufsize)) { - if (!r) - uprintf("Write error: %s", WindowsErrorString()); - else - uprintf("Write error: Wrote %d bytes, expected %d bytes\n", size, bufsize); + if(!WriteFileWithRetry(hDrive, buffer, bufsize, &size, WRITE_RETRIES)) { + uprintf("Write error: %s", WindowsErrorString()); return FALSE; } } diff --git a/src/format.c b/src/format.c index 5346c394..b70d8e9c 100644 --- a/src/format.c +++ b/src/format.c @@ -1251,7 +1251,7 @@ static BOOL SetupWinPE(char drive_letter) } } - if ((!WriteFile(handle, buf, size, &rw_size, NULL)) || (size != rw_size)) { + if (!WriteFileWithRetry(handle, buf, size, &rw_size, WRITE_RETRIES)) { uprintf("Could not write patched file: %s\n", WindowsErrorString()); goto out; } diff --git a/src/icon.c b/src/icon.c index afb3f750..ddd66eb9 100644 --- a/src/icon.c +++ b/src/icon.c @@ -1,7 +1,7 @@ /* * Rufus: The Reliable USB Formatting Utility * Extract icon from executable and set autorun.inf - * Copyright © 2012-2013 Pete Batard + * Copyright © 2012-2016 Pete Batard * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -74,7 +74,7 @@ typedef struct WORD nID; // the ID } GRPICONDIRENTRY, *LPGRPICONDIRENTRY; -typedef struct +typedef struct { WORD idReserved; // Reserved (must be 0) WORD idType; // Resource type (1 for icons) @@ -103,13 +103,13 @@ static BOOL SaveIcon(const char* filename) hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { - uprintf("Unable to create icon '%s': %s.\n", filename, WindowsErrorString()); + uprintf("Unable to create icon '%s': %s.", filename, WindowsErrorString()); goto out; } // Write .ico header - if ((!WriteFile(hFile, icondir, 3*sizeof(WORD), &Size, NULL)) || (Size != 3*sizeof(WORD))) { - uprintf("Couldn't write icon header: %s.\n", WindowsErrorString()); + if (!WriteFileWithRetry(hFile, icondir, 3*sizeof(WORD), &Size, WRITE_RETRIES)) { + uprintf("Could not write icon header: %s.", WindowsErrorString()); goto out; } @@ -117,15 +117,14 @@ static BOOL SaveIcon(const char* filename) offset = 3*sizeof(WORD) + icondir->idCount*sizeof(ICONDIRENTRY); for (i=0; iidCount; i++) { // Write the common part of ICONDIRENTRY - if ( (!WriteFile(hFile, &icondir->idEntries[i], sizeof(GRPICONDIRENTRY)-sizeof(WORD), &Size, NULL)) - || (Size != sizeof(GRPICONDIRENTRY)-sizeof(WORD)) ) { - uprintf("Couldn't write ICONDIRENTRY[%d]: %s.\n", i, WindowsErrorString()); + if (!WriteFileWithRetry(hFile, &icondir->idEntries[i], sizeof(GRPICONDIRENTRY)-sizeof(WORD), &Size, WRITE_RETRIES)) { + uprintf("Could not write ICONDIRENTRY[%d]: %s.", i, WindowsErrorString()); goto out; } res = FindResourceA(hMainInstance, MAKEINTRESOURCEA(icondir->idEntries[i].nID), _RT_ICON); // Write the DWORD offset - if ( (!WriteFile(hFile, &offset, sizeof(offset), &Size, NULL)) || (Size != sizeof(offset)) ) { - uprintf("Couldn't write ICONDIRENTRY[%d] offset: %s.\n", i, WindowsErrorString()); + if (!WriteFileWithRetry(hFile, &offset, sizeof(offset), &Size, WRITE_RETRIES)) { + uprintf("Could not write ICONDIRENTRY[%d] offset: %s.", i, WindowsErrorString()); goto out; } offset += SizeofResource(NULL, res); @@ -136,12 +135,12 @@ static BOOL SaveIcon(const char* filename) res_handle = LoadResource(NULL, res); res_data = (BYTE*)LockResource(res_handle); res_size = SizeofResource(NULL, res); - if ( (!WriteFile(hFile, res_data, res_size, &Size, NULL)) || (Size != res_size) ) { - uprintf("Couldn't write icon data #%d: %s.\n", i, WindowsErrorString()); + if (!WriteFileWithRetry(hFile, res_data, res_size, &Size, WRITE_RETRIES)) { + uprintf("Could not write icon data #%d: %s.", i, WindowsErrorString()); goto out; } } - uprintf("Created: %s\n", filename); + uprintf("Created: %s", filename); r = TRUE; out: @@ -163,14 +162,14 @@ BOOL SetAutorun(const char* path) safe_sprintf(filename, sizeof(filename), "%sautorun.inf", path); fd = fopen(filename, "r"); // If there's an existing autorun, don't overwrite if (fd != NULL) { - uprintf("%s already exists - keeping it\n", filename); + uprintf("%s already exists - keeping it", filename); fclose(fd); return FALSE; } // No "/autorun.inf" => create a new one in UTF-16 LE mode fd = fopen(filename, "w, ccs=UTF-16LE"); if (fd == NULL) { - uprintf("Unable to create %s\n", filename); + uprintf("Unable to create %s", filename); uprintf("NOTE: This may be caused by a poorly designed security solution. " "See http://rufus.akeo.ie/compatibility."); return FALSE; @@ -181,7 +180,7 @@ BOOL SetAutorun(const char* path) fwprintf(fd, L"; Created by %s\n; " LTEXT(RUFUS_URL) L"\n", wRufusVersion); fwprintf(fd, L"[autorun]\nicon = autorun.ico\nlabel = %s\n", wlabel); fclose(fd); - uprintf("Created: %s\n", filename); + uprintf("Created: %s", filename); // .inf -> .ico filename[strlen(filename)-1] = 'o'; diff --git a/src/iso.c b/src/iso.c index d125bdea..41769cbb 100644 --- a/src/iso.c +++ b/src/iso.c @@ -1,7 +1,7 @@ /* * Rufus: The Reliable USB Formatting Utility * ISO file extraction - * Copyright © 2011-2015 Pete Batard + * Copyright © 2011-2016 Pete Batard * Based on libcdio's iso & udf samples: * Copyright © 2003-2014 Rocky Bernstein * @@ -429,17 +429,11 @@ static int udf_extract_files(udf_t *p_udf, udf_dirent_t *p_udf_dirent, const cha goto out; } buf_size = (DWORD)MIN(i_file_length, i_read); - for (i=0; i= WRITE_RETRIES) goto out; i_file_length -= i_read; if (nb_blocks++ % PROGRESS_THRESHOLD == 0) UpdateProgress(OP_DOS, 100.0f*nb_blocks/total_blocks); @@ -476,7 +470,7 @@ static int iso_extract_files(iso9660_t* p_iso, const char *psz_path) HANDLE file_handle = NULL; DWORD buf_size, wr_size, err; EXTRACT_PROPS props; - BOOL s, is_symlink, is_identical; + BOOL is_symlink, is_identical; int i_length, r = 1; char tmp[128], psz_fullpath[MAX_PATH], *psz_basename, *psz_sanpath; const char *psz_iso_name = &psz_fullpath[strlen(psz_extract_dir)]; @@ -484,7 +478,7 @@ static int iso_extract_files(iso9660_t* p_iso, const char *psz_path) CdioListNode_t* p_entnode; iso9660_stat_t *p_statbuf; CdioList_t* p_entlist; - size_t i, j; + size_t i; lsn_t lsn; int64_t i_file_length; @@ -582,17 +576,11 @@ static int iso_extract_files(iso9660_t* p_iso, const char *psz_path) goto out; } buf_size = (DWORD)MIN(i_file_length, ISO_BLOCKSIZE); - for (j=0; j= WRITE_RETRIES) goto out; i_file_length -= ISO_BLOCKSIZE; if (nb_blocks++ % PROGRESS_THRESHOLD == 0) UpdateProgress(OP_DOS, 100.0f*nb_blocks/total_blocks); @@ -918,7 +906,6 @@ int64_t ExtractISOFile(const char* iso, const char* iso_file, const char* dest_f int64_t file_length, r = 0; char buf[UDF_BLOCKSIZE]; DWORD buf_size, wr_size; - BOOL s; iso9660_t* p_iso = NULL; udf_t* p_udf = NULL; udf_dirent_t *p_udf_root = NULL, *p_udf_file = NULL; @@ -957,8 +944,7 @@ int64_t ExtractISOFile(const char* iso, const char* iso_file, const char* dest_f goto out; } buf_size = (DWORD)MIN(file_length, read_size); - s = WriteFile(file_handle, buf, buf_size, &wr_size, NULL); - if ((!s) || (buf_size != wr_size)) { + if (!WriteFileWithRetry(file_handle, buf, buf_size, &wr_size, WRITE_RETRIES)) { uprintf(" Error writing file %s: %s\n", dest_file, WindowsErrorString()); goto out; } @@ -989,8 +975,7 @@ try_iso: goto out; } buf_size = (DWORD)MIN(file_length, ISO_BLOCKSIZE); - s = WriteFile(file_handle, buf, buf_size, &wr_size, NULL); - if ((!s) || (buf_size != wr_size)) { + if (!WriteFileWithRetry(file_handle, buf, buf_size, &wr_size, WRITE_RETRIES)) { uprintf(" Error writing file %s: %s\n", dest_file, WindowsErrorString()); goto out; } diff --git a/src/rufus.c b/src/rufus.c index 3cd0987a..89364e91 100644 --- a/src/rufus.c +++ b/src/rufus.c @@ -2947,7 +2947,7 @@ 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) || (!WriteFile(hFile, loc_data, loc_size, &size, 0)) || (loc_size != size)) { + 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()); safe_closehandle(hFile); goto out; diff --git a/src/rufus.h b/src/rufus.h index b363c27d..2e4e1ff3 100644 --- a/src/rufus.h +++ b/src/rufus.h @@ -1,6 +1,6 @@ /* * Rufus: The Reliable USB Formatting Utility - * Copyright © 2011-2015 Pete Batard + * Copyright © 2011-2016 Pete Batard * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -457,6 +457,8 @@ extern int IsHDD(DWORD DriveIndex, uint16_t vid, uint16_t pid, const char* strid extern void LostTranslatorCheck(void); extern LONG ValidateSignature(HWND hDlg, const char* path); extern BOOL IsFontAvailable(const char* font_name); +extern BOOL WriteFileWithRetry(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, + LPDWORD lpNumberOfBytesWritten, DWORD nNumRetries); DWORD WINAPI FormatThread(void* param); DWORD WINAPI SaveImageThread(void* param); diff --git a/src/rufus.rc b/src/rufus.rc index 40525fd9..fec7df72 100644 --- a/src/rufus.rc +++ b/src/rufus.rc @@ -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.830" +CAPTION "Rufus 2.7.831" 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,830,0 - PRODUCTVERSION 2,7,830,0 + FILEVERSION 2,7,831,0 + PRODUCTVERSION 2,7,831,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.830" + VALUE "FileVersion", "2.7.831" 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.830" + VALUE "ProductVersion", "2.7.831" END END BLOCK "VarFileInfo" diff --git a/src/stdfn.c b/src/stdfn.c index cff6bd09..a075a438 100644 --- a/src/stdfn.c +++ b/src/stdfn.c @@ -1,7 +1,7 @@ /* * Rufus: The Reliable USB Formatting Utility * Standard Windows function calls - * Copyright © 2013-2015 Pete Batard + * Copyright © 2013-2016 Pete Batard * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -810,3 +810,38 @@ 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; +} diff --git a/src/syslinux.c b/src/syslinux.c index 337ee206..2ee20bb6 100644 --- a/src/syslinux.c +++ b/src/syslinux.c @@ -2,7 +2,7 @@ * * Copyright 2003 Lars Munch Christensen - All Rights Reserved * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved - * Copyright 2012-2015 Pete Batard + * Copyright 2012-2016 Pete Batard * * Based on the Linux installer program for SYSLINUX by H. Peter Anvin * @@ -137,16 +137,16 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) img_report.sl_version_ext, ldlinux, i==0?"sys":"bss"); fd = fopen(path, "rb"); if (fd == NULL) { - uprintf("Could not open %s\n", path); + uprintf("Could not open %s", path); goto out; } length = fread(syslinux_ldlinux[i], 1, (size_t)syslinux_ldlinux_len[i], fd); fclose(fd); if (length != (size_t)syslinux_ldlinux_len[i]) { - uprintf("Could not read %s\n", path); + uprintf("Could not read %s", path); goto out; } - uprintf("Using existing './%s'\n", path); + uprintf("Using existing './%s'", path); } } else { for (i=0; i<2; i++) { @@ -167,38 +167,36 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) FILE_ATTRIBUTE_HIDDEN, NULL); if (f_handle == INVALID_HANDLE_VALUE) { - uprintf("Unable to create '%s'\n", &path[3]); + uprintf("Unable to create '%s'", &path[3]); goto out; } /* Write ldlinux.sys file */ - if (!WriteFile(f_handle, (const char _force *)syslinux_ldlinux[0], - syslinux_ldlinux_len[0], &bytes_written, NULL) || - bytes_written != syslinux_ldlinux_len[0]) { - uprintf("Could not write '%s'\n", &path[3]); + if (!WriteFileWithRetry(f_handle, (const char _force *)syslinux_ldlinux[0], + syslinux_ldlinux_len[0], &bytes_written, WRITE_RETRIES)) { + uprintf("Could not write '%s': %s", &path[3], WindowsErrorString()); goto out; } - if (!WriteFile(f_handle, syslinux_adv, 2 * ADV_SIZE, - &bytes_written, NULL) || - bytes_written != 2 * ADV_SIZE) { - uprintf("Could not write ADV to '%s'\n", &path[3]); + if (!WriteFileWithRetry(f_handle, syslinux_adv, 2 * ADV_SIZE, + &bytes_written, WRITE_RETRIES)) { + uprintf("Could not write ADV to '%s': %s", &path[3], WindowsErrorString()); goto out; } - uprintf("Successfully wrote '%s'\n", &path[3]); + uprintf("Successfully wrote '%s'", &path[3]); if (bt != BT_ISO) UpdateProgress(OP_DOS, -1.0f); /* Now flush the media */ if (!FlushFileBuffers(f_handle)) { - uprintf("FlushFileBuffers failed\n"); + uprintf("FlushFileBuffers failed"); goto out; } /* Reopen the volume (we already have a lock) */ d_handle = GetLogicalHandle(drive_index, TRUE, FALSE); if (d_handle == INVALID_HANDLE_VALUE) { - uprintf("Could open volume for Syslinux installation\n"); + uprintf("Could open volume for Syslinux installation"); goto out; } @@ -242,7 +240,7 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) case FS_EXFAT: fs = libfat_open(libfat_readfile, (intptr_t) d_handle); if (fs == NULL) { - uprintf("Syslinux FAT access error\n"); + uprintf("Syslinux FAT access error"); goto out; } ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL); @@ -257,7 +255,7 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) libfat_close(fs); break; default: - uprintf("Unsupported Syslinux filesystem\n"); + uprintf("Unsupported Syslinux filesystem"); goto out; } @@ -269,9 +267,8 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) /* Rewrite the file */ if (SetFilePointer(f_handle, 0, NULL, FILE_BEGIN) != 0 || - !WriteFile(f_handle, syslinux_ldlinux[0], syslinux_ldlinux_len[0], - &bytes_written, NULL) - || bytes_written != syslinux_ldlinux_len[0]) { + !WriteFileWithRetry(f_handle, syslinux_ldlinux[0], syslinux_ldlinux_len[0], + &bytes_written, WRITE_RETRIES)) { uprintf("Could not write '%s': %s\n", &path[3], WindowsErrorString()); goto out; } @@ -282,9 +279,12 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) /* Read existing FAT data into boot sector */ if (SetFilePointer(d_handle, 0, NULL, FILE_BEGIN) != 0 || !ReadFile(d_handle, sectbuf, SECTOR_SIZE, - &bytes_read, NULL) - || bytes_read != SECTOR_SIZE) { - uprintf("Could not read boot record: %s\n", WindowsErrorString()); + &bytes_read, NULL)) { + uprintf("Could not read Syslinux boot record: %s", WindowsErrorString()); + goto out; + } + if (bytes_read < SECTOR_SIZE) { + uprintf("Partial read of Syslinux boot record: read %d bytes but requested %d", bytes_read, SECTOR_SIZE); goto out; } @@ -293,14 +293,12 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) /* Write boot sector back */ if (SetFilePointer(d_handle, 0, NULL, FILE_BEGIN) != 0 || - !WriteFile(d_handle, sectbuf, SECTOR_SIZE, - &bytes_written, NULL) - || bytes_written != SECTOR_SIZE) { - uprintf("Could not write Syslinux boot record: %s\n", WindowsErrorString()); + !WriteFileWithRetry(d_handle, sectbuf, SECTOR_SIZE, + &bytes_written, WRITE_RETRIES)) { + uprintf("Could not write Syslinux boot record: %s", WindowsErrorString()); goto out; } - - uprintf("Successfully wrote Syslinux boot record\n"); + uprintf("Successfully wrote Syslinux boot record"); if (bt == BT_SYSLINUX_V6) { IGNORE_RETVAL(_chdirU(app_dir)); @@ -309,17 +307,17 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) static_sprintf(path, "%C:\\%s.%s", drive_letter, ldlinux, ldlinux_ext[2]); fd = fopen(&path[3], "rb"); if (fd == NULL) { - uprintf("Caution: No '%s' was provided. The target will be missing a required Syslinux file!\n", &path[3]); + uprintf("Caution: No '%s' was provided. The target will be missing a required Syslinux file!", &path[3]); } else { fclose(fd); if (CopyFileA(&path[3], path, TRUE)) { uprintf("Created '%s' (from '%s/%s-%s/%s')", path, FILES_DIR, syslinux, embedded_sl_version_str[1], &path[3]); } else { - uprintf("Failed to create '%s': %s\n", path, WindowsErrorString()); + uprintf("Failed to create '%s': %s", path, WindowsErrorString()); } } } else if (IS_REACTOS(img_report)) { - uprintf("Setting up ReactOS...\n"); + uprintf("Setting up ReactOS..."); syslinux_mboot = GetResource(hMainInstance, MAKEINTRESOURCEA(IDR_SL_MBOOT_C32), _RT_RCDATA, "mboot.c32", &syslinux_mboot_len, FALSE); if (syslinux_mboot == NULL) { @@ -334,21 +332,20 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) uprintf("Unable to create '%s'\n", path); goto out; } - if (!WriteFile(f_handle, syslinux_mboot, syslinux_mboot_len, - &bytes_written, NULL) || - bytes_written != syslinux_mboot_len) { - uprintf("Could not write '%s'\n", path); + if (!WriteFileWithRetry(f_handle, syslinux_mboot, syslinux_mboot_len, + &bytes_written, WRITE_RETRIES)) { + uprintf("Could not write '%s'", path); goto out; } safe_closehandle(f_handle); static_sprintf(path, "%C:\\syslinux.cfg", drive_letter); fd = fopen(path, "w"); if (fd == NULL) { - uprintf("Could not create ReactOS 'syslinux.cfg'\n"); + uprintf("Could not create ReactOS 'syslinux.cfg'"); goto out; } /* Write the syslinux.cfg for ReactOS */ - fprintf(fd, "DEFAULT ReactOS\nLABEL ReactOS\n KERNEL %s\n APPEND %s\n", + fprintf(fd, "DEFAULT ReactOS\nLABEL ReactOS\n KERNEL %s\n APPEND %s", mboot_c32, img_report.reactos_path); fclose(fd); } diff --git a/src/vhd.c b/src/vhd.c index c3321de1..b73749a2 100644 --- a/src/vhd.c +++ b/src/vhd.c @@ -1,7 +1,7 @@ /* * Rufus: The Reliable USB Formatting Utility * Virtual Disk Handling functions - * Copyright © 2013-2015 Pete Batard + * Copyright © 2013-2016 Pete Batard * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -178,7 +178,7 @@ BOOL AppendVHDFooter(const char* vhd_path) heads = 16; cylinderTimesHeads = (uint32_t)(totalSectors / sectorsPerTrack); } else { - sectorsPerTrack = 17; + sectorsPerTrack = 17; cylinderTimesHeads = (uint32_t)(totalSectors / sectorsPerTrack); heads = (cylinderTimesHeads + 1023) / 1024; @@ -207,12 +207,12 @@ BOOL AppendVHDFooter(const char* vhd_path) checksum += ((uint8_t*)footer)[i]; footer->checksum = bswap_uint32(~checksum); - if (!WriteFile(handle, footer, sizeof(vhd_footer), &size, NULL) || (size != sizeof(vhd_footer))) { + if (!WriteFileWithRetry(handle, footer, sizeof(vhd_footer), &size, WRITE_RETRIES)) { uprintf("Could not write VHD footer: %s", WindowsErrorString()); goto out; } r = TRUE; - + out: safe_free(footer); safe_closehandle(handle);