mirror of
				https://github.com/pbatard/rufus.git
				synced 2024-08-14 23:57:05 +00:00 
			
		
		
		
	[ui] report errors to the user
* added StrErr() call * also improved WindowsErrorString() and Notification() calls
This commit is contained in:
		
							parent
							
								
									5a001f40f4
								
							
						
					
					
						commit
						5065c02d63
					
				
					 6 changed files with 102 additions and 29 deletions
				
			
		
							
								
								
									
										8
									
								
								format.c
									
										
									
									
									
								
							
							
						
						
									
										8
									
								
								format.c
									
										
									
									
									
								
							|  | @ -77,7 +77,7 @@ static BOOLEAN __stdcall FormatExCallback(FILE_SYSTEM_CALLBACK_COMMAND Command, | |||
| 		break; | ||||
| 	case FCC_INCOMPATIBLE_FILE_SYSTEM: | ||||
| 		uprintf("Incompatible File System\n"); | ||||
| 		FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_INCOMPATIBLE_FS; | ||||
| 		FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_INCOMPATIBLE_FS); | ||||
| 		break; | ||||
| 	case FCC_ACCESS_DENIED: | ||||
| 		uprintf("Access denied\n"); | ||||
|  | @ -93,7 +93,7 @@ static BOOLEAN __stdcall FormatExCallback(FILE_SYSTEM_CALLBACK_COMMAND Command, | |||
| 		break; | ||||
| 	case FCC_CANT_QUICK_FORMAT: | ||||
| 		uprintf("Cannot quick format this volume\n"); | ||||
| 		FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_CANT_QUICK_FORMAT; | ||||
| 		FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_CANT_QUICK_FORMAT); | ||||
| 		break; | ||||
| 	case FCC_BAD_LABEL: | ||||
| 		uprintf("Bad label\n"); | ||||
|  | @ -105,12 +105,12 @@ static BOOLEAN __stdcall FormatExCallback(FILE_SYSTEM_CALLBACK_COMMAND Command, | |||
| 	case FCC_CLUSTER_SIZE_TOO_BIG: | ||||
| 	case FCC_CLUSTER_SIZE_TOO_SMALL: | ||||
| 		uprintf("Unsupported cluster size\n"); | ||||
| 		FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_INVALID_CLUSTER_SIZE; | ||||
| 		FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_INVALID_CLUSTER_SIZE); | ||||
| 		break; | ||||
| 	case FCC_VOLUME_TOO_BIG: | ||||
| 	case FCC_VOLUME_TOO_SMALL: | ||||
| 		uprintf("Volume is too %s\n", FCC_VOLUME_TOO_BIG?"big":"small"); | ||||
| 		FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_INVALID_VOLUME_SIZE; | ||||
| 		FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_INVALID_VOLUME_SIZE); | ||||
| 	case FCC_NO_MEDIA_IN_DRIVE: | ||||
| 		uprintf("No media in drive\n"); | ||||
| 		FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_NO_MEDIA_IN_DRIVE; | ||||
|  |  | |||
							
								
								
									
										14
									
								
								rufus.c
									
										
									
									
									
								
							
							
						
						
									
										14
									
								
								rufus.c
									
										
									
									
									
								
							|  | @ -718,7 +718,7 @@ static INT_PTR CALLBACK MainCallback(HWND hDlg, UINT message, WPARAM wParam, LPA | |||
| 					format_thid = _beginthread(FormatThread, 0, (void*)(uintptr_t)DeviceNum); | ||||
| 					if (format_thid == -1L) { | ||||
| 						uprintf("Unable to start formatting thread"); | ||||
| 						FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_CANT_START_THREAD; | ||||
| 						FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_CANT_START_THREAD); | ||||
| 						PostMessage(hMainDialog, UM_FORMAT_COMPLETED, 0, 0); | ||||
| 					} | ||||
| 				} | ||||
|  | @ -755,9 +755,15 @@ static INT_PTR CALLBACK MainCallback(HWND hDlg, UINT message, WPARAM wParam, LPA | |||
| 		SendMessage(hProgress, PBM_SETPOS, FormatStatus?0:100, 0); | ||||
| 		EnableControls(TRUE); | ||||
| 		GetUSBDevices(); | ||||
| 		// TODO: process and report error code to user
 | ||||
| 		PrintStatus(!IS_ERROR(FormatStatus)?"DONE": | ||||
| 			((SCODE_CODE(FormatStatus)==ERROR_CANCELLED)?"Cancelled":"FAILED")); | ||||
| 		if (!IS_ERROR(FormatStatus)) { | ||||
| 			PrintStatus("DONE"); | ||||
| 		} else if (SCODE_CODE(FormatStatus) == ERROR_CANCELLED) { | ||||
| 			PrintStatus("Cancelled"); | ||||
| 			Notification(MSG_INFO, "Cancelled", "Operation cancelled by the user."); | ||||
| 		} else { | ||||
| 			PrintStatus("FAILED"); | ||||
| 			Notification(MSG_ERROR, "Error", "Error: %s", StrError(FormatStatus)); | ||||
| 		} | ||||
| 		return (INT_PTR)TRUE; | ||||
| 	} | ||||
| 	return (INT_PTR)FALSE; | ||||
|  |  | |||
							
								
								
									
										16
									
								
								rufus.h
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								rufus.h
									
										
									
									
									
								
							|  | @ -122,16 +122,17 @@ extern RUFUS_DRIVE_INFO SelectedDrive; | |||
