Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Get rid of pkg_strel
Baptiste Daroussin committed 10 years ago
commit ed5762bddc0907570c74402268155bb605e876ae
parent 0560ab9
11 files changed +95 -116
modified libpkg/libpkg.ver
@@ -17,6 +17,7 @@ global:
	pkg_audit_new;
	pkg_audit_process;
	pkg_cache_full_clean;
+
	pkg_categories;
	pkg_compiled_for_same_os_major;
	pkg_conf;
	pkg_config_dump;
@@ -72,6 +73,7 @@ global:
	pkg_jobs_total;
	pkg_jobs_type;
	pkg_license_name;
+
	pkg_licenses;
	pkg_list_count;
	pkg_manifest_keys_free;
	pkg_manifest_keys_new;
modified libpkg/pkg.c
@@ -93,9 +93,9 @@ pkg_free(struct pkg *pkg)
	pkg_list_free(pkg, PKG_SHLIBS_PROVIDED);
	pkg_list_free(pkg, PKG_PROVIDES);
	pkg_list_free(pkg, PKG_REQUIRES);
+
	pkg_list_free(pkg, PKG_CATEGORIES);
+
	pkg_list_free(pkg, PKG_LICENSES);

-
	LL_FREE(pkg->categories, pkg_strel_free);
-
	LL_FREE(pkg->licenses, pkg_strel_free);
	LL_FREE(pkg->annotations, pkg_kv_free);

	if (pkg->rootfd != -1)
@@ -260,12 +260,6 @@ pkg_vget(const struct pkg * restrict pkg, va_list ap)
		case PKG_ANNOTATIONS:
			*va_arg(ap, const struct pkg_kv **) = pkg->annotations;
			break;
-
		case PKG_CATEGORIES:
-
			*va_arg(ap, const struct pkg_strel **) = pkg->categories;
-
			break;
-
		case PKG_LICENSES:
-
			*va_arg(ap, const struct pkg_strel **) = pkg->licenses;
-
			break;
		case PKG_UNIQUEID:
			*va_arg(ap, const char **) = pkg->uid;
			break;
@@ -589,6 +583,16 @@ pkg_provides(const struct pkg *pkg, char **c)
	kh_string_next(pkg->provides, (*c));
}

+
#define pkg_each_strings(name)			\
+
int						\
+
pkg_##name(const struct pkg *pkg, char **c) {	\
+
	assert(pkg != NULL);			\
+
	kh_string_next(pkg->name, (*c));	\
+
}
+

+
pkg_each_strings(categories);
+
pkg_each_strings(licenses);
+

int
pkg_requires(const struct pkg *pkg, char **c)
{
@@ -787,29 +791,27 @@ pkg_addconfig_file(struct pkg *pkg, const char *path, const char *content)
}

