libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit 78fb1b50905b8ea307f8d803db3225bb26350298
parent 04e2e25e12e93429aad8f9b1127fbc24bfa71796
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 15 Feb 2017 10:18:54 +0100

Fix(Config): Proper error handling, missing some tests

Diffstat:
Minc/config.h | 5+++--
Msrc/config.c | 18+++++++-----------
Mtests/base.conf | 1-
Mtests/test_config.c | 2+-
4 files changed, 11 insertions(+), 15 deletions(-)

diff --git a/inc/config.h b/inc/config.h @@ -21,6 +21,7 @@ # include <stdio.h> # include <confuse.h> # include <morphux.h> +# include <fcntl.h> # define CONFIG_DEF_PATH "/etc/mpm/mpm.conf" @@ -83,12 +84,12 @@ typedef struct s_config { cfg_t *ptr; char *fn; - u8_t ret; + char *err; } config_t; config_t *parse_config(const char *path, u8_t *ret); void config_free(config_t *ptr); void config_error_cb(cfg_t *ptr, const char *fmt, va_list ap); -const char *config_get_error_string(config_t *ptr); +void config_get_error_string(config_t *ptr); #endif /* CONFIG_H */ diff --git a/src/config.c b/src/config.c @@ -100,10 +100,11 @@ config_t *parse_config(const char *path, u8_t *ret) { config->ptr = cfg_init(opts, CFGF_NONE); cfg_set_error_function(config->ptr, &config_error_cb); *ret = cfg_parse(config->ptr, path != NULL ? path : CONFIG_DEF_PATH); - config->ret = *ret; config->fn = malloc(strlen(path != NULL ? path : CONFIG_DEF_PATH)); strcpy(config->fn, path != NULL ? path : CONFIG_DEF_PATH); + if (ret != 0) + config_get_error_string(config); return config; } @@ -117,6 +118,7 @@ void config_free(config_t *ptr) { cfg_free(ptr->ptr); free(ptr); free(ptr->fn); + free(ptr->err); } } @@ -129,16 +131,10 @@ void config_error_cb(cfg_t *ptr, const char *fmt, va_list ap) { strcpy(g_error, err); } -const char *config_get_error_string(config_t *ptr) { - char *ret = NULL; - size_t len; +void config_get_error_string(config_t *ptr) { + if (strlen(g_error) == 0) + return ; - len = strlen(g_error); - if (len == 0) - return NULL; - asprintf(&ret, "%s:%d: %s", ptr->fn, ptr->ptr->line, g_error); - -clean: + asprintf(&ptr->err, "%s:%d: %s", ptr->fn, ptr->ptr->line, g_error); strcpy(g_error, ""); - return (const char *)ret; } diff --git a/tests/base.conf b/tests/base.conf @@ -38,7 +38,6 @@ compile { sbu = 60 configure-args = {"--prefix=/usr"} make-args = {"-j2"} - oui } # Packages section diff --git a/tests/test_config.c b/tests/test_config.c @@ -6,7 +6,7 @@ TEST(config_basic) { conf = parse_config("base.conf", &ret); if (ret != 0) { - m_panic("Error: %s\n", config_get_error_string(conf)); + m_panic("%s\n", conf->err); } TEST_ASSERT((ret == 0), "Can't open configuration file"); config_free(conf);