Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Convert pkg_note and annotations into a pkg_object
Baptiste Daroussin committed 12 years ago
commit 6f18a14692051c3a620126c16ee89d874b187c13
parent ad016c2
10 files changed +65 -121
modified libpkg/pkg.c
@@ -481,14 +481,6 @@ pkg_provides(const struct pkg *pkg, struct pkg_provide **c)
}

int
-
pkg_annotations(const struct pkg *pkg, struct pkg_note **an)
-
{
-
	assert(pkg != NULL);
-

-
	HASH_NEXT(pkg->annotations, (*an));
-
}
-

-
int
pkg_addlicense(struct pkg *pkg, const char *name)
{
	struct pkg_license *l = NULL;
@@ -1041,7 +1033,7 @@ pkg_addprovide(struct pkg *pkg, const char *name)
int
pkg_addannotation(struct pkg *pkg, const char *tag, const char *value)
{
-
	struct pkg_note *an = NULL;
+
	ucl_object_t *an;

	assert(pkg != NULL);
	assert(tag != NULL);
@@ -1049,54 +1041,52 @@ pkg_addannotation(struct pkg *pkg, const char *tag, const char *value)

	/* Tags are unique per-package */

-
	HASH_FIND_STR(pkg->annotations, tag, an);
+
	an = ucl_object_find_key(pkg->annotations, tag);
	if (an != NULL) {
		pkg_emit_error("duplicate annotation tag: %s value: %s,"
-
			       " ignoring", tag, value);
+
	           " ignoring", tag, value);
		return (EPKG_OK);
	}
	an = NULL;
-
	pkg_annotation_new(&an);
+
	an = ucl_object_fromstring(value);
+
	pkg->annotations = ucl_object_insert_key(pkg->annotations,
+
	    an, tag, strlen(tag), false);

-
	sbuf_set(&an->tag, tag);
-
	sbuf_set(&an->value, value);
+
	return (EPKG_OK);
+
}

-
	HASH_ADD_KEYPTR(hh, pkg->annotations,
-
	    pkg_annotation_tag(an),
-
	    strlen(pkg_annotation_tag(an)), an);
+
pkg_object *
+
pkg_annotations(const struct pkg *pkg)
+
{
+
	assert(pkg != NULL);

-
	return (EPKG_OK);
+
	return (pkg->annotations);
}

-
struct pkg_note *
+
pkg_object *
pkg_annotation_lookup(const struct pkg *pkg, const char *tag)
{
-
	struct pkg_note *an = NULL;
-

	assert(pkg != NULL);
	assert(tag != NULL);

-
	HASH_FIND_STR(pkg->annotations, tag, an);
-

-
	return (an);
+
	return (ucl_object_find_key(pkg->annotations, tag));
}

int
pkg_delannotation(struct pkg *pkg, const char *tag)
{
-
	struct pkg_note *an = NULL;
+
	ucl_object_t *an;

	assert(pkg != NULL);
	assert(tag != NULL);

-
	HASH_FIND_STR(pkg->annotations, tag, an);
+
	an = ucl_object_pop_key(pkg->annotations, tag);
	if (an != NULL) {
-
		HASH_DEL(pkg->annotations, an);
-
		pkg_annotation_free(an);
+
		ucl_object_unref(an);
		return (EPKG_OK);
	} else {
		pkg_emit_error("deleting annotation tagged \'%s\' -- "
-
			       "not found", tag);
+
	           "not found", tag);
		return (EPKG_WARN);
	}
}
@@ -1128,7 +1118,7 @@ pkg_list_count(const struct pkg *pkg, pkg_list list)
	case PKG_SHLIBS_PROVIDED:
		return (HASH_COUNT(pkg->shlibs_provided));
	case PKG_ANNOTATIONS:
-
		return (HASH_COUNT(pkg->annotations));
+
		return (UCL_COUNT(pkg->annotations));
	case PKG_CONFLICTS:
		return (HASH_COUNT(pkg->conflicts));
	case PKG_PROVIDES:
