From 0f3021baccf129a740cadcbc56d72964bbd99a84 Mon Sep 17 00:00:00 2001 From: Jonathan Dieter Date: Tue, 24 Jul 2018 12:14:26 +0100 Subject: [PATCH] Add error handling functions Signed-off-by: Jonathan Dieter --- include/zck.h.in | 14 +++++++++- src/lib/error.c | 65 +++++++++++++++++++++++++++++++++++++++++++ src/lib/log.c | 9 ++++-- src/lib/meson.build | 2 +- src/lib/zck.c | 8 ++++++ src/lib/zck_private.h | 9 +++++- 6 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 src/lib/error.c diff --git a/include/zck.h.in b/include/zck.h.in index dfd818f..6f6e4c7 100644 --- a/include/zck.h.in +++ b/include/zck.h.in @@ -105,10 +105,22 @@ int zck_set_ioption(zckCtx *zck, zck_ioption option, ssize_t value) /******************************************************************* - * Miscellaneous utilities + * Error handling *******************************************************************/ /* Set logging level */ void zck_set_log_level(zck_log_type ll); +/* Set logging fd */ +void zck_set_log_fd(int fd); +/* Get error message + * Returns char* containing error message, or NULL if there isn't one */ +char *zck_get_error(zckCtx *zck); +/* Clear error message + * Returns 1 if message was cleared, 0 if error is fatal and can't be cleared */ +int zck_clear_error(zckCtx *zck); + +/******************************************************************* + * Miscellaneous utilities + *******************************************************************/ /* Validate the chunk and data checksums for the current file. * Returns -1 for error, 0 for invalid checksum and 1 for valid checksum */ int zck_validate_checksums(zckCtx *zck) diff --git a/src/lib/error.c b/src/lib/error.c new file mode 100644 index 0000000..b320736 --- /dev/null +++ b/src/lib/error.c @@ -0,0 +1,65 @@ +/* + * Copyright 2018 Jonathan Dieter + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include "zck_private.h" + +void set_fatal_error(zckCtx *zck, const char *msg) { + assert(zck != NULL && zck->msg == NULL && msg != NULL); + + zck->fatal_msg = zmalloc(strlen(msg)+1); + strncpy(zck->fatal_msg, msg, strlen(msg)); +} + +void set_error(zckCtx *zck, const char *msg) { + assert(zck != NULL && zck->msg == NULL && msg != NULL); + + zck->msg = zmalloc(strlen(msg)+1); + strncpy(zck->msg, msg, strlen(msg)); +} + +char PUBLIC *zck_get_error(zckCtx *zck) { + assert(zck != NULL); + + if(zck->fatal_msg) + return zck->fatal_msg; + return zck->msg; +} + +int PUBLIC zck_clear_error(zckCtx *zck) { + assert(zck != NULL); + + if(zck->fatal_msg) + return False; + + free(zck->msg); + zck->msg = NULL; + return True; +} diff --git a/src/lib/log.c b/src/lib/log.c index 56ea7eb..61195ca 100644 --- a/src/lib/log.c +++ b/src/lib/log.c @@ -33,17 +33,22 @@ #include "zck_private.h" static zck_log_type log_level = ZCK_LOG_ERROR; +static int log_fd = STDERR_FILENO; void PUBLIC zck_set_log_level(zck_log_type ll) { log_level = ll; } +void PUBLIC zck_set_log_fd(int fd) { + log_fd = fd; +} + void zck_log_wf(const char *function, zck_log_type lt, const char *format, ...) { if(lt >= log_level) { va_list args; va_start(args, format); - dprintf(STDERR_FILENO, "%s: ", function); - vdprintf(STDERR_FILENO, format, args); + dprintf(log_fd, "%s: ", function); + vdprintf(log_fd, format, args); va_end(args); } } diff --git a/src/lib/meson.build b/src/lib/meson.build index a8647b2..73f50f3 100644 --- a/src/lib/meson.build +++ b/src/lib/meson.build @@ -5,7 +5,7 @@ subdir('comp') subdir('hash') subdir('index') subdir('dl') -lib_sources += files('zck.c', 'header.c', 'io.c', 'log.c', 'compint.c') +lib_sources += files('zck.c', 'header.c', 'io.c', 'log.c', 'compint.c', 'error.c') zcklib = shared_library('zck', lib_sources, include_directories: inc, diff --git a/src/lib/zck.c b/src/lib/zck.c index 9cadc8e..2fab7f2 100644 --- a/src/lib/zck.c +++ b/src/lib/zck.c @@ -88,6 +88,14 @@ static void zck_clear(zckCtx *zck) { close(zck->temp_fd); zck->temp_fd = 0; } + if(zck->msg) { + free(zck->msg); + zck->msg = NULL; + } + if(zck->fatal_msg) { + free(zck->fatal_msg); + zck->fatal_msg = NULL; + } zck->fd = -1; } diff --git a/src/lib/zck_private.h b/src/lib/zck_private.h index 0306171..cf1ac5a 100644 --- a/src/lib/zck_private.h +++ b/src/lib/zck_private.h @@ -214,6 +214,9 @@ typedef struct zckCtx { int buzhash_match_bits; int buzhash_bitmask; int manual_chunk; + + char *msg; + char *fatal_msg; } zckCtx; int get_tmp_fd() @@ -334,7 +337,11 @@ int compint_to_size(size_t *val, const char *compint, size_t *length, size_t max_length) __attribute__ ((warn_unused_result)); - /* log.c */ void zck_log_wf(const char *function, zck_log_type lt, const char *format, ...); + +/* error.c */ +void set_fatal_error(zckCtx *zck, const char *msg); +void set_error(zckCtx *zck, const char *msg); + #endif -- 2.30.2