conf: Parse the whole config with properties

There is no need to call the callbacks when reading the config file,
just store all properties.
We're only going to extract one when iterating later.
This commit is contained in:
Wim Taymans 2022-02-02 09:07:56 +01:00
parent 9bac90882d
commit 58ebe0f88a
2 changed files with 10 additions and 47 deletions

View file

@ -351,48 +351,25 @@ error:
return res;
}
static int conf_for_each(const char *path, const char *section,
int (*callback)(void *user_data, const char *location, const char *section,
const char *val, size_t len), void *user_data)
static int conf_load(const char *path, struct pw_properties *conf)
{
char *data;
struct stat sbuf;
int fd, len, res;
struct spa_json it[2];
char key[1024];
const char *val;
int fd;
if ((fd = open(path, O_CLOEXEC | O_RDONLY)) < 0) {
pw_log_warn("%p: error loading config '%s': %m", user_data, path);
pw_log_warn("%p: error loading config '%s': %m", conf, path);
return -errno;
}
pw_log_info("%p: loading config '%s'", user_data, path);
pw_log_info("%p: loading config '%s'", conf, path);
if (fstat(fd, &sbuf) < 0)
goto error_close;
if ((data = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
goto error_close;
close(fd);
spa_json_init(&it[0], data, sbuf.st_size);
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
spa_json_init(&it[1], data, sbuf.st_size);
while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
if ((len = spa_json_next(&it[1], &val)) <= 0)
break;
if (section && !spa_streq(key, section))
continue;
if (spa_json_is_null(val, len))
val = NULL;
else if (spa_json_is_container(val, len))
len = spa_json_container_len(&it[1], val, len);
if ((res = callback(user_data, path, key, val, len)) != 0)
return res;
}
pw_properties_update_string(conf, data, sbuf.st_size);
munmap(data, sbuf.st_size);
return 0;
@ -402,20 +379,6 @@ error_close:
return -errno;
}
static int update_conf(void *user_data, const char *location, const char *key,
const char *val, size_t len)
{
struct pw_properties *conf = user_data;
char *v = NULL;
if (val != NULL && (v = malloc(len+1)) != NULL)
spa_json_parse_stringn(val, len, v, len+1);
pw_properties_set(conf, key, v);
free(v);
return 0;
}
SPA_EXPORT
int pw_conf_load_conf(const char *prefix, const char *name, struct pw_properties *conf)
{
@ -434,7 +397,7 @@ int pw_conf_load_conf(const char *prefix, const char *name, struct pw_properties
pw_properties_set(conf, "config.prefix", prefix);
pw_properties_set(conf, "config.name", name);
return conf_for_each(path, NULL, update_conf, conf);
return conf_load(path, conf);
}
SPA_EXPORT
@ -451,7 +414,7 @@ int pw_conf_load_state(const char *prefix, const char *name, struct pw_propertie
pw_log_debug("%p: can't load config '%s': %m", conf, path);
return -ENOENT;
}
return conf_for_each(path, NULL, update_conf, conf);
return conf_load(path, conf);
}
struct data {