int
-
pkg_strel_add(struct pkg_strel **list, const char *val, const char *title)
+
pkg_addstring(kh_strings_t **list, const char *val, const char *title)
{
-
	struct pkg_strel *c;
+
	char *store;

	assert(val != NULL);
	assert(title != NULL);

-
	LL_FOREACH(*list, c) {
-
		if (strcmp(c->value, val) == 0) {
-
			if (developer_mode) {
-
				pkg_emit_error("duplicate %s listing: %s, fatal"
-
				    " (developer mode)", title, val);
-
				return (EPKG_FATAL);
-
			} else {
-
				pkg_emit_error("duplicate %s listing: %s, "
-
				    "ignoring", title, val);
-
				return (EPKG_OK);
-
			}
+
	if (kh_contains(strings, *list, val)) {
+
		if (developer_mode) {
+
			pkg_emit_error("duplicate %s listing: %s, fatal"
+
			    " (developer mode)", title, val);
+
			return (EPKG_FATAL);
+
		} else {
+
			pkg_emit_error("duplicate %s listing: %s, "
+
			    "ignoring", title, val);
+
			return (EPKG_OK);
		}
	}

-
	pkg_strel_new(&c, val);
-
	LL_APPEND(*list, c);
+
	store = strdup(val);
+
	kh_add(strings, *list, store, store);

	return (EPKG_OK);
}
@@ -1293,6 +1295,10 @@ pkg_list_count(const struct pkg *pkg, pkg_list list)
		return (kh_count(pkg->requires));
	case PKG_CONFIG_FILES:
		return (kh_count(pkg->config_files));
+
	case PKG_CATEGORIES:
+
		return (kh_count(pkg->categories));
+
	case PKG_LICENSES:
+
		return (kh_count(pkg->licenses));
	}
	
	return (0);
@@ -1351,6 +1357,14 @@ pkg_list_free(struct pkg *pkg, pkg_list list) {
		kh_free(strings, pkg->requires, char, free);
		pkg->flags &= ~PKG_LOAD_REQUIRES;
		break;
+
	case PKG_CATEGORIES:
+
		kh_free(strings, pkg->categories, char, free);
+
		pkg->flags &= ~PKG_LOAD_CATEGORIES;
+
		break;
+
	case PKG_LICENSES:
+
		kh_free(strings, pkg->licenses, char, free);
+
		pkg->flags &= ~PKG_LOAD_LICENSES;
+
		break;
	}
}

modified libpkg/pkg.h.in
@@ -96,7 +96,6 @@ struct pkg_dep;
struct pkg_conflict;
struct pkg_file;
struct pkg_dir;
-
struct pkg_strel;
struct pkg_option;
struct pkg_license;
struct pkg_config_file;
@@ -123,11 +122,6 @@ struct pkg_kv {
	struct pkg_kv *next;
};

-
struct pkg_strel {
-
	char *value;
-
	struct pkg_strel *next;
-
};
-

/**
 * The system-wide pkg(8) status: ie. is it a) installed or otherwise
 * available on the sysem, b) database (local.sqlite) initialised and
@@ -284,8 +278,6 @@ typedef enum {
	PKG_ROWID,
	PKG_TIME,
	PKG_ANNOTATIONS,
-
	PKG_LICENSES,
-
	PKG_CATEGORIES,
	PKG_UNIQUEID,
	PKG_OLD_DIGEST,
	PKG_DEP_FORMULA,
@@ -327,6 +319,8 @@ typedef enum {
	PKG_PROVIDES,
	PKG_CONFIG_FILES,
	PKG_REQUIRES,
+
	PKG_CATEGORIES,
+
	PKG_LICENSES
} pkg_list;

typedef enum {
@@ -658,6 +652,9 @@ int pkg_conflicts(const struct pkg *pkg, struct pkg_conflict **conflict);
int pkg_provides(const struct pkg *pkg, char **provide);
int pkg_requires(const struct pkg *pkg, char **require);

+
int pkg_categories(const struct pkg *pkg, char **category);
+
int pkg_licenses(const struct pkg *pkg, char **licenses);
+

/**
 * Iterates over the config files registered in the package.
 * @param provide must be set to NULL for the first call.
modified libpkg/pkg_attributes.c
@@ -220,30 +220,6 @@ pkg_config_file_free(struct pkg_config_file *c)
	free(c);
}

-
/*
-
 * strel
-
 */
-

-
int
-
pkg_strel_new(struct pkg_strel **c, const char *val)
-
{
-
	if ((*c = calloc(1, sizeof(struct pkg_strel))) == NULL)
-
		return (EPKG_FATAL);
-

-
	(*c)->value = strdup(val);
-

-
	return (EPKG_OK);
-
}
-

-
void
-
pkg_strel_free(struct pkg_strel *c)
-
{
-
	if (c == NULL)
-
		return;
-

-
	free(c->value);
-
	free(c);
-
}

