Skip to content

Commit

Permalink
[misc] improve readability of error code handling
Browse files Browse the repository at this point in the history
* Also don't pass a read size value in WriteFileWithRetry() if we don't use it.
  • Loading branch information
pbatard committed Apr 1, 2024
1 parent 52ca798 commit 8a5a5a3
Show file tree
Hide file tree
Showing 25 changed files with 257 additions and 252 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
*.a
*.aps
*.appx
*.appxupload
*.bak
*.db
*.db-shm
*.db-wal
*.dep
*.diff
*.dll
*.efi
*.exe
*.htm
*.idb
*.ipch
*.la
*.lib
*.lo
*.lock
*.log
*.mac
*.mo
*.mp4
*.ncb
*.nope
*.o
*.obj
*.old
*.opendb
*.opt
*.org
*.p7x
*.patch
*.pc
*.pdb
*.pdf
*.plg
*.res
*.sig
Expand Down
4 changes: 2 additions & 2 deletions src/badblocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright 1992-1994 Remy Card <[email protected]>
* Copyright 1995-1999 Theodore Ts'o
* Copyright 1999 David Beattie
* Copyright 2011-2023 Pete Batard <[email protected]>
* Copyright 2011-2024 Pete Batard <[email protected]>
*
* This file is based on the minix file system programs fsck and mkfs
* written and copyrighted by Linus Torvalds <[email protected]>
Expand Down Expand Up @@ -318,7 +318,7 @@ static void CALLBACK alarm_intr(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dw
{
if (!num_blocks)
return;
if (FormatStatus) {
if (ErrorStatus) {
uprintf("%sInterrupting at block %" PRIu64 "\n", bb_prefix,
(unsigned long long) currently_testing);
cancel_ops = -1;
Expand Down
11 changes: 5 additions & 6 deletions src/badblocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Copyright 1995, 1996, 1997, 1998, 1999 by Theodore Ts'o
* Copyright 1999 by David Beattie
* Copyright 2011-2018 by Pete Batard
* Copyright 2011-2024 by Pete Batard
*
* This file is based on the minix file system programs fsck and mkfs
* written and copyrighted by Linus Torvalds <[email protected]>
Expand All @@ -28,12 +28,11 @@ typedef struct bb_struct_u64_iterate *bb_badblocks_iterate;
typedef struct bb_struct_u64_list *bb_u64_list;
typedef struct bb_struct_u64_iterate *bb_u64_iterate;

#define BB_ET_NO_MEMORY (ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_NOT_ENOUGH_MEMORY)
#define BB_ET_MAGIC_BADBLOCKS_LIST (ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OBJECT_IN_LIST)
#define BB_ET_MAGIC_BADBLOCKS_ITERATE (ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_INVALID_BLOCK)
#define BB_ET_NO_MEMORY RUFUS_ERROR(ERROR_NOT_ENOUGH_MEMORY)
#define BB_ET_MAGIC_BADBLOCKS_LIST RUFUS_ERROR(ERROR_OBJECT_IN_LIST)
#define BB_ET_MAGIC_BADBLOCKS_ITERATE RUFUS_ERROR(ERROR_INVALID_BLOCK)

#define BB_CHECK_MAGIC(struct, code) \
if ((struct)->magic != (code)) return (code)
#define BB_CHECK_MAGIC(struct, code) if ((struct)->magic != (code)) return (code)
#define BB_BAD_BLOCKS_THRESHOLD 256
#define BB_BLOCKS_AT_ONCE 64
#define BB_SYS_PAGE_SIZE 4096
Expand Down
10 changes: 5 additions & 5 deletions src/dos.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* 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-2023 Pete Batard <[email protected]>
* Copyright © 2011-2024 Pete Batard <[email protected]>
*
* 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
Expand Down Expand Up @@ -352,7 +352,7 @@ BOOL ExtractFreeDOS(const char* path)
IDR_FD_EGA17_CPX, IDR_FD_EGA18_CPX };
char filename[MAX_PATH], locale_path[MAX_PATH];
BYTE* res_data;
DWORD res_size, Size;
DWORD res_size;
HANDLE hFile;
int i;

Expand All @@ -366,10 +366,10 @@ BOOL ExtractFreeDOS(const char* path)
static_strcat(locale_path, "LOCALE\\");
CreateDirectoryA(locale_path, NULL);

for (i=0; i<ARRAYSIZE(res_name); i++) {
for (i = 0; i < ARRAYSIZE(res_name); i++) {
res_data = (BYTE*)GetResource(hMainInstance, MAKEINTRESOURCEA(res_id[i]), _RT_RCDATA, res_name[i], &res_size, FALSE);

static_strcpy(filename, ((i<2)?path:locale_path));
static_strcpy(filename, ((i<2) ? path : locale_path));
static_strcat(filename, res_name[i]);

hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL,
Expand All @@ -379,7 +379,7 @@ BOOL ExtractFreeDOS(const char* path)
return FALSE;
}

if (!WriteFileWithRetry(hFile, res_data, res_size, &Size, WRITE_RETRIES)) {
if (!WriteFileWithRetry(hFile, res_data, res_size, NULL, WRITE_RETRIES)) {
uprintf("Could not write file '%s': %s.", filename, WindowsErrorString());
safe_closehandle(hFile);
return FALSE;
Expand Down
10 changes: 5 additions & 5 deletions src/drive.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ static HANDLE GetHandle(char* Path, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWr
do {
if (DeviceIoControl(hDrive, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &size, NULL))
goto out;
if (IS_ERROR(FormatStatus)) // User cancel
if (IS_ERROR(ErrorStatus)) // User cancel
break;
Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES);
} while (GetTickCount64() < EndTime);
// If we reached this section, either we didn't manage to get a lock or the user cancelled
uprintf("Could not lock access to %s: %s", Path, WindowsErrorString());
// See if we can report the processes are accessing the drive
if (!IS_ERROR(FormatStatus) && (access_mask == 0))
if (!IS_ERROR(ErrorStatus) && (access_mask == 0))
access_mask = GetProcessSearch(SEARCH_PROCESS_TIMEOUT, 0x07, FALSE);
// Try to continue if the only access rights we saw were for read-only
if ((access_mask & 0x07) != 0x01)
Expand Down Expand Up @@ -1006,7 +1006,7 @@ BOOL WaitForLogical(DWORD DriveIndex, uint64_t PartitionOffset)
return TRUE;
}
free(LogicalPath);
if (IS_ERROR(FormatStatus)) // User cancel
if (IS_ERROR(ErrorStatus)) // User cancel
return FALSE;
Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES);
} while (GetTickCount64() < EndTime);
Expand Down Expand Up @@ -2219,7 +2219,7 @@ BOOL RemountVolume(char* drive_name, BOOL bSilent)
} else {
suprintf("Could not remount %s as %c: %s", volume_name, toupper(drive_name[0]), WindowsErrorString());
// This will leave the drive inaccessible and must be flagged as an error
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_CANT_REMOUNT_VOLUME);
ErrorStatus = RUFUS_ERROR(APPERR(ERROR_CANT_REMOUNT_VOLUME));
return FALSE;
}
}
Expand Down Expand Up @@ -2493,7 +2493,7 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m
uprintf(" Could not access source image");
return FALSE;
}
if(!WriteFileWithRetry(hDrive, buffer, bufsize, &size, WRITE_RETRIES)) {
if(!WriteFileWithRetry(hDrive, buffer, bufsize, NULL, WRITE_RETRIES)) {
uprintf(" Write error: %s", WindowsErrorString());
return FALSE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/drive.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#define VDS_RESCAN_REFRESH 0x00000001
#define VDS_RESCAN_REENUMERATE 0x00000002

#define VDS_SET_ERROR(hr) do { if (hr != S_OK) { SetLastError((DWORD)hr); FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_GEN_FAILURE; } } while(0)
#define VDS_SET_ERROR(hr) do { if (hr != S_OK) { SetLastError((DWORD)hr); ErrorStatus = RUFUS_ERROR(ERROR_GEN_FAILURE); } } while(0)

#if !defined(__MINGW32__)
typedef enum _FSINFOCLASS {
Expand Down
4 changes: 2 additions & 2 deletions src/ext2fs/nt_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
* Copyright (C) 1998 Andrey Shedel <[email protected]>
* Copyright (C) 2018-2019 Pete Batard <[email protected]>
* Copyright (C) 2018-2024 Pete Batard <[email protected]>
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Library
Expand Down Expand Up @@ -180,7 +180,7 @@ static __inline unsigned _MapNtStatus(IN NTSTATUS Status)
// Return the last Windows Error
DWORD ext2_last_winerror(DWORD default_error)
{
return ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | (LastWinError ? LastWinError : default_error);
return RUFUS_ERROR(LastWinError ? LastWinError : default_error);
}

//
Expand Down
Loading

0 comments on commit 8a5a5a3

Please sign in to comment.