Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
ucl_parse: add 2 helpers on top of ucl to deduplicate code
Baptiste Daroussin committed 8 days ago
commit b68de0a9cfa11a8a9f1f8f86a45df0154cdf93d5
parent ada7b0d
8 files changed +61 -110
modified libpkg/pkg_osvf.c
@@ -22,6 +22,7 @@
#include <xmalloc.h>

#include "private/pkg_osvf.h"
+
#include "private/utils.h"
#include "pkghash.h"

/*
@@ -575,7 +576,6 @@ create_schema_obj(void)
ucl_object_t *
pkg_osvf_open(const char *location)
{
-
	struct ucl_parser *uclparser;
	ucl_object_t *obj = NULL;
	int fd;
	ucl_object_t *schema = NULL;
@@ -588,20 +588,9 @@ pkg_osvf_open(const char *location)
		return (NULL);
	}

-
	uclparser = ucl_parser_new(0);
-
	if (!ucl_parser_add_fd(uclparser, fd))
-
	{
-
		pkg_emit_error("Error parsing UCL file '%s': %s'",
-
		               location, ucl_parser_get_error(uclparser));
-
		ucl_parser_free(uclparser);
-
		close(fd);
-
		return (NULL);
-
	}
+
	obj = ucl_parse_fd(fd, location);
	close(fd);

-
	obj = ucl_parser_get_object(uclparser);
-
	ucl_parser_free(uclparser);
-

	if (obj == NULL)
	{
		pkg_emit_error("UCL definition %s cannot be validated: %s",
modified libpkg/pkg_ports.c
@@ -88,7 +88,6 @@ static struct action_cmd {
static ucl_object_t *
keyword_open_schema(void)
{
-
	struct ucl_parser *parser;
	static const char keyword_schema_str[] = ""
		"{"
		"  type = object;"
@@ -137,17 +136,8 @@ keyword_open_schema(void)
	if (keyword_schema != NULL)
		return (keyword_schema);

-
	parser = ucl_parser_new(UCL_PARSER_NO_FILEVARS);
-
	if (!ucl_parser_add_chunk(parser, keyword_schema_str,
-
	    sizeof(keyword_schema_str) -1)) {
-
		pkg_emit_error("Cannot parse schema for keywords: %s",
-
		    ucl_parser_get_error(parser));
-
		ucl_parser_free(parser);
-
		return (NULL);
-
	}
-

-
	keyword_schema = ucl_parser_get_object(parser);
-
	ucl_parser_free(parser);
+
	keyword_schema = ucl_parse_buf(keyword_schema_str,
+
	    sizeof(keyword_schema_str) - 1, "keyword schema");

	return (keyword_schema);
}
modified libpkg/pkg_repo_create.c
@@ -411,7 +411,6 @@ pkg_repo_create_free(struct pkg_repo_create *prc)
static ucl_object_t*
ucl_load(int dfd, const char *name, ucl_object_t *schema)
{
-
	struct ucl_parser *p;
	ucl_object_t *obj = NULL;
	int fd;
	struct ucl_schema_error err;
@@ -422,18 +421,8 @@ ucl_load(int dfd, const char *name, ucl_object_t *schema)
		return (NULL);
	}

-
	p = ucl_parser_new(0);
-
	if (!ucl_parser_add_fd(p, fd)) {
-
		pkg_emit_error("Error parsing UCL file '%s': %s'",
-
		    name, ucl_parser_get_error(p));
-
		ucl_parser_free(p);
-
		close(fd);
-
		return (NULL);
-
	}
+
	obj = ucl_parse_fd(fd, name);
	close(fd);
-

-
	obj = ucl_parser_get_object(p);
-
	ucl_parser_free(p);
	if (obj == NULL)
		return (NULL);

@@ -479,19 +468,7 @@ static const char expired_schema_str[] = ""
static ucl_object_t *
open_schema(const char* schema_str, size_t schema_str_len)
{
-
	struct ucl_parser *parser;
-
	ucl_object_t *schema;
-
	parser = ucl_parser_new(UCL_PARSER_NO_FILEVARS);
-
	if (!ucl_parser_add_chunk(parser, schema_str,
-
	    schema_str_len - 1)) {
-
		pkg_emit_error("Cannot parse schema string: %s",
-
		    ucl_parser_get_error(parser));
-
		    ucl_parser_free(parser);
-
		    return (NULL);
-
	}
-
	schema = ucl_parser_get_object(parser);
-
	ucl_parser_free(parser);
-
	return (schema);
+
	return (ucl_parse_buf(schema_str, schema_str_len - 1, "schema"));
}

static void
modified libpkg/pkg_repo_meta.c
@@ -30,6 +30,7 @@
#include "pkg.h"
#include "private/event.h"
#include "private/pkg.h"
+
#include "private/utils.h"

/* Default to repo v1 for now */
#define	DEFAULT_META_VERSION	2
@@ -109,7 +110,6 @@ pkg_repo_meta_free(struct pkg_repo_meta *meta)
static ucl_object_t*
pkg_repo_meta_open_schema_v1(void)
{
-
	struct ucl_parser *parser;
	static const char meta_schema_str_v1[] = ""
			"{"
			"type = object;"
@@ -149,17 +149,8 @@ pkg_repo_meta_open_schema_v1(void)
	if (repo_meta_schema_v1 != NULL)
		return (repo_meta_schema_v1);

-
	parser = ucl_parser_new(UCL_PARSER_NO_FILEVARS);
-
	if (!ucl_parser_add_chunk(parser, meta_schema_str_v1,
-
			sizeof(meta_schema_str_v1) - 1)) {
-
		pkg_emit_error("cannot parse schema for repo meta: %s",
-
				ucl_parser_get_error(parser));
-
		ucl_parser_free(parser);
-
		return (NULL);
-
	}
-

-
	repo_meta_schema_v1 = ucl_parser_get_object(parser);
-
	ucl_parser_free(parser);
+
	repo_meta_schema_v1 = ucl_parse_buf(meta_schema_str_v1,
+
	    sizeof(meta_schema_str_v1) - 1, "repo meta schema v1");

	return (repo_meta_schema_v1);
}
@@ -167,7 +158,6 @@ pkg_repo_meta_open_schema_v1(void)
static ucl_object_t*
pkg_repo_meta_open_schema_v2(void)
{
-
	struct ucl_parser *parser;
	static const char meta_schema_str_v2[] = ""
			"{"
			"type = object;"
@@ -206,17 +196,8 @@ pkg_repo_meta_open_schema_v2(void)
	if (repo_meta_schema_v2 != NULL)
		return (repo_meta_schema_v2);

-
	parser = ucl_parser_new(UCL_PARSER_NO_FILEVARS);
-
	if (!ucl_parser_add_chunk(parser, meta_schema_str_v2,
-
			sizeof(meta_schema_str_v2) - 1)) {
-
		pkg_emit_error("cannot parse schema for repo meta: %s",
-
				ucl_parser_get_error(parser));
-
		ucl_parser_free(parser);
-
		return (NULL);
-
	}
-

-
	repo_meta_schema_v2 = ucl_parser_get_object(parser);
-
	ucl_parser_free(parser);
+
	repo_meta_schema_v2 = ucl_parse_buf(meta_schema_str_v2,
+
	    sizeof(meta_schema_str_v2) - 1, "repo meta schema v2");

	return (repo_meta_schema_v2);
}
modified libpkg/private/utils.h
@@ -106,4 +106,7 @@ DEFINE_VEC_INSERT_SORTED_PROTO(charv_t, charv, char *);
uid_t get_uid_from_uname(const char *);
gid_t get_gid_from_gname(const char *);

