Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/pkg/config.c
Show All 27 Lines | |||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/queue.h> | #include <sys/queue.h> | ||||
#include <sys/utsname.h> | #include <sys/utsname.h> | ||||
#include <sys/sbuf.h> | |||||
#include <sys/sysctl.h> | #include <sys/sysctl.h> | ||||
#include <dirent.h> | #include <dirent.h> | ||||
#include <ucl.h> | #include <ucl.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <libutil.h> | #include <libutil.h> | ||||
#include <paths.h> | #include <paths.h> | ||||
#include <stdbool.h> | #include <stdbool.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <ctype.h> | #include <ctype.h> | ||||
#include "config.h" | #include "config.h" | ||||
struct config_value { | struct config_value { | ||||
char *value; | char *value; | ||||
STAILQ_ENTRY(config_value) next; | STAILQ_ENTRY(config_value) next; | ||||
}; | }; | ||||
struct config_entry { | struct config_entry { | ||||
uint8_t type; | uint8_t type; | ||||
const char *key; | const char *key; | ||||
const char *val; | const char *val; | ||||
char *value; | char *value; | ||||
STAILQ_HEAD(, config_value) *list; | STAILQ_HEAD(, config_value) *list; | ||||
▲ Show 20 Lines • Show All 147 Lines • ▼ Show 20 Lines | if (str != NULL && (strcasecmp(str, "true") == 0 || | ||||
return (true); | return (true); | ||||
return (false); | return (false); | ||||
} | } | ||||
static void | static void | ||||
config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype) | config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype) | ||||
{ | { | ||||
struct sbuf *buf = sbuf_new_auto(); | FILE *buffp; | ||||
char *buf = NULL; | |||||
size_t bufsz = 0; | |||||
const ucl_object_t *cur, *seq, *tmp; | const ucl_object_t *cur, *seq, *tmp; | ||||
ucl_object_iter_t it = NULL, itseq = NULL, it_obj = NULL; | ucl_object_iter_t it = NULL, itseq = NULL, it_obj = NULL; | ||||
struct config_entry *temp_config; | struct config_entry *temp_config; | ||||
struct config_value *cv; | struct config_value *cv; | ||||
const char *key, *evkey; | const char *key, *evkey; | ||||
int i; | int i; | ||||
size_t j; | size_t j; | ||||
/* Temporary config for configs that may be disabled. */ | /* Temporary config for configs that may be disabled. */ | ||||
temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry)); | temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry)); | ||||
buffp = open_memstream(&buf, &bufsz); | |||||
if (buffp == NULL) | |||||
err(EXIT_FAILURE, "open_memstream()"); | |||||
while ((cur = ucl_iterate_object(obj, &it, true))) { | while ((cur = ucl_iterate_object(obj, &it, true))) { | ||||
key = ucl_object_key(cur); | key = ucl_object_key(cur); | ||||
if (key == NULL) | if (key == NULL) | ||||
continue; | continue; | ||||
sbuf_clear(buf); | if (buf != NULL) | ||||
memset(buf, 0, bufsz); | |||||
rewind(buffp); | |||||
if (conftype == CONFFILE_PKG) { | if (conftype == CONFFILE_PKG) { | ||||
for (j = 0; j < strlen(key); ++j) | for (j = 0; j < strlen(key); ++j) | ||||
sbuf_putc(buf, toupper(key[j])); | fputc(toupper(key[j]), buffp); | ||||
sbuf_finish(buf); | fflush(buffp); | ||||
} else if (conftype == CONFFILE_REPO) { | } else if (conftype == CONFFILE_REPO) { | ||||
if (strcasecmp(key, "url") == 0) | if (strcasecmp(key, "url") == 0) | ||||
sbuf_cpy(buf, "PACKAGESITE"); | fputs("PACKAGESITE", buffp); | ||||
else if (strcasecmp(key, "mirror_type") == 0) | else if (strcasecmp(key, "mirror_type") == 0) | ||||
sbuf_cpy(buf, "MIRROR_TYPE"); | fputs("MIRROR_TYPE", buffp); | ||||
else if (strcasecmp(key, "signature_type") == 0) | else if (strcasecmp(key, "signature_type") == 0) | ||||
sbuf_cpy(buf, "SIGNATURE_TYPE"); | fputs("SIGNATURE_TYPE", buffp); | ||||
else if (strcasecmp(key, "fingerprints") == 0) | else if (strcasecmp(key, "fingerprints") == 0) | ||||
sbuf_cpy(buf, "FINGERPRINTS"); | fputs("FINGERPRINTS", buffp); | ||||
else if (strcasecmp(key, "pubkey") == 0) | else if (strcasecmp(key, "pubkey") == 0) | ||||
sbuf_cpy(buf, "PUBKEY"); | fputs("PUBKEY", buffp); | ||||
else if (strcasecmp(key, "enabled") == 0) { | else if (strcasecmp(key, "enabled") == 0) { | ||||
if ((cur->type != UCL_BOOLEAN) || | if ((cur->type != UCL_BOOLEAN) || | ||||
!ucl_object_toboolean(cur)) | !ucl_object_toboolean(cur)) | ||||
goto cleanup; | goto cleanup; | ||||
} else | } else | ||||
continue; | continue; | ||||
sbuf_finish(buf); | fflush(buffp); | ||||
} | } | ||||
for (i = 0; i < CONFIG_SIZE; i++) { | for (i = 0; i < CONFIG_SIZE; i++) { | ||||
if (strcmp(sbuf_data(buf), c[i].key) == 0) | if (strcmp(buf, c[i].key) == 0) | ||||
break; | break; | ||||
} | } | ||||
/* Silently skip unknown keys to be future compatible. */ | /* Silently skip unknown keys to be future compatible. */ | ||||
if (i == CONFIG_SIZE) | if (i == CONFIG_SIZE) | ||||
continue; | continue; | ||||
/* env has priority over config file */ | /* env has priority over config file */ | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | for (i = 0; i < CONFIG_SIZE; i++) { | ||||
default: | default: | ||||
c[i].value = temp_config[i].value; | c[i].value = temp_config[i].value; | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
cleanup: | cleanup: | ||||
free(temp_config); | free(temp_config); | ||||
sbuf_delete(buf); | fclose(buffp); | ||||
free(buf); | |||||
} | } | ||||
/*- | /*- | ||||
* Parse new repo style configs in style: | * Parse new repo style configs in style: | ||||
* Name: | * Name: | ||||
* URL: | * URL: | ||||
* MIRROR_TYPE: | * MIRROR_TYPE: | ||||
* etc... | * etc... | ||||
▲ Show 20 Lines • Show All 219 Lines • Show Last 20 Lines |