diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-13 23:44:19 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-15 14:09:56 +0200 |
commit | fd5c9f1259d0191af57b20f06dda35e62acb6275 (patch) | |
tree | e45b73872c40531c5c2fa9c3b07096e5827ac6ea /lib/common | |
parent | 89cdef0859259fdea0165b0d3918777d1ed42955 (diff) |
Overhaul sqfs_istream_t/sqfs_ostream_t error handling
Report an error number from the implementations, change the users to
forward that error number (which also means libtar write header/link
now returns an error code) and all subsequent binaries to use
sqfs_perror() instead of relying on the function to print an error
internally.
Also, make sure to preserve errno/GetLastError() in the implementations
and print out a stringified error in sqfs_perror() if the error code
indicates an I/O error.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/common')
-rw-r--r-- | lib/common/src/data_reader_dump.c | 14 | ||||
-rw-r--r-- | lib/common/src/perror.c | 33 |
2 files changed, 41 insertions, 6 deletions
diff --git a/lib/common/src/data_reader_dump.c b/lib/common/src/data_reader_dump.c index e3cbb87..920b2bd 100644 --- a/lib/common/src/data_reader_dump.c +++ b/lib/common/src/data_reader_dump.c @@ -26,8 +26,7 @@ int sqfs_data_reader_dump(const char *name, sqfs_data_reader_t *data, diff = (filesz < block_size) ? filesz : block_size; if (SQFS_IS_SPARSE_BLOCK(inode->extra[i])) { - if (fp->append(fp, NULL, diff)) - return -1; + err = fp->append(fp, NULL, diff); } else { err = sqfs_data_reader_get_block(data, inode, i, &chunk_size, &chunk); @@ -38,11 +37,11 @@ int sqfs_data_reader_dump(const char *name, sqfs_data_reader_t *data, err = fp->append(fp, chunk, chunk_size); free(chunk); - - if (err) - return -1; } + if (err) + goto fail_io; + filesz -= diff; } @@ -58,8 +57,11 @@ int sqfs_data_reader_dump(const char *name, sqfs_data_reader_t *data, free(chunk); if (err) - return -1; + goto fail_io; } return 0; +fail_io: + sqfs_perror(fp->get_filename(fp), "writing data block", err); + return -1; } diff --git a/lib/common/src/perror.c b/lib/common/src/perror.c index 53a8c16..5f62748 100644 --- a/lib/common/src/perror.c +++ b/lib/common/src/perror.c @@ -8,9 +8,22 @@ #include <stdio.h> +#if defined(_WIN32) || defined(__WINDOWS__) +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#else +#include <errno.h> +#include <string.h> +#endif + void sqfs_perror(const char *file, const char *action, int error_code) { const char *errstr; +#if defined(_WIN32) || defined(__WINDOWS__) + DWORD syserror = GetLastError(); +#else + int syserror = errno; +#endif switch (error_code) { case SQFS_ERROR_ALLOC: @@ -76,4 +89,24 @@ void sqfs_perror(const char *file, const char *action, int error_code) fprintf(stderr, "%s: ", action); fprintf(stderr, "%s.\n", errstr); + + if (error_code == SQFS_ERROR_IO) { +#if defined(_WIN32) || defined(__WINDOWS__) + LPVOID msg = NULL; + + FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, syserror, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)&msg, 0, NULL); + + fprintf(stderr, "OS error: %s\n", (const char *)msg); + + if (msg != NULL) + LocalFree(msg); +#else + fprintf(stderr, "OS error: %s\n", strerror(syserror)); +#endif + } } |