Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Make struct pkg a proper structure
Baptiste Daroussin committed 11 years ago
commit 99c27684b9a525273a73bb8986d79651d360f085
parent 5d74e4d
27 files changed +593 -833
modified libpkg/pkg.c
@@ -133,6 +133,25 @@ pkg_free(struct pkg *pkg)

	ucl_object_unref(pkg->fields);

+
	free(pkg->name);
+
	free(pkg->origin);
+
	free(pkg->old_version);
+
	free(pkg->maintainer);
+
	free(pkg->www);
+
	free(pkg->arch);
+
	free(pkg->abi);
+
	free(pkg->uid);
+
	free(pkg->digest);
+
	free(pkg->old_digest);
+
	free(pkg->message);
+
	free(pkg->prefix);
+
	free(pkg->comment);
+
	free(pkg->desc);
+
	free(pkg->sum);
+
	free(pkg->repopath);
+
	free(pkg->repourl);
+
	free(pkg->reason);
+

	for (int i = 0; i < PKG_NUM_SCRIPTS; i++)
		sbuf_free(pkg->scripts[i]);

@@ -649,7 +668,6 @@ int
pkg_adddep(struct pkg *pkg, const char *name, const char *origin, const char *version, bool locked)
{
	struct pkg_dep *d = NULL;
-
	const char *n1, *v1;

	assert(pkg != NULL);
	assert(name != NULL && name[0] != '\0');
@@ -659,14 +677,13 @@ pkg_adddep(struct pkg *pkg, const char *name, const char *origin, const char *ve
	pkg_debug(3, "Pkg: add a new dependency origin: %s, name: %s, version: %s", origin, name, version);
	HASH_FIND_STR(pkg->deps, origin, d);
	if (d != NULL) {
-
		pkg_get(pkg, PKG_NAME, &n1, PKG_VERSION, &v1);
		if (pkg_object_bool(pkg_config_get("DEVELOPER_MODE"))) {
			pkg_emit_error("%s-%s: duplicate dependency listing: %s-%s, fatal (developer mode)",
-
			    n1, v1, name, version);
+
			    pkg->name, pkg->version, name, version);
			return (EPKG_FATAL);
		} else {
			pkg_emit_error("%s-%s: duplicate dependency listing: %s-%s, ignoring",
-
			    n1, v1, name, version);
+
			    pkg->name, pkg->version, name, version);
			return (EPKG_OK);
		}
	}
@@ -1133,8 +1150,6 @@ int
pkg_addshlib_required(struct pkg *pkg, const char *name)
{
	struct pkg_shlib *s = NULL, *f;
-
	const char *origin;
-


	assert(pkg != NULL);
	assert(name != NULL && name[0] != '\0');
@@ -1153,9 +1168,8 @@ pkg_addshlib_required(struct pkg *pkg, const char *name)
	    pkg_shlib_name(s),
	    strlen(pkg_shlib_name(s)), s);

-
	pkg_get(pkg, PKG_ORIGIN, &origin);
	pkg_debug(3, "added shlib deps for %s on %s",
-
			origin, name);
+
	    pkg->name, name);

	return (EPKG_OK);
}
@@ -1164,7 +1178,6 @@ int
pkg_addshlib_provided(struct pkg *pkg, const char *name)
{
	struct pkg_shlib *s = NULL, *f;
-
	const char *origin;

	assert(pkg != NULL);
	assert(name != NULL && name[0] != '\0');
@@ -1182,9 +1195,8 @@ pkg_addshlib_provided(struct pkg *pkg, const char *name)
	    pkg_shlib_name(s),
	    strlen(pkg_shlib_name(s)), s);

-
	pkg_get(pkg, PKG_ORIGIN, &origin);
	pkg_debug(3, "added shlib provide %s for %s",
-
			name, origin);
+
	    pkg->name, pkg->origin);

	return (EPKG_OK);
}
@@ -1193,7 +1205,6 @@ int
pkg_addconflict(struct pkg *pkg, const char *uniqueid)
{
	struct pkg_conflict *c = NULL;
-
	const char *uid;

	assert(pkg != NULL);
	assert(uniqueid != NULL && uniqueid[0] != '\0');
@@ -1205,8 +1216,7 @@ pkg_addconflict(struct pkg *pkg, const char *uniqueid)

	pkg_conflict_new(&c);
	sbuf_set(&c->uniqueid, uniqueid);
-
	pkg_get(pkg, PKG_UNIQUEID, &uid);
-
	pkg_debug(3, "Pkg: add a new conflict origin: %s, with %s", uid, uniqueid);
+
	pkg_debug(3, "Pkg: add a new conflict origin: %s, with %s", pkg->uid, uniqueid);

	HASH_ADD_KEYPTR(hh, pkg->conflicts,
	    __DECONST(char *, pkg_conflict_uniqueid(c)),
@@ -1485,12 +1495,9 @@ pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae,
		}
	}

-
	if (*pkg_p == NULL) {
-
		retcode = pkg_new(pkg_p, PKG_FILE);
-
		if (retcode != EPKG_OK)
-
			goto cleanup;
-
	} else
-
		pkg_reset(*pkg_p, PKG_FILE);
+
	retcode = pkg_new(pkg_p, PKG_FILE);
+
	if (retcode != EPKG_OK)
+
		goto cleanup;

	pkg = *pkg_p;

@@ -1562,8 +1569,6 @@ pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae,
							break;
					}
				}
-
				sbuf_finish(sbuf);
-
				pkg_set(pkg, PKG_MTREE, sbuf_data(sbuf));
				sbuf_delete(sbuf);
			}
		}
