From 8bff2715bfdfd2e6ce0c707f1310b169d4e454d0 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 15 Jun 2023 18:13:26 +0200 Subject: libcompat: Add a helper to get/set OS error state On Unix like OSes, this saves/restores errno, on Windows both errno and GetLastError state are saved/restored. This should make it simpler to preserve that across function calls. Additionally, while tracking down uses of GetLastError, some places where the error code was printed out directly where replaced with instances of w32_perror. Signed-off-by: David Oberhollenzer --- lib/sqfs/src/win32/io_file.c | 15 +++++++++------ lib/sqfs/src/win32/istream.c | 20 ++++++++++---------- lib/sqfs/src/win32/ostream.c | 12 ++++++------ 3 files changed, 25 insertions(+), 22 deletions(-) (limited to 'lib/sqfs/src') diff --git a/lib/sqfs/src/win32/io_file.c b/lib/sqfs/src/win32/io_file.c index dbd80d8..4d76b94 100644 --- a/lib/sqfs/src/win32/io_file.c +++ b/lib/sqfs/src/win32/io_file.c @@ -60,7 +60,9 @@ static sqfs_object_t *stdio_copy(const sqfs_object_t *base) 0, FALSE, DUPLICATE_SAME_ACCESS); if (!ret) { + os_error_t err = get_os_error_state(); free(copy); + set_os_error_state(err); return NULL; } @@ -163,7 +165,8 @@ int sqfs_open_native_file(sqfs_file_handle_t *out, const char *filename, { int access_flags, creation_mode, share_mode; WCHAR *wpath = NULL; - DWORD length, err; + os_error_t err; + DWORD length; *out = INVALID_HANDLE_VALUE; @@ -209,9 +212,9 @@ int sqfs_open_native_file(sqfs_file_handle_t *out, const char *filename, creation_mode, FILE_ATTRIBUTE_NORMAL, NULL); - err = GetLastError(); + err = get_os_error_state(); free(wpath); - SetLastError(err); + set_os_error_state(err); } return (*out == INVALID_HANDLE_VALUE) ? SQFS_ERROR_IO : 0; @@ -223,7 +226,7 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) size_t namelen, total; LARGE_INTEGER size; sqfs_file_t *base; - DWORD err; + os_error_t err; namelen = strlen(filename); total = sizeof(*file) + 1; @@ -259,10 +262,10 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) base->get_filename = stdio_get_filename; return base; fail: - err = GetLastError(); + err = get_os_error_state(); if (file->fd != INVALID_HANDLE_VALUE) CloseHandle(file->fd); free(file); - SetLastError(err); + set_os_error_state(err); return NULL; } diff --git a/lib/sqfs/src/win32/istream.c b/lib/sqfs/src/win32/istream.c index 3f7eb83..aeb3352 100644 --- a/lib/sqfs/src/win32/istream.c +++ b/lib/sqfs/src/win32/istream.c @@ -49,15 +49,15 @@ static int precache(sqfs_istream_t *strm) if (!ReadFile(file->hnd, file->buffer + file->buffer_used, diff, &actual, NULL)) { - DWORD error = GetLastError(); + os_error_t error = get_os_error_state(); - if (error == ERROR_HANDLE_EOF || - error == ERROR_BROKEN_PIPE) { + if (error.w32_errno == ERROR_HANDLE_EOF || + error.w32_errno == ERROR_BROKEN_PIPE) { file->eof = true; break; } - SetLastError(error); + set_os_error_state(error); return SQFS_ERROR_IO; } @@ -136,9 +136,9 @@ int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, file->path = strdup(path); if (file->path == NULL) { - DWORD temp = GetLastError(); + os_error_t err = get_os_error_state(); free(file); - SetLastError(temp); + set_os_error_state(err); return SQFS_ERROR_ALLOC; } @@ -146,10 +146,10 @@ int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, GetCurrentProcess(), &file->hnd, 0, FALSE, DUPLICATE_SAME_ACCESS); if (!ret) { - DWORD temp = GetLastError(); + os_error_t err = get_os_error_state(); free(file->path); free(file); - SetLastError(temp); + set_os_error_state(err); return SQFS_ERROR_IO; } @@ -180,9 +180,9 @@ int sqfs_istream_open_file(sqfs_istream_t **out, const char *path, ret = sqfs_istream_open_handle(out, path, hnd, flags); if (ret) { - DWORD temp = GetLastError(); + os_error_t err = get_os_error_state(); CloseHandle(hnd); - SetLastError(temp); + set_os_error_state(err); return ret; } diff --git a/lib/sqfs/src/win32/ostream.c b/lib/sqfs/src/win32/ostream.c index 3ebeb8b..2f85c29 100644 --- a/lib/sqfs/src/win32/ostream.c +++ b/lib/sqfs/src/win32/ostream.c @@ -59,9 +59,9 @@ static int realize_sparse(file_ostream_t *file) ret = write_data(file, buffer, diff); if (ret) { - DWORD temp = GetLastError(); + os_error_t err = get_os_error_state(); free(buffer); - SetLastError(temp); + set_os_error_state(err); return ret; } @@ -157,10 +157,10 @@ int sqfs_ostream_open_handle(sqfs_ostream_t **out, const char *path, GetCurrentProcess(), &file->hnd, 0, FALSE, DUPLICATE_SAME_ACCESS); if (!ret) { - DWORD temp = GetLastError(); + os_error_t err = get_os_error_state(); free(file->path); free(file); - SetLastError(temp); + set_os_error_state(err); return SQFS_ERROR_IO; } @@ -191,9 +191,9 @@ int sqfs_ostream_open_file(sqfs_ostream_t **out, const char *path, ret = sqfs_ostream_open_handle(out, path, hnd, flags); if (ret) { - DWORD temp = GetLastError(); + os_error_t err = get_os_error_state(); CloseHandle(hnd); - SetLastError(temp); + set_os_error_state(err); return SQFS_ERROR_IO; } -- cgit v1.2.3