/*
 * kv
modified libpkg/pkg_manifest.c
@@ -360,14 +360,14 @@ pkg_array(struct pkg *pkg, const ucl_object_t *obj, int attr)
			if (cur->type != UCL_STRING)
				pkg_emit_error("Skipping malformed category");
			else
-
				pkg_strel_add(&pkg->categories,
+
				pkg_addstring(&pkg->categories,
				    ucl_object_tostring(cur), "category");
			break;
		case PKG_LICENSES:
			if (cur->type != UCL_STRING)
				pkg_emit_error("Skipping malformed license");
			else
-
				pkg_strel_add(&pkg->licenses,
+
				pkg_addstring(&pkg->licenses,
				    ucl_object_tostring(cur), "license");
			break;
		case PKG_USERS:
@@ -971,11 +971,11 @@ pkg_emit_object(struct pkg *pkg, short flags)
	pkg_debug(4, "Emitting licenses");
	seq = NULL;
	el = NULL;
-
	LL_FOREACH(pkg->licenses, el) {
+
	kh_each_value(pkg->licenses, buf, {
		if (seq == NULL)
			seq = ucl_object_typed_new(UCL_ARRAY);
-
		ucl_array_append(seq, ucl_object_fromstring(el->value));
-
	}
+
		ucl_array_append(seq, ucl_object_fromstring(buf));
+
	});
	if (seq)
		ucl_object_insert_key(top, seq, "licenses", 8, false);

@@ -1005,11 +1005,11 @@ pkg_emit_object(struct pkg *pkg, short flags)
	pkg_debug(4, "Emitting categories");
	seq = NULL;
	el = NULL;
-
	LL_FOREACH(pkg->categories, el) {
+
	kh_each_value(pkg->categories, buf, {
		if (seq == NULL)
			seq = ucl_object_typed_new(UCL_ARRAY);
-
		ucl_array_append(seq, ucl_object_fromstring(el->value));
-
	}
+
		ucl_array_append(seq, ucl_object_fromstring(buf));
+
	});
	if (seq)
		ucl_object_insert_key(top, seq, "categories", 10, false);

modified libpkg/pkg_printf.c
@@ -933,25 +933,24 @@ struct sbuf *
format_categories(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	struct pkg_strel	*el;
	int			 count = 0;
+
	char			*cat;

	if (p->flags & (PP_ALTERNATE_FORM1|PP_ALTERNATE_FORM2)) {
-
		LL_COUNT(pkg->categories, el, count);
-
		return (list_count(sbuf, count, p));
+
		return (list_count(sbuf, pkg_list_count(pkg, PKG_CATEGORIES), p));
	} else {
		set_list_defaults(p, "%Cn", ", ");

		count = 1;
-
		LL_FOREACH(pkg->categories, el) {
+
		kh_each_value(pkg->categories, cat, {
			if (count > 1)
				iterate_item(sbuf, pkg, sbuf_data(p->sep_fmt),
-
				    el, count, PP_C);
+
				    cat, count, PP_C);

-
			iterate_item(sbuf, pkg, sbuf_data(p->item_fmt), el,
+
			iterate_item(sbuf, pkg, sbuf_data(p->item_fmt), cat,
			    count, PP_C);
			count++;
-
		}
+
		});
	}
	return (sbuf);
}
@@ -962,9 +961,9 @@ format_categories(struct sbuf *sbuf, const void *data, struct percent_esc *p)
struct sbuf *
format_category_name(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
-
	const struct pkg_strel	*el = data;
+
	const char *cat = data;

-
	return (string_val(sbuf, el->value, p));
+
	return (string_val(sbuf, cat, p));
}

/*
@@ -1199,25 +1198,24 @@ struct sbuf *
format_licenses(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	struct pkg_strel	*el;
+
	char			*lic;
	int			 count = 0;

	if (p->flags & (PP_ALTERNATE_FORM1|PP_ALTERNATE_FORM2)) {
-
		LL_COUNT(pkg->licenses, el, count);
-
		return (list_count(sbuf, count, p));
+
		return (list_count(sbuf, pkg_list_count(pkg, PKG_LICENSES), p));
	} else {
		set_list_defaults(p, "%Ln", " %l ");

		count = 1;
-
		LL_FOREACH(pkg->licenses, el) {
+
		kh_each_value(pkg->licenses, lic, {
			if (count > 1)
				iterate_item(sbuf, pkg, sbuf_data(p->sep_fmt),
-
				    el, count, PP_L);
+
				    lic, count, PP_L);

-
			iterate_item(sbuf, pkg, sbuf_data(p->item_fmt), el,
+
			iterate_item(sbuf, pkg, sbuf_data(p->item_fmt), lic,
			    count, PP_L);
			count++;
-
		}
+
		});
	}
	return (sbuf);
}
@@ -1228,9 +1226,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_strel	*el = data;
+
	const char *lic = data;

-
	return (string_val(sbuf, el->value, p));
+
	return (string_val(sbuf, lic, p));
}

/*
modified libpkg/pkgdb.c
@@ -1661,7 +1661,6 @@ int
pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
{
	struct pkg		*pkg2 = NULL;
-
	struct pkg_strel	*el;
	struct pkg_dep		*dep = NULL;
	struct pkg_file		*file = NULL;
	struct pkg_dir		*dir = NULL;
@@ -1843,30 +1842,30 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
	 * Insert categories
	 */

