Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
turn struct pkg_license into a pkg_object
Baptiste Daroussin committed 12 years ago
commit 2addf978bf6c4c2b78d17cb11c6d782e10158267
parent edcb3fb
10 files changed +53 -101
modified libpkg/pkg.c
@@ -364,14 +364,6 @@ pkg_set_from_file(struct pkg *pkg, pkg_attr attr, const char *path, bool trimcr)
}

int
-
pkg_licenses(const struct pkg *pkg, struct pkg_license **l)
-
{
-
	assert(pkg != NULL);
-

-
	HASH_NEXT(pkg->licenses, (*l));
-
}
-

-
int
pkg_users(const struct pkg *pkg, struct pkg_user **u)
{
	assert(pkg != NULL);
@@ -475,35 +467,34 @@ pkg_provides(const struct pkg *pkg, struct pkg_provide **c)
int
pkg_addlicense(struct pkg *pkg, const char *name)
{
-
	struct pkg_license *l = NULL;
+
	pkg_object *o = NULL;
+
	pkg_iter iter = NULL;

	assert(pkg != NULL);
	assert(name != NULL && name[0] != '\0');
	const char *pkgname;

-
	if (pkg->licenselogic == LICENSE_SINGLE && HASH_COUNT(pkg->licenses) != 0) {
+
	if (pkg->licenselogic == LICENSE_SINGLE && UCL_COUNT(pkg->licenses) != 0) {
		pkg_get(pkg, PKG_NAME, &pkgname);
		pkg_emit_error("%s have a single license which is already set",
		    pkgname);
		return (EPKG_FATAL);
	}

-
	HASH_FIND_STR(pkg->licenses, name, l);
-
	if (l != NULL) {
-
		if (pkg_object_bool(pkg_config_get("DEVELOPER_MODE"))) {
-
			pkg_emit_error("duplicate license listing: %s, fatal (developer mode)", name);
-
			return (EPKG_FATAL);
-
		} else {
-
			pkg_emit_error("duplicate license listing: %s, ignoring", name);
-
			return (EPKG_OK);
+
	while ((o = pkg_object_iterate(pkg->licenses, &iter))) {
+
		if (strcmp(pkg_object_string(o), name) == 0) {
+
			if (pkg_object_bool(pkg_config_get("DEVELOPER_MODE"))) {
+
				pkg_emit_error("duplicate license listing: %s, fatal (developer mode)", name);
+
				return (EPKG_FATAL);
+
			} else {
+
				pkg_emit_error("duplicate license listing: %s, ignoring", name);
+
				return (EPKG_OK);
+
			}
		}
	}

-
	pkg_license_new(&l);
-

-
	strlcpy(l->name, name, sizeof(l->name));
-

-
	HASH_ADD_STR(pkg->licenses, name, l);
+
	o = ucl_object_fromstring_common(name, strlen(name), 0);
+
	pkg->licenses = ucl_array_append(pkg->licenses, o);

	return (EPKG_OK);
}
@@ -1102,6 +1093,14 @@ pkg_addannotation(struct pkg *pkg, const char *tag, const char *value)
}

pkg_object *
+
pkg_licenses(const struct pkg *pkg)
+
{
+
	assert (pkg != NULL);
+

+
	return (pkg->licenses);
+
}
+

+
pkg_object *
pkg_categories(const struct pkg *pkg)
{
	assert (pkg != NULL);
@@ -1154,7 +1153,7 @@ pkg_list_count(const struct pkg *pkg, pkg_list list)
	case PKG_RDEPS:
		return (HASH_COUNT(pkg->rdeps));
	case PKG_LICENSES:
-
		return (HASH_COUNT(pkg->licenses));
+
		return (UCL_COUNT(pkg->licenses));
	case PKG_OPTIONS:
		return (HASH_COUNT(pkg->options));
	case PKG_CATEGORIES:
@@ -1194,7 +1193,10 @@ pkg_list_free(struct pkg *pkg, pkg_list list) {
		pkg->flags &= ~PKG_LOAD_RDEPS;
		break;
	case PKG_LICENSES:
-
		HASH_FREE(pkg->licenses, pkg_license_free);
+
		if (pkg->licenses != NULL) {
+
			ucl_object_unref(pkg->licenses);
+
			pkg->licenses = NULL;
+
		}
		pkg->flags &= ~PKG_LOAD_LICENSES;
		break;
	case PKG_OPTIONS:
modified libpkg/pkg.h.in
@@ -565,13 +565,7 @@ int pkg_files(const struct pkg *, struct pkg_file **file);
int pkg_dirs(const struct pkg *pkg, struct pkg_dir **dir);

pkg_object *pkg_categories(const struct pkg *pkg);
-

-
/**
-
 * Iterates over the licenses of the package.
-
 * @param Must be set to NULL for the first call.
-
 * @return An error code.
-
 */
-
int pkg_licenses(const struct pkg *pkg, struct pkg_license **license);
+
pkg_object *pkg_licenses(const struct pkg *pkg);

/**
 * Iterates over the users of the package.
modified libpkg/pkg_attributes.c
@@ -215,34 +215,6 @@ pkg_dir_try(struct pkg_dir const * const d)
}

/*
-
 * License
-
 */
-
int
-
pkg_license_new(struct pkg_license **l)
-
{
-
	if ((*l = calloc(1, sizeof(struct pkg_license))) == NULL) {
-
		pkg_emit_errno("calloc", "pkg_license");
-
		return (EPKG_FATAL);
-
	}
-

-
	return (EPKG_OK);
-
}
-

-
void
-
pkg_license_free(struct pkg_license *l)
-
{
-
	free(l);
-
}
-

-
const char *
-
pkg_license_name(struct pkg_license const * const l)
-
{
-
	assert(l != NULL);
-

-
	return (l->name);
-
}
-

-
/*
 * User
 */

modified libpkg/pkg_manifest.c
@@ -840,7 +840,6 @@ emit_manifest(struct pkg *pkg, struct sbuf **out, short flags)
	struct pkg_option	*option   = NULL;
	struct pkg_file		*file     = NULL;
	struct pkg_dir		*dir      = NULL;
-
	struct pkg_license	*license  = NULL;
	struct pkg_user		*user     = NULL;
	struct pkg_group	*group    = NULL;
	struct pkg_shlib	*shlib    = NULL;
@@ -890,10 +889,9 @@ emit_manifest(struct pkg *pkg, struct sbuf **out, short flags)
	}

	pkg_debug(4, "Emitting licenses");
-
	seq = NULL;
-
	while (pkg_licenses(pkg, &license) == EPKG_OK)
-
		seq = ucl_array_append(seq, ucl_object_fromstring(pkg_license_name(license)));
-
	obj = ucl_object_insert_key(top, seq, "licenses", 8, false);
+
	if (pkg->categories != NULL)
+
		obj = ucl_object_insert_key(top,
+
		    ucl_object_ref(pkg->categories), "licenses", 8, false);

	obj = ucl_object_insert_key(top, ucl_object_fromint(flatsize), "flatsize", 8, false);
	if (pkgsize > 0)
@@ -915,7 +913,6 @@ emit_manifest(struct pkg *pkg, struct sbuf **out, short flags)
	obj = ucl_object_insert_key(top, map, "deps", 4, false);

	pkg_debug(4, "Emitting categories");
-
	
	if (pkg->categories != NULL)
		obj = ucl_object_insert_key(top,
		    ucl_object_ref(pkg->categories), "categories", 10, false);
modified libpkg/pkg_printf.c
@@ -1250,16 +1250,17 @@ format_licenses(struct sbuf *sbuf, const void *data, struct percent_esc *p)
	const struct pkg	*pkg = data;

	if (p->flags & (PP_ALTERNATE_FORM1|PP_ALTERNATE_FORM2))
-
		return (list_count(sbuf, pkg_list_count(pkg, PKG_LICENSES),
+
		return (list_count(sbuf, pkg_object_count(pkg_licenses(pkg)),
				   p));
	else {
-
		struct pkg_license	*lic = NULL;
+
		pkg_object	*lic;
+
		pkg_iter	 iter = NULL;
		int			 count;

		set_list_defaults(p, "%Ln", " %l ");

		count = 1;
-
		while (pkg_licenses(pkg, &lic) == EPKG_OK) {
+
		while ((lic = pkg_object_iterate(pkg->licenses, &iter))) {
			if (count > 1)
				iterate_item(sbuf, pkg, sbuf_data(p->sep_fmt),
					     lic, count, PP_L);
@@ -1278,9 +1279,9 @@ format_licenses(struct sbuf *sbuf, const void *data, struct percent_esc *p)
struct sbuf *
format_license_name(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
-
	const struct pkg_license	*license = data;
+
	pkg_object *o = (pkg_object *) data;

-
	return (string_val(sbuf, pkg_license_name(license), p));
+
	return (string_val(sbuf, pkg_object_string(o), p));
}

/*
modified libpkg/pkgdb.c
@@ -2569,7 +2569,6 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
	struct pkg_file		*file = NULL;
	struct pkg_dir		*dir = NULL;
	struct pkg_option	*option = NULL;
-
	struct pkg_license	*license = NULL;
	struct pkg_user		*user = NULL;
	struct pkg_group	*group = NULL;
	struct pkg_conflict	*conflict = NULL;
@@ -2776,11 +2775,12 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
	 * Insert licenses
	 */

-
	while (pkg_licenses(pkg, &license) == EPKG_OK) {
-
		if (run_prstmt(LICENSES1, pkg_license_name(license))
+
	iter = NULL;
+
	while ((obj = pkg_object_iterate(pkg->licenses, &iter))) {
+
		if (run_prstmt(LICENSES1, pkg_object_string(obj))
		    != SQLITE_DONE
		    ||
-
		    run_prstmt(LICENSES2, package_id, pkg_license_name(license))
+
		    run_prstmt(LICENSES2, package_id, pkg_object_string(obj))
		    != SQLITE_DONE) {
			ERROR_SQLITE(s);
			goto cleanup;
modified libpkg/pkgdb_repo.c
@@ -472,7 +472,6 @@ pkgdb_repo_add_package(struct pkg *pkg, const char *pkg_path,
	lic_t			 licenselogic;
	int			 ret;
	struct pkg_dep		*dep      = NULL;
-
	struct pkg_license	*license  = NULL;
	struct pkg_option	*option   = NULL;
	struct pkg_shlib	*shlib    = NULL;
	pkg_object		*obj;
@@ -538,14 +537,12 @@ try_again:
		}
	}

-
	license = NULL;
-
	while (pkg_licenses(pkg, &license) == EPKG_OK) {
-
		const char *lic_name = pkg_license_name(license);
-

-
		ret = run_prepared_statement(LIC1, lic_name);
+
	it = NULL;
+
	while ((obj = pkg_object_iterate(pkg->licenses, &it))) {
+
		ret = run_prepared_statement(LIC1, pkg_object_string(obj));
		if (ret == SQLITE_DONE)
			ret = run_prepared_statement(LIC2, package_id,
-
					lic_name);
+
			    pkg_object_string(obj));
		if (ret != SQLITE_DONE) {
			ERROR_SQLITE(sqlite);
			return (EPKG_FATAL);
modified libpkg/private/pkg.h
@@ -117,7 +117,7 @@ struct pkg {
	int64_t		 old_flatsize;
	int64_t		 pkgsize;
	struct sbuf	*scripts[PKG_NUM_SCRIPTS];
-
	struct pkg_license	*licenses;
+
	ucl_object_t		*licenses;
	ucl_object_t		*categories;
	struct pkg_dep		*deps;
	struct pkg_dep		*rdeps;
@@ -166,12 +166,6 @@ struct pkg_provide {
	UT_hash_handle	hh;
};

-
struct pkg_license {
-
	/* should be enough to match a license name */
-
	char name[64];
-
	UT_hash_handle	hh;
-
};
-

struct pkg_file {
	char		 path[MAXPATHLEN];
	int64_t		 size;
@@ -366,9 +360,6 @@ void pkg_file_free(struct pkg_file *);
int pkg_dir_new(struct pkg_dir **);
void pkg_dir_free(struct pkg_dir *);

-
int pkg_license_new(struct pkg_license **);
-
void pkg_license_free(struct pkg_license *);
-

int pkg_option_new(struct pkg_option **);
void pkg_option_free(struct pkg_option *);

modified src/query.c
@@ -332,11 +332,10 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
	struct pkg_option	*option = NULL;
	struct pkg_file		*file   = NULL;
	struct pkg_dir		*dir    = NULL;
-
	struct pkg_license	*lic    = NULL;
	struct pkg_user		*user   = NULL;
	struct pkg_group	*group  = NULL;
	struct pkg_shlib	*shlib  = NULL;
-
	pkg_object		*obj, *o;
+
	pkg_object		*o;
	pkg_iter		 it;

	switch (multiline) {
@@ -353,9 +352,8 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		}
		break;
	case 'C':
-
		obj = pkg_categories(pkg);
		it = NULL;
-
		while ((o = pkg_object_iterate(obj, &it))) {
+
		while ((o = pkg_object_iterate(pkg_categories(pkg), &it))) {
			format_str(pkg, output, qstr, o);
			printf("%s\n", sbuf_data(output));
		}
@@ -379,8 +377,9 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		}
		break;
	case 'L':
-
		while (pkg_licenses(pkg, &lic) == EPKG_OK) {
-
			format_str(pkg, output, qstr, lic);
+
		it = NULL;
+
		while ((o = pkg_object_iterate(pkg_licenses(pkg), &it))) {
+
			format_str(pkg, output, qstr, o);
			printf("%s\n", sbuf_data(output));
		}
		break;
@@ -409,9 +408,8 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		}
		break;
	case 'A':
-
		obj = pkg_annotations(pkg);
		it = NULL;
-
		while ((o = pkg_object_iterate(obj, &it))) {
+
		while ((o = pkg_object_iterate(pkg_annotations(pkg), &it))) {
			format_str(pkg, output, qstr, o);
			printf("%s\n", sbuf_data(output));
		}
modified src/utils.c
@@ -340,7 +340,7 @@ print_info(struct pkg * const pkg, uint64_t options)
				printf("\n");
			break;
		case INFO_LICENSES:
-
			if (pkg_list_count(pkg, PKG_LICENSES) > 0) {
+
			if (pkg_object_count(pkg_licenses(pkg)) > 0) {
				if (print_tag)
					printf("%-15s: ", "Licenses");
				pkg_printf("%L%{%Ln%| %l %}\n", pkg);