@@ -1602,25 +1607,18 @@ pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae,
int
pkg_validate(struct pkg *pkg)
{
-
	const char *uid, *md;
-

	assert(pkg != NULL);

-
	pkg_get(pkg, PKG_UNIQUEID, &uid, PKG_DIGEST, &md);
-

-
	if (uid == NULL) {
+
	if (pkg->uid == NULL) {
		/* Keep that part for the day we have to change it */
		/* Generate uid from name*/
-
		char *name;
-

-
		pkg_get(pkg, PKG_NAME, &name);
-
		if (name == NULL)
+
		if (pkg->name == NULL)
			return (EPKG_FATAL);

-
		pkg_set(pkg, PKG_UNIQUEID, name);
+
		pkg->uid = strdup(pkg->name);
	}

-
	if (md == NULL || !pkg_checksum_is_valid(md, strlen(md))) {
+
	if (pkg->digest == NULL || !pkg_checksum_is_valid(pkg->digest, strlen(pkg->digest))) {
		/* Calculate new digest */
		return (pkg_checksum_calculate(pkg, NULL));
	}
@@ -1700,7 +1698,6 @@ pkg_recompute(struct pkgdb *db, struct pkg *pkg)
	struct pkg_file *f = NULL;
	struct hardlinks *hl = NULL;
	int64_t flatsize = 0;
-
	int64_t oldflatsize;
	struct stat st;
	bool regular = false;
	char sha256[SHA256_DIGEST_LENGTH * 2 + 1];
@@ -1730,9 +1727,8 @@ pkg_recompute(struct pkgdb *db, struct pkg *pkg)
	}
	HASH_FREE(hl, free);

-
	pkg_get(pkg, PKG_FLATSIZE, &oldflatsize);
-
	if (flatsize != oldflatsize)
-
		pkgdb_set(db, pkg, PKG_SET_FLATSIZE, flatsize);
+
	if (flatsize != pkg->flatsize)
+
		pkg->flatsize = flatsize;

	return (rc);
}
@@ -1767,26 +1763,15 @@ pkg_is_installed(struct pkgdb *db, const char *origin)
bool
pkg_has_message(struct pkg *p)
{
-
	const char *msg;
-

-
	pkg_get(p, PKG_MESSAGE, &msg);
-

-
	if (msg != NULL)
-
		return (true);
-

-
	return (false);
+
	return (p->message != NULL);
}

bool
pkg_is_locked(const struct pkg * restrict p)
{
-
	bool ret;
-

	assert(p != NULL);

-
	pkg_get(p, PKG_LOCKED, &ret);
-

-
	return (ret);
+
	return (p->locked);
}

bool
modified libpkg/pkg_add.c
@@ -286,9 +286,6 @@ pkg_add_check_pkg_archive(struct pkgdb *db, struct pkg *pkg,
	struct pkg_manifest_key *keys, const char *location)
{
	const char	*arch;
-
	const char	*abi;
-
	const char	*origin;
-
	const char	*name;
	int	ret, retcode;
	struct pkg_dep	*dep = NULL;
	char	bd[MAXPATHLEN], *basedir;
@@ -296,16 +293,16 @@ pkg_add_check_pkg_archive(struct pkgdb *db, struct pkg *pkg,
	const char	*ext;
	struct pkg	*pkg_inst = NULL;

-
	pkg_get(pkg, PKG_ARCH, &arch, PKG_ABI, &abi, PKG_ORIGIN, &origin, PKG_NAME, &name);
-
	if (abi != NULL)
-
		arch = abi;
+
	arch = pkg->abi != NULL ? pkg->abi : pkg->arch;

	if (!is_valid_abi(arch, true)) {
		if ((flags & PKG_ADD_FORCE) == 0) {
			return (EPKG_FATAL);
		}
	}
-
	ret = pkg_try_installed(db, origin, &pkg_inst, PKG_LOAD_BASIC);
+

+
	/* XX check */
+
	ret = pkg_try_installed(db, pkg->origin, &pkg_inst, PKG_LOAD_BASIC);
	if (ret == EPKG_OK) {
		if ((flags & PKG_ADD_FORCE) == 0) {
			pkg_emit_already_installed(pkg_inst);
@@ -321,7 +318,7 @@ pkg_add_check_pkg_archive(struct pkgdb *db, struct pkg *pkg,
		}
		else {
			pkg_emit_notice("package %s is already installed, forced install",
-
				name);
+
				pkg->name);
			pkg_free(pkg_inst);
			pkg_inst = NULL;
		}
@@ -444,7 +441,6 @@ pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
	struct pkg	*pkg = NULL;
	bool		 extract = true;
	bool		 handle_rc = false;
-
	bool		 automatic;
	int		 retcode = EPKG_OK;
	int		 ret;
	int nfiles;
@@ -474,9 +470,8 @@ pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
		return (EPKG_FATAL);
	}

-
	if (flags & PKG_ADD_AUTOMATIC) {
-
		pkg_set(pkg, PKG_AUTOMATIC, (bool)true);
-
	}
+
	if (flags & PKG_ADD_AUTOMATIC)
+
		pkg->automatic = true;

	/*
	 * Additional checks for non-remote package
@@ -489,19 +484,17 @@ pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
		}
	}
	else {
-
		const char *manifestdigest;
-

		if (remote->repo != NULL) {
			/* Save reponame */
			pkg_addannotation(pkg, "repository", remote->repo->name);
			pkg_addannotation(pkg, "repo_type", remote->repo->ops->type);
		}

-
		pkg_get(remote, PKG_DIGEST, &manifestdigest, PKG_AUTOMATIC, &automatic);
-
		pkg_set(pkg, PKG_DIGEST, manifestdigest);
+
		free(pkg->digest);
+
		pkg->digest = strdup(remote->digest);
		/* only preserve flags is -A has not been passed */
		if ((flags & PKG_ADD_AUTOMATIC) == 0)
-
			pkg_set(pkg, PKG_AUTOMATIC, automatic);
+
			pkg->automatic = remote->automatic;
	}

	if (location != NULL)
modified libpkg/pkg_audit.c
@@ -771,8 +771,6 @@ pkg_audit_is_vulnerable(struct pkg_audit *audit, struct pkg *pkg,
{
	struct pkg_audit_entry *e;
	struct pkg_audit_versions_range *vers;
-
	const char *pkgname;
-
	const char *pkgversion;
	struct sbuf *sb;
	struct pkg_audit_item *a;
	bool res = false, res1, res2;
@@ -780,13 +778,8 @@ pkg_audit_is_vulnerable(struct pkg_audit *audit, struct pkg *pkg,
	if (!audit->parsed)
		return false;

-
	pkg_get(pkg,
-
		PKG_NAME, &pkgname,
-
		PKG_VERSION, &pkgversion
-
	);
-

	a = audit->items;
-
	a += audit_entry_first_byte_idx[(size_t)pkgname[0]];
+
	a += audit_entry_first_byte_idx[(size_t)pkg->name[0]];
	sb = sbuf_new_auto();

	for (; (e = a->e) != NULL; a += a->next_pfx_incr) {
@@ -798,7 +791,7 @@ pkg_audit_is_vulnerable(struct pkg_audit *audit, struct pkg *pkg,
		 * that is lexicographically greater than our name,
		 * it and the rest won't match our name.
		 */
-
		cmp = strncmp(pkgname, e->pkgname, a->noglob_len);
+
		cmp = strncmp(pkg->name, e->pkgname, a->noglob_len);
		if (cmp > 0)
			continue;
		else if (cmp < 0)
@@ -806,24 +799,24 @@ pkg_audit_is_vulnerable(struct pkg_audit *audit, struct pkg *pkg,

		for (i = 0; i < a->next_pfx_incr; i++) {
			e = a[i].e;
-
			if (fnmatch(e->pkgname, pkgname, 0) != 0)
+
			if (fnmatch(e->pkgname, pkg->name, 0) != 0)
				continue;

-
			if (pkgversion == NULL) {
+
			if (pkg->version == NULL) {
				/*
				 * Assume that all versions should be checked
				 */
				res = true;
-
				pkg_audit_print_entry(e, sb, pkgname, NULL, quiet);
+
				pkg_audit_print_entry(e, sb, pkg->name, NULL, quiet);
			}
			else {
				LL_FOREACH(e->versions, vers) {
-
					res1 = pkg_audit_version_match(pkgversion, &vers->v1);
-
					res2 = pkg_audit_version_match(pkgversion, &vers->v2);
+
					res1 = pkg_audit_version_match(pkg->version, &vers->v1);
+
					res2 = pkg_audit_version_match(pkg->version, &vers->v2);

					if (res1 && res2) {
						res = true;
-
						pkg_audit_print_entry(e, sb, pkgname, pkgversion, quiet);
+
						pkg_audit_print_entry(e, sb, pkg->name, pkg->version, quiet);
						break;
					}
				}
modified libpkg/pkg_checksum.c
@@ -127,34 +127,24 @@ int
pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,
	pkg_checksum_type_t type)
{
-
	const char *key;
	unsigned char *bdigest;
	size_t blen;
	struct pkg_checksum_entry *entries = NULL;
-
	const ucl_object_t *o;
	struct pkg_option *option = NULL;
	struct pkg_shlib *shlib = NULL;
	struct pkg_user *user = NULL;
	struct pkg_group *group = NULL;
	struct pkg_dep *dep = NULL;
	int i;
-
	int recopies[] = {
-
		PKG_NAME,
-
		PKG_ORIGIN,
-
		PKG_VERSION,
-
		PKG_ARCH,
-
		-1
-
	};

	if (pkg == NULL || type >= PKG_HASH_TYPE_UNKNOWN ||
					destlen < checksum_types[type].hlen)
		return (EPKG_FATAL);

-
	for (i = 0; recopies[i] != -1; i++) {
-
		key = pkg_keys[recopies[i]].name;
-
		if ((o = ucl_object_find_key(pkg->fields, key)))
-
			pkg_checksum_add_entry(key, ucl_object_tostring(o), &entries);
-
	}
+
	pkg_checksum_add_entry("name", pkg->name, &entries);
+
	pkg_checksum_add_entry("origin", pkg->origin, &entries);
+
	pkg_checksum_add_entry("version", pkg->version, &entries);
+
	pkg_checksum_add_entry("arch", pkg->arch, &entries);

	while (pkg_options(pkg, &option) == EPKG_OK) {
		pkg_checksum_add_entry(pkg_option_opt(option), pkg_option_value(option),
@@ -385,13 +375,11 @@ pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db)
{
	char *new_digest;
	struct pkg_repo *repo;
-
	const char *reponame;
	int rc = EPKG_OK;
	pkg_checksum_type_t type = 0;

-
	pkg_get(pkg, PKG_REPONAME, &reponame);
-
	if (reponame != NULL) {
-
		repo = pkg_repo_find(reponame);
+
	if (pkg->reponame != NULL) {
+
		repo = pkg_repo_find(pkg->reponame);

		if (repo != NULL)
			type = repo->meta->digest_format;
@@ -409,12 +397,11 @@ pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db)
		return (EPKG_FATAL);
	}

-
	pkg_set(pkg, PKG_DIGEST, new_digest);
+
	free(pkg->digest);
+
	pkg->digest = new_digest;

	if (db != NULL)
		pkgdb_set_pkg_digest(db, pkg);

-
	free(new_digest);
-

	return (rc);
}
modified libpkg/pkg_create.c
@@ -106,21 +106,19 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,
			}
		}
	}
-
	pkg_set(pkg, PKG_FLATSIZE, flatsize);
+
	pkg->flatsize = flatsize;
	HASH_FREE(hardlinks, free);

	if (pkg->type == PKG_OLD_FILE) {
-
		const char *desc, *display, *comment;
		char oldcomment[BUFSIZ];

		pkg_old_emit_content(pkg, &m);
		packing_append_buffer(pkg_archive, m, "+CONTENTS", strlen(m));
		free(m);

-
		pkg_get(pkg, PKG_DESC, &desc, PKG_MESSAGE, &display, PKG_COMMENT, &comment);
-
		packing_append_buffer(pkg_archive, desc, "+DESC", strlen(desc));
-
		packing_append_buffer(pkg_archive, display, "+DISPLAY", strlen(display));
-
		snprintf(oldcomment, sizeof(oldcomment), "%s\n", comment);
+
		packing_append_buffer(pkg_archive, pkg->desc, "+DESC", strlen(pkg->desc));
+
		packing_append_buffer(pkg_archive, pkg->message, "+DISPLAY", strlen(pkg->message));
+
		pkg_snprintf(oldcomment, sizeof(oldcomment), "%c\n", pkg);
		packing_append_buffer(pkg_archive, oldcomment, "+COMMENT", strlen(oldcomment));
	} else {
		/*
@@ -229,7 +227,6 @@ pkg_create_from_manifest(const char *outdir, pkg_formats format,
	struct packing	*pkg_archive = NULL;
	char		 arch[BUFSIZ];
	int		 ret = ENOMEM;
-
	char		*buf;
	struct pkg_manifest_key *keys = NULL;

	pkg_debug(1, "Creating package from stage directory: '%s'", rootdir);
@@ -246,10 +243,9 @@ pkg_create_from_manifest(const char *outdir, pkg_formats format,
	}

	/* if no arch autodetermine it */
-
	pkg_get(pkg, PKG_ABI, &buf);
-
	if (buf == NULL) {
+
	if (pkg->abi == NULL) {
		pkg_get_myarch(arch, BUFSIZ);
-
		pkg_set(pkg, PKG_ABI, arch);
+
		pkg->abi = strdup(arch);
	}

	/* Create the archive */
@@ -291,12 +287,10 @@ pkg_create_staged(const char *outdir, pkg_formats format, const char *rootdir,
	char		*manifest = NULL;
	char		 arch[BUFSIZ];
	int		 ret = ENOMEM;
-
	char		*buf;
	int		 i, mfd;
	regex_t		 preg;
	regmatch_t	 pmatch[2];
	size_t		 size;
-
	char		*www = NULL;
	struct pkg_manifest_key *keys = NULL;

	mfd = -1;
@@ -322,20 +316,17 @@ pkg_create_staged(const char *outdir, pkg_formats format, const char *rootdir,
	}

	/* if no descriptions provided then try to get it from a file */
-
	pkg_get(pkg, PKG_DESC, &buf);
-
	if (buf == NULL)
+
	if (pkg->desc == NULL)
		pkg_load_from_file(mfd, pkg, PKG_DESC, "+DESC");

	/* if no message try to get it from a file */
-
	pkg_get(pkg, PKG_MESSAGE, &buf);
-
	if (buf == NULL)
+
	if (pkg->message == NULL)
		pkg_load_from_file(mfd, pkg, PKG_MESSAGE, "+DISPLAY");

	/* if no arch autodetermine it */
-
	pkg_get(pkg, PKG_ABI, &buf);
-
	if (buf == NULL) {
+
	if (pkg->abi == NULL) {
		pkg_get_myarch(arch, BUFSIZ);
-
		pkg_set(pkg, PKG_ABI, arch);
+
		pkg->abi = strdup(arch);
	}

	for (i = 0; scripts[i] != NULL; i++) {
@@ -349,29 +340,19 @@ pkg_create_staged(const char *outdir, pkg_formats format, const char *rootdir,
		goto cleanup;
	}

-
	/* if www is not given then try to determine it from description */
-
	if (www != NULL) {
-
		pkg_set(pkg, PKG_WWW, www);
-
		free(www);
-
	}
-

-
	pkg_get(pkg, PKG_WWW, &www);
-
	if (www == NULL) {
-
		pkg_get(pkg, PKG_DESC, &buf);
-
		if (buf == NULL) {
+
	if (pkg->www == NULL) {
+
		if (pkg->desc == NULL) {
			pkg_emit_error("No www or desc defined in manifest");
			ret = EPKG_FATAL;
			goto cleanup;
		}
		regcomp(&preg, "^WWW:[[:space:]]*(.*)$",
		    REG_EXTENDED|REG_ICASE|REG_NEWLINE);
-
		if (regexec(&preg, buf, 2, pmatch, 0) == 0) {
+
		if (regexec(&preg, pkg->desc, 2, pmatch, 0) == 0) {
			size = pmatch[1].rm_eo - pmatch[1].rm_so;
-
			www = strndup(&buf[pmatch[1].rm_so], size);
-
			pkg_set(pkg, PKG_WWW, www);
-
			free(www);
+
			pkg->www = strndup(&pkg->desc[pmatch[1].rm_so], size);
		} else {
-
			pkg_set(pkg, PKG_WWW, "UNKNOWN");
+
			pkg->www = strdup("UNKNOWN");
		}
		regfree(&preg);
	}
modified libpkg/pkg_cudf.c
@@ -111,18 +111,16 @@ static int
cudf_emit_pkg(struct pkg *pkg, int version, FILE *f,
		struct pkg_job_universe_item *conflicts_chain)
{
-
	const char *uid;
	struct pkg_dep *dep, *dtmp;
	struct pkg_provide *prov, *ptmp;
	struct pkg_conflict *conflict, *ctmp;
	struct pkg_job_universe_item *u;
	int column = 0, ver;

-
	pkg_get(pkg, PKG_UNIQUEID, &uid);
	if (fprintf(f, "package: ") < 0)
		return (EPKG_FATAL);

-
	if (cudf_print_package_name(f, uid) < 0)
+
	if (cudf_print_package_name(f, pkg->uid) < 0)
		return (EPKG_FATAL);

	if (fprintf(f, "\nversion: %d\n", version) < 0)
@@ -166,8 +164,8 @@ cudf_emit_pkg(struct pkg *pkg, int version, FILE *f,
		ver = 1;
		LL_FOREACH(conflicts_chain, u) {
			if (u->pkg != pkg && u->priority != INT_MIN) {
-
				if (cudf_print_conflict(f, uid, ver,
-
						(u->next != NULL && u->next->pkg != pkg), &column) < 0) {
+
				if (cudf_print_conflict(f, pkg->uid, ver,
+
				   (u->next != NULL && u->next->pkg != pkg), &column) < 0) {
					return (EPKG_FATAL);
				}
			}
@@ -186,7 +184,6 @@ static int
cudf_emit_request_packages(const char *op, struct pkg_jobs *j, FILE *f)
{
	struct pkg_job_request *req, *tmp;
-
	const char *uid;
	int column = 0;
	bool printed = false;

@@ -195,9 +192,8 @@ cudf_emit_request_packages(const char *op, struct pkg_jobs *j, FILE *f)
	HASH_ITER(hh, j->request_add, req, tmp) {
		if (req->skip)
			continue;
-
		pkg_get(req->item->pkg, PKG_ORIGIN, &uid);
-
		if (cudf_print_element(f, uid,
-
				(req->hh.next != NULL), &column) < 0) {
+
		if (cudf_print_element(f, req->item->pkg->uid,
+
		    (req->hh.next != NULL), &column) < 0) {
			return (EPKG_FATAL);
		}
		printed = true;
@@ -214,9 +210,8 @@ cudf_emit_request_packages(const char *op, struct pkg_jobs *j, FILE *f)
	HASH_ITER(hh, j->request_delete, req, tmp) {
		if (req->skip)
			continue;
-
		pkg_get(req->item->pkg, PKG_ORIGIN, &uid);
-
		if (cudf_print_element(f, uid,
-
				(req->hh.next != NULL), &column) < 0) {
+
		if (cudf_print_element(f, req->item->pkg->uid,
+
		    (req->hh.next != NULL), &column) < 0) {
			return (EPKG_FATAL);
		}
		printed = true;
@@ -232,13 +227,9 @@ cudf_emit_request_packages(const char *op, struct pkg_jobs *j, FILE *f)
static int
pkg_cudf_version_cmp(struct pkg_job_universe_item *a, struct pkg_job_universe_item *b)
{
-
	const char *vera, *verb;
	int ret;

-
	pkg_get(a->pkg, PKG_VERSION, &vera);
-
	pkg_get(b->pkg, PKG_VERSION, &verb);
-

-
	ret = pkg_version_cmp(vera, verb);
+
	ret = pkg_version_cmp(a->pkg->version, b->pkg->version);
	if (ret == 0) {
		/* Ignore remote packages whose versions are equal to ours */
		if (a->pkg->type != PKG_INSTALLED)
@@ -369,7 +360,6 @@ static int
pkg_jobs_cudf_add_package(struct pkg_jobs *j, struct pkg_cudf_entry *entry)
{
	struct pkg_job_universe_item *it, *cur, *selected = NULL, *old = NULL, *head;
-
	const char *uid, *oldversion;
	int ver, n;

	it = pkg_jobs_universe_find(j->universe, entry->uid);
@@ -409,7 +399,6 @@ pkg_jobs_cudf_add_package(struct pkg_jobs *j, struct pkg_cudf_entry *entry)
		return (EPKG_FATAL);
	}

-
	pkg_get(selected->pkg, PKG_ORIGIN, &uid);
	if (n == 1) {
		if (entry->installed && selected->pkg->type != PKG_INSTALLED) {
			pkg_debug(3, "pkg_cudf: schedule installation of %s(%d)",
@@ -436,8 +425,7 @@ pkg_jobs_cudf_add_package(struct pkg_jobs *j, struct pkg_cudf_entry *entry)
				entry->uid, ver);
		assert(old != NULL);
		/* XXX: this is a hack due to iterators stupidity */
-
		pkg_get(old->pkg, PKG_VERSION, &oldversion);
-
		pkg_set(selected->pkg, PKG_OLD_VERSION, oldversion);
+
		selected->pkg->old_version = old->pkg->version;
		pkg_jobs_cudf_insert_res_job (&j->jobs, selected, old, PKG_SOLVED_UPGRADE);
		j->count ++;
	}
modified libpkg/pkg_delete.c
@@ -46,7 +46,6 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags)
{
	int		 ret;
	bool		 handle_rc = false;
-
	int64_t		id;
	const unsigned load_flags = PKG_LOAD_RDEPS|PKG_LOAD_FILES|PKG_LOAD_DIRS|
					PKG_LOAD_SCRIPTS|PKG_LOAD_ANNOTATIONS;

@@ -102,9 +101,7 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags)
	if ((flags & PKG_DELETE_UPGRADE) == 0)
		pkg_emit_deinstall_finished(pkg);

-
	pkg_get(pkg, PKG_ROWID, &id);
-

-
	return (pkgdb_unregister_pkg(db, id));
+
	return (pkgdb_unregister_pkg(db, pkg->id));
}

void
@@ -208,11 +205,9 @@ static void
pkg_effective_rmdir(struct pkgdb *db, struct pkg *pkg)
{
	char prefix_r[MAXPATHLEN];
-
	const char *prefix;
	size_t i;

-
	pkg_get(pkg, PKG_PREFIX, &prefix);
-
	snprintf(prefix_r, sizeof(prefix_r), "%s/", prefix + 1);
+
	snprintf(prefix_r, sizeof(prefix_r), "%s/", pkg->prefix + 1);
	for (i = 0; i < pkg->dir_to_del_len; i++)
		rmdir_p(db, pkg, pkg->dir_to_del[i], prefix_r);
}
@@ -231,7 +226,7 @@ pkg_delete_file(struct pkg *pkg, struct pkg_file *file, unsigned force)
	path = file->path;
	path++;

-
	pkg_get(pkg, PKG_PREFIX, &prefix_rel);
+
	prefix_rel = pkg->prefix;
	prefix_rel++;
	len = strlen(prefix_rel);

@@ -316,7 +311,7 @@ pkg_delete_dir(struct pkg *pkg, struct pkg_dir *dir)
	/* remove the first / */
	path++;

-
	pkg_get(pkg, PKG_PREFIX, &prefix_rel);
+
	prefix_rel = pkg->prefix;
	prefix_rel++;
	len = strlen(prefix_rel);

modified libpkg/pkg_elf.c
@@ -104,7 +104,6 @@ static int
add_shlibs_to_pkg(__unused void *actdata, struct pkg *pkg, const char *fpath,
		  const char *name, bool is_shlib)
{
-
	const char *pkgname, *pkgversion;
	struct pkg_file *file = NULL;
	const char *filepath;

@@ -128,9 +127,8 @@ add_shlibs_to_pkg(__unused void *actdata, struct pkg *pkg, const char *fpath,
			}
		}

-
		pkg_get(pkg, PKG_NAME, &pkgname, PKG_VERSION, &pkgversion);
		pkg_emit_notice("(%s-%s) %s - required shared library %s not "
-
		    "found", pkgname, pkgversion, fpath, name);
+
		    "found", pkg->name, pkg->version, fpath, name);

		return (EPKG_FATAL);
	}
@@ -448,7 +446,6 @@ pkg_analyse_files(struct pkgdb *db, struct pkg *pkg, const char *stage)
	struct pkg_shlib *sh, *shtmp, *found;
	int ret = EPKG_OK;
	char fpath[MAXPATHLEN];
-
	const char *origin;
	bool developer = false, failures = false;

	developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE"));
@@ -487,7 +484,6 @@ pkg_analyse_files(struct pkgdb *db, struct pkg *pkg, const char *stage)
		}
	}

-
	pkg_get(pkg, PKG_ORIGIN, &origin);
	/*
	 * Do not depend on libraries that a package provides itself
	 */
@@ -496,7 +492,7 @@ pkg_analyse_files(struct pkgdb *db, struct pkg *pkg, const char *stage)
		if (found != NULL) {
			pkg_debug(2, "remove %s from required shlibs as the "
			    "package %s provides this library itself",
-
			    pkg_shlib_name(sh), origin);
+
			    pkg_shlib_name(sh), pkg->name);
			HASH_DEL(pkg->shlibs_required, sh);
		}
	}
modified libpkg/pkg_event.c
@@ -58,7 +58,6 @@ pipeevent(struct pkg_event *ev)
	int i;
	struct pkg_dep *dep = NULL;
	struct sbuf *msg, *buf;
-
	const char *message;
	struct pkg_event_conflict *cur_conflict;
	if (eventpipe < 0)
		return;
@@ -150,9 +149,6 @@ pipeevent(struct pkg_event *ev)
		    "}}", ev->e_extract_finished.pkg, ev->e_extract_finished.pkg);
		break;
	case PKG_EVENT_INSTALL_FINISHED:
-
		pkg_get(ev->e_install_finished.pkg,
-
		    PKG_MESSAGE, &message);
-

		pkg_sbuf_printf(msg, "{ \"type\": \"INFO_INSTALL_FINISHED\", "
		    "\"data\": { "
		    "\"pkgname\": \"%n\", "
@@ -161,7 +157,7 @@ pipeevent(struct pkg_event *ev)
		    "}}",
		    ev->e_install_finished.pkg,
		    ev->e_install_finished.pkg,
-
		    sbuf_json_escape(buf, message));
+
		    sbuf_json_escape(buf, ev->e_install_finished.pkg->message));
		break;
	case PKG_EVENT_INTEGRITYCHECK_BEGIN:
		sbuf_printf(msg, "{ \"type\": \"INFO_INTEGRITYCHECK_BEGIN\", "
@@ -547,15 +543,14 @@ pkg_emit_install_finished(struct pkg *p)
{
	struct pkg_event ev;
	bool syslog_enabled = false;
-
	char *name, *version;

	ev.type = PKG_EVENT_INSTALL_FINISHED;
	ev.e_install_finished.pkg = p;

	syslog_enabled = pkg_object_bool(pkg_config_get("SYSLOG"));
	if (syslog_enabled) {
-
		pkg_get(p, PKG_NAME, &name, PKG_VERSION, &version);
-
		syslog(LOG_NOTICE, "%s-%s installed", name, version);
+
		syslog(LOG_NOTICE, "%s-%s installed",
+
		    p->name, p->version);
	}

	pkg_emit_event(&ev);
@@ -675,15 +670,14 @@ pkg_emit_deinstall_finished(struct pkg *p)
{
	struct pkg_event ev;
	bool syslog_enabled = false;
-
	char *name, *version;

	ev.type = PKG_EVENT_DEINSTALL_FINISHED;
	ev.e_deinstall_finished.pkg = p;

	syslog_enabled = pkg_object_bool(pkg_config_get("SYSLOG"));
	if (syslog_enabled) {
-
		pkg_get(p, PKG_NAME, &name, PKG_VERSION, &version);
-
		syslog(LOG_NOTICE, "%s-%s deinstalled", name, version);
+
		syslog(LOG_NOTICE, "%s-%s deinstalled",
+
		    p->name, p->version);
	}

	pkg_emit_event(&ev);
@@ -706,7 +700,6 @@ pkg_emit_upgrade_finished(struct pkg *new, struct pkg *old)
{
	struct pkg_event ev;
	bool syslog_enabled = false;
-
	char *name, *oldversion, *version;

	ev.type = PKG_EVENT_UPGRADE_FINISHED;
	ev.e_upgrade_finished.new = new;
@@ -721,14 +714,12 @@ pkg_emit_upgrade_finished(struct pkg *new, struct pkg *old)
		};
		pkg_change_t action;

-
		pkg_get(new, PKG_NAME, &name, PKG_VERSION, &version);
-
		pkg_get(old, PKG_VERSION, &oldversion);
		action = pkg_version_change_between(new, old);
		syslog(LOG_NOTICE, "%s %s: %s %s %s ",
-
		    name, actions[action],
-
		    oldversion != NULL ? oldversion : version,
-
		    oldversion != NULL ? "->" : "",
-
		    oldversion != NULL ? version : "");
+
		    new->name, actions[action],
+
		    old->version != NULL ? old->version : new->version,
+
		    old->version != NULL ? "->" : "",
+
		    old->version != NULL ? new->version : "");
	}

	pkg_emit_event(&ev);
modified libpkg/pkg_jobs.c
@@ -257,13 +257,11 @@ pkg_jobs_add_req_from_universe(struct pkg_job_request **head,
{
	struct pkg_job_request *req;
	struct pkg_job_request_item *nit;
-
	const char *uid;
	struct pkg_job_universe_item *uit;
	bool new_req = false;

	assert(un != NULL);
-
	pkg_get(un->pkg, PKG_UNIQUEID, &uid);
-
	HASH_FIND_STR(*head, uid, req);
+
	HASH_FIND_STR(*head, un->pkg->uid, req);

	if (req == NULL) {
		req = calloc(1, sizeof(*req));
@@ -273,7 +271,7 @@ pkg_jobs_add_req_from_universe(struct pkg_job_request **head,
		}
		new_req = true;
		req->automatic = automatic;
-
		pkg_debug(4, "add new uid %s to the request", uid);
+
		pkg_debug(4, "add new uid %s to the request", un->pkg->uid);
	}
	else {
		if (req->item->unit == un) {
@@ -298,7 +296,7 @@ pkg_jobs_add_req_from_universe(struct pkg_job_request **head,

	if (new_req) {
		if (req->item != NULL) {
-
			HASH_ADD_KEYPTR(hh, *head, uid, strlen(uid), req);
+
			HASH_ADD_KEYPTR(hh, *head, un->pkg->uid, strlen(un->pkg->uid), req);
		}
		else {
			free(req);
@@ -315,7 +313,6 @@ pkg_jobs_add_req(struct pkg_jobs *j, struct pkg *pkg)
	struct pkg_job_request *req, **head;
	struct pkg_job_request_item *nit;
	struct pkg_job_universe_item *un;
-
	const char *uid;
	int rc;

	assert(pkg != NULL);
@@ -335,8 +332,7 @@ pkg_jobs_add_req(struct pkg_jobs *j, struct pkg *pkg)
		 * digest. In turn, that means that two upgrade candidates are equal,
		 * we thus won't do anything with this item, as it is definitely useless
		 */
-
		pkg_get(pkg, PKG_UNIQUEID, &uid);
-
		HASH_FIND_STR(*head, uid, req);
+
		HASH_FIND_STR(*head, pkg->uid, req);
		if (req != NULL) {
			DL_FOREACH(req->item, nit) {
				if (nit->unit == un)
@@ -364,8 +360,7 @@ pkg_jobs_add_req(struct pkg_jobs *j, struct pkg *pkg)
		return (NULL);
	}

-
	pkg_get(pkg, PKG_UNIQUEID, &uid);
-
	HASH_FIND_STR(*head, uid, req);
+
	HASH_FIND_STR(*head, pkg->uid, req);

	nit = calloc(1, sizeof(*nit));
	if (nit == NULL) {
@@ -382,7 +377,7 @@ pkg_jobs_add_req(struct pkg_jobs *j, struct pkg *pkg)
			pkg_emit_errno("malloc", "struct pkg_job_request");
			return (NULL);
		}
-
		HASH_ADD_KEYPTR(hh, *head, uid, strlen(uid), req);
+
		HASH_ADD_KEYPTR(hh, *head, pkg->uid, strlen(pkg->uid), req);
	}

	/* Append candidate to the list of candidates */
@@ -410,7 +405,6 @@ pkg_jobs_process_add_request(struct pkg_jobs *j, bool top)
	struct pkg *lp;
	int (*deps_func)(const struct pkg *pkg, struct pkg_dep **d);
	UT_array *to_process = NULL;
-
	const char *uid;

	if (upgrade || reverse) {
		utarray_new(to_process, &ut_ptr_icd);
@@ -424,13 +418,12 @@ pkg_jobs_process_add_request(struct pkg_jobs *j, bool top)
				deps_func = pkg_deps;

			d = NULL;
-
			pkg_get(it->pkg, PKG_UNIQUEID, &uid);
			/*
			 * Here we get deps of local packages only since we are pretty sure
			 * that they are completely expanded
			 */
			lp = pkg_jobs_universe_get_local(j->universe,
-
				uid, 0);
+
			    it->pkg->uid, 0);
			while (lp != NULL && deps_func(lp, &d) == EPKG_OK) {
				/*
				 * Do not add duplicated upgrade candidates
@@ -488,19 +481,18 @@ pkg_jobs_process_add_request(struct pkg_jobs *j, bool top)
		 * sorted based on the priority of a repo
		 */
		HASH_ITER(hh, j->request_add, req, tmp) {
-
			const char *uid, *lrepo, *rrepo;
+
			const char *lrepo, *rrepo;
			struct pkg_job_request_item *selected = req->item;

			lrepo = NULL;

-
			pkg_get(req->item->pkg, PKG_UNIQUEID, &uid);
-
			lp = pkg_jobs_universe_get_local(j->universe, uid, 0);
+
			lp = pkg_jobs_universe_get_local(j->universe, req->item->pkg->uid, 0);
			/* Check reponame */
			if (lp != NULL)
				lrepo = pkg_getannotation(lp, "repository");

			DL_FOREACH(req->item, it) {
-
				pkg_get(it->pkg, PKG_REPONAME, &rrepo);
+
				rrepo = it->pkg->reponame;
				if (lrepo != NULL && rrepo != NULL && strcmp(rrepo, lrepo) == 0) {
					/*
					 * Always prefer the same repo
@@ -574,7 +566,6 @@ static int
pkg_jobs_set_execute_priority(struct pkg_jobs *j, struct pkg_solved *solved)
{
	struct pkg_solved *ts;
-
	const char *uid;

	if (solved->type == PKG_SOLVED_UPGRADE
			&& solved->items[1]->pkg->conflicts != NULL) {
@@ -601,8 +592,8 @@ pkg_jobs_set_execute_priority(struct pkg_jobs *j, struct pkg_solved *solved)
			DL_APPEND(j->jobs, ts);
			j->count ++;
			solved->already_deleted = true;
-
			pkg_get(ts->items[0]->pkg, PKG_UNIQUEID, &uid);
-
			pkg_debug(2, "split upgrade request for %s", uid);
+
			pkg_debug(2, "split upgrade request for %s",
+
			   ts->items[0]->pkg->uid);
			return (EPKG_CONFLICT);
		}
	}
@@ -667,13 +658,11 @@ pkg_jobs_test_automatic(struct pkg_jobs *j, struct pkg *p)
	struct pkg_job_universe_item *unit;
	struct pkg *npkg;
	bool ret = true;
-
	bool automatic;

	while (pkg_rdeps(p, &d) == EPKG_OK && ret) {
		unit = pkg_jobs_universe_find(j->universe, d->uid);
		if (unit != NULL) {
-
			pkg_get(unit->pkg, PKG_AUTOMATIC, &automatic);
-
			if (!automatic) {
+
			if (!unit->pkg->automatic) {
				return (false);
			}
			npkg = unit->pkg;
@@ -681,10 +670,9 @@ pkg_jobs_test_automatic(struct pkg_jobs *j, struct pkg *p)
		else {
			npkg = pkg_jobs_universe_get_local(j->universe, d->uid,
					PKG_LOAD_BASIC|PKG_LOAD_RDEPS|PKG_LOAD_ANNOTATIONS);
-
			pkg_get(npkg, PKG_AUTOMATIC, &automatic);
			if (npkg == NULL)
				return (false);
-
			if (!automatic) {
+
			if (!npkg->automatic) {
				/*
				 * Safe to free, as d->uid is not in the universe
				 */
@@ -745,21 +733,17 @@ static int
pkg_jobs_process_remote_pkg(struct pkg_jobs *j, struct pkg *rp,
	struct pkg_job_request_item **req)
{
-
	const char *digest, *uid;
	struct pkg_job_universe_item *nit;
	struct pkg_job_request_item *nrit = NULL;
	struct pkg *lp;

-
	pkg_get(rp, PKG_DIGEST, &digest, PKG_UNIQUEID, &uid);
-

-
	if (digest == NULL) {
+
	if (rp->digest == NULL) {
		if (pkg_checksum_calculate(rp, j->db) != EPKG_OK) {
			return (EPKG_FATAL);
		}
-
		pkg_get(rp, PKG_DIGEST, &digest);
	}
-
	lp = pkg_jobs_universe_get_local(j->universe, uid, 0);
-
	nit = pkg_jobs_universe_get_upgrade_candidates(j->universe, uid, lp,
+
	lp = pkg_jobs_universe_get_local(j->universe, rp->uid, 0);
+
	nit = pkg_jobs_universe_get_upgrade_candidates(j->universe, rp->uid, lp,
		j->flags & PKG_FLAG_FORCE);

	if (nit != NULL) {
@@ -776,7 +760,7 @@ pkg_jobs_process_remote_pkg(struct pkg_jobs *j, struct pkg *rp,

static int
pkg_jobs_try_remote_candidate(struct pkg_jobs *j, const char *pattern,
-
		const char *uid, match_t m)
+
    const char *uid, match_t m)
{
	struct pkg *p = NULL;
	struct pkgdb_it *it;
@@ -785,7 +769,6 @@ pkg_jobs_try_remote_candidate(struct pkg_jobs *j, const char *pattern,
				PKG_LOAD_ANNOTATIONS|PKG_LOAD_CONFLICTS;
	int rc = EPKG_FATAL;
	struct sbuf *qmsg;
-
	const char *fuid;
	struct pkg_job_universe_item *unit;

	if ((it = pkgdb_repo_query(j->db, pattern, m, j->reponame)) == NULL)
@@ -794,17 +777,16 @@ pkg_jobs_try_remote_candidate(struct pkg_jobs *j, const char *pattern,
	qmsg = sbuf_new_auto();

	while (it != NULL && pkgdb_it_next(it, &p, flags) == EPKG_OK) {
-
		pkg_get(p, PKG_UNIQUEID, &fuid);
		sbuf_printf(qmsg, "%s has no direct installation candidates, change it to "
-
				"%s? [Y/n]: ", uid, fuid);
+
				"%s? [Y/n]: ", uid, p->uid);
		sbuf_finish(qmsg);
		if (pkg_emit_query_yesno(true, sbuf_data(qmsg))) {
			/* Change the origin of the local package */
			pkg_validate(p);
			unit = pkg_jobs_universe_find(j->universe, uid);
			if (unit != NULL)
-
				pkg_jobs_universe_change_uid(j->universe, unit, fuid,
-
					strlen(fuid), false);
+
				pkg_jobs_universe_change_uid(j->universe, unit, p->uid,
+
					strlen(p->uid), false);
			else
				assert(0);

@@ -972,7 +954,6 @@ pkg_jobs_find_remote_pattern(struct pkg_jobs *j, struct job_pattern *jp,
	struct pkg_manifest_key *keys = NULL;
	struct pkg_job_request *req;
	struct job_pattern jfp;
-
	const char *pkgname, *uid;

	if (!jp->is_file) {
		if (j->type == PKG_JOBS_UPGRADE) {
@@ -995,9 +976,8 @@ pkg_jobs_find_remote_pattern(struct pkg_jobs *j, struct job_pattern *jp,
			rc = EPKG_FATAL;
		else if (pkg_validate(pkg) == EPKG_OK) {
			if (j->type == PKG_JOBS_UPGRADE) {
-
				pkg_get(pkg, PKG_NAME, &pkgname);
				jfp.match = MATCH_EXACT;
-
				jfp.pattern = __DECONST(char *, pkgname);
+
				jfp.pattern = pkg->name;
				if (pkg_jobs_check_local_pkg (j, &jfp) != EPKG_OK) {
					pkg_emit_error("%s is not installed, therefore upgrade is impossible",
							jfp.pattern);
@@ -1007,9 +987,8 @@ pkg_jobs_find_remote_pattern(struct pkg_jobs *j, struct job_pattern *jp,
			}
			pkg->type = PKG_FILE;
			pkg_jobs_add_req(j, pkg);
-
			pkg_get(pkg, PKG_UNIQUEID, &uid);

-
			HASH_FIND_STR(j->request_add, uid, req);
+
			HASH_FIND_STR(j->request_add, pkg->uid, req);
			if (req != NULL)
				req->item->jp = jp;

@@ -1030,8 +1009,6 @@ bool
pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
{
	int ret, ret1, ret2;
-
	const char *lversion, *rversion, *larch, *rarch, *origin;
-
	const char *ldigest, *rdigest;
	struct pkg_option *lo = NULL, *ro = NULL;
	struct pkg_dep *ld = NULL, *rd = NULL;
	struct pkg_shlib *ls = NULL, *rs = NULL;
@@ -1048,12 +1025,8 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
		return (false);
	}

-
	pkg_get(lp, PKG_VERSION, &lversion, PKG_ARCH, &larch, PKG_ORIGIN, &origin,
-
			PKG_DIGEST, &ldigest);
-
	pkg_get(rp, PKG_VERSION, &rversion, PKG_ARCH, &rarch, PKG_DIGEST, &rdigest);
-

-
	if (ldigest != NULL && rdigest != NULL &&
-
			strcmp(ldigest, rdigest) == 0) {
+
	if (lp->digest != NULL && rp->digest != NULL &&
+
	    strcmp(lp->digest, rp->digest) == 0) {
		/* Remote and local packages has the same digest, hence they are the same */
		return (false);
	}
@@ -1061,15 +1034,16 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
	 * XXX: for a remote package we also need to check whether options
	 * are compatible.
	 */
-
	ret = pkg_version_cmp(lversion, rversion);
+
	ret = pkg_version_cmp(lp->version, rp->version);
	if (ret > 0)
		return (false);
	else if (ret < 0)
		return (true);

	/* Compare archs */
-
	if (strcmp (larch, rarch) != 0) {
-
		pkg_set(rp, PKG_REASON, "ABI changed");
+
	if (strcmp (lp->arch, rp->arch) != 0) {
+
		free(rp->reason);
+
		rp->reason = strdup("ABI changed");
		return (true);
	}

@@ -1078,13 +1052,15 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
		ret1 = pkg_options(rp, &ro);
		ret2 = pkg_options(lp, &lo);
		if (ret1 != ret2) {
-
			pkg_set(rp, PKG_REASON, "options changed");
+
			free(rp->reason);
+
			rp->reason = strdup("options changed");
			return (true);
		}
		if (ret1 == EPKG_OK) {
			if (strcmp(pkg_option_opt(lo), pkg_option_opt(ro)) != 0 ||
					strcmp(pkg_option_value(lo), pkg_option_value(ro)) != 0) {
-
				pkg_set(rp, PKG_REASON, "options changed");
+
				free(rp->reason);
+
				rp->reason = strdup("options changed");
				return (true);
			}
		}
@@ -1097,13 +1073,15 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
		ret1 = pkg_deps(rp, &rd);
		ret2 = pkg_deps(lp, &ld);
		if (ret1 != ret2) {
-
			pkg_set(rp, PKG_REASON, "direct dependency changed");
+
			free(rp->reason);
+
			rp->reason = strdup("direct dependency changed");
			return (true);
		}
		if (ret1 == EPKG_OK) {
			if (strcmp(pkg_dep_get(rd, PKG_DEP_NAME),
					pkg_dep_get(ld, PKG_DEP_NAME)) != 0) {
-
				pkg_set(rp, PKG_REASON, "direct dependency changed");
+
				free(rp->reason);
+
				rp->reason = strdup("direct dependency changed");
				return (true);
			}
		}
@@ -1116,13 +1094,15 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
		ret1 = pkg_conflicts(rp, &rc);
		ret2 = pkg_conflicts(lp, &lc);
		if (ret1 != ret2) {
-
			pkg_set(rp, PKG_REASON, "direct conflict changed");
+
			free(rp->reason);
+
			rp->reason = strdup("direct conflict changed");
			return (true);
		}
		if (ret1 == EPKG_OK) {
			if (strcmp(pkg_conflict_uniqueid(rc),
					pkg_conflict_uniqueid(lc)) != 0) {
-
				pkg_set(rp, PKG_REASON, "direct conflict changed");
+
				free(rp->reason);
+
				rp->reason = strdup("direct conflict changed");
				return (true);
			}
		}
@@ -1135,13 +1115,15 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
		ret1 = pkg_provides(rp, &rpr);
		ret2 = pkg_provides(lp, &lpr);
		if (ret1 != ret2) {
-
			pkg_set(rp, PKG_REASON, "provides changed");
+
			free(rp->reason);
+
			rp->reason = strdup("provides changed");
			return (true);
		}
		if (ret1 == EPKG_OK) {
			if (strcmp(pkg_provide_name(rpr),
					pkg_provide_name(lpr)) != 0) {
-
				pkg_set(rp, PKG_REASON, "provides changed");
+
				free(rp->reason);
+
				rp->reason = strdup("provides changed");
				return (true);
			}
		}
@@ -1154,13 +1136,15 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
		ret1 = pkg_shlibs_required(rp, &rs);
		ret2 = pkg_shlibs_required(lp, &ls);
		if (ret1 != ret2) {
-
			pkg_set(rp, PKG_REASON, "needed shared library changed");
+
			free(rp->reason);
+
			rp->reason = strdup("needed shared library changed");
			return (true);
		}
		if (ret1 == EPKG_OK) {
			if (strcmp(pkg_shlib_name(rs),
					pkg_shlib_name(ls)) != 0) {
-
				pkg_set(rp, PKG_REASON, "needed shared library changed");
+
				free(rp->reason);
+
				rp->reason = strdup("needed shared library changed");
				pkg_debug(1, "shlib changed %s->%s", pkg_shlib_name(ls), pkg_shlib_name(rs));
				return (true);
			}
@@ -1177,7 +1161,6 @@ pkg_jobs_propagate_automatic(struct pkg_jobs *j)
{
	struct pkg_job_universe_item *unit, *utmp, *cur, *local;
	struct pkg_job_request *req;
-
	const char *uid;
	bool automatic;

	HASH_ITER(hh, j->universe->items, unit, utmp) {
@@ -1186,18 +1169,17 @@ pkg_jobs_propagate_automatic(struct pkg_jobs *j)
			 * For packages that are alone in the installation list
			 * we search them in the corresponding request
			 */
-
			pkg_get(unit->pkg, PKG_UNIQUEID, &uid);
-
			HASH_FIND_STR(j->request_add, uid, req);
+
			HASH_FIND_STR(j->request_add, unit->pkg->uid, req);
			if ((req == NULL || req->automatic) &&
-
							unit->pkg->type != PKG_INSTALLED) {
-
				automatic = 1;
-
				pkg_debug(2, "set automatic flag for %s", uid);
-
				pkg_set(unit->pkg, PKG_AUTOMATIC, automatic);
+
			    unit->pkg->type != PKG_INSTALLED) {
+
				automatic = true;
+
				pkg_debug(2, "set automatic flag for %s", unit->pkg->uid);
+
				unit->pkg->automatic = automatic;
			}
			else {
				if (j->type == PKG_JOBS_INSTALL) {
-
					automatic = 0;
-
					pkg_set(unit->pkg, PKG_AUTOMATIC, automatic);
+
					automatic = false;
+
					unit->pkg->automatic = false;
				}
			}
		}
@@ -1207,18 +1189,18 @@ pkg_jobs_propagate_automatic(struct pkg_jobs *j)
			 * automatic flag from the local package
			 */
			local = NULL;
-
			automatic = 0;
+
			automatic = false;
			LL_FOREACH(unit, cur) {
				if (cur->pkg->type == PKG_INSTALLED) {
					local = cur;
-
					pkg_get(local->pkg, PKG_AUTOMATIC, &automatic);
+
					automatic = local->pkg->automatic;
					break;
				}
			}
			if (local != NULL)
				LL_FOREACH(unit, cur)
					if (cur->pkg->type != PKG_INSTALLED)
-
						pkg_set(cur->pkg, PKG_AUTOMATIC, automatic);
+
						cur->pkg->automatic = automatic;
		}
	}
}
@@ -1230,17 +1212,15 @@ pkg_jobs_find_deinstall_request(struct pkg_job_universe_item *item,
	struct pkg_job_request *found;
	struct pkg_job_universe_item *dep_item;
	struct pkg_dep *d = NULL;
-
	const char *uid;
	struct pkg *pkg = item->pkg;

-
	pkg_get(pkg, PKG_UNIQUEID, &uid);
	if (rec_level > 128) {
		pkg_debug(2, "cannot find deinstall request after 128 iterations for %s,"
-
				"circular dependency maybe", uid);
+
		    "circular dependency maybe", pkg->uid);
		return (NULL);
	}

-
	HASH_FIND_STR(j->request_delete, uid, found);
+
	HASH_FIND_STR(j->request_delete, pkg->uid, found);
	if (found == NULL) {
		while (pkg_deps(pkg, &d) == EPKG_OK) {
			dep_item = pkg_jobs_universe_find(j->universe, d->uid);
@@ -1261,11 +1241,9 @@ pkg_jobs_find_deinstall_request(struct pkg_job_universe_item *item,
static void
pkg_jobs_set_deinstall_reasons(struct pkg_jobs *j)
{
-
	struct sbuf *reason = sbuf_new_auto();
	struct pkg_solved *sit;
	struct pkg_job_request *jreq;
	struct pkg *req_pkg, *pkg;
-
	const char *name, *version;

	LL_FOREACH(j->jobs, sit) {
		jreq = pkg_jobs_find_deinstall_request(sit->items[0], j, 0);
@@ -1273,16 +1251,10 @@ pkg_jobs_set_deinstall_reasons(struct pkg_jobs *j)
			req_pkg = jreq->item->pkg;
			pkg = sit->items[0]->pkg;
			/* Set the reason */
-
			pkg_get(req_pkg, PKG_NAME, &name, PKG_VERSION, &version);
-
			sbuf_printf(reason, "depends on %s-%s", name, version);
-
			sbuf_finish(reason);
-

-
			pkg_set(pkg, PKG_REASON, sbuf_data(reason));
-
			sbuf_clear(reason);
+
			free(pkg->reason);
+
			pkg_asprintf(&pkg->reason, "depends on %n-%v", req_pkg, req_pkg);
		}
	}
-

-
	sbuf_delete(reason);
}

static int
@@ -1352,31 +1324,27 @@ static struct pkg_jobs_install_candidate *
pkg_jobs_new_candidate(struct pkg *pkg)
{
	struct pkg_jobs_install_candidate *n;
-
	int64_t id;

-
	pkg_get(pkg, PKG_ROWID, &id);
	n = malloc(sizeof(*n));
	if (n == NULL) {
		pkg_emit_errno("malloc", "pkg_jobs_install_candidate");
		return (NULL);
	}
-
	n->id = id;
+
	n->id = pkg->id;
	return (n);
}

static bool
pkg_jobs_check_remote_candidate(struct pkg_jobs *j, struct pkg *pkg)
{
-
	const char *digest, *uid;
	struct pkgdb_it *it;
	struct pkg *p = NULL;

-
	pkg_get(pkg, PKG_DIGEST, &digest, PKG_UNIQUEID, &uid);
	/* If we have no digest, we need to check this package */
-
	if (digest == NULL || digest[0] == '\0')
+
	if (pkg->digest == NULL)
		return (true);

-
	it = pkgdb_repo_query(j->db, uid, MATCH_EXACT, j->reponame);
+
	it = pkgdb_repo_query(j->db, pkg->uid, MATCH_EXACT, j->reponame);
	if (it != NULL) {
		/*
		 * If we have the same package in a remote repo, it is not an
@@ -1385,14 +1353,11 @@ pkg_jobs_check_remote_candidate(struct pkg_jobs *j, struct pkg *pkg)
		int npkg = 0;

		while (pkgdb_it_next(it, &p, PKG_LOAD_BASIC) == EPKG_OK) {
-
			const char *rdigest;
-

-
			pkg_get(p, PKG_DIGEST, &rdigest);
			/*
			 * Check package with the same uid and explore whether digest
			 * has been changed
			 */
-
			if (strcmp(rdigest, digest) != 0)
+
			if (strcmp(p->digest, pkg->digest) != 0)
				npkg ++;

			pkg_free(p);
@@ -1438,7 +1403,6 @@ jobs_solve_install_upgrade(struct pkg_jobs *j)
{
	struct pkg *pkg = NULL;
	struct pkgdb_it *it;
-
	char *uid;
	char sqlbuf[256];
	bool got_local;
	size_t jcount = 0;
@@ -1480,9 +1444,8 @@ jobs_solve_install_upgrade(struct pkg_jobs *j)
				pkg = NULL;
				while (pkgdb_it_next(it, &pkg, flags) == EPKG_OK) {
					/* TODO: use repository priority here */
-
					pkg_get(pkg, PKG_UNIQUEID, &uid);
					/* Do not test we ignore what doesn't exists remotely */
-
					pkg_jobs_find_upgrade(j, uid, MATCH_EXACT);
+
					pkg_jobs_find_upgrade(j, pkg->uid, MATCH_EXACT);
				}
				pkg_free(pkg);
				pkgdb_it_free(it);
@@ -1548,7 +1511,6 @@ jobs_solve_fetch(struct pkg_jobs *j)
	struct job_pattern *jp, *jtmp;
	struct pkg *pkg = NULL;
	struct pkgdb_it *it;
-
	char *uid;
	unsigned flag = PKG_LOAD_BASIC|PKG_LOAD_ANNOTATIONS;

	if ((j->flags & PKG_FLAG_WITH_DEPS) == PKG_FLAG_WITH_DEPS)
@@ -1563,9 +1525,8 @@ jobs_solve_fetch(struct pkg_jobs *j)
				pkg_emit_locked(pkg);
			}
			else {
-
				pkg_get(pkg, PKG_UNIQUEID, &uid);
				/* Do not test we ignore what doesn't exists remotely */
-
				pkg_jobs_find_upgrade(j, uid, MATCH_EXACT);
+
				pkg_jobs_find_upgrade(j, pkg->uid, MATCH_EXACT);
			}
			pkg = NULL;
		}
@@ -1784,29 +1745,22 @@ pkg_jobs_handle_install(struct pkg_solved *ps, struct pkg_jobs *j, bool handle_r
		struct pkg_manifest_key *keys)
{
	struct pkg *new, *old;
-
	const char *pkguid, *oldversion = NULL;
	struct pkg_job_request *req;
	char path[MAXPATHLEN], *target;
-
	bool automatic, upgrade = false;
	int flags = 0;
	int retcode = EPKG_FATAL;

	old = ps->items[1] ? ps->items[1]->pkg : NULL;
	new = ps->items[0]->pkg;

-
	pkg_get(new, PKG_UNIQUEID, &pkguid, PKG_AUTOMATIC, &automatic);
-
	if (old != NULL) {
-
		pkg_get(old, PKG_VERSION, &oldversion);
-
		upgrade = true;
-
	}
-

-
	HASH_FIND_STR(j->request_add, pkguid, req);
+
	HASH_FIND_STR(j->request_add, new->uid, req);
	if (req != NULL && req->item->jp != NULL && req->item->jp->is_file) {
		/*
		 * We have package as a file, set special repository name
		 */
		target = req->item->jp->path;
-
		pkg_set(new, PKG_REPONAME, "local file");
+
		free(new->reponame);
+
		new->reponame = strdup("local file");
	}
	else {
		pkg_snprintf(path, sizeof(path), "%R", new);
@@ -1815,8 +1769,8 @@ pkg_jobs_handle_install(struct pkg_solved *ps, struct pkg_jobs *j, bool handle_r
		target = path;
	}

-
	if (oldversion != NULL) {
-
		pkg_set(new, PKG_OLD_VERSION, oldversion);
+
	if (old != NULL) {
+
		new->old_version = strdup(old->version);
		pkg_emit_upgrade_begin(new, old);
	} else {
		pkg_emit_install_begin(new);
@@ -1829,7 +1783,7 @@ pkg_jobs_handle_install(struct pkg_solved *ps, struct pkg_jobs *j, bool handle_r
	if ((j->flags & PKG_FLAG_FORCE_MISSING) == PKG_FLAG_FORCE_MISSING)
		flags |= PKG_ADD_FORCE_MISSING;
	flags |= PKG_ADD_UPGRADE;
-
	if (automatic || (j->flags & PKG_FLAG_AUTOMATIC) == PKG_FLAG_AUTOMATIC)
+
	if (new->automatic || (j->flags & PKG_FLAG_AUTOMATIC) == PKG_FLAG_AUTOMATIC)
		flags |= PKG_ADD_AUTOMATIC;

#if 0
@@ -1840,26 +1794,22 @@ pkg_jobs_handle_install(struct pkg_solved *ps, struct pkg_jobs *j, bool handle_r
		}
	}
#endif
-
	if (upgrade)
+
	if (old != NULL)
		retcode = pkg_add_upgrade(j->db, target, flags, keys, NULL, new, old);
	else
		retcode = pkg_add_from_remote(j->db, target, flags, keys, NULL, new);

	if (retcode != EPKG_OK) {
		pkgdb_transaction_rollback(j->db->sqlite, "upgrade");
-
		goto cleanup;
+
		return (retcode);
	}

-
	if (upgrade)
+
	if (old != NULL)
		pkg_emit_upgrade_finished(new, old);
	else
		pkg_emit_install_finished(new);

-
	retcode = EPKG_OK;
-

-
cleanup:
-

-
	return (retcode);
+
	return (EPKG_OK);
}

static int
@@ -1868,7 +1818,6 @@ pkg_jobs_execute(struct pkg_jobs *j)
	struct pkg *p = NULL;
	struct pkg_solved *ps;
	struct pkg_manifest_key *keys = NULL;
-
	const char *name;
	int flags = 0;
	int retcode = EPKG_FATAL;
	bool handle_rc = false;
@@ -1903,10 +1852,9 @@ pkg_jobs_execute(struct pkg_jobs *j)
		case PKG_SOLVED_DELETE:
		case PKG_SOLVED_UPGRADE_REMOVE:
			p = ps->items[0]->pkg;
-
			pkg_get(p, PKG_NAME, &name);
			if (ps->type == PKG_SOLVED_DELETE &&
-
			    (strcmp(name, "pkg") == 0 ||
-
			    strcmp(name, "pkg-devel") == 0) &&
+
			    (strcmp(p->name, "pkg") == 0 ||
+
			    strcmp(p->name, "pkg-devel") == 0) &&
			    (flags & PKG_DELETE_FORCE) == 0) {
				pkg_emit_error("Cannot delete pkg itself without force flag");
				continue;
@@ -2054,7 +2002,7 @@ pkg_jobs_fetch(struct pkg_jobs *j)
	struct statfs fs;
	struct stat st;
	int64_t dlsize = 0;
-
	const char *cachedir = NULL, *repopath;
+
	const char *cachedir = NULL;
	char cachedpath[MAXPATHLEN];
	bool mirror = (j->flags & PKG_FLAG_FETCH_MIRROR) ? true : false;

@@ -2067,24 +2015,21 @@ pkg_jobs_fetch(struct pkg_jobs *j)
	/* check for available size to fetch */
	DL_FOREACH(j->jobs, ps) {
		if (ps->type != PKG_SOLVED_DELETE && ps->type != PKG_SOLVED_UPGRADE_REMOVE) {
-
			int64_t pkgsize;
-

			p = ps->items[0]->pkg;
			if (p->type != PKG_REMOTE)
				continue;

-
			pkg_get(p, PKG_PKGSIZE, &pkgsize, PKG_REPOPATH, &repopath);
			if (mirror) {
-
				snprintf(cachedpath, sizeof(cachedpath), "%s/%s", cachedir,
-
					repopath);
+
				snprintf(cachedpath, sizeof(cachedpath),
+
				   "%s/%s", cachedir, p->repopath);
			}
			else
				pkg_repo_cached_name(p, cachedpath, sizeof(cachedpath));

			if (stat(cachedpath, &st) == -1)
-
				dlsize += pkgsize;
+
				dlsize += p->pkgsize;
			else
-
				dlsize += pkgsize - st.st_size;
+
				dlsize += p->pkgsize - st.st_size;
		}
	}

modified libpkg/pkg_jobs_conflicts.c
@@ -68,8 +68,8 @@ pkg_conflicts_chain_cmp_cb(struct pkg_conflict_chain *a, struct pkg_conflict_cha
		return (a->req->skip - b->req->skip);
	}

-
	pkg_get(a->req->item->pkg, PKG_VERSION, &vera);
-
	pkg_get(b->req->item->pkg, PKG_VERSION, &verb);
+
	vera = a->req->item->pkg->version;
+
	verb = b->req->item->pkg->version;

	/* Inverse sort to get the maximum version as the first element */
	return (pkg_version_cmp(vera, verb));
@@ -79,18 +79,16 @@ static int
pkg_conflicts_request_resolve_chain(struct pkg *req, struct pkg_conflict_chain *chain)
{
	struct pkg_conflict_chain *elt, *selected = NULL;
-
	const char *name, *origin, *slash_pos;
+
	const char *slash_pos;

-
	pkg_get(req, PKG_NAME, &name);
	/*
	 * First of all prefer pure origins, where the last element of
	 * an origin is pkg name
	 */
	LL_FOREACH(chain, elt) {
-
		pkg_get(elt->req->item->pkg, PKG_ORIGIN, &origin);
-
		slash_pos = strrchr(origin, '/');
+
		slash_pos = strrchr(elt->req->item->pkg->origin, '/');
		if (slash_pos != NULL) {
-
			if (strcmp(slash_pos + 1, name) == 0) {
+
			if (strcmp(slash_pos + 1, req->name) == 0) {
				selected = elt;
				break;
			}
@@ -104,8 +102,8 @@ pkg_conflicts_request_resolve_chain(struct pkg *req, struct pkg_conflict_chain *
		selected = chain;
	}

-
	pkg_get(selected->req->item->pkg, PKG_ORIGIN, &origin);
-
	pkg_debug(2, "select %s in the chain of conflicts for %s", origin, name);
+
	pkg_debug(2, "select %s in the chain of conflicts for %s",
+
	    selected->req->item->pkg->name, req->name);
	/* Disable conflicts from a request */
	LL_FOREACH(chain, elt) {
		if (elt != selected)
@@ -135,7 +133,6 @@ pkg_conflicts_request_resolve(struct pkg_jobs *j)
	struct pkg_conflict *c, *ctmp;
	struct pkg_conflict_chain *chain;
	struct pkg_job_universe_item *unit;
-
	const char *uid;

	HASH_ITER(hh, j->request_add, req, rtmp) {
		chain = NULL;
@@ -145,8 +142,7 @@ pkg_conflicts_request_resolve(struct pkg_jobs *j)
		HASH_ITER(hh, req->item->pkg->conflicts, c, ctmp) {
			unit = pkg_jobs_universe_find(j->universe, pkg_conflict_uniqueid(c));
			if (unit != NULL) {
-
				pkg_get(unit->pkg, PKG_UNIQUEID, &uid);
-
				HASH_FIND_STR(j->request_add, uid, found);
+
				HASH_FIND_STR(j->request_add, unit->pkg->uid, found);
				if (found && !found->skip) {
					pkg_conflicts_request_add_chain(&chain, found);
				}
@@ -171,27 +167,23 @@ void
pkg_conflicts_register(struct pkg *p1, struct pkg *p2, enum pkg_conflict_type type)
{
	struct pkg_conflict *c1, *c2, *test;
-
	const char *u1, *u2;
-

-
	pkg_get(p1, PKG_UNIQUEID, &u1);
-
	pkg_get(p2, PKG_UNIQUEID, &u2);

	pkg_conflict_new(&c1);
	pkg_conflict_new(&c2);
	if (c1 != NULL && c2 != NULL) {
		c1->type = c2->type = type;
-
		HASH_FIND_STR(p1->conflicts, u2, test);
+
		HASH_FIND_STR(p1->conflicts, p2->uid, test);
		if (test == NULL) {
-
			sbuf_set(&c1->uniqueid, u2);
+
			sbuf_set(&c1->uniqueid, p2->uid);
			HASH_ADD_KEYPTR(hh, p1->conflicts, pkg_conflict_uniqueid(c1), sbuf_size(c1->uniqueid), c1);
-
			pkg_debug(2, "registering conflict between %s and %s", u1, u2);
+
			pkg_debug(2, "registering conflict between %s and %s", p1->uid, p2->uid);
		}

-
		HASH_FIND_STR(p2->conflicts, u1, test);
+
		HASH_FIND_STR(p2->conflicts, p1->uid, test);
		if (test == NULL) {
-
			sbuf_set(&c2->uniqueid, u1);
+
			sbuf_set(&c2->uniqueid, p1->uid);
			HASH_ADD_KEYPTR(hh, p2->conflicts, pkg_conflict_uniqueid(c2), sbuf_size(c2->uniqueid), c2);
-
			pkg_debug(2, "registering conflict between %s and %s", u2, u1);
+
			pkg_debug(2, "registering conflict between %s and %s", p2->uid, p1->uid);
		}
	}
}
@@ -213,19 +205,15 @@ pkg_conflicts_need_conflict(struct pkg_jobs *j, struct pkg *p1, struct pkg *p2)
{
	struct pkg_file *fcur, *ftmp, *ff;
	struct pkg_dir *df;
-
	const char *uid1, *uid2;
	struct pkg_conflict *c;

-
	pkg_get(p1, PKG_UNIQUEID, &uid1);
-
	pkg_get(p2, PKG_UNIQUEID, &uid2);
-

	assert(pkgdb_ensure_loaded(j->db, p1, PKG_LOAD_FILES|PKG_LOAD_DIRS) == EPKG_OK);
	assert(pkgdb_ensure_loaded(j->db, p2, PKG_LOAD_FILES|PKG_LOAD_DIRS) == EPKG_OK);

	/*
	 * Check if we already have this conflict registered
	 */
-
	HASH_FIND_STR(p1->conflicts, uid2, c);
+
	HASH_FIND_STR(p1->conflicts, p2->uid, c);
	if (c != NULL)
		return false;

@@ -254,23 +242,19 @@ pkg_conflicts_register_unsafe(struct pkg *p1, struct pkg *p2,
	const char *path,
	enum pkg_conflict_type type)
{
-
	const char *uid1, *uid2;
	struct pkg_conflict *c1, *c2;

-
	pkg_get(p1, PKG_UNIQUEID, &uid1);
-
	pkg_get(p2, PKG_UNIQUEID, &uid2);
-

	pkg_conflict_new(&c1);
	pkg_conflict_new(&c2);
	c1->type = c2->type = type;
-
	sbuf_set(&c1->uniqueid, uid2);
-
	sbuf_set(&c2->uniqueid, uid1);
+
	sbuf_set(&c1->uniqueid, p2->uid);
+
	sbuf_set(&c2->uniqueid, p1->uid);
	HASH_ADD_KEYPTR(hh, p1->conflicts, pkg_conflict_uniqueid(c1),
		sbuf_size(c1->uniqueid), c1);
	HASH_ADD_KEYPTR(hh, p2->conflicts, pkg_conflict_uniqueid(c2),
		sbuf_size(c2->uniqueid), c2);
	pkg_debug(2, "registering conflict between %s and %s on path %s",
-
		uid1, uid2, path);
+
		p1->uid, p2->uid, path);
}

/*
@@ -281,7 +265,6 @@ pkg_conflicts_register_chain(struct pkg_jobs *j, struct pkg_job_universe_item *u
	struct pkg_job_universe_item *u2, const char *path)
{
	struct pkg_job_universe_item *cur1, *cur2;
-
	const char *uid1, *uid2;
	bool ret = false;

	cur1 = u1;
@@ -291,8 +274,6 @@ pkg_conflicts_register_chain(struct pkg_jobs *j, struct pkg_job_universe_item *u
		cur2 = u2;
		do {
			struct pkg *p1 = cur1->pkg, *p2 = cur2->pkg;
-
			pkg_get(p1, PKG_UNIQUEID, &uid1);
-
			pkg_get(p2, PKG_UNIQUEID, &uid2);

			if (p1->type == PKG_INSTALLED && p2->type == PKG_INSTALLED) {
				/* Local and local packages cannot conflict */
@@ -343,7 +324,6 @@ pkg_conflicts_check_local_path(const char *path, const char *uid,
	int ret;
	struct pkg *p = NULL;
	struct pkg_conflict *c;
-
	const char *uido;

	pkg_debug(4, "Pkgdb: running '%s'", sql_local_conflict);
	ret = sqlite3_prepare_v2(j->db->sqlite, sql_local_conflict, -1,
@@ -369,8 +349,7 @@ pkg_conflicts_check_local_path(const char *path, const char *uid,
			uid_local, 0);
		assert(p != NULL);

-
		pkg_get(p, PKG_UNIQUEID, &uido);
-
		assert(strcmp(uid, uido) != 0);
+
		assert(strcmp(uid, p->uid) != 0);

		HASH_FIND_STR(p->conflicts, uid, c);
		if (c == NULL) {
@@ -414,8 +393,8 @@ pkg_conflicts_check_all_paths(struct pkg_jobs *j, const char *path,
		if (cit->item == it)
			return (NULL);

-
		pkg_get(it->pkg, PKG_UNIQUEID, &uid1);
-
		pkg_get(cit->item->pkg, PKG_UNIQUEID, &uid2);
+
		uid1 = it->pkg->uid;
+
		uid2 = cit->item->pkg->uid;
		if (strcmp(uid1, uid2) == 0) {
			/* The same upgrade chain, just upgrade item for speed */
			cit->item = it;
@@ -450,13 +429,10 @@ pkg_conflicts_check_chain_conflict(struct pkg_job_universe_item *it,
	struct pkg_job_universe_item *local, struct pkg_jobs *j)
{
	struct pkg_file *fcur, *ftmp, *ff;
-
	const char *uid;
	struct pkg *p;
	struct pkg_job_universe_item *cun;
	struct sipkey *k;

-
	pkg_get(it->pkg, PKG_UNIQUEID, &uid);
-

	HASH_ITER(hh, it->pkg->files, fcur, ftmp) {
		k = pkg_conflicts_sipkey_init();
		/* Check in hash tree */
@@ -469,7 +445,7 @@ pkg_conflicts_check_chain_conflict(struct pkg_job_universe_item *it,
				continue;
		}
		/* Check for local conflict in db */
-
		p = pkg_conflicts_check_local_path(fcur->path, uid, j);
+
		p = pkg_conflicts_check_local_path(fcur->path, it->pkg->uid, j);
		if (p != NULL) {
			pkg_jobs_universe_process_item(j->universe, p, &cun);
			assert(cun != NULL);
modified libpkg/pkg_jobs_universe.c
@@ -155,23 +155,19 @@ pkg_jobs_universe_add_pkg(struct pkg_jobs_universe *universe, struct pkg *pkg,
		bool force, struct pkg_job_universe_item **found)
{
	struct pkg_job_universe_item *item, *tmp = NULL;
-
	const char *uid, *digest, *version, *name;
	struct pkg_job_seen *seen;

	pkg_validate(pkg);
-
	pkg_get(pkg, PKG_UNIQUEID, &uid, PKG_DIGEST, &digest,
-
			PKG_VERSION, &version, PKG_NAME, &name);
-
	if (digest == NULL) {
-
		pkg_debug(3, "no digest found for package %s (%s-%s)", uid,
-
				name, version);
+
	if (pkg->digest == NULL) {
+
		pkg_debug(3, "no digest found for package %s (%s-%s)",
+
		    pkg->uid, pkg->name, pkg->version);
		if (pkg_checksum_calculate(pkg, universe->j->db) != EPKG_OK) {
			*found = NULL;
			return (EPKG_FATAL);
		}
-
		pkg_get(pkg, PKG_DIGEST, &digest);
	}

-
	HASH_FIND_STR(universe->seen, digest, seen);
+
	HASH_FIND_STR(universe->seen, pkg->digest, seen);
	if (seen != NULL && !force) {
		if (found != NULL)
			*found = seen->un;
@@ -180,8 +176,8 @@ pkg_jobs_universe_add_pkg(struct pkg_jobs_universe *universe, struct pkg *pkg,
	}

	pkg_debug(2, "universe: add new %s pkg: %s, (%s-%s:%s)",
-
				(pkg->type == PKG_INSTALLED ? "local" : "remote"), uid,
-
				name, version, digest);
+
	    (pkg->type == PKG_INSTALLED ? "local" : "remote"), pkg->uid,
+
	    pkg->name, pkg->version, pkg->digest);

	item = calloc(1, sizeof (struct pkg_job_universe_item));
	if (item == NULL) {
@@ -192,9 +188,9 @@ pkg_jobs_universe_add_pkg(struct pkg_jobs_universe *universe, struct pkg *pkg,
	item->pkg = pkg;


-
	HASH_FIND_STR(universe->items, uid, tmp);
+
	HASH_FIND_STR(universe->items, pkg->uid, tmp);
	if (tmp == NULL)
-
		HASH_ADD_KEYPTR(hh, universe->items, uid, strlen(uid), item);
+
		HASH_ADD_KEYPTR(hh, universe->items, pkg->uid, strlen(pkg->uid), item);

	DL_APPEND(tmp, item);

@@ -204,7 +200,7 @@ pkg_jobs_universe_add_pkg(struct pkg_jobs_universe *universe, struct pkg *pkg,
			pkg_emit_errno("pkg_jobs_universe_add_pkg", "calloc: struct pkg_job_seen)");
			return (EPKG_FATAL);
		}
-
		seen->digest = digest;
+
		seen->digest = pkg->digest;
		seen->un = item;
		HASH_ADD_KEYPTR(hh, universe->seen, seen->digest, strlen(seen->digest),
			seen);
@@ -265,11 +261,8 @@ pkg_jobs_universe_process_deps(struct pkg_jobs_universe *universe,
		}

		if (npkg == NULL && rpkg == NULL) {
-
			const char *name;
-

-
			pkg_get(pkg, PKG_NAME, &name);
			pkg_emit_error("%s has a missing dependency: %s",
-
				name, pkg_dep_get(d, PKG_DEP_NAME));
+
				pkg->name, pkg_dep_get(d, PKG_DEP_NAME));

			if (flags & DEPS_FLAG_FORCE_MISSING)
				continue;
@@ -284,10 +277,7 @@ pkg_jobs_universe_process_deps(struct pkg_jobs_universe *universe,
		if (rpkg != NULL) {
			if (npkg != NULL) {
				/* Save automatic flag */
-
				bool automatic;
-

-
				pkg_get(npkg, PKG_AUTOMATIC, &automatic);
-
				pkg_set(rpkg, PKG_AUTOMATIC, automatic);
+
				rpkg->automatic = npkg->automatic;
			}

			pkg_jobs_universe_process_item(universe, rpkg, NULL);
@@ -367,11 +357,8 @@ pkg_jobs_universe_process_shlibs(struct pkg_jobs_universe *universe,
			rpkg = NULL;
			prhead = NULL;
			while (pkgdb_it_next(it, &rpkg, flags) == EPKG_OK) {
-
				const char *digest, *uid;
-

-
				pkg_get(rpkg, PKG_DIGEST, &digest, PKG_UNIQUEID, &uid);
				/* Check for local packages */
-
				HASH_FIND_STR(universe->items, uid, unit);
+
				HASH_FIND_STR(universe->items, rpkg->uid, unit);
				if (unit != NULL) {
					if (pkg_jobs_need_upgrade (rpkg, unit->pkg)) {
						/* Remote provide is newer, so we can add it */
@@ -384,7 +371,7 @@ pkg_jobs_universe_process_shlibs(struct pkg_jobs_universe *universe,
				}
				else {
					/* Maybe local package has just been not added */
-
					npkg = pkg_jobs_universe_get_local(universe, uid, 0);
+
					npkg = pkg_jobs_universe_get_local(universe, rpkg->uid, 0);
					if (npkg != NULL) {
						if (pkg_jobs_universe_process_item(universe, npkg,
							&unit) != EPKG_OK)
@@ -402,14 +389,13 @@ pkg_jobs_universe_process_shlibs(struct pkg_jobs_universe *universe,
				if (unit == NULL) {
					struct pkg_job_seen *seen;

-
					if (digest == NULL) {
-
						pkg_debug(3, "no digest found for package %s", uid);
+
					if (rpkg->digest == NULL) {
+
						pkg_debug(3, "no digest found for package %s", rpkg->uid);
						if (pkg_checksum_calculate(pkg, universe->j->db) != EPKG_OK) {
							return (EPKG_FATAL);
						}
-
						pkg_get(pkg, PKG_DIGEST, &digest);
					}
-
					HASH_FIND_STR(universe->seen, digest, seen);
+
					HASH_FIND_STR(universe->seen, rpkg->digest, seen);
					if (seen == NULL) {
						pkg_jobs_universe_process_item(universe, rpkg,
							&unit);
@@ -443,11 +429,8 @@ pkg_jobs_universe_process_shlibs(struct pkg_jobs_universe *universe,
			}
			pkgdb_it_free(it);
			if (prhead == NULL) {
-
				const char *name;
-

-
				pkg_get(pkg, PKG_NAME, &name);
				pkg_debug(1, "cannot find packages that provide %s required for %s",
-
					pkg_shlib_name(shlib), name);
+
				    pkg_shlib_name(shlib), pkg->name);
				/*
				 * XXX: this is not normal but it is very common for the existing
				 * repos, hence we just ignore this stale dependency
@@ -550,7 +533,6 @@ pkg_jobs_update_universe_item_priority(struct pkg_jobs_universe *universe,
		struct pkg_job_universe_item *item, int priority,
		enum pkg_priority_update_type type)
{
-
	const char *uid, *digest;
	struct pkg_dep *d = NULL;
	struct pkg_conflict *c = NULL;
	struct pkg_job_universe_item *found, *cur, *it;
@@ -566,24 +548,22 @@ pkg_jobs_update_universe_item_priority(struct pkg_jobs_universe *universe,
		return;
	}
	else if (priority + 10 > RECURSION_LIMIT) {
-
		pkg_get(item->pkg, PKG_UNIQUEID, &uid);
		pkg_debug(2, "approaching recursion limit at %d, while processing of"
-
					" package %s", priority, uid);
+
		    " package %s", priority, item->pkg->uid);
	}

	LL_FOREACH(item, it) {
-

-
		pkg_get(it->pkg, PKG_UNIQUEID, &uid, PKG_DIGEST, &digest);
		if ((item->next != NULL || item->prev != NULL) &&
-
				it->pkg->type != PKG_INSTALLED &&
-
				(type == PKG_PRIORITY_UPDATE_CONFLICT ||
-
				 type == PKG_PRIORITY_UPDATE_DELETE)) {
+
		    it->pkg->type != PKG_INSTALLED &&
+
		    (type == PKG_PRIORITY_UPDATE_CONFLICT ||
+
		     type == PKG_PRIORITY_UPDATE_DELETE)) {
			/*
			 * We do not update priority of a remote part of conflict, as we know
			 * that remote packages should not contain conflicts (they should be
			 * resolved in request prior to calling of this function)
			 */
-
			pkg_debug(4, "skip update priority for %s-%s", uid, digest);
+
			pkg_debug(4, "skip update priority for %s-%s",
+
			    it->pkg->uid, it->pkg->digest);
			continue;
		}
		if (it->priority > priority)
@@ -591,7 +571,7 @@ pkg_jobs_update_universe_item_priority(struct pkg_jobs_universe *universe,

		is_local = it->pkg->type == PKG_INSTALLED ? "local" : "remote";
		pkg_debug(2, "universe: update %s priority of %s(%s): %d -> %d, reason: %d",
-
				is_local, uid, digest, it->priority, priority, type);
+
		    is_local, it->pkg->uid, it->pkg->digest, it->priority, priority, type);
		it->priority = priority;

		if (type == PKG_PRIORITY_UPDATE_DELETE) {
@@ -777,11 +757,8 @@ pkg_jobs_universe_change_uid(struct pkg_jobs_universe *universe,
	struct pkg_job_universe_item *found;

	struct pkg *lp;
-
	const char *old_uid;
	struct pkg_job_replace *replacement;

-
	pkg_get(unit->pkg, PKG_UNIQUEID, &old_uid);
-

	if (update_rdeps) {
		/* For all rdeps update deps accordingly */
		while (pkg_rdeps(unit->pkg, &rd) == EPKG_OK) {
@@ -795,7 +772,7 @@ pkg_jobs_universe_change_uid(struct pkg_jobs_universe *universe,

			if (found != NULL) {
				while (pkg_deps(found->pkg, &d) == EPKG_OK) {
-
					if (strcmp(d->uid, old_uid) == 0) {
+
					if (strcmp(d->uid, unit->pkg->uid) == 0) {
						free(d->uid);
						d->uid = strdup(new_uid);
					}
@@ -806,15 +783,14 @@ pkg_jobs_universe_change_uid(struct pkg_jobs_universe *universe,

	replacement = calloc(1, sizeof(*replacement));
	if (replacement != NULL) {
-
		replacement->old_uid = strdup(old_uid);
+
		replacement->old_uid = strdup(unit->pkg->uid);
		replacement->new_uid = strdup(new_uid);
		LL_PREPEND(universe->uid_replaces, replacement);
	}

	HASH_DELETE(hh, universe->items, unit);
-
	pkg_set(unit->pkg, PKG_UNIQUEID, new_uid);
-
	/* This involves copying, so we need to get new_uid from a persistent storage */
-
	pkg_get(unit->pkg, PKG_UNIQUEID, &new_uid);
+
	free(unit->pkg->uid);
+
	unit->pkg->uid = strdup(new_uid);

	HASH_FIND(hh, universe->items, new_uid, uidlen, found);
	if (found != NULL)
modified libpkg/pkg_manifest.c
@@ -275,27 +275,59 @@ pkg_string(struct pkg *pkg, const ucl_object_t *obj, int attr)
	{
	case PKG_LICENSE_LOGIC:
		if (!strcmp(str, "single"))
-
			pkg_set(pkg, PKG_LICENSE_LOGIC, (int64_t) LICENSE_SINGLE);
+
			pkg->licenselogic = LICENSE_SINGLE;
		else if (!strcmp(str, "or") ||
		         !strcmp(str, "dual"))
-
			pkg_set(pkg, PKG_LICENSE_LOGIC, (int64_t)LICENSE_OR);
+
			pkg->licenselogic = LICENSE_OR;
		else if (!strcmp(str, "and") ||
		         !strcmp(str, "multi"))
-
			pkg_set(pkg, PKG_LICENSE_LOGIC, (int64_t)LICENSE_AND);
+
			pkg->licenselogic = LICENSE_AND;
		else {
			pkg_emit_error("Unknown license logic: %s", str);
			ret = EPKG_FATAL;
		}
		break;
-
	default:
-
		if (attr == PKG_DESC) {
-
			urldecode(str, &buf);
-
			sbuf_finish(buf);
-
			str = sbuf_data(buf);
-
		}
-
		ret = pkg_set(pkg, attr, str);
-
		if (buf != NULL)
-
			sbuf_delete(buf);
+
	case PKG_ABI:
+
		pkg->abi = strdup(str);
+
		break;
+
	case PKG_ARCH:
+
		pkg->arch = strdup(str);
+
		break;
+
	case PKG_COMMENT:
+
		pkg->comment = strdup(str);
+
		break;
+
	case PKG_DESC:
+
		urldecode(str, &buf);
+
		sbuf_finish(buf);
+
		pkg->desc = strdup(sbuf_data(buf));
+
		sbuf_delete(buf);
+
		break;
+
	case PKG_MAINTAINER:
+
		pkg->maintainer = strdup(str);
+
		break;
+
	case PKG_MESSAGE:
+
		pkg->message = strdup(str);
+
		break;
+
	case PKG_NAME:
+
		pkg->name = strdup(str);
+
		break;
+
	case PKG_ORIGIN:
+
		pkg->origin = strdup(str);
+
		break;
+
	case PKG_PREFIX:
+
		pkg->prefix = strdup(str);
+
		break;
+
	case PKG_REPOPATH:
+
		pkg->repopath = strdup(str);
+
		break;
+
	case PKG_CKSUM:
+
		pkg->sum = strdup(str);
+
		break;
+
	case PKG_VERSION:
+
		pkg->version = strdup(str);
+
		break;
+
	case PKG_WWW:
+
		pkg->www = strdup(str);
		break;
	}

@@ -305,7 +337,15 @@ pkg_string(struct pkg *pkg, const ucl_object_t *obj, int attr)
static int
pkg_int(struct pkg *pkg, const ucl_object_t *obj, int attr)
{
-
	return (pkg_set(pkg, attr, ucl_object_toint(obj)));
+
	switch (attr) {
+
	case PKG_FLATSIZE:
+
		pkg->flatsize = ucl_object_toint(obj);
+
		break;
+
	case PKG_PKGSIZE:
+
		pkg->pkgsize = ucl_object_toint(obj);
+
		break;
+
	}
+
	return (EPKG_OK);
}

static int
@@ -838,14 +878,12 @@ pkg_emit_filelist(struct pkg *pkg, FILE *f)
{
	ucl_object_t *obj = NULL, *seq;
	struct pkg_file *file = NULL;
-
	const char *name, *origin, *version;
	struct sbuf *b = NULL;

-
	pkg_get(pkg, PKG_NAME, &name, PKG_ORIGIN, &origin, PKG_VERSION, &version);
	obj = ucl_object_typed_new(UCL_OBJECT);
-
	ucl_object_insert_key(obj, ucl_object_fromstring(origin), "origin", 6, false);
-
	ucl_object_insert_key(obj, ucl_object_fromstring(name), "name", 4, false);
-
	ucl_object_insert_key(obj, ucl_object_fromstring(version), "version", 7, false);
+
	ucl_object_insert_key(obj, ucl_object_fromstring(pkg->origin), "origin", 6, false);
+
	ucl_object_insert_key(obj, ucl_object_fromstring(pkg->name), "name", 4, false);
+
	ucl_object_insert_key(obj, ucl_object_fromstring(pkg->version), "version", 7, false);

	seq = NULL;
	while (pkg_files(pkg, &file) == EPKG_OK) {
@@ -882,62 +920,55 @@ pkg_emit_object(struct pkg *pkg, short flags)
	struct pkg_config_file	*cf       = NULL;
	struct sbuf		*tmpsbuf  = NULL;
	int i;
-
	const char *comment, *desc, *message, *repopath, *abi;
	const char *script_types = NULL;
	char legacyarch[BUFSIZ];
-
	lic_t licenselogic;
-
	int64_t pkgsize;
	ucl_object_iter_t it = NULL;
	ucl_object_t *annotations, *categories, *licenses;
	ucl_object_t *map, *seq, *submap;
	ucl_object_t *top = ucl_object_typed_new(UCL_OBJECT);
	const ucl_object_t *o;
	const char *key;
-
	int recopies[] = {
-
		PKG_NAME,
-
		PKG_ORIGIN,
-
		PKG_VERSION,
-
		PKG_ABI,
-
		PKG_ARCH,
-
		PKG_MAINTAINER,
-
		PKG_PREFIX,
-
		PKG_WWW,
-
		PKG_CKSUM,
-
		PKG_FLATSIZE,
-
		-1
-
	};
	size_t key_len;

-
	pkg_get(pkg, PKG_COMMENT, &comment, PKG_LICENSE_LOGIC, &licenselogic,
-
	    PKG_DESC, &desc, PKG_MESSAGE, &message, PKG_PKGSIZE, &pkgsize,
+
	pkg_get(pkg, 
	    PKG_ANNOTATIONS, &annotations, PKG_LICENSES, &licenses,
-
	    PKG_CATEGORIES, &categories, PKG_REPOPATH, &repopath);
+
	    PKG_CATEGORIES, &categories);

-
	pkg_get(pkg, PKG_ABI, &abi);
-
	pkg_arch_to_legacy(abi, legacyarch, BUFSIZ);
-
	pkg_set(pkg, PKG_ARCH, legacyarch);
+
	pkg_arch_to_legacy(pkg->abi, legacyarch, BUFSIZ);
+
	pkg->arch = strdup(pkg->arch);
	pkg_debug(4, "Emitting basic metadata");
-
	for (i = 0; recopies[i] != -1; i++) {
-
		key = pkg_keys[recopies[i]].name;
-
		if ((o = ucl_object_find_key(pkg->fields, key)))
-
			ucl_object_insert_key(top, ucl_object_ref(o),
-
			    key, strlen(key), false);
-
	}
-
	if (comment)
-
		ucl_object_insert_key(top, ucl_object_fromstring_common(comment, 0,
-
			UCL_STRING_TRIM), "comment", 7, false);
+
	ucl_object_insert_key(top, ucl_object_fromstring_common(pkg->name, 0,
+
	    UCL_STRING_TRIM), "name", 4, false);
+
	ucl_object_insert_key(top, ucl_object_fromstring_common(pkg->origin, 0,
+
	    UCL_STRING_TRIM), "origin", 6, false);
+
	ucl_object_insert_key(top, ucl_object_fromstring_common(pkg->version, 0,
+
	    UCL_STRING_TRIM), "version", 6, false);
+
	ucl_object_insert_key(top, ucl_object_fromstring_common(pkg->comment, 0,
+
	    UCL_STRING_TRIM), "comment", 7, false);
+
	ucl_object_insert_key(top, ucl_object_fromstring_common(pkg->maintainer, 0,
+
	    UCL_STRING_TRIM), "maintainer", 10, false);
+
	ucl_object_insert_key(top, ucl_object_fromstring_common(pkg->www, 0,
+
	    UCL_STRING_TRIM), "www", 3, false);
+
	ucl_object_insert_key(top, ucl_object_fromstring_common(pkg->abi, 0,
+
	    UCL_STRING_TRIM), "abi", 3, false);
+
	ucl_object_insert_key(top, ucl_object_fromstring_common(pkg->arch, 0,
+
	    UCL_STRING_TRIM), "arch", 3, false);
+
	ucl_object_insert_key(top, ucl_object_fromstring_common(pkg->prefix, 0,
+
	    UCL_STRING_TRIM), "prefix", 6, false);
+
	ucl_object_insert_key(top, ucl_object_fromstring_common(pkg->sum, 0,
+
	    UCL_STRING_TRIM), "sum", 3, false);
+
	ucl_object_insert_key(top, ucl_object_fromint(pkg->flatsize), "flatsize", 8, false);
	/*
	 * XXX: dirty hack to be compatible with pkg 1.2
	 */
-
	if (repopath) {
+
	if (pkg->repopath) {
		ucl_object_insert_key(top,
-
			ucl_object_fromstring(repopath), "path", sizeof("path") - 1, false);
+
			ucl_object_fromstring(pkg->repopath), "path", 4, false);
		ucl_object_insert_key(top,
-
			ucl_object_fromstring(repopath), "repopath", sizeof("repopath") - 1,
-
			false);
+
			ucl_object_fromstring(pkg->repopath), "repopath", 8, false);
	}

-
	switch (licenselogic) {
+
	switch (pkg->licenselogic) {
	case LICENSE_SINGLE:
		ucl_object_insert_key(top, ucl_object_fromlstring("single", 6), "licenselogic", 12, false);
		break;
@@ -954,11 +985,11 @@ pkg_emit_object(struct pkg *pkg, short flags)
		ucl_object_insert_key(top,
		    ucl_object_ref(licenses), "licenses", 8, false);

-
	if (pkgsize > 0)
-
		ucl_object_insert_key(top, ucl_object_fromint(pkgsize), "pkgsize", 7, false);
+
	if (pkg->pkgsize > 0)
+
		ucl_object_insert_key(top, ucl_object_fromint(pkg->pkgsize), "pkgsize", 7, false);

-
	if (desc != NULL) {
-
		urlencode(desc, &tmpsbuf);
+
	if (pkg->desc != NULL) {
+
		urlencode(pkg->desc, &tmpsbuf);
		ucl_object_insert_key(top,
			ucl_object_fromstring_common(sbuf_data(tmpsbuf), sbuf_len(tmpsbuf), UCL_STRING_TRIM),
			"desc", 4, false);
@@ -1168,8 +1199,8 @@ pkg_emit_object(struct pkg *pkg, short flags)
	}

	pkg_debug(4, "Emitting message");
-
	if (message != NULL && *message != '\0') {
-
		urlencode(message, &tmpsbuf);
+
	if (pkg->message != NULL) {
+
		urlencode(pkg->message, &tmpsbuf);
		ucl_object_insert_key(top,
		    ucl_object_fromstring_common(sbuf_data(tmpsbuf), sbuf_len(tmpsbuf), UCL_STRING_TRIM),
		    "message", 7, false);
modified libpkg/pkg_old.c
@@ -56,8 +56,6 @@ static const char * const scripts[] = {
int
pkg_old_load_from_path(struct pkg *pkg, const char *path)
{
-
	char *desc;
-
	char *www;
	char fpath[MAXPATHLEN];
	regex_t preg;
	regmatch_t pmatch[2];
@@ -95,17 +93,14 @@ pkg_old_load_from_path(struct pkg *pkg, const char *path)
	}

	pkg_get_myarch(myarch, BUFSIZ);
-
	pkg_set(pkg, PKG_ARCH, myarch);
-
	pkg_set(pkg, PKG_MAINTAINER, "unknown");
-
	pkg_get(pkg, PKG_DESC, &desc);
+
	pkg->arch = strdup(myarch);
+
	pkg->maintainer = strdup("unknown");
	regcomp(&preg, "^WWW:[[:space:]]*(.*)$", REG_EXTENDED|REG_ICASE|REG_NEWLINE);
-
	if (regexec(&preg, desc, 2, pmatch, 0) == 0) {
+
	if (regexec(&preg, pkg->desc, 2, pmatch, 0) == 0) {
		size = pmatch[1].rm_eo - pmatch[1].rm_so;
-
		www = strndup(&desc[pmatch[1].rm_so], size);
-
		pkg_set(pkg, PKG_WWW, www);
-
		free(www);
+
		pkg->www = strndup(&pkg->desc[pmatch[1].rm_so], size);
	} else {
-
		pkg_set(pkg, PKG_WWW, "UNKNOWN");
+
		pkg->www = strdup("UNKNOWN");
	}
	regfree(&preg);

modified libpkg/pkg_ports.c
@@ -176,19 +176,15 @@ sbuf_append(struct sbuf *buf, __unused const char *comment, const char *str, ...
static int
setprefix(struct plist *p, char *line, struct file_attr *a)
{
-
	char *pkgprefix;
-

	/* if no arguments then set default prefix */
	if (line[0] == '\0') {
-
		pkg_get(p->pkg, PKG_PREFIX, &pkgprefix);
-
		strlcpy(p->prefix, pkgprefix, sizeof(p->prefix));
+
		strlcpy(p->prefix, p->pkg->prefix, sizeof(p->prefix));
	}
	else
		strlcpy(p->prefix, line, sizeof(p->prefix));

-
	pkg_get(p->pkg, PKG_PREFIX, &pkgprefix);
-
	if (pkgprefix == NULL || *pkgprefix == '\0')
-
		pkg_set(p->pkg, PKG_PREFIX, line);
+
	if (p->pkg->prefix == NULL)
+
		p->pkg->prefix = strdup(line);

	p->slash = p->prefix[strlen(p->prefix) -1] == '/' ? "" : "/";

@@ -204,18 +200,17 @@ setprefix(struct plist *p, char *line, struct file_attr *a)
static int
name_key(struct plist *p, char *line, struct file_attr *a)
{
-
	char *name;
	char *tmp;

-
	pkg_get(p->pkg, PKG_NAME, &name);
-
	if (name != NULL && *name != '\0') {
+
	if (p->pkg->name != NULL) {
		free(a);
		return (EPKG_OK);
	}
	tmp = strrchr(line, '-');
	tmp[0] = '\0';
	tmp++;
-
	pkg_set(p->pkg, PKG_NAME, line, PKG_VERSION, tmp);
+
	p->pkg->name = strdup(line);
+
	p->pkg->version = strdup(tmp);

	free(a);
	return (EPKG_OK);
@@ -499,7 +494,8 @@ comment_key(struct plist *p, char *line, struct file_attr *a)
		p->pkgdep = NULL;
	} else if (strncmp(line, "ORIGIN:", 7) == 0) {
		line += 7;
-
		pkg_set(p->pkg, PKG_ORIGIN, line);
+
		free(p->pkg->origin);
+
		p->pkg->origin = strdup(line);
	} else if (strncmp(line, "OPTIONS:", 8) == 0) {
		line += 8;
		/* OPTIONS:+OPTION -OPTION */
@@ -1123,16 +1119,14 @@ struct plist *
plist_new(struct pkg *pkg, const char *stage)
{
	struct plist *p;
-
	const char *prefix;

	p = calloc(1, sizeof(struct plist));
	if (p == NULL)
		return (NULL);

	p->pkg = pkg;
-
	pkg_get(pkg, PKG_PREFIX, &prefix);
-
	if (prefix != NULL)
-
		strlcpy(p->prefix, prefix, sizeof(p->prefix));
+
	if (pkg->prefix != NULL)
+
		strlcpy(p->prefix, pkg->prefix, sizeof(p->prefix));
	p->slash = p->prefix[strlen(p->prefix) - 1] == '/' ? "" : "/";
	p->stage = stage;
	p->uname = strdup("root");
@@ -1207,7 +1201,7 @@ ports_parse_plist(struct pkg *pkg, const char *plist, const char *stage)

	free(line);

-
	pkg_set(pkg, PKG_FLATSIZE, pplist->flatsize);
+
	pkg->flatsize = pplist->flatsize;

	flush_script_buffer(pplist->pre_install_buf, pkg,
	    PKG_SCRIPT_PRE_INSTALL);
modified libpkg/pkg_printf.c
@@ -1236,10 +1236,8 @@ struct sbuf *
format_message(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*message;

-
	pkg_get(pkg, PKG_MESSAGE, &message);
-
	return (string_val(sbuf, message, p));
+
	return (string_val(sbuf, pkg->message, p));
}

/*
@@ -1251,7 +1249,7 @@ format_repo_ident(struct sbuf *sbuf, const void *data, struct percent_esc *p)
	const struct pkg	*pkg = data;
	const char		*reponame;

-
	pkg_get(pkg, PKG_REPONAME, &reponame);
+
	reponame = pkg->reponame;
	if (reponame == NULL) {
		reponame = pkg_getannotation(pkg, "repository");
		if (reponame == NULL)
@@ -1343,10 +1341,8 @@ struct sbuf *
format_repo_path(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*repo_path;

-
	pkg_get(pkg, PKG_REPOPATH, &repo_path);
-
	return (string_val(sbuf, repo_path, p));
+
	return (string_val(sbuf, pkg->repopath, p));
}

/*
@@ -1422,10 +1418,8 @@ struct sbuf *
format_old_version(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*old_version;

-
	pkg_get(pkg, PKG_OLD_VERSION, &old_version);
-
	return (string_val(sbuf, old_version, p));
+
	return (string_val(sbuf, pkg->old_version, p));
}

/*
@@ -1437,10 +1431,8 @@ struct sbuf *
format_autoremove(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	bool			 automatic;

-
	pkg_get(pkg, PKG_AUTOMATIC, &automatic);
-
	return (bool_val(sbuf, automatic, p));
+
	return (bool_val(sbuf, pkg->automatic, p));
}


@@ -1483,10 +1475,8 @@ struct sbuf *
format_comment(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*comment;

-
	pkg_get(pkg, PKG_COMMENT, &comment);
-
	return (string_val(sbuf, comment, p));
+
	return (string_val(sbuf, pkg->comment, p));
}

/*
@@ -1576,10 +1566,8 @@ struct sbuf *
format_description(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*desc;

-
	pkg_get(pkg, PKG_DESC, &desc);
-
	return (string_val(sbuf, desc, p));
+
	return (string_val(sbuf, pkg->desc, p));
}

/*
@@ -1604,10 +1592,8 @@ struct sbuf *
format_license_logic(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	int64_t			 licenselogic;

-
	pkg_get(pkg, PKG_LICENSE_LOGIC, &licenselogic);
-
	return (liclog_val(sbuf, licenselogic, p));
+
	return (liclog_val(sbuf, pkg->licenselogic, p));
}

/*
@@ -1617,10 +1603,8 @@ struct sbuf *
format_maintainer(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*maintainer;

-
	pkg_get(pkg, PKG_MAINTAINER, &maintainer);
-
	return (string_val(sbuf, maintainer, p));
+
	return (string_val(sbuf, pkg->maintainer, p));
}

/*
@@ -1630,10 +1614,8 @@ struct sbuf *
format_name(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*name;

-
	pkg_get(pkg, PKG_NAME, &name);
-
	return (string_val(sbuf, name, p));
+
	return (string_val(sbuf, pkg->name, p));
}

/*
@@ -1643,10 +1625,8 @@ struct sbuf *
format_origin(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*origin;

-
	pkg_get(pkg, PKG_ORIGIN, &origin);
-
	return (string_val(sbuf, origin, p));
+
	return (string_val(sbuf, pkg->origin, p));
}

/*
@@ -1656,10 +1636,8 @@ struct sbuf *
format_prefix(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*prefix;

-
	pkg_get(pkg, PKG_PREFIX, &prefix);
-
	return (string_val(sbuf, prefix, p));
+
	return (string_val(sbuf, pkg->prefix, p));
}

/*
@@ -1669,10 +1647,8 @@ struct sbuf *
format_architecture(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*arch;

-
	pkg_get(pkg, PKG_ARCH, &arch);
-
	return (string_val(sbuf, arch, p));
+
	return (string_val(sbuf, pkg->arch, p));
}

/*
@@ -1718,10 +1694,8 @@ struct sbuf *
format_flatsize(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	int64_t			 flatsize;

-
	pkg_get(pkg, PKG_FLATSIZE, &flatsize);
-
	return (int_val(sbuf, flatsize, p));
+
	return (int_val(sbuf, pkg->flatsize, p));
}

/*
@@ -1734,17 +1708,14 @@ struct sbuf *
format_install_tstamp(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	int64_t			 timestamp;
-

-
	pkg_get(pkg, PKG_TIME, &timestamp);

	if (sbuf_len(p->item_fmt) == 0)
-
		return (int_val(sbuf, timestamp, p));
+
		return (int_val(sbuf, pkg->timestamp, p));
	else {
		char	 buf[1024];
		time_t	 tsv;

-
		tsv = (time_t)timestamp;
+
		tsv = (time_t)pkg->timestamp;
		strftime(buf, sizeof(buf), sbuf_data(p->item_fmt),
			 localtime(&tsv));
		sbuf_cat(sbuf, buf); 
@@ -1759,10 +1730,8 @@ struct sbuf *
format_version(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*version;

-
	pkg_get(pkg, PKG_VERSION, &version);
-
	return (string_val(sbuf, version, p));
+
	return (string_val(sbuf, pkg->version, p));
}

/*
@@ -1772,10 +1741,8 @@ struct sbuf *
format_checksum(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*checksum;

-
	pkg_get(pkg, PKG_CKSUM, &checksum);
-
	return (string_val(sbuf, checksum, p));
+
	return (string_val(sbuf, pkg->sum, p));
}

/*
@@ -1785,17 +1752,14 @@ struct sbuf *
format_short_checksum(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*checksum;
	char	 csum[PKG_FILE_CKSUM_CHARS + 1];
	int slen;

-
	pkg_get(pkg, PKG_CKSUM, &checksum);
-

-
	if (checksum != NULL)
-
		slen = MIN(PKG_FILE_CKSUM_CHARS, strlen(checksum));
+
	if (pkg->sum != NULL)
+
		slen = MIN(PKG_FILE_CKSUM_CHARS, strlen(pkg->sum));
	else
		slen = 0;
-
	memcpy(csum, checksum, slen);
+
	memcpy(csum, pkg->sum, slen);
	csum[slen] = '\0';

	return (string_val(sbuf, csum, p));
@@ -1808,10 +1772,8 @@ struct sbuf *
format_home_url(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	const char		*url;

-
	pkg_get(pkg, PKG_WWW, &url);
-
	return (string_val(sbuf, url, p));
+
	return (string_val(sbuf, pkg->www, p));
}

/*
modified libpkg/pkg_repo_create.c
@@ -322,13 +322,11 @@ pkg_create_repo_worker(struct pkg_fts_item *start, size_t nelts,
			int r;
			off_t mpos, fpos = 0;
			size_t mlen;
-
			const char *origin;

			sha256_file(cur->fts_accpath, checksum);
-
			pkg_set(pkg, PKG_CKSUM, checksum,
-
				PKG_REPOPATH, cur->pkg_path,
-
				PKG_PKGSIZE, cur->fts_size);
-
			pkg_get(pkg, PKG_ORIGIN, &origin);
+
			pkg->pkgsize = cur->fts_size;
+
			pkg->sum = strdup(checksum);
+
			pkg->repopath = strdup(cur->pkg_path);

			/*
			 * TODO: use pkg_checksum for new manifests
@@ -391,11 +389,12 @@ pkg_create_repo_worker(struct pkg_fts_item *start, size_t nelts,
			}

			r = snprintf(digestbuf, sizeof(digestbuf), "%s:%s:%ld:%ld:%ld:%s\n",
-
				origin, mdigest,
+
				pkg->origin,
+
				mdigest,
				(long)mpos,
				(long)fpos,
				(long)mlen,
-
				checksum);
+
				pkg->sum);

			free(mdigest);
			mdigest = NULL;
modified libpkg/pkg_solve.c
@@ -160,13 +160,10 @@ static void
pkg_solve_variable_set(struct pkg_solve_variable *var,
	struct pkg_job_universe_item *item)
{
-
	const char *digest, *uid;
-

	var->unit = item;
-
	pkg_get(item->pkg, PKG_UNIQUEID, &uid, PKG_DIGEST, &digest);
	/* XXX: Is it safe to save a ptr here ? */
-
	var->digest = digest;
-
	var->uid = uid;
+
	var->digest = item->pkg->digest;
+
	var->uid = item->pkg->uid;
	var->prev = var;
}

@@ -211,7 +208,6 @@ static void
pkg_print_rule_sbuf(struct pkg_solve_rule *rule, struct sbuf *sb)
{
	struct pkg_solve_item *it = rule->items, *key_elt = NULL;
-
	const char *v1, *v2;

	sbuf_printf(sb, "%s rule: ", rule_reasons[rule->reason]);
	switch(rule->reason) {
@@ -234,16 +230,14 @@ pkg_print_rule_sbuf(struct pkg_solve_rule *rule, struct sbuf *sb)
		}
		break;
	case PKG_RULE_UPGRADE_CONFLICT:
-
		pkg_get(it->next->var->unit->pkg, PKG_VERSION, &v1);
-
		pkg_get(it->var->unit->pkg, PKG_VERSION, &v2);
		sbuf_printf(sb, "upgrade local %s-%s to remote %s-%s",
-
			it->next->var->uid, v1, it->var->uid, v2);
+
			it->next->var->uid, it->next->var->unit->pkg->version,
+
			it->var->uid, it->var->unit->pkg->version);
		break;
	case PKG_RULE_EXPLICIT_CONFLICT:
		sbuf_printf(sb, "The following packages conflict with each other: ");
		LL_FOREACH(rule->items, it) {
-
			pkg_get(it->var->unit->pkg, PKG_VERSION, &v1);
-
			sbuf_printf(sb, "%s-%s%s%s", it->var->uid, v1,
+
			sbuf_printf(sb, "%s-%s%s%s", it->var->unit->pkg->uid, it->var->unit->pkg->version,
				(it->var->unit->pkg->type == PKG_INSTALLED) ? "(l)" : "(r)",
				it->next ? ", " : "");
		}
@@ -270,8 +264,7 @@ pkg_print_rule_sbuf(struct pkg_solve_rule *rule, struct sbuf *sb)
	case PKG_RULE_REQUEST_CONFLICT:
		sbuf_printf(sb, "The following packages in request are candidates for installation: ");
		LL_FOREACH(rule->items, it) {
-
			pkg_get(it->var->unit->pkg, PKG_VERSION, &v1);
-
			sbuf_printf(sb, "%s-%s%s", it->var->uid, v1,
+
			sbuf_printf(sb, "%s-%s%s", it->var->uid, it->var->unit->pkg->version,
					it->next ? ", " : "");
		}
		break;
@@ -307,7 +300,6 @@ pkg_solve_handle_provide (struct pkg_solve_problem *problem,
		struct pkg_job_provide *pr, struct pkg_solve_rule *rule, int *cnt)
{
	struct pkg_solve_item *it = NULL;
-
	const char *uid, *digest;
	struct pkg_solve_variable *var, *curvar;
	struct pkg_job_universe_item *un;

@@ -318,8 +310,7 @@ pkg_solve_handle_provide (struct pkg_solve_problem *problem,
	}

	/* Find the corresponding variables chain */
-
	pkg_get(un->pkg, PKG_DIGEST, &digest, PKG_UNIQUEID, &uid);
-
	HASH_FIND_STR(problem->variables_by_uid, uid, var);
+
	HASH_FIND_STR(problem->variables_by_uid, un->pkg->uid, var);

	LL_FOREACH(var, curvar) {
		/* For each provide */
@@ -519,7 +510,6 @@ static int
pkg_solve_add_request_rule(struct pkg_solve_problem *problem,
	struct pkg_solve_variable *var, struct pkg_job_request *req, int inverse)
{
-
	const char *uid;
	struct pkg_solve_rule *rule = NULL;
	struct pkg_solve_item *it = NULL;
	struct pkg_job_request_item *item, *confitem;
@@ -532,8 +522,7 @@ pkg_solve_add_request_rule(struct pkg_solve_problem *problem,
	/*
	 * Get the suggested item
	 */
-
	pkg_get(req->item->pkg, PKG_UNIQUEID, &uid);
-
	HASH_FIND_STR(problem->variables_by_uid, uid, var);
+
	HASH_FIND_STR(problem->variables_by_uid, req->item->pkg->uid, var);
	var = pkg_solve_find_var_in_chain(var, req->item->unit);
	assert(var != NULL);
	/* Assume the most significant variable */
@@ -715,12 +704,10 @@ pkg_solve_add_variable(struct pkg_job_universe_item *un,
{
	struct pkg_job_universe_item *ucur;
	struct pkg_solve_variable *var = NULL, *tvar = NULL;
-
	const char *uid, *digest;

	LL_FOREACH(un, ucur) {
		assert(*n < problem->nvars);

-
		pkg_get(ucur->pkg, PKG_UNIQUEID, &uid, PKG_DIGEST, &digest);
		/* Add new variable */
		var = &problem->variables[*n];
		pkg_solve_variable_set(var, ucur);
@@ -783,14 +770,12 @@ pkg_solve_jobs_to_sat(struct pkg_jobs *j)

	/* Add rules for all conflict chains */
	HASH_ITER(hh, j->universe->items, un, utmp) {
-
		const char *uid;
		struct pkg_solve_variable *var;

-
		pkg_get(un->pkg, PKG_UNIQUEID, &uid);
-
		HASH_FIND_STR(problem->variables_by_uid, uid, var);
+
		HASH_FIND_STR(problem->variables_by_uid, un->pkg->uid, var);
		if (var == NULL) {
			pkg_emit_error("internal solver error: variable %s is not found",
-
				uid);
+
			    var->uid);
			goto err;
		}
		if (pkg_solve_process_universe_variable(problem, var) != EPKG_OK)
modified libpkg/pkg_version.c
@@ -33,6 +33,7 @@

#include "pkg.h"
#include "private/event.h"
+
#include "private/pkg.h"

/*
 * split_version(pkgname, endname, epoch, revision) returns a pointer to
@@ -338,15 +339,11 @@ pkg_version_cmp(const char * const pkg1, const char * const pkg2)
pkg_change_t
pkg_version_change(const struct pkg * restrict pkg)
{
-
	const char *version, *oldversion;

-
	pkg_get(pkg, PKG_VERSION, &version,
-
	    PKG_OLD_VERSION, &oldversion);
-

-
	if (oldversion == NULL)
+
	if (pkg->old_version == NULL)
		return (PKG_REINSTALL);

-
	switch (pkg_version_cmp(oldversion, version)) {
+
	switch (pkg_version_cmp(pkg->old_version, pkg->version)) {
	case -1:
		return (PKG_UPGRADE);
	default:		/* placate the compiler */
@@ -360,15 +357,10 @@ pkg_version_change(const struct pkg * restrict pkg)
pkg_change_t
pkg_version_change_between(const struct pkg * pkg1, const struct pkg *pkg2)
{
-
	const char *version, *oldversion;
-

	if (pkg2 == NULL)
		return PKG_REINSTALL;

-
	pkg_get(pkg1, PKG_VERSION, &version);
-
	pkg_get(pkg2, PKG_VERSION, &oldversion);
-

-
	switch (pkg_version_cmp(oldversion, version)) {
+
	switch (pkg_version_cmp(pkg2->version, pkg1->version)) {
	case -1:
		return (PKG_UPGRADE);
	default:		/* placate the compiler */
modified libpkg/pkgdb.c
@@ -1556,13 +1556,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
	int			 retcode = EPKG_FATAL;
	int64_t			 package_id;

-
	const char		*mtree, *origin, *name, *version, *name2;
-
	const char		*version2, *comment, *desc, *message;
-
	const char		*arch, *maintainer, *www, *prefix;
-
	const char		*digest, *abi;
-

-
	bool			 automatic;
-
	int64_t			 flatsize, licenselogic;
+
	const char		*arch;

	const pkg_object	*licenses, *categories;

@@ -1579,43 +1573,18 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
		return (EPKG_FATAL);

	pkg_get(pkg,
-
		PKG_MTREE,	&mtree,
-
		PKG_ORIGIN,	&origin,
-
		PKG_VERSION,	&version,
-
		PKG_COMMENT,	&comment,
-
		PKG_DESC,	&desc,
-
		PKG_MESSAGE,	&message,
-
		PKG_ARCH,	&arch,
-
		PKG_ABI,	&abi,
-
		PKG_MAINTAINER,	&maintainer,
-
		PKG_WWW,	&www,
-
		PKG_PREFIX,	&prefix,
-
		PKG_FLATSIZE,	&flatsize,
-
		PKG_AUTOMATIC,	&automatic,
-
		PKG_LICENSE_LOGIC, &licenselogic,
-
		PKG_NAME,	&name,
-
		PKG_DIGEST,	&digest,
-
		PKG_LICENSES,	&licenses,
-
		PKG_CATEGORIES,	&categories);
-

+
	    PKG_LICENSES,	&licenses,
+
	    PKG_CATEGORIES,	&categories); 
	/* Prefer new ABI over old one */
-
	if (abi != NULL)
-
		arch = abi;
-
	/*
-
	 * Insert mtree record
-
	 */
-

-
	if (run_prstmt(MTREE, mtree) != SQLITE_DONE) {
-
		ERROR_SQLITE(s, SQL(MTREE));
-
		goto cleanup;
-
	}
+
	arch = pkg->abi != NULL ? pkg->abi : pkg->arch;

	/*
	 * Insert package record
	 */
-
	ret = run_prstmt(PKG, origin, name, version, comment, desc, message,
-
	    arch, maintainer, www, prefix, flatsize, (int64_t)automatic,
-
	    (int64_t)licenselogic, mtree, digest);
+
	ret = run_prstmt(PKG, pkg->origin, pkg->name, pkg->version,
+
	    pkg->comment, pkg->desc, pkg->message, pkg->arch, pkg->maintainer,
+
	    pkg->www, pkg->prefix, pkg->flatsize, (int64_t)pkg->automatic,
+
	    (int64_t)pkg->licenselogic, NULL, pkg->digest);
	if (ret != SQLITE_DONE) {
		ERROR_SQLITE(s, SQL(PKG));
		goto cleanup;
@@ -1623,7 +1592,8 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)

	package_id = sqlite3_last_insert_rowid(s);

-
	if (run_prstmt(FTS_APPEND, package_id, name, version, origin) != SQLITE_DONE) {
+
	if (run_prstmt(FTS_APPEND, package_id, pkg->name, pkg->version,
+
	    pkg->origin) != SQLITE_DONE) {
		ERROR_SQLITE(s, SQL(FTS_APPEND));
		goto cleanup;
	}
@@ -1633,7 +1603,8 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
	 * package
	 */

-
	if (run_prstmt(DEPS_UPDATE, name, version, origin) != SQLITE_DONE) {
+
	if (run_prstmt(DEPS_UPDATE, pkg->name, pkg->version, pkg->origin)
+
	    != SQLITE_DONE) {
		ERROR_SQLITE(s, SQL(DEPS_UPDATE));
		goto cleanup;
	}
@@ -1691,16 +1662,15 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
			ERROR_SQLITE(s, SQL(FILES_REPLACE));
			goto cleanup;
		}
-
		pkg_get(pkg2, PKG_NAME, &name2, PKG_VERSION, &version2);
		if (!forced) {
			devmode = pkg_object_bool(pkg_config_get("DEVELOPER_MODE"));
			if (!devmode)
				permissive = pkg_object_bool(pkg_config_get("PERMISSIVE"));
			pkg_emit_error("%s-%s conflicts with %s-%s"
-
					" (installs files into the same place). "
-
					" Problematic file: %s%s",
-
					name, version, name2, version2, file->path,
-
					permissive ? " ignored by permissive mode" : "");
+
			    " (installs files into the same place). "
+
			    " Problematic file: %s%s",
+
			    pkg->name, pkg->version, pkg2->name, pkg2->version, file->path,
+
			    permissive ? " ignored by permissive mode" : "");
			pkg_free(pkg2);
			if (!permissive) {
				pkgdb_it_free(it);
@@ -1708,9 +1678,9 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
			}
		} else {
			pkg_emit_error("%s-%s conflicts with %s-%s"
-
					" (installs files into the same place). "
-
					" Problematic file: %s ignored by forced mode",
-
					name, version, name2, version2, file->path);
+
			    " (installs files into the same place). "
+
			    " Problematic file: %s ignored by forced mode",
+
			    pkg->name, pkg->version, pkg2->name, pkg2->version, file->path);
			pkg_free(pkg2);
		}
		pkgdb_it_free(it);
@@ -2022,7 +1992,7 @@ pkgdb_reanalyse_shlibs(struct pkgdb *db, struct pkg *pkg)

	if ((ret = pkg_analyse_files(db, pkg, NULL)) == EPKG_OK) {
		s = db->sqlite;
-
		pkg_get(pkg, PKG_ROWID, &package_id);
+
		package_id = pkg->id;

		for (i = 0; i < 2; i++) {
			/* Clean out old shlibs first */
@@ -2062,19 +2032,16 @@ pkgdb_add_annotation(struct pkgdb *db, struct pkg *pkg, const char *tag,
    const char *value)
{
	int		 rows_changed;
-
	const char	*uniqueid;

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

-
	pkg_get(pkg, PKG_UNIQUEID, &uniqueid);
-

	if (run_prstmt(ANNOTATE1, tag) != SQLITE_DONE
	    ||
	    run_prstmt(ANNOTATE1, value) != SQLITE_DONE
	    ||
-
	    run_prstmt(ANNOTATE_ADD1, uniqueid, tag, value)
+
	    run_prstmt(ANNOTATE_ADD1, pkg->uid, tag, value)
	    != SQLITE_DONE) {
		ERROR_SQLITE(db->sqlite, SQL(ANNOTATE_ADD1));
		pkgdb_transaction_rollback(db->sqlite, NULL);
@@ -2092,14 +2059,11 @@ pkgdb_add_annotation(struct pkgdb *db, struct pkg *pkg, const char *tag,
int
pkgdb_set_pkg_digest(struct pkgdb *db, struct pkg *pkg)
{
-
	const char *digest;
-
	int64_t id;

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

-
	pkg_get(pkg, PKG_DIGEST, &digest, PKG_ROWID, &id);
-
	if (run_prstmt(UPDATE_DIGEST, digest, id) != SQLITE_DONE) {
+
	if (run_prstmt(UPDATE_DIGEST, pkg->digest, pkg->id) != SQLITE_DONE) {
		ERROR_SQLITE(db->sqlite, SQL(UPDATE_DIGEST));
		return (EPKG_FATAL);
	}
@@ -2111,8 +2075,7 @@ int
pkgdb_modify_annotation(struct pkgdb *db, struct pkg *pkg, const char *tag,
        const char *value)
{
-
	int		 rows_changed;
-
	const char	*uniqueid;
+
	int rows_changed;

	assert(pkg!= NULL);
	assert(tag != NULL);
@@ -2121,15 +2084,13 @@ pkgdb_modify_annotation(struct pkgdb *db, struct pkg *pkg, const char *tag,
	if (pkgdb_transaction_begin(db->sqlite, NULL) != EPKG_OK)
		return (EPKG_FATAL);

-
	pkg_get(pkg, PKG_UNIQUEID, &uniqueid);
-

-
	if (run_prstmt(ANNOTATE_DEL1, uniqueid, tag) != SQLITE_DONE
+
	if (run_prstmt(ANNOTATE_DEL1, pkg->uid, tag) != SQLITE_DONE
	    ||
	    run_prstmt(ANNOTATE1, tag) != SQLITE_DONE
	    ||
	    run_prstmt(ANNOTATE1, value) != SQLITE_DONE
	    ||
-
	    run_prstmt(ANNOTATE_ADD1, uniqueid, tag, value) !=
+
	    run_prstmt(ANNOTATE_ADD1, pkg->uid, tag, value) !=
	        SQLITE_DONE
	    ||
	    run_prstmt(ANNOTATE_DEL2) != SQLITE_DONE) {
@@ -2152,9 +2113,8 @@ pkgdb_modify_annotation(struct pkgdb *db, struct pkg *pkg, const char *tag,
int
pkgdb_delete_annotation(struct pkgdb *db, struct pkg *pkg, const char *tag)
{
-
	int		 rows_changed;
-
	bool		 result;
-
	const char	*uniqueid;
+
	int rows_changed;
+
	bool result;

	assert(pkg != NULL);
	assert(tag != NULL);
@@ -2162,9 +2122,7 @@ pkgdb_delete_annotation(struct pkgdb *db, struct pkg *pkg, const char *tag)
	if (pkgdb_transaction_begin(db->sqlite, NULL) != EPKG_OK)
		return (EPKG_FATAL);

-
	pkg_get(pkg, PKG_UNIQUEID, &uniqueid);
-

-
	result = (run_prstmt(ANNOTATE_DEL1, uniqueid, tag)
+
	result = (run_prstmt(ANNOTATE_DEL1, pkg->uid, tag)
		  == SQLITE_DONE);

	rows_changed = sqlite3_changes(db->sqlite);
@@ -2519,16 +2477,13 @@ pkgdb_vset(struct pkgdb *db, int64_t id, va_list ap)
int
pkgdb_set2(struct pkgdb *db, struct pkg *pkg, ...)
{
-
	int	ret = EPKG_OK;
-
	int64_t	id;
-

+
	int ret = EPKG_OK;
	va_list	ap;

	assert(pkg != NULL);

	va_start(ap, pkg);
-
	pkg_get(pkg, PKG_ROWID, &id);
-
	ret = pkgdb_vset(db, id, ap);
+
	ret = pkgdb_vset(db, pkg->id, ap);
	va_end(ap);

	return (ret);
@@ -2978,11 +2933,10 @@ pkgdb_begin_solver(struct pkgdb *db)
	const char end_update_sql[] = ""
		"END TRANSACTION;"
		"CREATE INDEX pkg_digest_id ON packages(origin, manifestdigest);";
-
	const char *digest;
	struct pkgdb_it *it;
	struct pkg *pkglist = NULL, *p = NULL;
	int rc = EPKG_OK;
-
	int64_t id, cnt = 0, cur = 0;
+
	int64_t cnt = 0, cur = 0;

	it = pkgdb_query(db, " WHERE manifestdigest IS NULL OR manifestdigest==''",
		MATCH_CONDITION);
@@ -3004,8 +2958,7 @@ pkgdb_begin_solver(struct pkgdb *db)
				pkg_emit_progress_start("Updating database digests format");
				LL_FOREACH(pkglist, p) {
					pkg_emit_progress_tick(cur++, cnt);
-
					pkg_get(p, PKG_ROWID, &id, PKG_DIGEST, &digest);
-
					rc = run_prstmt(UPDATE_DIGEST, digest, id);
+
					rc = run_prstmt(UPDATE_DIGEST, p->digest, p->id);
					if (rc != SQLITE_DONE) {
						assert(0);
						ERROR_SQLITE(db->sqlite, SQL(UPDATE_DIGEST));
@@ -3049,8 +3002,6 @@ pkgdb_is_dir_used(struct pkgdb *db, struct pkg *p, const char *dir, int64_t *res
{
	sqlite3_stmt *stmt;
	int ret;
-
	const char *name;
-
	const char *origin;

	const char sql[] = ""
		"SELECT count(package_id) FROM pkg_directories, directories "
@@ -3063,11 +3014,9 @@ pkgdb_is_dir_used(struct pkgdb *db, struct pkg *p, const char *dir, int64_t *res
		return (EPKG_FATAL);
	}

-
	pkg_get(p, PKG_NAME, &name, PKG_ORIGIN, &origin);
-

	sqlite3_bind_text(stmt, 1, dir, -1, SQLITE_TRANSIENT);
-
	sqlite3_bind_text(stmt, 2, name, -1, SQLITE_TRANSIENT);
-
	sqlite3_bind_text(stmt, 3, origin, -1, SQLITE_TRANSIENT);
+
	sqlite3_bind_text(stmt, 2, p->name, -1, SQLITE_TRANSIENT);
+
	sqlite3_bind_text(stmt, 3, p->origin, -1, SQLITE_TRANSIENT);

	ret = sqlite3_step(stmt);

modified libpkg/pkgdb_iterator.c
@@ -100,7 +100,6 @@ load_val(sqlite3 *db, struct pkg *pkg, const char *sql, unsigned flags,
{
	sqlite3_stmt	*stmt;
	int		 ret;
-
	int64_t		 rowid;

	assert(db != NULL && pkg != NULL);

@@ -113,8 +112,7 @@ load_val(sqlite3 *db, struct pkg *pkg, const char *sql, unsigned flags,
		return (EPKG_FATAL);
	}

-
	pkg_get(pkg, PKG_ROWID, &rowid);
-
	sqlite3_bind_int64(stmt, 1, rowid);
+
	sqlite3_bind_int64(stmt, 1, pkg->id);

	while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
		pkg_adddata(pkg, sqlite3_column_text(stmt, 0));
@@ -140,7 +138,6 @@ load_tag_val(sqlite3 *db, struct pkg *pkg, const char *sql, unsigned flags,
{
	sqlite3_stmt	*stmt;
	int		 ret;
-
	int64_t		 rowid;

	assert(db != NULL && pkg != NULL);

@@ -153,8 +150,7 @@ load_tag_val(sqlite3 *db, struct pkg *pkg, const char *sql, unsigned flags,
		return (EPKG_FATAL);
	}

-
	pkg_get(pkg, PKG_ROWID, &rowid);
-
	sqlite3_bind_int64(stmt, 1, rowid);
+
	sqlite3_bind_int64(stmt, 1, pkg->id);

	while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
		pkg_addtagval(pkg, sqlite3_column_text(stmt, 0),
@@ -188,7 +184,6 @@ pkgdb_load_deps(sqlite3 *sqlite, struct pkg *pkg)
{
	sqlite3_stmt	*stmt = NULL;
	int		 ret = EPKG_OK;
-
	int64_t		 rowid;
	char		 sql[BUFSIZ];
	const char	*mainsql = ""
		"SELECT d.name, d.origin, d.version, 0 "
@@ -211,8 +206,7 @@ pkgdb_load_deps(sqlite3 *sqlite, struct pkg *pkg)
		return (EPKG_FATAL);
	}

-
	pkg_get(pkg, PKG_ROWID, &rowid);
-
	sqlite3_bind_int64(stmt, 1, rowid);
+
	sqlite3_bind_int64(stmt, 1, pkg->id);

	/* XXX: why we used locked here ? */
	while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
@@ -238,7 +232,6 @@ pkgdb_load_rdeps(sqlite3 *sqlite, struct pkg *pkg)
{
	sqlite3_stmt	*stmt = NULL;
	int		 ret;
-
	const char	*uniqueid;
	const char	*mainsql = ""
		"SELECT p.name, p.origin, p.version, 0 "
		"FROM main.packages AS p "
@@ -259,8 +252,7 @@ pkgdb_load_rdeps(sqlite3 *sqlite, struct pkg *pkg)
		return (EPKG_FATAL);
	}

-
	pkg_get(pkg, PKG_UNIQUEID, &uniqueid);
-
	sqlite3_bind_text(stmt, 1, uniqueid, -1, SQLITE_STATIC);
+
	sqlite3_bind_text(stmt, 1, pkg->uid, -1, SQLITE_STATIC);

	/* XXX: why we used locked here ? */
	while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
@@ -286,7 +278,6 @@ pkgdb_load_files(sqlite3 *sqlite, struct pkg *pkg)
{
	sqlite3_stmt	*stmt = NULL;
	int		 ret;
-
	int64_t		 rowid;
	const char	 sql[] = ""
		"SELECT path, sha256 "
		"FROM files "
@@ -310,8 +301,7 @@ pkgdb_load_files(sqlite3 *sqlite, struct pkg *pkg)
		return (EPKG_FATAL);
	}

-
	pkg_get(pkg, PKG_ROWID, &rowid);
-
	sqlite3_bind_int64(stmt, 1, rowid);
+
	sqlite3_bind_int64(stmt, 1, pkg->id);

	while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
		pkg_addfile(pkg, sqlite3_column_text(stmt, 0),
@@ -325,7 +315,7 @@ pkgdb_load_files(sqlite3 *sqlite, struct pkg *pkg)
		return (EPKG_FATAL);
	}

-
	sqlite3_bind_int64(stmt, 1, rowid);
+
	sqlite3_bind_int64(stmt, 1, pkg->id);

	while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
		pkg_addconfig_file(pkg, sqlite3_column_text(stmt, 0),
@@ -354,7 +344,6 @@ pkgdb_load_dirs(sqlite3 *sqlite, struct pkg *pkg)
		"ORDER by path DESC";
	sqlite3_stmt	*stmt;
	int		 ret;
-
	int64_t		 rowid;

	assert(pkg != NULL);
	assert(pkg->type == PKG_INSTALLED);
@@ -368,8 +357,7 @@ pkgdb_load_dirs(sqlite3 *sqlite, struct pkg *pkg)
		return (EPKG_FATAL);
	}

-
	pkg_get(pkg, PKG_ROWID, &rowid);
-
	sqlite3_bind_int64(stmt, 1, rowid);
+
	sqlite3_bind_int64(stmt, 1, pkg->id);

	while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
		pkg_adddir(pkg, sqlite3_column_text(stmt, 0),
@@ -548,7 +536,6 @@ pkgdb_load_scripts(sqlite3 *sqlite, struct pkg *pkg)
{
	sqlite3_stmt	*stmt = NULL;
	int		 ret;
-
	int64_t		 rowid;
	const char	 sql[] = ""
		"SELECT script, type "
		"FROM pkg_script JOIN script USING(script_id) "
@@ -566,8 +553,7 @@ pkgdb_load_scripts(sqlite3 *sqlite, struct pkg *pkg)
		return (EPKG_FATAL);
	}

-
	pkg_get(pkg, PKG_ROWID, &rowid);
-
	sqlite3_bind_int64(stmt, 1, rowid);
+
	sqlite3_bind_int64(stmt, 1, pkg->id);

	while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
		pkg_addscript(pkg, sqlite3_column_text(stmt, 0),
@@ -707,7 +693,6 @@ populate_pkg(sqlite3_stmt *stmt, struct pkg *pkg) {
	int		 icol = 0;
	const char	*colname;
	char legacyarch[BUFSIZ];
-
	const char *abi;

	assert(stmt != NULL);

@@ -720,14 +705,64 @@ populate_pkg(sqlite3_stmt *stmt, struct pkg *pkg) {
					sizeof(columns[0]), compare_column_func);
			if (column == NULL) {
				pkg_emit_error("unknown column %s", colname);
+
				continue;
			}
-
			else {
-
				if (column->pkg_type == PKG_SQLITE_STRING)
-
					pkg_set(pkg, column->type,
-
						sqlite3_column_text(stmt, icol));
-
				else
-
					pkg_emit_error("want string for column %s and got number",
-
							colname);
+

+
			switch (column->type) {
+
			case PKG_ABI:
+
				pkg->abi = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_CKSUM:
+
				pkg->sum = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_COMMENT:
+
				pkg->comment = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_REPONAME:
+
				pkg->reponame = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_DESC:
+
				pkg->desc = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_MAINTAINER:
+
				pkg->maintainer = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_DIGEST:
+
				pkg->digest = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_MESSAGE:
+
				pkg->message = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_NAME:
+
				pkg->name = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_OLD_VERSION:
+
				pkg->old_version = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_ORIGIN:
+
				pkg->origin = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_PREFIX:
+
				pkg->prefix = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_REPOPATH:
+
				pkg->repopath = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_REPOURL:
+
				pkg->repourl = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_UNIQUEID:
+
				pkg->uid = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_VERSION:
+
				pkg->version = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			case PKG_WWW:
+
				pkg->www = strdup(sqlite3_column_text(stmt, icol));
+
				break;
+
			default:
+
				pkg_emit_error("Unexpected text value for %s", colname);
+
				break;
			}
			break;
		case SQLITE_INTEGER:
@@ -735,17 +770,37 @@ populate_pkg(sqlite3_stmt *stmt, struct pkg *pkg) {
					sizeof(columns[0]), compare_column_func);
			if (column == NULL) {
				pkg_emit_error("Unknown column %s", colname);
+
				continue;
			}
-
			else {
-
				if (column->pkg_type == PKG_SQLITE_INT64)
-
					pkg_set(pkg, column->type,
-
						sqlite3_column_int64(stmt, icol));
-
				else if (column->pkg_type == PKG_SQLITE_BOOL)
-
					pkg_set(pkg, column->type,
-
							(bool)sqlite3_column_int(stmt, icol));
-
				else
-
					pkg_emit_error("want number for column %s and got string",
-
							colname);
+

+
			switch (column->type) {
+
			case PKG_AUTOMATIC:
+
				pkg->automatic = (bool)sqlite3_column_int(stmt, icol);
+
				break;
+
			case PKG_LOCKED:
+
				pkg->locked = (bool)sqlite3_column_int(stmt, icol);
+
				break;
+
			case PKG_FLATSIZE:
+
				pkg->flatsize = sqlite3_column_int(stmt, icol);
+
				break;
+
			case PKG_ROWID:
+
				pkg->id = sqlite3_column_int(stmt, icol);
+
				break;
+
			case PKG_LICENSE_LOGIC:
+
				pkg->licenselogic = (lic_t)sqlite3_column_int(stmt, icol);
+
				break;
+
			case PKG_OLD_FLATSIZE:
+
				pkg->old_flatsize = sqlite3_column_int(stmt, icol);
+
				break;
+
			case PKG_PKGSIZE:
+
				pkg->pkgsize = sqlite3_column_int(stmt, icol);
+
				break;
+
			case PKG_TIME:
+
				pkg->timestamp = sqlite3_column_int(stmt, icol);
+
				break;
+
			default:
+
				pkg_emit_error("Unexpected integer value for %s", colname);
+
				break;
			}
			break;
		case SQLITE_BLOB:
@@ -759,9 +814,8 @@ populate_pkg(sqlite3_stmt *stmt, struct pkg *pkg) {
		}
	}

-
	pkg_get(pkg, PKG_ABI, &abi);
-
	pkg_arch_to_legacy(abi, legacyarch, BUFSIZ);
-
	pkg_set(pkg, PKG_ARCH, legacyarch);
+
	pkg_arch_to_legacy(pkg->abi, legacyarch, BUFSIZ);
+
	pkg->arch = strdup(legacyarch);
}

static struct load_on_flag {
@@ -812,7 +866,6 @@ pkgdb_sqlite_it_next(struct pkgdb_sqlite_it *it,
	struct pkg	*pkg;
	int		 i;
	int		 ret;
-
	const char *digest;

	assert(it != NULL);

@@ -847,9 +900,10 @@ pkgdb_sqlite_it_next(struct pkgdb_sqlite_it *it,

		populate_pkg(it->stmt, pkg);

-
		pkg_get(pkg, PKG_DIGEST, &digest);
-
		if (digest != NULL && !pkg_checksum_is_valid(digest, strlen(digest)))
-
			pkg_set(pkg, PKG_DIGEST, NULL);
+
		if (pkg->digest != NULL && !pkg_checksum_is_valid(pkg->digest, strlen(pkg->digest))) {
+
			free(pkg->digest);
+
			pkg->digest = NULL;
+
		}

		for (i = 0; load_on_flag[i].load != NULL; i++) {
			if (flags & load_on_flag[i].flag) {
modified libpkg/private/pkg.h
@@ -123,7 +123,35 @@ struct pkg_repo;
struct pkg {
	ucl_object_t	*fields;
	bool		 direct;
+
	bool		 locked;
+
	bool		 automatic;
+
	int64_t		 id;
	struct sbuf	*scripts[PKG_NUM_SCRIPTS];
+
	char			*name;
+
	char			*origin;
+
	char			*version;
+
	char			*old_version;
+
	char			*maintainer;
+
	char			*www;
+
	char			*arch;
+
	char			*abi;
+
	char			*uid;
+
	char			*digest;
+
	char			*old_digest;
+
	char			*message;
+
	char			*prefix;
+
	char			*comment;
+
	char			*desc;
+
	char			*sum;
+
	char			*repopath;
+
	char			*reponame;
+
	char			*repourl;
+
	char			*reason;
+
	lic_t			 licenselogic;
+
	int64_t			 pkgsize;
+
	int64_t			 flatsize;
+
	int64_t			 old_flatsize;
+
	int64_t			 timestamp;
	struct pkg_dep		*deps;
	struct pkg_dep		*rdeps;
	struct pkg_file		*files;
modified libpkg/rcscripts.c
@@ -50,11 +50,8 @@ pkg_start_stop_rc_scripts(struct pkg *pkg, pkg_rc_attr attr)
	const char *rc;
	size_t len = 0;
	int ret = 0;
-
	const char *prefix;

-
	pkg_get(pkg, PKG_PREFIX, &prefix);
-

-
	snprintf(rc_d_path, sizeof(rc_d_path), "%s/etc/rc.d/", prefix);
+
	snprintf(rc_d_path, sizeof(rc_d_path), "%s/etc/rc.d/", pkg->prefix);
	len = strlen(rc_d_path);

	while (pkg_files(pkg, &file) == EPKG_OK) {
modified libpkg/repo/binary/fetch.c
@@ -54,17 +54,14 @@ int
pkg_repo_binary_get_cached_name(struct pkg_repo *repo, struct pkg *pkg,
	char *dest, size_t destlen)
{
-
	const char *sum, *name, *version, *repourl, *ext = NULL;
+
	const char *ext = NULL;
	const char *cachedir = NULL;
	struct stat st;

	cachedir = pkg_object_string(pkg_config_get("PKG_CACHEDIR"));

-
	pkg_get(pkg, PKG_CKSUM, &sum, PKG_NAME, &name, PKG_VERSION, &version,
-
			PKG_REPOPATH, &repourl);
-

-
	if (repourl != NULL)
-
		ext = strrchr(repourl, '.');
+
	if (pkg->repopath != NULL)
+
		ext = strrchr(pkg->repopath, '.');

	if (ext != NULL) {
		/*
@@ -134,20 +131,14 @@ pkg_repo_binary_try_fetch(struct pkg_repo *repo, struct pkg *pkg,
	char *dir = NULL;
	int fetched = 0;
	char cksum[SHA256_DIGEST_LENGTH * 2 +1];
-
	int64_t pkgsize;
	struct stat st;
	char *path = NULL;
-
	const char *packagesite = NULL, *repourl;
+
	const char *packagesite = NULL;

	int retcode = EPKG_OK;
-
	const char *name, *version, *sum;

	assert((pkg->type & PKG_REMOTE) == PKG_REMOTE);

-
	pkg_get(pkg, PKG_CKSUM, &sum,
-
			PKG_NAME, &name, PKG_VERSION, &version, PKG_PKGSIZE, &pkgsize,
-
			PKG_REPOPATH, &repourl);
-

	if (mirror) {
		const char *cachedir;

@@ -156,8 +147,7 @@ pkg_repo_binary_try_fetch(struct pkg_repo *repo, struct pkg *pkg,
		else
			cachedir = pkg_object_string(pkg_config_get("PKG_CACHEDIR"));

-
		snprintf(dest, sizeof(dest), "%s/%s",
-
						cachedir, repourl);
+
		snprintf(dest, sizeof(dest), "%s/%s", cachedir, pkg->repopath);
	}
	else
		pkg_repo_binary_get_cached_name(repo, pkg, dest, sizeof(dest));
@@ -197,7 +187,8 @@ pkg_repo_binary_try_fetch(struct pkg_repo *repo, struct pkg *pkg,
		pkg_snprintf(url, sizeof(url), "%S/%R", packagesite, pkg);

	if (!mirror && strncasecmp(packagesite, "file://", 7) == 0) {
-
		pkg_set(pkg, PKG_REPOPATH, url + 7);
+
		free(pkg->repopath);
+
		pkg->repopath = strdup(url + 7);
		return (EPKG_OK);
	}

@@ -210,32 +201,32 @@ pkg_repo_binary_try_fetch(struct pkg_repo *repo, struct pkg *pkg,
checksum:
	/*	checksum calculation is expensive, if size does not
		match, skip it and assume failed checksum. */
-
	if (stat(dest, &st) == -1 || pkgsize != st.st_size) {
+
	if (stat(dest, &st) == -1 || pkg->pkgsize != st.st_size) {
		if (already_tried) {
			pkg_emit_error("cached package %s-%s: "
-
				"size mismatch, cannot continue",
-
				name, version);
+
			    "size mismatch, cannot continue",
+
			    pkg->name, pkg->version);
			retcode = EPKG_FATAL;
			goto cleanup;
		}

		unlink(dest);
		pkg_emit_error("cached package %s-%s: "
-
			"size mismatch, fetching from remote",
-
			name, version);
+
		    "size mismatch, fetching from remote",
+
		    pkg->name, pkg->version);
		return (pkg_repo_binary_try_fetch(repo, pkg, true, mirror, destdir));
	}
	retcode = sha256_file(dest, cksum);
	if (retcode == EPKG_OK) {
-
		if (strcmp(cksum, sum)) {
+
		if (strcmp(cksum, pkg->sum)) {
			if (already_tried || fetched == 1) {
				pkg_emit_error("%s-%s failed checksum "
-
				    "from repository", name, version);
+
				    "from repository", pkg->name, pkg->version);
				retcode = EPKG_FATAL;
			} else {
				pkg_emit_error("cached package %s-%s: "
				    "checksum mismatch, fetching from remote",
-
				    name, version);
+
				    pkg->name, pkg->version);
				unlink(dest);
				return (pkg_repo_binary_try_fetch(repo, pkg, true, mirror, destdir));
			}
modified libpkg/repo/binary/init.c
@@ -301,7 +301,6 @@ pkg_repo_binary_open(struct pkg_repo *repo, unsigned mode)
	int64_t res;
	struct pkg_repo_it *it;
	struct pkg *pkg = NULL;
-
	const char *digest;

	sqlite3_initialize();
	dbdir = pkg_object_string(pkg_config_get("PKG_DBDIR"));
@@ -383,8 +382,7 @@ pkg_repo_binary_open(struct pkg_repo *repo, unsigned mode)
		return (EPKG_OK);
	}
	it->ops->free(it);
-
	pkg_get(pkg, PKG_DIGEST, &digest);
-
	if (digest == NULL || !pkg_checksum_is_valid(digest, strlen(digest))) {
+
	if (pkg->digest == NULL || !pkg_checksum_is_valid(pkg->digest, strlen(pkg->digest))) {
		pkg_emit_notice("Repository %s has incompatible checksum format, need to "
			"re-create database", repo->name);
		pkg_free(pkg);
modified libpkg/repo/binary/update.c
@@ -134,42 +134,31 @@ static int
pkg_repo_binary_add_pkg(struct pkg *pkg, const char *pkg_path,
		sqlite3 *sqlite, bool forced)
{
-
	const char *name, *version, *origin, *comment, *desc;
-
	const char *arch, *maintainer, *www, *prefix, *sum, *rpath;
-
	const char *olddigest, *manifestdigest, *abi;
-
	int64_t			 flatsize, pkgsize;
-
	int64_t			 licenselogic;
	int			 ret;
	struct pkg_dep		*dep      = NULL;
	struct pkg_option	*option   = NULL;
	struct pkg_shlib	*shlib    = NULL;
	const pkg_object	*obj, *licenses, *categories, *annotations;
+
	const char		*arch;
	pkg_iter		 it;
	int64_t			 package_id;

-
	pkg_get(pkg, PKG_ORIGIN, &origin, PKG_NAME, &name,
-
			    PKG_VERSION, &version, PKG_COMMENT, &comment,
-
			    PKG_DESC, &desc, PKG_ARCH, &arch,
-
			    PKG_ABI, &abi,
-
			    PKG_MAINTAINER, &maintainer, PKG_WWW, &www,
-
			    PKG_PREFIX, &prefix, PKG_FLATSIZE, &flatsize,
-
			    PKG_LICENSE_LOGIC, &licenselogic, PKG_CKSUM, &sum,
-
			    PKG_PKGSIZE, &pkgsize, PKG_REPOPATH, &rpath,
-
			    PKG_LICENSES, &licenses, PKG_CATEGORIES, &categories,
-
			    PKG_ANNOTATIONS, &annotations, PKG_OLD_DIGEST, &olddigest,
-
			    PKG_DIGEST, &manifestdigest);
-

-
	if (abi != NULL)
-
		arch = abi;
+
	pkg_get(pkg,
+
	    PKG_LICENSES, &licenses, PKG_CATEGORIES, &categories,
+
	    PKG_ANNOTATIONS, &annotations);
+

+
	arch = pkg->abi != NULL ? pkg->abi : pkg->arch;

try_again:
-
	if ((ret = pkg_repo_binary_run_prstatement(PKG, origin, name, version,
-
			comment, desc, arch, maintainer, www, prefix,
-
			pkgsize, flatsize, (int64_t)licenselogic, sum,
-
			rpath, manifestdigest, olddigest)) != SQLITE_DONE) {
+
	if ((ret = pkg_repo_binary_run_prstatement(PKG,
+
	    pkg->origin, pkg->name, pkg->version, pkg->comment, pkg->desc,
+
	    arch, pkg->maintainer, pkg->www, pkg->prefix, pkg->pkgsize,
+
	    pkg->flatsize, (int64_t)pkg->licenselogic, pkg->sum, pkg->repopath,
+
	    pkg->digest, pkg->old_digest)) != SQLITE_DONE) {
		if (ret == SQLITE_CONSTRAINT) {
-
			switch(pkg_repo_binary_delete_conflicting(origin,
-
					version, pkg_path, forced)) {
+
			ERROR_SQLITE(sqlite, "grmbl");
+
			switch(pkg_repo_binary_delete_conflicting(pkg->origin,
+
			    pkg->version, pkg_path, forced)) {
			case EPKG_FATAL: /* sqlite error */
				ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(PKG));
				return (EPKG_FATAL);
@@ -383,47 +372,39 @@ pkg_repo_binary_register_conflicts(const char *origin, char **conflicts,

static int
pkg_repo_binary_add_from_manifest(char *buf, sqlite3 *sqlite, size_t len,
-
		struct pkg_manifest_key **keys, struct pkg **p, bool is_legacy,
+
		struct pkg_manifest_key **keys, struct pkg **p __unused, bool is_legacy,
		struct pkg_repo *repo)
{
	int rc = EPKG_OK;
	struct pkg *pkg;
-
	const char *pkg_arch;
-

-
	if (*p == NULL) {
-
		rc = pkg_new(p, PKG_REMOTE);
-
		if (rc != EPKG_OK)
-
			return (EPKG_FATAL);
-
	} else {
-
		pkg_reset(*p, PKG_REMOTE);
-
	}

-
	pkg = *p;
+
	rc = pkg_new(&pkg, PKG_REMOTE);
+
	if (rc != EPKG_OK)
+
		return (EPKG_FATAL);

	pkg_manifest_keys_new(keys);
	rc = pkg_parse_manifest(pkg, buf, len, *keys);
	if (rc != EPKG_OK) {
		goto cleanup;
	}
-
	rc = pkg_is_valid(pkg);
-
	if (rc != EPKG_OK) {
-
		goto cleanup;
-
	}

-
	pkg_checksum_calculate(pkg, NULL);
-
	pkg_get(pkg, PKG_ARCH, &pkg_arch);
-
	if (pkg_arch == NULL || !is_valid_abi(pkg_arch, true)) {
+
	if (pkg->digest == NULL || !pkg_checksum_is_valid(pkg->digest, strlen(pkg->digest)))
+
		pkg_checksum_calculate(pkg, NULL);
+
	if (pkg->arch == NULL || !is_valid_abi(pkg->arch, true)) {
		rc = EPKG_FATAL;
		pkg_emit_error("repository %s contains packages with wrong ABI: %s",
-
			repo->name, pkg_arch);
+
			repo->name, pkg->arch);
		goto cleanup;
	}

-
	pkg_set(pkg, PKG_REPONAME, repo->name);
+
	free(pkg->reponame);
+
	pkg->reponame = strdup(repo->name);

	rc = pkg_repo_binary_add_pkg(pkg, NULL, sqlite, true);

cleanup:
+
	pkg_free(pkg);
+

	return (rc);
}

modified libpkg/scripts.c
@@ -49,7 +49,7 @@ pkg_script_run(struct pkg * const pkg, pkg_script type)
	size_t i, j;
	int error, pstat;
	pid_t pid;
-
	const char *prefix, *script_cmd_p;
+
	const char *script_cmd_p;
	const char *argv[4];
	char **ep;
	int ret = EPKG_OK;
@@ -78,8 +78,6 @@ pkg_script_run(struct pkg * const pkg, pkg_script type)
	if (!pkg_object_bool(pkg_config_get("RUN_SCRIPTS")))
		return (EPKG_OK);

-
	pkg_get(pkg, PKG_PREFIX, &prefix);
-

	for (i = 0; i < sizeof(map) / sizeof(map[0]); i++) {
		if (map[i].a == type)
			break;
@@ -92,7 +90,7 @@ pkg_script_run(struct pkg * const pkg, pkg_script type)
			continue;
		if (j == map[i].a || j == map[i].b) {
			sbuf_reset(script_cmd);
-
			setenv("PKG_PREFIX", prefix, 1);
+
			setenv("PKG_PREFIX", pkg->prefix, 1);
			debug = pkg_object_bool(pkg_config_get("DEBUG_SCRIPTS"));
			if (debug)
				sbuf_printf(script_cmd, "set -x\n");