From 744511ffe254e98044ac46e10938724c2f6c6b5f Mon Sep 17 00:00:00 2001 From: Moritz Grimm Date: Tue, 9 Jun 2015 23:02:45 +0200 Subject: [PATCH] Expose "configuration stashing" API --- src/cfg.c | 35 +++++++++++++++++++++++++++++------ src/cfg.h | 4 ++++ src/cfg_private.h | 1 + tests/check_cfg.c | 20 ++++++++++++++++++++ 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/cfg.c b/src/cfg.c index 91d0128..acc302e 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -63,6 +63,8 @@ _cfg_reset(struct cfg *c) static void _cfg_copy(struct cfg *dst, struct cfg *src) { + dst->_master_cfg = src->_master_cfg; + memcpy(&dst->program, &src->program, sizeof(dst->program)); memcpy(&dst->server, &src->server, sizeof(dst->server)); @@ -121,6 +123,7 @@ int cfg_init(void) { _cfg_reset(&cfg); + cfg._master_cfg = 1; return (0); } @@ -130,18 +133,38 @@ cfg_exit(void) _cfg_reset(&cfg); } +void +cfg_save(void) +{ + _cfg_reset(&cfg_tmp); + _cfg_copy(&cfg_tmp, &cfg); +} + +void +cfg_pop(void) +{ + if (!cfg_tmp._master_cfg) + return; + _cfg_reset(&cfg); + _cfg_copy(&cfg, &cfg_tmp); + _cfg_reset(&cfg_tmp); +} + +void +cfg_clear(void) +{ + _cfg_reset(&cfg_tmp); +} + int cfg_reload(void) { - _cfg_copy(&cfg_tmp, &cfg); + cfg_save(); if (0 > _cfg_load()) { - /* roll back */ - _cfg_reset(&cfg); - _cfg_copy(&cfg, &cfg_tmp); - _cfg_reset(&cfg_tmp); + cfg_pop(); return (-1); } - _cfg_reset(&cfg_tmp); + cfg_clear(); return (0); } diff --git a/src/cfg.h b/src/cfg.h index 8c03ca2..57795a1 100644 --- a/src/cfg.h +++ b/src/cfg.h @@ -65,6 +65,10 @@ enum cfg_stream_format { int cfg_init(void); void cfg_exit(void); +void cfg_save(void); +void cfg_pop(void); +void cfg_clear(void); + int cfg_reload(void); int cfg_stream_str2fmt(const char *, enum cfg_stream_format *); diff --git a/src/cfg_private.h b/src/cfg_private.h index 31b759f..eb1dd2a 100644 --- a/src/cfg_private.h +++ b/src/cfg_private.h @@ -31,6 +31,7 @@ #define DEFAULT_USER "source" struct cfg { + int _master_cfg; struct program { char name[PATH_MAX]; enum cfg_config_type config_type; diff --git a/tests/check_cfg.c b/tests/check_cfg.c index b5dfd76..d4abc57 100644 --- a/tests/check_cfg.c +++ b/tests/check_cfg.c @@ -100,6 +100,25 @@ Suite * cfg_suite(void); void setup_checked(void); void teardown_checked(void); +START_TEST(test_stash) +{ + ck_assert_ptr_eq(cfg_get_stream_name(), NULL); + ck_assert_int_eq(cfg_set_stream_name("test_stash", NULL), 0); + ck_assert_str_eq(cfg_get_stream_name(), "test_stash"); + cfg_save(); + ck_assert_int_eq(cfg_set_stream_name("test_stash2", NULL), 0); + ck_assert_str_eq(cfg_get_stream_name(), "test_stash2"); + cfg_pop(); + ck_assert_str_eq(cfg_get_stream_name(), "test_stash"); + cfg_pop(); + ck_assert_str_eq(cfg_get_stream_name(), "test_stash"); + cfg_save(); + cfg_clear(); + cfg_pop(); + ck_assert_str_eq(cfg_get_stream_name(), "test_stash"); +} +END_TEST + START_TEST(test_stream_str2fmt) { enum cfg_stream_format fmt; @@ -729,6 +748,7 @@ cfg_suite(void) tc_core = tcase_create("Core"); tcase_add_checked_fixture(tc_core, setup_checked, teardown_checked); + tcase_add_test(tc_core, test_stash); tcase_add_test(tc_core, test_stream_str2fmt); tcase_add_test(tc_core, test_stream_fmt2str); tcase_add_test(tc_core, test_file_check);