| /*
 | ||||
|  * Shared prototypes | ||||
|  */ | ||||
| extern char *WindowsErrorString(void); | ||||
| extern const char *WindowsErrorString(void); | ||||
| extern void DumpBufferHex(void *buf, size_t size); | ||||
| extern void PrintStatus(const char *format, ...); | ||||
| extern const char* StrError(DWORD error_code); | ||||
| extern void CenterDialog(HWND hDlg); | ||||
| extern void CreateStatusBar(void); | ||||
| extern INT_PTR CreateAboutBox(void); | ||||
| extern HWND CreateTooltip(HWND hControl, const char* message, int duration); | ||||
| extern void DestroyTooltip(HWND hWnd); | ||||
| extern void DestroyAllTooltips(void); | ||||
| extern void Notification(int type, char* text, char* title); | ||||
| extern BOOL Notification(int type, char* title, char* format, ...); | ||||
| extern BOOL ExtractMSDOS(const char* path); | ||||
| extern void __cdecl FormatThread(void* param); | ||||
| extern BOOL CreatePartition(HANDLE hDrive); | ||||
|  | @ -176,8 +177,9 @@ typedef struct { | |||
| 
 | ||||
| /* Custom application errors */ | ||||
| #define FAC(f)                         (f<<16) | ||||
| #define ERROR_INCOMPATIBLE_FS          (APPLICATION_ERROR_MASK|0x1201) | ||||
| #define ERROR_CANT_QUICK_FORMAT        (APPLICATION_ERROR_MASK|0x1202) | ||||
| #define ERROR_INVALID_CLUSTER_SIZE     (APPLICATION_ERROR_MASK|0x1203) | ||||
| #define ERROR_INVALID_VOLUME_SIZE      (APPLICATION_ERROR_MASK|0x1204) | ||||
| #define ERROR_CANT_START_THREAD        (APPLICATION_ERROR_MASK|0x1205) | ||||
| #define APPERR(err)                    (APPLICATION_ERROR_MASK|err) | ||||
| #define ERROR_INCOMPATIBLE_FS          0x1201 | ||||
| #define ERROR_CANT_QUICK_FORMAT        0x1202 | ||||
| #define ERROR_INVALID_CLUSTER_SIZE     0x1203 | ||||
| #define ERROR_INVALID_VOLUME_SIZE      0x1204 | ||||
| #define ERROR_CANT_START_THREAD        0x1205 | ||||
|  |  | |||
							
								
								
									
										12
									
								
								rufus.rc
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								rufus.rc
									
										
									
									
									
								
							|  | @ -30,7 +30,7 @@ LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL | |||
| IDD_DIALOG DIALOGEX 12, 12, 206, 263 | ||||
| STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU | ||||
| EXSTYLE WS_EX_APPWINDOW | ||||
| CAPTION "Rufus v1.0.0.65" | ||||
| CAPTION "Rufus v1.0.0.66" | ||||
| FONT 8, "MS Shell Dlg", 400, 0, 0x1 | ||||
| BEGIN | ||||
|     DEFPUSHBUTTON   "Start",IDC_START,94,223,50,14 | ||||
|  | @ -63,7 +63,7 @@ BEGIN | |||
|     DEFPUSHBUTTON   "OK",IDOK,231,175,50,14,WS_GROUP | ||||
|     CONTROL         "<a href=""https://github.com/pbatard/rufus/wiki/Rufus"">https://github.com/pbatard/rufus</a>",IDC_ABOUT_RUFUS_URL, | ||||
|                     "SysLink",WS_TABSTOP,46,47,114,9 | ||||
|     LTEXT           "Version 1.0.0 (Build 65)",IDC_STATIC,46,19,78,8 | ||||
|     LTEXT           "Version 1.0.0 (Build 66)",IDC_STATIC,46,19,78,8 | ||||
|     PUSHBUTTON      "License...",IDC_ABOUT_LICENSE,46,175,50,14,WS_GROUP | ||||
|     EDITTEXT        IDC_ABOUT_COPYRIGHTS,46,107,235,63,ES_MULTILINE | ES_READONLY | WS_VSCROLL | ||||
|     LTEXT           "Report bugs or request enhancements at:",IDC_STATIC,46,66,187,8 | ||||
|  | @ -162,8 +162,8 @@ END | |||
| // | ||||
| 
 | ||||
| VS_VERSION_INFO VERSIONINFO | ||||
|  FILEVERSION 1,0,0,65 | ||||
|  PRODUCTVERSION 1,0,0,65 | ||||
|  FILEVERSION 1,0,0,66 | ||||
|  PRODUCTVERSION 1,0,0,66 | ||||
|  FILEFLAGSMASK 0x3fL | ||||
| #ifdef _DEBUG | ||||
|  FILEFLAGS 0x1L | ||||
|  | @ -180,13 +180,13 @@ BEGIN | |||
|         BEGIN | ||||
|             VALUE "CompanyName", "akeo.ie" | ||||
|             VALUE "FileDescription", "Rufus" | ||||
|             VALUE "FileVersion", "1.0.0.65" | ||||
|             VALUE "FileVersion", "1.0.0.66" | ||||
|             VALUE "InternalName", "Rufus" | ||||
|             VALUE "LegalCopyright", "© 2011 Pete Batard (GPL v3)" | ||||
|             VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html" | ||||
|             VALUE "OriginalFilename", "rufus.exe" | ||||
|             VALUE "ProductName", "Rufus" | ||||
|             VALUE "ProductVersion", "1.0.0.65" | ||||
|             VALUE "ProductVersion", "1.0.0.66" | ||||
|         END | ||||
|     END | ||||
|     BLOCK "VarFileInfo" | ||||
|  |  | |||
							
								
								
									
										67
									
								
								stdio.c
									
										
									
									
									
								
							
							
						
						
									
										67
									
								
								stdio.c
									
										
									
									
									
								
							|  | @ -97,7 +97,7 @@ void DumpBufferHex(void *buf, size_t size) | |||
|  * Convert a windows error to human readable string | ||||
|  * uses retval as errorcode, or, if 0, use GetLastError() | ||||
|  */ | ||||
| char *WindowsErrorString(void) | ||||
| const char *WindowsErrorString(void) | ||||
| { | ||||
| static char err_string[256]; | ||||
| 
 | ||||
|  | @ -106,18 +106,18 @@ static char err_string[256]; | |||
| 
 | ||||
| 	error_code = GetLastError(); | ||||
| 
 | ||||
| 	safe_sprintf(err_string, sizeof(err_string), "[%d] ", error_code); | ||||
| 	safe_sprintf(err_string, sizeof(err_string), "[0x%08X] ", error_code); | ||||
| 
 | ||||
| 	size = FormatMessageU(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, | ||||
| 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &err_string[strlen(err_string)], | ||||
| 		sizeof(err_string)-(DWORD)strlen(err_string), NULL); | ||||
| 	if (size == 0) { | ||||
| 		format_error = GetLastError(); | ||||
| 		if (format_error) | ||||
| 		if ((format_error) && (format_error != 0x13D))		// 0x13D, decode error, is returned for unknown codes
 | ||||
| 			safe_sprintf(err_string, sizeof(err_string), | ||||
| 				"Windows error code %u (FormatMessage error code %u)", error_code, format_error); | ||||
| 				"Windows error code 0x%08X (FormatMessage error code 0x%08X)", error_code, format_error); | ||||
| 		else | ||||
| 			safe_sprintf(err_string, sizeof(err_string), "Unknown error code %u", error_code); | ||||
| 			safe_sprintf(err_string, sizeof(err_string), "Unknown error 0x%08X", error_code); | ||||
| 	} | ||||
| 	return err_string; | ||||
| } | ||||
|  | @ -141,3 +141,60 @@ void PrintStatus(const char *format, ...) | |||
| 
 | ||||