-
	LL_FOREACH(pkg->categories, el) {
-
		ret = run_prstmt(CATEGORY1, el->value);
+
	kh_each_value(pkg->categories, buf, {
+
		ret = run_prstmt(CATEGORY1, buf);
		if (ret == SQLITE_DONE)
-
			ret = run_prstmt(CATEGORY2, package_id, el->value);
+
			ret = run_prstmt(CATEGORY2, package_id, buf);
		if (ret != SQLITE_DONE) {
			ERROR_SQLITE(s, SQL(CATEGORY2));
			goto cleanup;
		}
-
	}
+
	});

	/*
	 * Insert licenses
	 */

-
	LL_FOREACH(pkg->licenses, el) {
-
		if (run_prstmt(LICENSES1, el->value)
+
	kh_each_value(pkg->licenses, buf, {
+
		if (run_prstmt(LICENSES1, buf)
		    != SQLITE_DONE
		    ||
-
		    run_prstmt(LICENSES2, package_id, el->value)
+
		    run_prstmt(LICENSES2, package_id, buf)
		    != SQLITE_DONE) {
			ERROR_SQLITE(s, SQL(LICENSES2));
			goto cleanup;
		}
-
	}
+
	});

	/*
	 * Insert users
modified libpkg/pkgdb_iterator.c
@@ -107,13 +107,13 @@ static struct column_mapping {
static int
pkg_addcategory(struct pkg *pkg, const char *data)
{
-
	return (pkg_strel_add(&pkg->categories, data, "category"));
+
	return (pkg_addstring(&pkg->categories, data, "category"));
}

static int
pkg_addlicense(struct pkg *pkg, const char *data)
{
-
	return (pkg_strel_add(&pkg->licenses, data, "license"));
+
	return (pkg_addstring(&pkg->licenses, data, "license"));
}

static int
modified libpkg/private/pkg.h
@@ -237,8 +237,8 @@ struct pkg {
	int64_t			 timestamp;
	kh_pkg_deps_t		*deps;
	kh_pkg_deps_t		*rdeps;
-
	struct pkg_strel	*categories;
-
	struct pkg_strel	*licenses;
+
	kh_strings_t		*categories;
+
	kh_strings_t		*licenses;
	kh_pkg_files_t		*files;
	kh_pkg_dirs_t		*dirs;
	struct pkg_option	*options;
@@ -580,9 +580,6 @@ int pkg_validate(struct pkg *pkg);

void pkg_list_free(struct pkg *, pkg_list);

-
int pkg_strel_new(struct pkg_strel **, const char *val);
-
void pkg_strel_free(struct pkg_strel *);
-

int pkg_kv_new(struct pkg_kv **, const char *key, const char *val);
void pkg_kv_free(struct pkg_kv *);

@@ -710,7 +707,7 @@ int pkg_adddir(struct pkg *pkg, const char *path, bool check_duplicates);
int pkg_adddir_attr(struct pkg *pkg, const char *path, const char *uname,
    const char *gname, mode_t perm, u_long fflags, bool check_duplicates);

-
int pkg_strel_add(struct pkg_strel **l, const char *name, const char *title);
+
int pkg_addstring(kh_strings_t **s, const char *value, const char *title);
int pkg_kv_add(struct pkg_kv **kv, const char *key, const char *value, const char *title);
const char *pkg_kv_get(struct pkg_kv *const*kv, const char *key);
int pkg_adduser(struct pkg *pkg, const char *name);
modified libpkg/repo/binary/update.c
@@ -139,7 +139,6 @@ pkg_repo_binary_add_pkg(struct pkg *pkg, const char *pkg_path,
	struct pkg_dep		*dep      = NULL;
	struct pkg_option	*option   = NULL;
	char			*buf;
-
	struct pkg_strel	*el;
	struct pkg_kv		*kv;
	const char		*arch;
	int64_t			 package_id;
@@ -189,27 +188,27 @@ try_again:
		}
	}

-
	LL_FOREACH(pkg->categories, el) {
-
		ret = pkg_repo_binary_run_prstatement(CAT1, el->value);
+
	kh_each_value(pkg->categories, buf, {
+
		ret = pkg_repo_binary_run_prstatement(CAT1, buf);
		if (ret == SQLITE_DONE)
			ret = pkg_repo_binary_run_prstatement(CAT2, package_id,
-
			    el->value);
+
			    buf);
		if (ret != SQLITE_DONE) {
			ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(CAT2));
			return (EPKG_FATAL);
		}
-
	}
+
	});

-
	LL_FOREACH(pkg->licenses, el) {
-
		ret = pkg_repo_binary_run_prstatement(LIC1, el->value);
+
	kh_each_value(pkg->licenses, buf, {
+
		ret = pkg_repo_binary_run_prstatement(LIC1, buf);
		if (ret == SQLITE_DONE)
			ret = pkg_repo_binary_run_prstatement(LIC2, package_id,
-
			    el->value);
+
			    buf);
		if (ret != SQLITE_DONE) {
			ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(LIC2));
			return (EPKG_FATAL);
		}
-
	}
+
	});

	option = NULL;
	while (pkg_options(pkg, &option) == EPKG_OK) {
modified src/query.c
@@ -336,7 +336,6 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
	struct pkg_dir		*dir    = NULL;
	char			*buf;
	struct pkg_kv		*kv;
-
	struct pkg_strel	*list;

	switch (multiline) {
	case 'd':
@@ -352,11 +351,10 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		}
		break;
	case 'C':
-
		pkg_get(pkg, PKG_CATEGORIES, &list);
-
		while (list != NULL) {
-
			format_str(pkg, output, qstr, list);
+
		buf = NULL;
+
		while (pkg_categories(pkg, &buf) == EPKG_OK) {
+
			format_str(pkg, output, qstr, buf);
			printf("%s\n", sbuf_data(output));
-
			list = list->next;
		}
		break;
	case 'O':
@@ -378,11 +376,10 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		}
		break;
	case 'L':
-
		pkg_get(pkg, PKG_LICENSES, &list);
-
		while (list != NULL) {
-
			format_str(pkg, output, qstr, list);
+
		buf = NULL;
+
		while (pkg_licenses(pkg, &buf) == EPKG_OK) {
+
			format_str(pkg, output, qstr, buf);
			printf("%s\n", sbuf_data(output));
-
			list = list->next;
		}
		break;
	case 'U':