Add error handling functions
authorJonathan Dieter <jdieter@gmail.com>
Tue, 24 Jul 2018 11:14:26 +0000 (12:14 +0100)
committerJonathan Dieter <jdieter@gmail.com>
Tue, 24 Jul 2018 11:14:26 +0000 (12:14 +0100)
Signed-off-by: Jonathan Dieter <jdieter@gmail.com>
include/zck.h.in
src/lib/error.c [new file with mode: 0644]
src/lib/log.c
src/lib/meson.build
src/lib/zck.c
src/lib/zck_private.h

index dfd818fccf8a58d63ed29362ba33f4da3c3403d7..6f6e4c7b90f164eb1f743a2a97d42d729a48de24 100644 (file)
@@ -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 (file)
index 0000000..b320736
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2018 Jonathan Dieter <jdieter@gmail.com>
+ *
+ * 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 <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <zck.h>
+
+#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;
+}
index 56ea7eba607e7fc3f894c07edbead83053ae445b..61195ca069bc4bca40086b656862826c31168903 100644 (file)
 #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);
     }
 }
index a8647b26d64fdd06b9442393c92313b366242573..73f50f3835b73ce793ce7eafcf12d9f2e8db4a32 100644 (file)
@@ -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,
index 9cadc8e76f7598153725a04557c26eb8eb47031a..2fab7f2ad27420972c150c7890e41cf9b8eff654 100644 (file)
@@ -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;
 }
 
index 03061715f5886ba2cb40f0742015a9b7ff1c1a78..cf1ac5aedf6a777fd98b6a817567e5f93bfc519e 100644 (file)
@@ -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