From a85aa08276898bfebf0c4f53e0ed2080e65e3f1d Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Thu, 4 Apr 2013 19:30:24 +0200 Subject: [PATCH] Prints also the Windows GetLastError() in strerror(). --- handle.c | 3 +-- mingw.c | 32 ++++++++++++++++++++++++++++++++ mingw.h | 6 ++++++ parity.c | 2 +- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/handle.c b/handle.c index 80dd215b..0ba280d7 100644 --- a/handle.c +++ b/handle.c @@ -220,7 +220,7 @@ int handle_read(struct snapraid_handle* handle, struct snapraid_block* block, un #endif if (read_ret < 0) { - fprintf(out, "Error reading file '%s' at offset %"PRIu64". %s.\n", handle->path, offset, strerror(errno)); + fprintf(out, "Error reading file '%s' at offset %"PRIu64" for size %u. %s.\n", handle->path, offset + count, read_size - count, strerror(errno)); return -1; } if (read_ret == 0) { @@ -230,7 +230,6 @@ int handle_read(struct snapraid_handle* handle, struct snapraid_block* block, un count += read_ret; } while (count < read_size); - /* pad with 0 */ if (read_size < block_size) { diff --git a/mingw.c b/mingw.c index 725ce324..988fed82 100644 --- a/mingw.c +++ b/mingw.c @@ -17,6 +17,8 @@ #include "portable.h" +#include "util.h" + #ifdef __MINGW32__ /* Only for MingW */ /* Adds missing Windows declaration */ @@ -64,6 +66,11 @@ static BOOL (WINAPI* ptr_GetFileInformationByHandleEx)(HANDLE, DWORD, LPVOID, DW */ static BOOLEAN (WINAPI* ptr_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); +/** + * Description of the last error. + */ +static char* last_error; + void os_init(void) { HMODULE kernel32 = GetModuleHandle("KERNEL32.DLL"); @@ -85,10 +92,14 @@ void os_init(void) ptr_SetThreadExecutionState(WIN32_ES_CONTINUOUS | WIN32_ES_SYSTEM_REQUIRED); } } + + last_error = 0; } void os_done(void) { + free(last_error); + /* restore the normal execution level */ if (ptr_SetThreadExecutionState) { ptr_SetThreadExecutionState(WIN32_ES_CONTINUOUS); @@ -1037,5 +1048,26 @@ int devuuid(uint64_t device, char* uuid, size_t uuid_size) return 0; } +#undef strerror + +const char* windows_strerror(int err) +{ + /* get the normal C error from the specified err */ + const char* str = strerror(err); + size_t len = strlen(str); + + /* adds space for GetLastError() */ + len += 32; + + /* reallocate */ + free(last_error); + last_error = malloc(len); + if (!last_error) + return str; + + snprintf(last_error, len, "%s [%u]", str, (unsigned)GetLastError()); + return last_error; +} + #endif diff --git a/mingw.h b/mingw.h index 94640dc2..3726d11c 100644 --- a/mingw.h +++ b/mingw.h @@ -68,6 +68,7 @@ #define readlink windows_readlink #define symlink windows_symlink #define link windows_link +#define strerror windows_strerror /* We have nano second support */ #define STAT_NSEC(st) (st)->st_mtimensec @@ -250,6 +251,11 @@ int windows_symlink(const char* existing, const char* file); */ int windows_link(const char* existing, const char* file); +/** + * Like strerror(). + */ +const char* windows_strerror(int err); + #endif #endif diff --git a/parity.c b/parity.c index adf64475..55c3781a 100644 --- a/parity.c +++ b/parity.c @@ -393,7 +393,7 @@ int parity_read(struct snapraid_parity* parity, block_off_t pos, unsigned char* read_ret = read(parity->f, block_buffer + count, block_size - count); #endif if (read_ret < 0) { - fprintf(out, "Error reading file '%s' at offset %"PRIu64". %s.\n", parity->path, offset, strerror(errno)); + fprintf(out, "Error reading file '%s' at offset %"PRIu64" for size %u. %s.\n", parity->path, offset + count, block_size - count, strerror(errno)); return -1; } if (read_ret == 0) {