| 	SetDlgItemTextU(hMainDialog, IDC_STATUS, buf); | ||||
| } | ||||
| 
 | ||||
| const char* StrError(DWORD error_code) | ||||
| { | ||||
| 	if ( (!IS_ERROR(error_code)) || (SCODE_CODE(error_code) == ERROR_SUCCESS)) { | ||||
| 		return "Success"; | ||||
| 	} | ||||
| 	if (SCODE_FACILITY(error_code) != FACILITY_STORAGE) { | ||||
| 		uprintf("StrError: non storage - %08X (%X)\n", error_code, SCODE_FACILITY(error_code)); | ||||
| 		SetLastError(error_code); | ||||
| 		return WindowsErrorString(); | ||||
| 	} | ||||
| 	switch (SCODE_CODE(error_code)) { | ||||
| 	case ERROR_GEN_FAILURE: | ||||
| 		return "Undetermined error while formatting"; | ||||
| 	case ERROR_INCOMPATIBLE_FS: | ||||
| 		return "Cannot use the selected file system for this media"; | ||||
| 	case ERROR_ACCESS_DENIED: | ||||
| 		return "Access to the media is denied"; | ||||
| 	case ERROR_WRITE_PROTECT: | ||||
| 		return "Media is write protected"; | ||||
| 	case ERROR_DEVICE_IN_USE: | ||||
| 		return "The device is in use by another process\n" | ||||
| 			"Please close any other process that may be accessing the device"; | ||||
| 	case ERROR_CANT_QUICK_FORMAT: | ||||
| 		return "Quick format is not available for this device"; | ||||
| 	case ERROR_LABEL_TOO_LONG: | ||||
| 		return "The volume label is invalid"; | ||||
| 	case ERROR_INVALID_CLUSTER_SIZE: | ||||
| 		return "The selected allocation unit size is not valid for this device"; | ||||
| 	case ERROR_INVALID_VOLUME_SIZE: | ||||
| 		return "The volume size is invalid"; | ||||
| 	case ERROR_NO_MEDIA_IN_DRIVE: | ||||
| 		return "Please insert a media in drive"; | ||||
| 	case ERROR_NOT_SUPPORTED: | ||||
| 		return "An unsupported command was received"; | ||||
| 	case ERROR_NOT_ENOUGH_MEMORY: | ||||
| 		return "Memory allocation error"; | ||||
| 	case ERROR_READ_FAULT: | ||||
| 		return "Read error"; | ||||
| 	case ERROR_WRITE_FAULT: | ||||
| 		return "Write error"; | ||||
| 	case ERROR_OPEN_FAILED: | ||||
| 		return "Could not open media"; | ||||
| 	case ERROR_PARTITION_FAILURE: | ||||
| 		return "Error while partitioning drive"; | ||||
| 	case ERROR_CANNOT_COPY: | ||||
| 		return "Could not copy MS-DOS files"; | ||||
| 	case ERROR_CANCELLED: | ||||
| 		return "Cancelled by user"; | ||||
| 	case ERROR_CANT_START_THREAD: | ||||
| 		return "Unable to create formatting thread"; | ||||
| 	default: | ||||
| 		uprintf("StrError: hit default - %08X\n", error_code); | ||||
| 		SetLastError(error_code); | ||||
| 		return WindowsErrorString(); | ||||
| 	} | ||||
| } | ||||
|  |  | |||
							
								
								
									
										14
									
								
								stdlg.c
									
										
									
									
									
								
							
							
						
						
									
										14
									
								
								stdlg.c
									
										
									
									
									
								
							|  | @ -512,10 +512,17 @@ INT_PTR CALLBACK NotificationCallback(HWND hDlg, UINT message, WPARAM wParam, LP | |||
| /*
 | ||||
|  * Display a custom notification | ||||
|  */ | ||||
| void Notification(int type, char* text, char* title) | ||||
| BOOL Notification(int type, char* title, char* format, ...) | ||||
| { | ||||
| 	szMessageText = text; | ||||
| 	va_list args; | ||||
| 	szMessageText = (char*)malloc(MAX_PATH); | ||||
| 	if (szMessageText == NULL) return FALSE; | ||||
| 	szMessageTitle = title; | ||||
| 	va_start(args, format); | ||||
| 	safe_vsnprintf(szMessageText, MAX_PATH-1, format, args); | ||||
| 	va_end(args); | ||||
| 	szMessageText[MAX_PATH-1] = 0; | ||||
| 
 | ||||
| 	switch(type) { | ||||
| 	case MSG_WARNING: | ||||
| 		hMessageIcon = LoadIcon(NULL, IDI_WARNING); | ||||
|  | @ -529,7 +536,8 @@ void Notification(int type, char* text, char* title) | |||
| 		break; | ||||
| 	} | ||||
| 	DialogBox(hMainInstance, MAKEINTRESOURCE(IDD_NOTIFICATION), hMainDialog, NotificationCallback); | ||||
| 	szMessageText = NULL; | ||||
| 	safe_free(szMessageText); | ||||
| 	return TRUE; | ||||
| } | ||||
| 
 | ||||
| struct { | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue