src/confd: refactor sr_get_bool() into srx_get_bool()

Note: new API, srx_get_bool() returns -1 on error, leaving 0 and 1 as
false and true, respectively.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-04-17 14:09:24 +02:00
parent 4654e169a5
commit ce5ff75c9c
3 changed files with 55 additions and 55 deletions
+3 -35
View File
@@ -309,38 +309,6 @@ fail:
return rc;
}
int sr_get_bool(sr_session_ctx_t *session, const char *fmt, ...)
{
sr_val_t *val = NULL;
char *xpath;
va_list ap;
int rc = 0;
int len;
va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap) + 1;
va_end(ap);
xpath = alloca(len);
if (!xpath)
goto fail;
va_start(ap, fmt);
vsnprintf(xpath, len, fmt, ap);
va_end(ap);
if (sr_get_item(session, xpath, 0, &val))
goto fail;
if (!val || val->type != SR_BOOL_T)
goto fail;
rc = val->data.bool_val;
fail:
if (val)
sr_free_val(val);
return rc;
}
#define TIMEZONE_CONF "/etc/timezone"
#define TIMEZONE_PREV TIMEZONE_CONF "-"
#define TIMEZONE_NEXT TIMEZONE_CONF "+"
@@ -436,7 +404,7 @@ static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
remove(CHRONY_PREV);
rename(CHRONY_CONF, CHRONY_PREV);
rename(CHRONY_NEXT, CHRONY_CONF);
if (!sr_get_bool(session, "/ietf-system:system/ntp/enabled")) {
if (srx_get_bool(session, "/ietf-system:system/ntp/enabled") == 0) {
systemf("initctl -nbq disable chronyd");
return SR_ERR_OK;
}
@@ -495,9 +463,9 @@ static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
}
if (server) {
if (sr_get_bool(session, "%s/iburst", xpath))
if (srx_get_bool(session, "%s/iburst", xpath) > 0)
fprintf(fp, " iburst");
if (sr_get_bool(session, "%s/prefer", xpath))
if (srx_get_bool(session, "%s/prefer", xpath) > 0)
fprintf(fp, " prefer");
fprintf(fp, "\n");
+50 -19
View File
@@ -1,37 +1,68 @@
/* SPDX-License-Identifier: BSD-3-Clause */
#include <stdarg.h>
#include "core.h"
char *srx_get_str(sr_session_ctx_t *session, const char *fmt, ...)
static int srx_vaget(sr_session_ctx_t *session, const char *fmt, va_list ap, sr_val_t **val, sr_val_type_t type)
{
char *str = NULL;
sr_val_t *val;
va_list apdup;
char *xpath;
va_list ap;
int len;
va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap) + 1;
va_end(ap);
va_copy(apdup, ap);
len = vsnprintf(NULL, 0, fmt, apdup) + 1;
va_end(apdup);
xpath = alloca(len);
if (!xpath)
return NULL;
return -1;
va_copy(apdup, ap);
vsnprintf(xpath, len, fmt, apdup);
va_end(apdup);
if (sr_get_item(session, xpath, 0, val)) {
ERROR("Failed reading xpath %s", xpath);
return -1;
}
if (type != SR_UNKNOWN_T && (*val)->type != type)
return -1;
return 0;
}
int srx_get_bool(sr_session_ctx_t *session, const char *fmt, ...)
{
sr_val_t *val = NULL;
int result = -1;
va_list ap;
va_start(ap, fmt);
vsnprintf(xpath, len, fmt, ap);
va_end(ap);
if (sr_get_item(session, xpath, 0, &val)) {
ERROR("Failed reading string value from xpath %s", xpath);
goto fail;
}
if (!val || val->type != SR_STRING_T)
if (srx_vaget(session, fmt, ap, &val, SR_BOOL_T))
goto fail;
str = strdup(val->data.string_val);
result = val->data.bool_val;
sr_free_val(val);
fail:
if (val)
sr_free_val(val);
va_end(ap);
return result;
}
char *srx_get_str(sr_session_ctx_t *session, const char *fmt, ...)
{
sr_val_t *val = NULL;
char *str = NULL;
va_list ap;
va_start(ap, fmt);
if (srx_vaget(session, fmt, ap, &val, SR_STRING_T))
goto fail;
str = sr_val_to_str(val);
sr_free_val(val);
fail:
va_end(ap);
return str;
}
+2 -1
View File
@@ -6,6 +6,7 @@
#include "core.h"
char *srx_get_str(sr_session_ctx_t *session, const char *fmt, ...);
char *srx_get_str (sr_session_ctx_t *session, const char *fmt, ...);
int srx_get_bool (sr_session_ctx_t *session, const char *fmt, ...);
#endif /* CONFD_SRX_VAL_H_ */