1/* 2 * Copyright (c) 2005-2007 Rob Braun 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of Rob Braun nor the names of his contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29/* 30 * 03-Apr-2005 31 * DRI: Rob Braun <bbraun@synack.net> 32 */ 33 34#include <stdlib.h> 35#include <string.h> 36#include <errno.h> 37#include "xar.h" 38#include "archive.h" 39 40#define ECTX(x) ((struct errctx *)(x)) 41 42void xar_register_errhandler(xar_t x, err_handler callback, void *usrctx) { 43 ECTX(&XAR(x)->errctx)->x = x; 44 ECTX(&XAR(x)->errctx)->usrctx = usrctx; 45 XAR(x)->ercallback = callback; 46 return; 47} 48 49xar_t xar_err_get_archive(xar_errctx_t ctx) { 50 return ECTX(ctx)->x; 51} 52 53xar_file_t xar_err_get_file(xar_errctx_t ctx) { 54 return ECTX(ctx)->file; 55} 56 57void xar_err_set_file(xar_t x, xar_file_t f) { 58 XAR(x)->errctx.file = f; 59 return; 60} 61 62const char *xar_err_get_string(xar_errctx_t ctx) { 63 return ECTX(ctx)->str; 64} 65 66void xar_err_set_string(xar_t x, const char *str) { 67 XAR(x)->errctx.str = strdup(str); // this leaks right now, but it's safer than the alternative 68 return; 69} 70 71void xar_err_set_formatted_string(xar_t x, const char *format, ...) { 72 va_list arg; 73 char *msg; 74 va_start(arg, format); 75 vasprintf(&msg, format, arg); 76 va_end(arg); 77 xar_err_set_string(x, msg); 78 free(msg); 79} 80 81 82int xar_err_get_errno(xar_errctx_t ctx) { 83 return ECTX(ctx)->saved_errno; 84} 85 86void xar_err_set_errno(xar_t x, int e) { 87 XAR(x)->errctx.saved_errno = e; 88 return; 89} 90 91void xar_err_new(xar_t x) { 92 memset(&XAR(x)->errctx, 0, sizeof(struct errctx)); 93 XAR(x)->errctx.saved_errno = errno; 94 return; 95} 96 97int32_t xar_err_callback(xar_t x, int32_t sev, int32_t err) { 98 if( XAR(x)->ercallback ) 99 return XAR(x)->ercallback(sev, err, &XAR(x)->errctx, ECTX(&XAR(x)->errctx)->usrctx); 100 return 0; 101} 102