+
ucl_object_t *ucl_parse_fd(int fd, const char *name);
+
ucl_object_t *ucl_parse_buf(const char *buf, size_t len, const char *name);
+

#endif
modified libpkg/repo/binary/query.c
@@ -528,7 +528,6 @@ pkg_repo_binary_groupsearch(struct pkg_repo *repo, const char *pattern, match_t
	ucl_object_t *groups, *ar, *el;
	const ucl_object_t *o;
	const char *cmp;
-
	struct ucl_parser *p;
	int fd;
	regex_t *re = NULL;
	int flag = 0;
@@ -552,18 +551,10 @@ pkg_repo_binary_groupsearch(struct pkg_repo *repo, const char *pattern, match_t
	fd = openat(repo->dfd, "groups.ucl", O_RDONLY|O_CLOEXEC);
	if (fd == -1)
		return (NULL);
-
	p = ucl_parser_new(0);
-
	if (!ucl_parser_add_fd(p, fd)) {
-
		pkg_emit_error("Error parsing groups for: %s'",
-
		    repo->name);
-
		ucl_parser_free(p);
-
		close(fd);
-
		return (NULL);
-

-
	}
-
	groups = ucl_parser_get_object(p);
-
	ucl_parser_free(p);
+
	groups = ucl_parse_fd(fd, repo->name);
	close(fd);
+
	if (groups == NULL)
+
		return (NULL);

	if (ucl_object_type(groups) != UCL_ARRAY) {
		ucl_object_unref(groups);
modified libpkg/triggers.c
@@ -26,6 +26,7 @@
#include <private/pkg.h>
#include <private/event.h>
#include <private/lua.h>
+
#include <private/utils.h>

extern char **environ;

@@ -43,7 +44,6 @@ get_script_type(const char *str)
static ucl_object_t *
trigger_open_schema(void)
{
-
	struct ucl_parser *parser;
	ucl_object_t *trigger_schema;
	static const char trigger_schema_str[] = ""
		"{"
@@ -102,17 +102,8 @@ trigger_open_schema(void)
		"  required = [ trigger ];"
		"}";

-
	parser = ucl_parser_new(UCL_PARSER_NO_FILEVARS);
-
	if (!ucl_parser_add_chunk(parser, trigger_schema_str,
-
	    sizeof(trigger_schema_str) -1)) {
-
		pkg_emit_error("Cannot parse schema for trigger: %s",
-
		    ucl_parser_get_error(parser));
-
		ucl_parser_free(parser);
-
		return (NULL);
-
	}
-

-
	trigger_schema = ucl_parser_get_object(parser);
-
	ucl_parser_free(parser);
+
	trigger_schema = ucl_parse_buf(trigger_schema_str,
+
	    sizeof(trigger_schema_str) - 1, "trigger schema");
	return (trigger_schema);
}

@@ -146,7 +137,6 @@ parse_trigger_script_block(const ucl_object_t *block, const char *block_name,
static struct trigger *
trigger_load(int dfd, const char *name, bool cleanup_only, ucl_object_t *schema)
{
-
	struct ucl_parser *p;
	ucl_object_t *obj = NULL;
	const ucl_object_t *block = NULL;
	int fd;
@@ -159,18 +149,8 @@ trigger_load(int dfd, const char *name, bool cleanup_only, ucl_object_t *schema)
		return (NULL);
	}

-
	p = ucl_parser_new(0);
-
	if (!ucl_parser_add_fd(p, fd)) {
-
		pkg_emit_error("Error parsing trigger '%s': %s", name,
-
		    ucl_parser_get_error(p));
-
		ucl_parser_free(p);
-
		close(fd);
-
		return (NULL);
-
	}
+
	obj = ucl_parse_fd(fd, name);
	close(fd);
-

-
	obj = ucl_parser_get_object(p);
-
	ucl_parser_free(p);
	if (obj == NULL)
		return (NULL);

modified libpkg/utils.c
@@ -1203,3 +1203,43 @@ pkg_meta_attribute_tostring(enum pkg_meta_attribute attrib)

	return "???";
}
+

+
ucl_object_t *
+
ucl_parse_fd(int fd, const char *name)
+
{
+
	struct ucl_parser *p;
+
	ucl_object_t *obj;
+

+
	p = ucl_parser_new(0);
+
	if (!ucl_parser_add_fd(p, fd)) {
+
		pkg_emit_error("Error parsing '%s': %s",
+
		    name, ucl_parser_get_error(p));
+
		ucl_parser_free(p);
+
		return (NULL);
+
	}
+

+
	obj = ucl_parser_get_object(p);
+
	ucl_parser_free(p);
+

+
	return (obj);
+
}
+

+
ucl_object_t *
+
ucl_parse_buf(const char *buf, size_t len, const char *name)
+
{
+
	struct ucl_parser *p;
+
	ucl_object_t *obj;
+

+
	p = ucl_parser_new(UCL_PARSER_NO_FILEVARS);
+
	if (!ucl_parser_add_chunk(p, buf, len)) {
+
		pkg_emit_error("Error parsing '%s': %s",
+
		    name, ucl_parser_get_error(p));
+
		ucl_parser_free(p);
+
		return (NULL);
+
	}
+

+
	obj = ucl_parser_get_object(p);
+
	ucl_parser_free(p);
+

+
	return (obj);
+
}