@@ -1186,7 +1176,10 @@ pkg_list_free(struct pkg *pkg, pkg_list list) {
		pkg->flags &= ~PKG_LOAD_SHLIBS_PROVIDED;
		break;
	case PKG_ANNOTATIONS:
-
		HASH_FREE(pkg->annotations, pkg_note, pkg_annotation_free);
+
		if (pkg->annotations != NULL) {
+
			ucl_object_unref(pkg->annotations);
+
			pkg->annotations = NULL;
+
		}
		pkg->flags &= ~PKG_LOAD_ANNOTATIONS;
		break;
	case PKG_CONFLICTS:
modified libpkg/pkg.h.in
@@ -632,9 +632,8 @@ int pkg_provides(const struct pkg *pkg, struct pkg_provide **provide);
 * @param note must be set to NULL for the first call.
 * @return An error code
 */
-
int pkg_annotations(const struct pkg *pkg, struct pkg_note **note);
-

-
struct pkg_note *pkg_annotation_lookup(const struct pkg *p, const char *tag);
+
pkg_object *pkg_annotation_lookup(const struct pkg *p, const char *tag);
+
pkg_object *pkg_annotations(const struct pkg *p);

/**
 * Iterate over all of the files within the package pkg, ensuring the
@@ -928,10 +927,6 @@ const char *pkg_conflict_origin(const struct pkg_conflict *);
/* pkg_provide */
const char *pkg_provide_name(const struct pkg_provide *);

-
/* pkg_note */
-
const char *pkg_annotation_tag(struct pkg_note const * const);
-
const char *pkg_annotation_value(struct pkg_note const * const);
-

/**
 * @param db A pointer to a struct pkgdb object
 * @param origin Package origin
modified libpkg/pkg_attributes.c
@@ -510,44 +510,3 @@ pkg_provide_name(const struct pkg_provide *c)

	return (sbuf_get(c->provide));
}
-

-

-
/*
-
 * Annotations
-
 */
-

-
int
-
pkg_annotation_new(struct pkg_note **an)
-
{
-
	if ((*an = calloc(1, sizeof(struct pkg_note))) == NULL)
-
		return (EPKG_FATAL);
-

-
	return (EPKG_OK);
-
}
-

-
void
-
pkg_annotation_free(struct pkg_note *an)
-
{
-
	if (an == NULL)
-
		return;
-

-
	sbuf_free(an->tag);
-
	sbuf_free(an->value);
-
	free(an);
-
}
-

-
const char *
-
pkg_annotation_tag(struct pkg_note const * const an)
-
{
-
	assert(an != NULL);
-

-
	return (sbuf_get(an->tag));
-
}
-

-
const char *
-
pkg_annotation_value(struct pkg_note const * const an)
-
{
-
	assert(an != NULL);
-

-
	return (sbuf_get(an->value));
-
}
modified libpkg/pkg_jobs.c
@@ -1027,7 +1027,7 @@ static bool
newer_than_local_pkg(struct pkg_jobs *j, struct pkg *rp, bool force)
{
	char *origin, *newversion, *oldversion, *reponame;
-
	struct pkg_note *an;
+
	ucl_object_t *an;
	int64_t oldsize;
	struct pkg *lp;
	bool automatic;
@@ -1050,10 +1050,10 @@ newer_than_local_pkg(struct pkg_jobs *j, struct pkg *rp, bool force)
	an = pkg_annotation_lookup(lp, "repository");
	if (an != NULL)  {
		if (strcmp(pkg_repo_ident(pkg_repo_find_name(reponame)),
-
		    pkg_annotation_value(an)) != 0)  {
+
		    ucl_object_tostring(an)) != 0)  {
			return (false);
		} else {
-
			pkg_addannotation(rp, "repository", pkg_annotation_value(an));
+
			pkg_addannotation(rp, "repository", ucl_object_tostring(an));
		}
	}

@@ -1573,7 +1573,7 @@ pkg_jobs_handle_install(struct pkg_solved *ps, struct pkg_jobs *j, bool handle_r
{
	struct pkg *new, *old;
	const char *pkgorigin, *oldversion = NULL;
-
	struct pkg_note *an;
+
	ucl_object_t *an;
	char path[MAXPATHLEN], *target;
	bool automatic = false;
	int flags = 0;
@@ -1629,7 +1629,7 @@ pkg_jobs_handle_install(struct pkg_solved *ps, struct pkg_jobs *j, bool handle_r
	}

	if (an != NULL) {
-
		pkgdb_add_annotation(j->db, new, "repository", pkg_annotation_value(an));
+
		pkgdb_add_annotation(j->db, new, "repository", ucl_object_tostring(an));
	}

	if (oldversion != NULL)
modified libpkg/pkg_manifest.c
@@ -970,13 +970,10 @@ emit_manifest(struct pkg *pkg, struct sbuf **out, short flags)
		    ucl_object_fromstring(pkg_option_value(option)),
		    pkg_option_opt(option), 0, false);
	}
-
	map = NULL;
-
	while (pkg_annotations(pkg, &note) == EPKG_OK) {
-
		map = ucl_object_insert_key(map,
-
		    ucl_object_fromstring(pkg_annotation_value(note)),
-
		    pkg_annotation_tag(note), 0, false);
-
	}
-
	obj = ucl_object_insert_key(top, map, "annotations", 11, false);
+

+
	if (pkg->annotations != NULL)
+
		obj = ucl_object_insert_key(top,
+
		    ucl_object_ref(map), "annotations", 11, false);

	if ((flags & PKG_MANIFEST_EMIT_COMPACT) == 0) {
		if ((flags & PKG_MANIFEST_EMIT_NOFILES) == 0) {
modified libpkg/pkg_printf.c
@@ -39,6 +39,7 @@

#include "pkg.h"
#include <private/pkg_printf.h>
+
#include <private/pkg.h>

/*
 * Format codes
@@ -828,13 +829,14 @@ format_annotations(struct sbuf *sbuf, const void *data, struct percent_esc *p)
	if (p->flags & (PP_ALTERNATE_FORM1|PP_ALTERNATE_FORM2))
		return (list_count(sbuf, pkg_list_count(pkg, PKG_ANNOTATIONS), p));
	else {
-
		struct pkg_note	*note = NULL;
+
		pkg_object	*note;
+
		pkg_iter	it = NULL;
		int		 count;

		set_list_defaults(p, "%An: %Av\n", "");

		count = 1;
-
		while (pkg_annotations(pkg, &note) == EPKG_OK) {
+
		while ((note = pkg_object_iterate(pkg->annotations, &it))) {
			if (count > 1)
				iterate_item(sbuf, pkg, sbuf_data(p->sep_fmt),
					     note, count, PP_A);
@@ -853,9 +855,9 @@ format_annotations(struct sbuf *sbuf, const void *data, struct percent_esc *p)
struct sbuf *
format_annotation_name(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
-
	const struct pkg_note	*note = data;
+
	pkg_object	*o = data;

-
	return (string_val(sbuf, pkg_annotation_tag(note), p));
+
	return (string_val(sbuf, pkg_object_key(o), p));
}

/*
@@ -864,9 +866,9 @@ format_annotation_name(struct sbuf *sbuf, const void *data, struct percent_esc *
struct sbuf *
format_annotation_value(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
-
	const struct pkg_note	*note = data;
+
	pkg_object	*o = data;

-
	return (string_val(sbuf, pkg_annotation_value(note), p));
+
	return (string_val(sbuf, pkg_object_string(o), p));
}

/*
modified libpkg/pkgdb.c
@@ -2982,18 +2982,19 @@ pkgdb_update_provides(struct pkg *pkg, int64_t package_id, sqlite3 *s)
int
pkgdb_insert_annotations(struct pkg *pkg, int64_t package_id, sqlite3 *s)
{
-
	struct pkg_note	*note = NULL;
+
	pkg_object	*note;
+
	pkg_iter	 it = NULL;

-
	while (pkg_annotations(pkg, &note) == EPKG_OK) {
-
		if (run_prstmt(ANNOTATE1, pkg_annotation_tag(note))
+
	while ((note = pkg_object_iterate(pkg->annotations, &it))) {
+
		if (run_prstmt(ANNOTATE1, pkg_object_key(note))
		    != SQLITE_DONE
		    ||
-
		    run_prstmt(ANNOTATE1, pkg_annotation_value(note))
+
		    run_prstmt(ANNOTATE1, pkg_object_string(note))
		    != SQLITE_DONE
		    ||
		    run_prstmt(ANNOTATE2, package_id,
-
			pkg_annotation_tag(note),
-
			pkg_annotation_value(note))
+
			pkg_object_key(note),
+
			pkg_object_string(note))
		    != SQLITE_DONE) {
			ERROR_SQLITE(s);
			return (EPKG_FATAL);
modified libpkg/pkgdb_repo.c
@@ -476,7 +476,8 @@ pkgdb_repo_add_package(struct pkg *pkg, const char *pkg_path,
	struct pkg_license	*license  = NULL;
	struct pkg_option	*option   = NULL;
	struct pkg_shlib	*shlib    = NULL;
-
	struct pkg_note		*note     = NULL;
+
	pkg_object		*obj;
+
	pkg_iter		 it;
	int64_t			 package_id;

	pkg_get(pkg, PKG_ORIGIN, &origin, PKG_NAME, &name,
@@ -593,10 +594,10 @@ try_again:
		}
	}

-
	note = NULL;
-
	while (pkg_annotations(pkg, &note) == EPKG_OK) {
-
		const char *note_tag = pkg_annotation_tag(note);
-
		const char *note_val = pkg_annotation_value(note);
+
	it = NULL;
+
	while ((obj = pkg_object_iterate(pkg->annotations, &it))) {
+
		const char *note_tag = pkg_object_key(obj);
+
		const char *note_val = pkg_object_string(obj);

		ret = run_prepared_statement(ANNOTATE1, note_tag);
		if (ret == SQLITE_DONE)
modified libpkg/private/pkg.h
@@ -45,6 +45,8 @@

#include "private/utils.h"

+
#define UCL_COUNT(obj) ((obj)?((obj)->len):0)
+

#define PKG_NUM_SCRIPTS 9

#if ARCHIVE_VERSION_NUMBER < 3000002
@@ -126,7 +128,7 @@ struct pkg {
	struct pkg_group	*groups;
	struct pkg_shlib	*shlibs_required;
	struct pkg_shlib	*shlibs_provided;
-
	struct pkg_note		*annotations;
+
	ucl_object_t		*annotations;
	struct pkg_conflict *conflicts;
	struct pkg_provide	*provides;
	unsigned       	 flags;
@@ -265,12 +267,6 @@ struct pkg_shlib {
	UT_hash_handle	hh;
};

-
struct pkg_note {
-
	struct sbuf	*tag;
-
	struct sbuf	*value;
-
	UT_hash_handle	 hh;
-
};
-

struct http_mirror {
	struct url *url;
	struct http_mirror *next;
@@ -385,9 +381,6 @@ void pkg_conflict_free(struct pkg_conflict *);
int pkg_provide_new(struct pkg_provide **);
void pkg_provide_free(struct pkg_provide *);

-
int pkg_annotation_new(struct pkg_note **);
-
void pkg_annotation_free(struct pkg_note *);
-

struct packing;

int packing_init(struct packing **pack, const char *path, pkg_formats format);
modified src/query.c
@@ -337,7 +337,8 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
	struct pkg_user		*user   = NULL;
	struct pkg_group	*group  = NULL;
	struct pkg_shlib	*shlib  = NULL;
-
	struct pkg_note		*note   = NULL;
+
	pkg_object		*obj, *o;
+
	pkg_iter		 it;

	switch (multiline) {
	case 'd':
@@ -407,8 +408,10 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		}
		break;
	case 'A':
-
		while (pkg_annotations(pkg, &note) == EPKG_OK) {
-
			format_str(pkg, output, qstr, note);
+
		obj = pkg_annotations(pkg);
+
		it = NULL;
+
		while ((o = pkg_object_iterate(obj, &it))) {
+
			format_str(pkg, output, qstr, obj);
			printf("%s\n", sbuf_data(output));
		}
		break;