Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Revert the multi-repos support and start from scratch
Marin Atanasov Nikolov committed 14 years ago
commit a4e4d0508b36ca58a934ed3c5c2f691ae045f5cd
parent cbeb3bc
19 files changed +156 -847
modified libpkg/pkg.c
@@ -55,7 +55,6 @@ pkg_new(struct pkg **pkg, pkg_t type)
	STAILQ_INIT(&(*pkg)->conflicts);
	STAILQ_INIT(&(*pkg)->scripts);
	STAILQ_INIT(&(*pkg)->options);
-
	STAILQ_INIT(&(*pkg)->repos);

	(*pkg)->automatic = false;
	(*pkg)->type = type;
@@ -89,7 +88,6 @@ pkg_reset(struct pkg *pkg, pkg_t type)
	pkg_freeconflicts(pkg);
	pkg_freescripts(pkg);
	pkg_freeoptions(pkg);
-
	pkg_repos_free_in_pkg(pkg);

	pkg->type = type;
}
@@ -112,7 +110,6 @@ pkg_free(struct pkg *pkg)
	pkg_freeconflicts(pkg);
	pkg_freescripts(pkg);
	pkg_freeoptions(pkg);
-
	pkg_repos_free_in_pkg(pkg);

	free(pkg);
}
modified libpkg/pkg.h
@@ -19,10 +19,6 @@ struct pkgdb;
struct pkgdb_it;

struct pkg_jobs;
-
struct pkg_jobs_entry;
-

-
struct pkg_repos;
-
struct pkg_repos_entry;

typedef enum {
	/**
@@ -467,12 +463,11 @@ int pkg_create_repo(char *path, void (*callback)(struct pkg *, void *), void *);
int pkg_finish_repo(char *patj, pem_password_cb *cb, char *rsa_key_path);

/**
-
 * Open the package database.
+
 * Open the local package database.
 * The db must be free'ed with pkgdb_close().
-
 * @param dbfile Name of the package database file
 * @return An error code.
 */
-
int pkgdb_open(struct pkgdb **db, pkgdb_t type, const char *dbfile);
+
int pkgdb_open(struct pkgdb **db, pkgdb_t type);

/**
 * Close and free the struct pkgdb.
@@ -600,58 +595,35 @@ int pkgdb_compact(struct pkgdb *db);
int pkg_add(struct pkgdb *db, const char *path);

/**
-
 * Create a new jobs object
-
 * @return EPKG_OK on success, EPKG_FATAL on error
-
 */
-
int pkg_jobs_new(struct pkg_jobs **jobs);
-

-
/**
-
 * Allocate a new pkg_jobs_entry object.
+
 * Allocate a new pkg_jobs.
 * @param db A pkgdb open with PKGDB_REMOTE.
 * @return An error code.
 */
-
int pkg_jobs_new_entry(struct pkg_jobs *jobs, struct pkg_jobs_entry **je, pkg_jobs_t type, struct pkgdb *db);
+
int pkg_jobs_new(struct pkg_jobs **jobs, pkg_jobs_t type, struct pkgdb *db);

/**
-
 * Free a pkg_jobs object
+
 * Free a pkg_jobs
 */
void pkg_jobs_free(struct pkg_jobs *jobs);

/**
-
 * Add a pkg to the jobs entry queue.
+
 * Add a pkg to the jobs queue.
 * @return An error code.
 */
-
int pkg_jobs_add(struct pkg_jobs_entry *je, struct pkg *pkg);
-

-
/**
-
 * Iterates over the jobs entry objects
-
 * @param je Returns the next entry in a jobs object.
-
 * Must be set to NULL for the first call.
-
 * @return EPKG_OK on success, EPKG_END if end of tail is reached.
-
 */
-
int pkg_jobs(struct pkg_jobs *jobs, struct pkg_jobs_entry **je);
+
int pkg_jobs_add(struct pkg_jobs *jobs, struct pkg *pkg);

/**
 * Iterates over the packages in the jobs queue.
 * @param pkg Must be set to NULL for the first call.
 * @return An error code.
 */
-
int pkg_jobs_entry(struct pkg_jobs_entry *je, struct pkg **pkg);
+
int pkg_jobs(struct pkg_jobs *jobs, struct pkg **pkg);

/**
-
 * Apply the jobs in the queue (fetch/install/deinstall).
+
 * Apply the jobs in the queue (fetch and install).
 * @return An error code.
 */
-
int pkg_jobs_apply(struct pkg_jobs_entry *je, int force);
-

-
/**
-
 * Checks if a given job is already added in another jobs entry
-
 * @param jobs A valid jobs object as received from pkg_jobs_new()
-
 * @param pkg A package that will be checked if it exists already as a job
-
 * @param res A pkg where to store the result if a package is found to exist
-
 * @return EPKG_OK if the is not added yet and EPKG_FATAL otherwise
-
 */
-
int pkg_jobs_exists(struct pkg_jobs *jobs, struct pkg *pkg, struct pkg **res);
+
int pkg_jobs_apply(struct pkg_jobs *jobs, int force);

/**
 * Archive formats options.
@@ -681,80 +653,6 @@ int pkg_delete(struct pkg *pkg, struct pkgdb *db, int force);
int pkg_repo_fetch(struct pkg *pkg);

/**
-
 * Initializes the repositories object
-
 * @return EPKG_OK on success, otherwise EPKG_FATAL
-
 */
-
int pkg_repos_new(struct pkg_repos **repos);
-

-
/**
-
 * Loads the remote repositories from file
-
 * @return EPKG_OK on success, and EPKG_FATAL on error
-
 */
-
int pkg_repos_load(struct pkg_repos *repos);
-

-
/**
-
 * Adds a repository entry to the tail
-
 * @param repos A valid repository object received from pkg_repos_new()
-
 * @param re A valid repository entry object
-
 * @return EPKG_OK on success, EPKG_FATAL on error
-
 */
-
int pkg_repos_add(struct pkg_repos *repos, struct pkg_repos_entry *re);
-

-
/**
-
 * Adds a repository entry to a package object
-
 * @param pkg A valid package object
-
 * @param re A valid repository entry object
-
 * @return EPKG_OK on success, EPKG_FATAL on error
-
 */
-
int pkg_repos_add_in_pkg(struct pkg *pkg, struct pkg_repos_entry *re);
-

-
/**
-
 * Get the next repository from the tail
-
 * @param repos A valid repository pointer as returned by pkg_repos_new()
-
 * @param re A pointer to a repository entry to save the result. Must be set to
-
 * NULL for the first repository entry
-
 * @return EPKG_OK on success, EPKG_END if end of repository is reached
-
 */
-
int pkg_repos_next(struct pkg_repos *repos, struct pkg_repos_entry **re);
-

-
/**
-
 * Get the next repository assigned to a package object
-
 * @param pkg A valid package object
-
 * @param re A pointer to a repository entry to save the result. Must be set to
-
 * NULL for the first repository entry
-
 * @return EPKG_OK on success, EPKG_END if end of repository is reached
-
 */
-
int pkg_repos_next_in_pkg(struct pkg *pkg, struct pkg_repos_entry **re);
-

-
/**
-
 * Returns the name associated with a repository entry object
-
 * @param re A valid repository entry object
-
 */
-
const char * pkg_repos_get_name(struct pkg_repos_entry *re);
-

-
/**
-
 * Returns the URL associated wth a repository entry object
-
 * @param re A valid repository entry object
-
 */
-
const char * pkg_repos_get_url(struct pkg_repos_entry *re);
-

-
/**
-
 * Returns the line in the configuration where a repository is found
-
 * @param re A valid repository entry
-
 */
-
unsigned int pkg_repos_get_line(struct pkg_repos_entry *re);
-

-
/**
-
 * Free the memory used by the repository objects
-
 */
-
void pkg_repos_free(struct pkg_repos *repos);
-

-
/**
-
 * Free the memory used by the repository objects in a package
-
 */
-
void pkg_repos_free_in_pkg(struct pkg *pkg);
-

-
/**
 * Get the value of a configuration key
 */
const char * pkg_config(const char *key);
modified libpkg/pkg_jobs.c
@@ -8,59 +8,26 @@
#include "pkg_private.h"

int
-
pkg_jobs_new(struct pkg_jobs **jm)
+
pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db)
{
-
	if ((*jm = calloc(1, sizeof(struct pkg_jobs))) == NULL) {
-
		EMIT_ERRNO("calloc", "pkg_jobs_new");
-
		return (EPKG_FATAL);
-
	}
-

-
	STAILQ_INIT(&(*jm)->multi);
-

-
	return (EPKG_OK);
-
}
-

-
int
-
pkg_jobs_new_entry(struct pkg_jobs *jm, struct pkg_jobs_entry **je, pkg_jobs_t t, struct pkgdb *db)
-
{
-
	assert(jm != NULL);
	assert(db != NULL);
	assert(t != PKG_JOBS_INSTALL || db->type == PKGDB_REMOTE);

-
	if((*je = calloc(1, sizeof(struct pkg_jobs_entry))) == NULL) {
-
		EMIT_ERRNO("calloc", "pkg_jobs_new_entry");
+
	if((*j = calloc(1, sizeof(struct pkg_jobs))) == NULL) {
+
		EMIT_ERRNO("calloc", "pkg_jobs");
		return (EPKG_FATAL);
	}

-
	STAILQ_INIT(&(*je)->jobs);
-
	LIST_INIT(&(*je)->nodes);
-
	(*je)->db = db;
-
	(*je)->type = t;
-

-
	STAILQ_INSERT_TAIL(&jm->multi, *je, next);
+
	STAILQ_INIT(&(*j)->jobs);
+
	LIST_INIT(&(*j)->nodes);
+
	(*j)->db = db;
+
	(*j)->type = t;

	return (EPKG_OK);
}

void
-
pkg_jobs_free(struct pkg_jobs *jm)
-
{
-
	struct pkg_jobs_entry *je;
-

-
	if (jm == NULL)
-
		return;
-

-
	while (!STAILQ_EMPTY(&jm->multi)) {
-
		je = STAILQ_FIRST(&jm->multi);
-
		STAILQ_REMOVE_HEAD(&jm->multi, next);
-
		pkg_jobs_free_entry(je);
-
	}
-

-
	free(jm);
-
}
-

-
void
-
pkg_jobs_free_entry(struct pkg_jobs_entry *j)
+
pkg_jobs_free(struct pkg_jobs *j)
{
	struct pkg *p;

@@ -72,46 +39,29 @@ pkg_jobs_free_entry(struct pkg_jobs_entry *j)
		STAILQ_REMOVE_HEAD(&j->jobs, next);
		pkg_free(p);
	}
-

	free(j);
}

int
-
pkg_jobs_add(struct pkg_jobs_entry *je, struct pkg *pkg)
+
pkg_jobs_add(struct pkg_jobs *j, struct pkg *pkg)
{
-
	assert(je != NULL);
+
	assert(j != NULL);
	assert(pkg != NULL);

-
	STAILQ_INSERT_TAIL(&je->jobs, pkg, next);
+
	STAILQ_INSERT_TAIL(&j->jobs, pkg, next);

	return (EPKG_OK);
}

int
-
pkg_jobs(struct pkg_jobs *jm, struct pkg_jobs_entry **je)
+
pkg_jobs(struct pkg_jobs *j, struct pkg **pkg)
{
-
	assert(jm != NULL);
+
	assert(j != NULL);

-
	if (*je == NULL)
-
		*je = STAILQ_FIRST(&jm->multi);
-
	else
-
		*je = STAILQ_NEXT(*je, next);
-

-
	if (*je == NULL)
-
		return (EPKG_END);
-
	else
-
		return (EPKG_OK);
-
}
-

-
int
-
pkg_jobs_entry(struct pkg_jobs_entry *je, struct pkg **pkg)
-
{
-
	assert(je != NULL);
-

-
	pkg_jobs_resolv(je);
+
	pkg_jobs_resolv(j);

	if (*pkg == NULL)
-
		*pkg = STAILQ_FIRST(&je->jobs);
+
		*pkg = STAILQ_FIRST(&j->jobs);
	else
		*pkg = STAILQ_NEXT(*pkg, next);

@@ -121,34 +71,15 @@ pkg_jobs_entry(struct pkg_jobs_entry *je, struct pkg **pkg)
		return (EPKG_OK);
}

-
int
-
pkg_jobs_exists(struct pkg_jobs *jobs, struct pkg *pkg, struct pkg **res)
-
{
-
	struct pkg_jobs_entry *je = NULL;
-
	struct pkg *p = NULL;
-

-
	while (pkg_jobs(jobs, &je) == EPKG_OK) {
-
		p = NULL; /* starts with the first package job */
-
		while (pkg_jobs_entry(je, &p) == EPKG_OK) {
-
			if (strcmp(pkg_get(p, PKG_ORIGIN), pkg_get(pkg, PKG_ORIGIN)) == 0) {
-
				*res = p;
-
				return (EPKG_FATAL);
-
			}
-
		}
-
	}
-

-
	return (EPKG_OK);
-
}
-

static int
-
pkg_jobs_install(struct pkg_jobs_entry *je)
+
pkg_jobs_install(struct pkg_jobs *j)
{
	struct pkg *p = NULL;
	const char *cachedir;
	char path[MAXPATHLEN];

	/* Fetch */
-
	while (pkg_jobs_entry(je, &p) == EPKG_OK) {
+
	while (pkg_jobs(j, &p) == EPKG_OK) {
		if (pkg_repo_fetch(p) != EPKG_OK)
			return (EPKG_FATAL);
	}
@@ -156,11 +87,11 @@ pkg_jobs_install(struct pkg_jobs_entry *je)
	/* Install */
	cachedir = pkg_config("PKG_CACHEDIR");
	p = NULL;
-
	while (pkg_jobs_entry(je, &p) == EPKG_OK) {
+
	while (pkg_jobs(j, &p) == EPKG_OK) {
		snprintf(path, sizeof(path), "%s/%s", cachedir,
				 pkg_get(p, PKG_REPOPATH));

-
		if (pkg_add(je->db, path) != EPKG_OK) {
+
		if (pkg_add(j->db, path) != EPKG_OK) {
			return (EPKG_FATAL);
		}
	}
@@ -169,13 +100,13 @@ pkg_jobs_install(struct pkg_jobs_entry *je)
}

static int
-
pkg_jobs_deinstall(struct pkg_jobs_entry *je, int force)
+
pkg_jobs_deinstall(struct pkg_jobs *j, int force)
{
	struct pkg *p = NULL;
-
	int retcode = EPKG_OK;
+
	int retcode;

-
	while (pkg_jobs_entry(je, &p) == EPKG_OK) {
-
		retcode = pkg_delete(p, je->db, force);
+
	while (pkg_jobs(j, &p) == EPKG_OK) {
+
		retcode = pkg_delete(p, j->db, force);
		if (retcode != EPKG_OK)
			return (retcode);
	}
@@ -184,24 +115,24 @@ pkg_jobs_deinstall(struct pkg_jobs_entry *je, int force)
}

int
-
pkg_jobs_apply(struct pkg_jobs_entry *je, int force)
+
pkg_jobs_apply(struct pkg_jobs *j, int force)
{
-
	if (je->type == PKG_JOBS_INSTALL)
-
		return (pkg_jobs_install(je));
-
	if (je->type == PKG_JOBS_DEINSTALL)
-
		return (pkg_jobs_deinstall(je, force));
+
	if (j->type == PKG_JOBS_INSTALL)
+
		return (pkg_jobs_install(j));
+
	if (j->type == PKG_JOBS_DEINSTALL)
+
		return (pkg_jobs_deinstall(j, force));

	EMIT_PKG_ERROR("%s", "bad jobs argument");
	return (EPKG_FATAL);
}

static struct pkg_jobs_node *
-
get_node(struct pkg_jobs_entry *je, const char *name, int create)
+
get_node(struct pkg_jobs *j, const char *name, int create)
{
-
	struct pkg_jobs_node *n = NULL;
+
	struct pkg_jobs_node *n;

	/* XXX hashmap? */
-
	LIST_FOREACH(n, &je->nodes, entries) {
+
	LIST_FOREACH(n, &j->nodes, entries) {
		if (strcmp(name, pkg_get(n->pkg, PKG_ORIGIN)) == 0) {
			return (n);
		}
@@ -211,7 +142,7 @@ get_node(struct pkg_jobs_entry *je, const char *name, int create)
		return (NULL);

	n = calloc(1, sizeof(struct pkg_jobs_node));
-
	LIST_INSERT_HEAD(&je->nodes, n, entries);
+
	LIST_INSERT_HEAD(&j->nodes, n, entries);
	return (n);
}

@@ -233,48 +164,48 @@ add_parent(struct pkg_jobs_node *n, struct pkg_jobs_node *p)
}

static void
-
add_dep(struct pkg_jobs_entry *je, struct pkg_jobs_node *n)
+
add_dep(struct pkg_jobs *j, struct pkg_jobs_node *n)
{
	struct pkg_dep *dep = NULL;
-
	struct pkg_jobs_node *ndep = NULL;
+
	struct pkg_jobs_node *ndep;

	while (pkg_deps(n->pkg, &dep) != EPKG_END) {
-
		ndep = get_node(je, pkg_dep_origin(dep), 1);
+
		ndep = get_node(j, pkg_dep_origin(dep), 1);
		if (ndep->pkg == NULL) {
-
			ndep->pkg = pkgdb_query_remote(je->db, pkg_dep_origin(dep));
+
			ndep->pkg = pkgdb_query_remote(j->db, pkg_dep_origin(dep));
			if (ndep->pkg == NULL)
				EMIT_MISSING_DEP(n->pkg, dep);
			else
-
				add_dep(je, ndep);
+
				add_dep(j, ndep);
		}
		add_parent(ndep, n);
	}
}

static void
-
add_rdep(struct pkg_jobs_entry *je, struct pkg_jobs_node *n)
+
add_rdep(struct pkg_jobs *j, struct pkg_jobs_node *n)
{
-
	struct pkg_jobs_node *nrdep = NULL;
+
	struct pkg_jobs_node *nrdep;
	struct pkg_dep *rdep = NULL;

-
	pkgdb_loadrdeps(je->db, n->pkg);
+
	pkgdb_loadrdeps(j->db, n->pkg);

	while (pkg_rdeps(n->pkg, &rdep) == EPKG_OK) {
-
		nrdep = get_node(je, pkg_dep_origin(rdep), 0);
+
		nrdep = get_node(j, pkg_dep_origin(rdep), 0);
		if (nrdep != NULL)
			add_parent(nrdep, n);
	}
}

static void
-
remove_node(struct pkg_jobs_entry *je, struct pkg_jobs_node *n)
+
remove_node(struct pkg_jobs *j, struct pkg_jobs_node *n)
{
-
	struct pkg_jobs_node *np = 0;
-
	size_t i = 0;
+
	struct pkg_jobs_node *np;
+
	size_t i;

	assert(n->nrefs == 0);

-
	STAILQ_INSERT_TAIL(&je->jobs, n->pkg, next);
+
	STAILQ_INSERT_TAIL(&j->jobs, n->pkg, next);

	LIST_REMOVE(n, entries);

@@ -287,42 +218,42 @@ remove_node(struct pkg_jobs_entry *je, struct pkg_jobs_node *n)
}

int
-
pkg_jobs_resolv(struct pkg_jobs_entry *je)
+
pkg_jobs_resolv(struct pkg_jobs *j)
{
-
	struct pkg_jobs_node *n = NULL, *tmp = NULL;
-
	struct pkg *p = NULL;
+
	struct pkg_jobs_node *n, *tmp;
+
	struct pkg *p;

-
	assert(je != NULL);
+
	assert(j != NULL);

-
	if (je->resolved == 1)
+
	if (j->resolved == 1)
		return (EPKG_OK);

	/* Create nodes and remove jobs form the queue */
-
	while (!STAILQ_EMPTY(&je->jobs)) {
-
		p = STAILQ_FIRST(&je->jobs);
-
		STAILQ_REMOVE_HEAD(&je->jobs, next);
+
	while (!STAILQ_EMPTY(&j->jobs)) {
+
		p = STAILQ_FIRST(&j->jobs);
+
		STAILQ_REMOVE_HEAD(&j->jobs, next);

-
		n = get_node(je, pkg_get(p, PKG_ORIGIN), 1);
+
		n = get_node(j, pkg_get(p, PKG_ORIGIN), 1);

		n->pkg = p;
	}

	/* Add dependencies into nodes */
-
	LIST_FOREACH(n, &je->nodes, entries) {
-
		if (je->type == PKG_JOBS_INSTALL)
-
			add_dep(je, n);
-
		if (je->type == PKG_JOBS_DEINSTALL)
-
			add_rdep(je, n);
+
	LIST_FOREACH(n, &j->nodes, entries) {
+
		if (j->type == PKG_JOBS_INSTALL)
+
			add_dep(j, n);
+
		if (j->type == PKG_JOBS_DEINSTALL)
+
			add_rdep(j, n);
	}

	/* Resolv !*/
	do {
-
		LIST_FOREACH_SAFE(n, &je->nodes, entries, tmp) {
+
		LIST_FOREACH_SAFE(n, &j->nodes, entries, tmp) {
			if (n->nrefs == 0)
-
				remove_node(je, n);
+
				remove_node(j, n);
		}
-
	} while (!LIST_EMPTY(&je->nodes));
+
	} while (!LIST_EMPTY(&j->nodes));

-
	je->resolved = 1;
+
	j->resolved = 1;
	return (EPKG_OK);
}
modified libpkg/pkg_private.h
@@ -36,7 +36,6 @@ struct pkg {
	STAILQ_HEAD(conflicts, pkg_conflict) conflicts;
	STAILQ_HEAD(scripts, pkg_script) scripts;
	STAILQ_HEAD(options, pkg_option) options;
-
	STAILQ_HEAD(repos_entry, pkg_repos_entry) repos;
	int flags;
	int64_t rowid;
	lic_t licenselogic;
@@ -96,16 +95,11 @@ struct pkg_option {
};

struct pkg_jobs {
-
	struct pkg_jobs_entry {
-
		STAILQ_HEAD(jobs, pkg) jobs;
-
		LIST_HEAD(nodes, pkg_jobs_node) nodes;
-
		struct pkgdb *db;
-
		pkg_jobs_t type;
-
		unsigned int resolved :1;
-
		STAILQ_ENTRY(pkg_jobs_entry) next;
-
	} j;
-

-
	STAILQ_HEAD(jobs_multi, pkg_jobs_entry) multi;
+
	STAILQ_HEAD(jobs, pkg) jobs;
+
	LIST_HEAD(nodes, pkg_jobs_node) nodes;
+
	struct pkgdb *db;
+
	pkg_jobs_t type;
+
	unsigned int resolved :1;
};

struct pkg_jobs_node {
@@ -117,15 +111,10 @@ struct pkg_jobs_node {
	LIST_ENTRY(pkg_jobs_node) entries;
};

-
struct pkg_repos {
-
	struct pkg_repos_entry {
-
		char *name;
-
		char *url;
-
		unsigned int line;
-
		STAILQ_ENTRY(pkg_repos_entry) entries;
-
	} r;
-

-
	STAILQ_HEAD(repos, pkg_repos_entry) nodes;
+
struct pkg_remote_repo {
+
	char *name;
+
	char *url;
+
	STAILQ_ENTRY(pkg_remote_repo) entries;
};

int pkg_open2(struct pkg **p, struct archive **a, struct archive_entry **ae, const char *path);
@@ -163,8 +152,7 @@ void pkg_script_free(struct pkg_script *);
int pkg_option_new(struct pkg_option **);
void pkg_option_free(struct pkg_option *);

-
int pkg_jobs_resolv(struct pkg_jobs_entry *je);
-
void pkg_jobs_free_entry(struct pkg_jobs_entry *je);
+
int pkg_jobs_resolv(struct pkg_jobs *jobs);

struct packing;

modified libpkg/pkg_repo.c
@@ -1,5 +1,3 @@
-
#include <err.h>
-
#include <ctype.h>
#include <assert.h>
#include <errno.h>
#include <libgen.h>
@@ -10,20 +8,14 @@
#include "pkg_event.h"
#include "pkg_private.h"

-
static int pkg_repos_is_reserved_name(struct pkg_repos *repos, struct pkg_repos_entry *re);
-
static int pkg_repos_exists_in_pkg(struct pkg *pkg, struct pkg_repos_entry *re);
-

int
pkg_repo_fetch(struct pkg *pkg)
{
	char dest[MAXPATHLEN];
	char cksum[65];
-
	char *path = NULL;
-
	char *url = NULL;
+
	char *path;
+
	char *url;
	int retcode = EPKG_OK;
-
	int fetch_ok = 0;
-

-
	struct pkg_repos_entry *re = NULL;

	assert((pkg->type & PKG_REMOTE) == PKG_REMOTE ||
		(pkg->type & PKG_UPGRADE) == PKG_UPGRADE);
@@ -31,7 +23,7 @@ pkg_repo_fetch(struct pkg *pkg)
	snprintf(dest, sizeof(dest), "%s/%s", pkg_config("PKG_CACHEDIR"),
			 pkg_get(pkg, PKG_REPOPATH));

-
	/* If it is already in the local cachedir, don't bother to download it */
+
	/* If it is already in the local cachedir, dont bother to download it */
	if (access(dest, F_OK) == 0)
		goto checksum;

@@ -44,37 +36,11 @@ pkg_repo_fetch(struct pkg *pkg)
	if ((retcode = mkdirs(path)) != 0)
		goto cleanup;

-
	if (pkg_config("PACKAGESITE") != NULL) {
-
		asprintf(&url, "%s/%s", pkg_config("PACKAGESITE"),
-
				 pkg_get(pkg, PKG_REPOPATH));
-

-
		retcode = pkg_fetch_file(url, dest);
-
		free(url);
-
	} else {
-
		/* 
-
		 * Get the repository URL from the package itself 
-
		 * Working on multiple repos here.
-
		 *
-
		 * If a package fetch is not successful go to the next
-
		 * repository in the list and give it more chance for
-
		 * successful fetch :)
-
		 */
-

-
		re = NULL; /* starts with the first repository entry */
-
		fetch_ok = EPKG_FATAL;
-
		while ((pkg_repos_next_in_pkg(pkg, &re) == EPKG_OK) && fetch_ok != EPKG_OK) {
-
			printf("Fetching package from repository '%s' [%s]\n",
-
					pkg_repos_get_name(re),
-
					pkg_repos_get_url(re));
-

-
			asprintf(&url, "%s/%s", pkg_repos_get_url(re),
-
					 pkg_get(pkg, PKG_REPOPATH));
-

-
			fetch_ok = retcode = pkg_fetch_file(url, dest);
-
			free(url);
-
		}
-
	}
+
	asprintf(&url, "%s/%s", pkg_config("PACKAGESITE"),
+
			 pkg_get(pkg, PKG_REPOPATH));

+
	retcode = pkg_fetch_file(url, dest);
+
	free(url);
	if (retcode != EPKG_OK)
		goto cleanup;

@@ -92,260 +58,3 @@ pkg_repo_fetch(struct pkg *pkg)

	return (retcode);
}
-

-
int
-
pkg_repos_new(struct pkg_repos **repos)
-
{
-
	if ((*repos = calloc(1, sizeof(struct pkg_repos))) == NULL) {
-
		EMIT_ERRNO("calloc", "pkg_repos");
-
		return (EPKG_FATAL);
-
	}
-

-
	STAILQ_INIT(&(*repos)->nodes);
-

-
	return (EPKG_OK);
-
}
-

-
int
-
pkg_repos_load(struct pkg_repos *repos)
-
{
-
        FILE *fp;
-
	char *repo_buf[MAXPATHLEN];
-
        char buf[MAXPATHLEN];
-
        char *token = NULL, *tmp = NULL;
-
        unsigned int count = 0, line = 0;
-
	struct pkg_repos_entry *re = NULL;
-

-
	assert(repos != NULL);
-

-
        if ((fp = fopen("/etc/pkg/repositories", "r")) == NULL) {
-
		EMIT_ERRNO("fopen", "/etc/pkg/repositories");
-
		return (EPKG_FATAL);
-
	}
-

-
        while (fgets(buf, MAXPATHLEN, fp)) {
-
                line++;
-

-
                if (buf[0] == '\n' || buf[0] == '#' || buf[0] == ';')
-
                        continue;
-

-
                count = 0;
-

-
                buf[strlen(buf) - 1] = '\0';
-
                tmp = buf;
-

-
		/* get the repository entries */
-
		while ((token = strsep(&tmp, " \t=")) != NULL)
-
			if (*token != '\0')
-
				repo_buf[count++] = token;
-

-
		if (count != 2) {
-
                        warnx("Wrong repository format at line %d (ignoring repository)", line);
-
                        continue;
-
                }
-

-
		if ((re = calloc(1, sizeof(struct pkg_repos_entry))) == NULL) {
-
			EMIT_ERRNO("calloc", "pkg_repos_entry");
-
			return (EPKG_FATAL);
-
		}
-

-
		re->name = strdup(repo_buf[0]);
-
		re->url  = strdup(repo_buf[1]);
-
		re->line = line;
-

-
		assert(re->name != NULL && re->url != NULL);
-
                
-
                pkg_repos_add(repos, re);
-
        }
-

-
        fclose(fp);
-

-
        return (EPKG_OK);
-
}
-

-
int
-
pkg_repos_add(struct pkg_repos *repos, struct pkg_repos_entry *re)
-
{
-
	assert(repos != NULL && re != NULL);
-

-
	if (pkg_repos_is_reserved_name(repos, re) != EPKG_OK) {
-
		warnx("Repository name for '%s' is already reserved (ignoring repository at line %d)",
-
				pkg_repos_get_name(re), pkg_repos_get_line(re));
-

-
		if (re->name != NULL)
-
			free(re->name);
-
		if (re->url != NULL)
-
			free(re->url);
-

-
		free(re);
-

-
		return (EPKG_FATAL);
-
	}
-

-
        STAILQ_INSERT_TAIL(&repos->nodes, re, entries);
-

-
        return (EPKG_OK);
-
}
-

-
int
-
pkg_repos_add_in_pkg(struct pkg *pkg, struct pkg_repos_entry *re)
-
{
-
	struct pkg_repos_entry *newre;
-

-
	assert(pkg != NULL && re != NULL);
-

-
	if (pkg_repos_exists_in_pkg(pkg, re) == EPKG_FATAL)
-
		return (EPKG_FATAL);
-

-
	if ((newre = calloc(1, sizeof(struct pkg_repos_entry))) == NULL) {
-
		EMIT_ERRNO("calloc", "pkg_repos_entry");
-
		return (EPKG_FATAL);
-
	}
-

-
	newre->name = strdup(re->name);
-
	newre->url  = strdup(re->url);
-
	newre->line = re->line;
-

-
	assert (newre->name != NULL && newre->url != NULL);
-

-
	STAILQ_INSERT_TAIL(&pkg->repos, newre, entries);
-

-
	return (EPKG_OK);
-
}
-

-
int
-
pkg_repos_next(struct pkg_repos *repos, struct pkg_repos_entry **re)
-
{
-
	assert(repos != NULL);
-

-
	if (*re == NULL)
-
		*re = STAILQ_FIRST(&repos->nodes);
-
	else
-
		*re = STAILQ_NEXT(*re, entries);
-

-
	if (*re == NULL)
-
		return (EPKG_END);
-
	else
-
		return (EPKG_OK);
-
}
-

-
int
-
pkg_repos_next_in_pkg(struct pkg *pkg, struct pkg_repos_entry **re)
-
{
-
	assert(pkg != NULL);
-

-
	if (*re == NULL)
-
		*re = STAILQ_FIRST(&pkg->repos);
-
	else
-
		*re = STAILQ_NEXT(*re, entries);
-

-
	if (*re == NULL)
-
		return (EPKG_END);
-
	else
-
		return (EPKG_OK);
-
}
-

-
const char *
-
pkg_repos_get_name(struct pkg_repos_entry *re)
-
{
-
	assert(re != NULL);
-

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

-
const char *
-
pkg_repos_get_url(struct pkg_repos_entry *re)
-
{
-
	assert(re != NULL);
-

-
	return (re->url);
-
}
-

-
unsigned int
-
pkg_repos_get_line(struct pkg_repos_entry *re)
-
{
-
	assert(re != NULL);
-

-
	return(re->line);
-
}
-

-
void
-
pkg_repos_free(struct pkg_repos *repos)
-
{
-
	struct pkg_repos_entry *re1, *re2;
-

-
	if (repos == NULL)
-
		return;
-

-
        re1 = STAILQ_FIRST(&repos->nodes);
-
        while (re1 != NULL) {
-
                re2 = STAILQ_NEXT(re1, entries);
-
                
-
                if (re1->name != NULL)
-
                        free(re1->name);
-
                if (re1->url != NULL)
-
                        free(re1->url);
-

-
                free(re1);
-
                re1 = re2;
-
        }
-

-
	free(repos);
-
}
-

-
void
-
pkg_repos_free_in_pkg(struct pkg *pkg)
-
{
-
	struct pkg_repos_entry *re1, *re2;
-

-
	if (pkg == NULL)
-
		return;
-

-
	re1 = STAILQ_FIRST(&pkg->repos);
-
	while (re1 != NULL) {
-
		re2 = STAILQ_NEXT(re1, entries);
-

-
		if (re1->name != NULL)
-
			free(re1->name);
-
		if (re1->url != NULL)
-
			free(re1->url);
-

-
		free(re1);
-
		re1 = re2;
-
	}
-
}
-

-
static int
-
pkg_repos_is_reserved_name(struct pkg_repos *repos, struct pkg_repos_entry *re)
-
{
-
	struct pkg_repos_entry *next = NULL;
-

-
	assert(repos != NULL && re != NULL);
-

-
	/* 
-
	 * Find if a repository name already exists.
-
	 * NOTE: The 'repo' name is always reserved, 
-
	 * as it is being used by default when 
-
	 * PACKAGESITE is defined.
-
	 */
-
	while (pkg_repos_next(repos, &next) == EPKG_OK)
-
		if ((strcmp(pkg_repos_get_name(re), pkg_repos_get_name(next)) == 0) || \
-
		    (strcmp(pkg_repos_get_name(re), "repo") == 0))
-
			return (EPKG_FATAL);
-

-
	return (EPKG_OK);
-
}
-

-
static int
-
pkg_repos_exists_in_pkg(struct pkg *pkg, struct pkg_repos_entry *re)
-
{
-
	struct pkg_repos_entry *tmp = NULL;
-

-
	assert(pkg != NULL && re != NULL);
-

-
	while(pkg_repos_next_in_pkg(pkg, &tmp) == EPKG_OK)
-
		if (strcmp(pkg_repos_get_name(tmp), pkg_repos_get_name(re)) == 0)
-
			return (EPKG_FATAL);
-

-
	return (EPKG_OK);
-
}
modified libpkg/pkgdb.c
@@ -378,7 +378,7 @@ pkgdb_init(sqlite3 *sdb)
}

int
-
pkgdb_open(struct pkgdb **db, pkgdb_t type, const char *dbfile)
+
pkgdb_open(struct pkgdb **db, pkgdb_t type)
{
	int retcode;
	char *errmsg;
@@ -419,7 +419,7 @@ pkgdb_open(struct pkgdb **db, pkgdb_t type, const char *dbfile)
	}

	if (type == PKGDB_REMOTE) {
-
		snprintf(remotepath, sizeof(remotepath), "%s/%s", dbdir, dbfile);
+
		snprintf(remotepath, sizeof(remotepath), "%s/repo.sqlite", dbdir);

		if (access(remotepath, R_OK) != 0) {
			EMIT_ERRNO("access", remotepath);
modified pkg/add.c
@@ -51,7 +51,7 @@ exec_add(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, "local.sqlite") != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		return (EX_IOERR);
	}

modified pkg/autoremove.c
@@ -61,7 +61,7 @@ exec_autoremove(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, "local.sqlite") != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		return (EX_IOERR);
	}

modified pkg/backup.c
@@ -24,7 +24,7 @@ exec_backup(int argc, char **argv)
	if (argc == 3)
		dest = argv[2];

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, "local.sqlite") != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		return (EX_IOERR);
	}

modified pkg/create.c
@@ -30,7 +30,7 @@ pkg_create_matches(int argc, char **argv, match_t match, pkg_formats fmt, const
					  PKG_LOAD_DIRS | PKG_LOAD_SCRIPTS | PKG_LOAD_OPTIONS |
					  PKG_LOAD_MTREE | PKG_LOAD_LICENSES;

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, "local.sqlite") != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		pkgdb_close(db);
		return (EX_IOERR);
	}
modified pkg/delete.c
@@ -21,12 +21,9 @@ int
exec_delete(int argc, char **argv)
{
	struct pkg_jobs *jobs = NULL;
-
	struct pkg_jobs_entry *je = NULL;
-

	struct pkg *pkg = NULL;
	struct pkgdb *db = NULL;
	struct pkgdb_it *it = NULL;
-

	match_t match = MATCH_EXACT;
	char *origin = NULL;
	int ch;
@@ -60,22 +57,14 @@ exec_delete(int argc, char **argv)
		return (EX_NOPERM);
	}
	
-
	if ((retcode = pkgdb_open(&db, PKGDB_DEFAULT, "local.sqlite")) != EPKG_OK) {
+
	if ((retcode = pkgdb_open(&db, PKGDB_DEFAULT)) != EPKG_OK) {
		goto cleanup;
	}

	if (argc == 1)
		origin = argv[0];

-
	/* create a jobs object */
-
	if (pkg_jobs_new(&jobs) != EPKG_OK) {
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	/* create a jobs entry */
-
	if (pkg_jobs_new_entry(jobs, &je, PKG_JOBS_DEINSTALL, db) != EPKG_OK) {
-
		retcode = EPKG_FATAL;
+
	if ((retcode = pkg_jobs_new(&jobs, PKG_JOBS_DEINSTALL, db)) != EPKG_OK) {
		goto cleanup;
	}

@@ -85,7 +74,7 @@ exec_delete(int argc, char **argv)
	}

	while ((retcode = pkgdb_it_next(it, &pkg, flags)) == EPKG_OK) {
-
		pkg_jobs_add(je, pkg);
+
		pkg_jobs_add(jobs, pkg);
		pkg = NULL;
	}

@@ -93,7 +82,7 @@ exec_delete(int argc, char **argv)
		goto cleanup;
	}

-
	if ((retcode = pkg_jobs_apply(je, force)) != EPKG_OK) {
+
	if ((retcode = pkg_jobs_apply(jobs, force)) != EPKG_OK) {
		goto cleanup;
	}

@@ -101,6 +90,7 @@ exec_delete(int argc, char **argv)

	cleanup:
	pkgdb_it_free(it);
+
	pkgdb_close(db);
	pkg_jobs_free(jobs);
	pkgdb_close(db);

modified pkg/event.c
@@ -20,7 +20,7 @@ event_callback(void *data __unused, struct pkg_event *ev)
		break;
	case PKG_EVENT_FETCHING:
		percent = ((float)ev->e_fetching.done / (float)ev->e_fetching.total) * 100;
-
		printf("\rFetching from %s... %d%%", ev->e_fetching.url, percent);
+
		printf("\rFetching %s... %d%%", ev->e_fetching.url, percent);
		if (ev->e_fetching.done == ev->e_fetching.total)
			printf("\n");
		fflush(stdout);
modified pkg/info.c
@@ -172,7 +172,7 @@ exec_info(int argc, char **argv)
		return (EX_USAGE);
	}

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, "local.sqlite") != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		return (EX_IOERR);
	}

modified pkg/install.c
@@ -1,5 +1,4 @@
#include <sys/types.h>
-
#include <sys/param.h>

#include <err.h>
#include <libgen.h>
@@ -23,19 +22,11 @@ usage_install(void)
int
exec_install(int argc, char **argv)
{
-
	char dbfile[MAXPATHLEN];
-

-
	int retcode = EPKG_OK;
-
	int i, multi_repos = 0;
-

-
	struct pkg_jobs *jobs = NULL;
-
	struct pkg_jobs_entry *je = NULL;
-

-
	struct pkg_repos *repos = NULL;
-
	struct pkg_repos_entry *re = NULL;
-

-
	struct pkg *pkg = NULL, *tmp = NULL;
+
	struct pkg *pkg = NULL;
	struct pkgdb *db = NULL;
+
	struct pkg_jobs *jobs = NULL;
+
	int retcode = EPKG_OK;
+
	int i;

	if (argc < 2) {
		usage_install();
@@ -47,127 +38,35 @@ exec_install(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	/* create a jobs object */
-
	if (pkg_jobs_new(&jobs) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
+
		return (EX_IOERR);
+
	}
+

+
	if (pkg_jobs_new(&jobs, PKG_JOBS_INSTALL, db) != EPKG_OK) {
		retcode = EPKG_FATAL;
		goto cleanup;
	}

-
	/*
-
	 * Honor PACKAGESITE if specified 
-
	 * Working on a single repo database
-
	 */
-
	if (pkg_config("PACKAGESITE") != NULL) {
-
                if (pkgdb_open(&db, PKGDB_REMOTE, "repo.sqlite") != EPKG_OK) {
-
                        warnx("cannot open repository database: %s/repo.sqlite\n", pkg_config("PKG_DBDIR"));
-
                        retcode = EPKG_FATAL;
-
                        goto cleanup;
-
                }
-

-
                /* create a jobs entry */
-
                if (pkg_jobs_new_entry(jobs, &je, PKG_JOBS_INSTALL, db) != EPKG_OK) {
-
                        retcode = EPKG_FATAL;
-
                        goto cleanup;
-
                }
-

-
		for (i = 1; i < argc; i++) {
-
			if ((pkg = pkgdb_query_remote(db, argv[i])) == NULL) {
-
				retcode = EPKG_FATAL;
-
				goto cleanup;
-
			}
-
			
-
			/* pkg_jobs_resolv() will be enough here for jobs resolving :) */
-
			pkg_jobs_add(je, pkg);
+
	for (i = 1; i < argc; i++) {
+
		if ((pkg = pkgdb_query_remote(db, argv[i])) == NULL) {
+
			retcode = EPKG_FATAL;
+
			goto cleanup;
		}
-
	} else {
-
		/* MULTI_REPOS_INSTALL */
-

-
		multi_repos = 1;

-
                fprintf(stderr, "\n");
-
                warnx("/!\\     Working on multiple repositories     /!\\");
-
                warnx("/!\\  This is an unsupported preview feature  /!\\");
-
                warnx("/!\\     It can kill kittens and puppies      /!\\");
-
                fprintf(stderr, "\n");
-

-
                if (pkg_repos_new(&repos) != EPKG_OK) {
-
                        retcode = EPKG_FATAL;
-
                        goto cleanup;
-
                }
-

-
                if (pkg_repos_load(repos) != EPKG_OK) {
-
                        retcode = EPKG_FATAL;
-
                        goto cleanup;
-
                }
-
        
-
                while (pkg_repos_next(repos, &re) == EPKG_OK) { 
-
                        snprintf(dbfile, MAXPATHLEN, "%s.sqlite", pkg_repos_get_name(re));
-

-
                        if (pkgdb_open(&db, PKGDB_REMOTE, dbfile) != EPKG_OK) {
-
                                warnx("cannot open repository database: %s/%s\n", 
-
                                                pkg_config("PKG_DBDIR"), dbfile);
-
                                retcode = EPKG_FATAL;
-
                                goto cleanup;
-
                        }
-

-
                        /* create a jobs entry for each db connection */
-
                        if (pkg_jobs_new_entry(jobs, &je, PKG_JOBS_INSTALL, db) != EPKG_OK) {
-
                                retcode = EPKG_FATAL;
-
                                goto cleanup;
-
                        }
-

-
        		for (i = 1; i < argc; i++) {
-
				if ((pkg = pkgdb_query_remote(db, argv[i])) == NULL) {
-
					retcode = EPKG_FATAL;
-
					goto cleanup;
-
				}
-
				
-
				/* 
-
				 * check if the job already exists in other job entries
-
				 * If it exists we add additional repo to the package.
-
				 * Otherwise we have a new package job.
-
				 */
-
				tmp = NULL;
-
				if (pkg_jobs_exists(jobs, pkg, &tmp) == EPKG_OK)
-
					pkg_jobs_add(je, pkg);
-
				else
-
					pkg_repos_add_in_pkg(tmp, re);
-
			}
-
                }
-
	} /* !MULTI_REPOS_INSTALL */
+
		pkg_jobs_add(jobs, pkg);
+
	}

	/* print a summary before applying the jobs */
+
	pkg = NULL;
	printf("The following packages will be installed:\n");
-

-
	je = NULL; /* starts with the first job entry */
-
	while (pkg_jobs(jobs, &je) == EPKG_OK) {
-
		pkg = NULL; /* start with the first package in a job entry */
-
		while (pkg_jobs_entry(je, &pkg) == EPKG_OK) {
-
			printf("\t%s-%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
-

-
			if (multi_repos == 1) {
-
				printf(" [ found in repos: ");
-

-
				re = NULL;
-
				while (pkg_repos_next_in_pkg(pkg, &re) == EPKG_OK)
-
					printf("%s ", pkg_repos_get_name(re));
-

-
				printf(" ]");
-
			}
-

-
			printf("\n");
-
		}
+
	while (pkg_jobs(jobs, &pkg) == EPKG_OK) {
+
		printf("%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
	}

-
	je = NULL;
-
	while (pkg_jobs(jobs, &je) == EPKG_OK)
-
		retcode = pkg_jobs_apply(je, 0);
+
	retcode = pkg_jobs_apply(jobs, 0);

	cleanup:
	
-
	if (multi_repos == 1)
-
		pkg_repos_free(repos);
-

	pkg_jobs_free(jobs);
	pkgdb_close(db);

modified pkg/register.c
@@ -181,7 +181,7 @@ exec_register(int argc, char **argv)
	if (plist != NULL)
		free(plist);

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, "local.sqlite") != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		return (EX_IOERR);
	}

modified pkg/search.c
@@ -1,6 +1,3 @@
-
#include <sys/param.h>
-

-
#include <err.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
@@ -11,26 +8,25 @@

#include "search.h"

-
static int search_remote_repo(const char *pattern, match_t match, 
-
		unsigned int field, const char *dbname);
-

void
usage_search(void)
{
-
	fprintf(stderr, "usage: pkg search [-gxXcd] pattern\n\n");
+
	fprintf(stderr, "usage, pkg search [-gxXcd] pattern\n");
	fprintf(stderr, "For more information see 'pkg help search'.\n");
}

int
exec_search(int argc, char **argv)
{
-
	char *pattern = NULL;
+
	char *pattern;
	match_t match = MATCH_EXACT;
+
	int  retcode = EPKG_OK;
	unsigned int field = REPO_SEARCH_NAME;
-
	int retcode = EPKG_OK;
	int ch;
-
	struct pkg_repos *repos = NULL;
-
	struct pkg_repos_entry *re = NULL;
+
	char size[7];
+
	struct pkgdb *db = NULL;
+
	struct pkgdb_it *it = NULL;
+
	struct pkg *pkg = NULL;

	while ((ch = getopt(argc, argv, "gxXcd")) != -1) {
		switch (ch) {
@@ -65,82 +61,26 @@ exec_search(int argc, char **argv)

	pattern = argv[0];

-
	/*
-
	 * TODO: Implement a feature to search only
-
	 * in a given repository specified in the argument list
-
	 */
-

-
	/* 
-
	 * Honor PACKAGESITE if specified
-
	 */
-
	if (pkg_config("PACKAGESITE") != NULL) {
-
		retcode = search_remote_repo(pattern, match, field, "repo");
-
	} else {
-
		fprintf(stderr, "\n");
-
		warnx("/!\\     Working on multiple repositories     /!\\");
-
		warnx("/!\\  This is an unsupported preview feature  /!\\");
-
		warnx("/!\\     It can kill kittens and puppies      /!\\");
-
		fprintf(stderr, "\n");
-

-
		if (pkg_repos_new(&repos) != EPKG_OK)
-
			return (EPKG_FATAL);
-

-
		if (pkg_repos_load(repos) != EPKG_OK)
-
			return (EPKG_FATAL);
-
	
-
		while (pkg_repos_next(repos, &re) == EPKG_OK)
-
			retcode = search_remote_repo(pattern, match, field, pkg_repos_get_name(re));
-

-
		pkg_repos_free(repos);
+
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
	}

-
	return (retcode);
-
}
-

-
static int
-
search_remote_repo(const char *pattern, match_t match, unsigned int field, const char *dbname)
-
{
-
	char size[7];
-
	char dbfile[MAXPATHLEN];
-
	int  retcode = EPKG_OK;
-
	struct pkgdb *db = NULL;
-
	struct pkgdb_it *it = NULL;
-
	struct pkg *pkg = NULL;
-

-
	snprintf(dbfile, MAXPATHLEN, "%s.sqlite", dbname);
-

-
	if (pkgdb_open(&db, PKGDB_REMOTE, dbfile) != EPKG_OK) {
-
		warnx("cannot open repository database: %s/%s\n", 
-
				pkg_config("PKG_DBDIR"), dbfile);
-
		return (EPKG_FATAL);
-
	}
-

-
	if ((it = pkgdb_rquery(db, pattern, match, field)) == NULL) {
-
		warnx("cannot query repository database: %s/%s\n",
-
				pkg_config("PKG_DBDIR"), dbfile);
-
		pkgdb_it_free(it);
-
		pkgdb_close(db);
-
		return (EPKG_FATAL);
-
	}
-

-
	while ((retcode = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
-
		printf("Name:       %s\n", pkg_get(pkg, PKG_NAME));
-
		printf("Version:    %s\n", pkg_get(pkg, PKG_VERSION));
-
		printf("Origin:     %s\n", pkg_get(pkg, PKG_ORIGIN));
-
		printf("Arch:       %s\n", pkg_get(pkg, PKG_ARCH));
+
	while (( retcode = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
+
		printf("Name: %s\n", pkg_get(pkg, PKG_NAME));
+
		printf("Version: %s\n", pkg_get(pkg, PKG_VERSION));
+
		printf("Origin: %s\n", pkg_get(pkg, PKG_ORIGIN));
		printf("Maintainer: %s\n", pkg_get(pkg, PKG_MAINTAINER));
-
		printf("WWW:        %s\n", pkg_get(pkg, PKG_WWW));
-
		printf("Comment:    %s\n", pkg_get(pkg, PKG_COMMENT));
-
		printf("Repository: %s\n", dbname);
+
		printf("WWW: %s\n", pkg_get(pkg, PKG_WWW));
+
		printf("Comment: %s\n", pkg_get(pkg, PKG_COMMENT));
		humanize_number(size, sizeof(size), pkg_new_flatsize(pkg), "B", HN_AUTOSCALE, 0);
-
		printf("Flat size:  %s\n", size);
+
		printf("Flat size: %s\n", size);
		humanize_number(size, sizeof(size), pkg_new_pkgsize(pkg), "B", HN_AUTOSCALE, 0);
-
		printf("Pkg size:   %s\n", size);
+
		printf("Pkg size: %s\n", size);
		printf("\n");
-

	}

-
	pkg_free(pkg);
+
	cleanup:
	pkgdb_it_free(it);
	pkgdb_close(db);

modified pkg/update.c
@@ -21,8 +21,6 @@
		ARCHIVE_EXTRACT_TIME  |ARCHIVE_EXTRACT_ACL | \
		ARCHIVE_EXTRACT_FFLAGS|ARCHIVE_EXTRACT_XATTR)

-
static int update_from_remote_repo(const char *name, const char *url);
-

void
usage_update(void)
{
@@ -35,9 +33,10 @@ exec_update(int argc, char **argv)
{
	char url[MAXPATHLEN];
	const char *packagesite = NULL;
+
	char *tmp = NULL;
	int retcode = EPKG_OK;
-
	struct pkg_repos *repos = NULL;
-
	struct pkg_repos_entry *re = NULL;
+
	struct archive *a;
+
	struct archive_entry *ae;

	(void)argv;
	if (argc != 1) {
@@ -50,55 +49,15 @@ exec_update(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	/* 
-
	 * If PACKAGESITE is defined fetch only the remote
-
	 * database to which PACKAGESITE refers, otherwise
-
	 * fetch all remote databases found in the configuration file.
-
	 */
-
	if ((packagesite = pkg_config("PACKAGESITE")) != NULL) {
-
		if (packagesite[strlen(packagesite) - 1] == '/')
-
			snprintf(url, MAXPATHLEN, "%srepo.txz", packagesite);
-
		else
-
			snprintf(url, MAXPATHLEN, "%s/repo.txz", packagesite);
-

-
		retcode = update_from_remote_repo("repo", url);
-
	} else {
-
		fprintf(stderr, "\n");
-
                warnx("/!\\     Working on multiple repositories     /!\\");
-
                warnx("/!\\  This is an unsupported preview feature  /!\\");
-
                warnx("/!\\     It can kill kittens and puppies      /!\\\n");
-
		fprintf(stderr, "\n");
-

-
		if (pkg_repos_new(&repos) != EPKG_OK)
-
			return (EPKG_FATAL);
-

-
		if (pkg_repos_load(repos) != EPKG_OK)
-
			return (EPKG_FATAL);
-

-
		while (pkg_repos_next(repos, &re) == EPKG_OK) {
-
			packagesite = pkg_repos_get_url(re);
-

-
			if (packagesite[strlen(packagesite) - 1] == '/')
-
				snprintf(url, MAXPATHLEN, "%srepo.txz", packagesite);
-
			else
-
				snprintf(url, MAXPATHLEN, "%s/repo.txz", packagesite);
-

-
			retcode = update_from_remote_repo(pkg_repos_get_name(re), url);
-
		}
-

-
		pkg_repos_free(repos);
+
	if ((packagesite = pkg_config("PACKAGESITE")) == NULL) {
+
		warnx("unable to determine PACKAGESITE");
+
		return (EPKG_FATAL);
	}

-
	return (retcode);
-
}
-

-
int
-
update_from_remote_repo(const char *name, const char *url)
-
{
-
	struct archive *a = NULL;
-
	struct archive_entry *ae;
-
	char repofile[MAXPATHLEN];
-
	char *tmp = NULL;
+
	if (packagesite[strlen(packagesite) - 1] == '/')
+
		snprintf(url, MAXPATHLEN, "%srepo.txz", packagesite);
+
	else
+
		snprintf(url, MAXPATHLEN, "%s/repo.txz", packagesite);

	tmp = mktemp(strdup("/tmp/repo.txz.XXXXXX"));

@@ -116,9 +75,7 @@ update_from_remote_repo(const char *name, const char *url)

	while (archive_read_next_header(a, &ae) == ARCHIVE_OK) {
		if (strcmp(archive_entry_pathname(ae), "repo.sqlite") == 0) {
-
			snprintf(repofile, MAXPATHLEN, "%s/%s.sqlite",
-
				       pkg_config("PKG_DBDIR"), name);
-
			archive_entry_set_pathname(ae, repofile);
+
			archive_entry_set_pathname(ae, "/var/db/pkg/repo.sqlite");
			archive_read_extract(a, ae, EXTRACT_ARCHIVE_FLAGS);
			break;
		}
@@ -130,5 +87,5 @@ update_from_remote_repo(const char *name, const char *url)
	unlink(tmp);
	free(tmp);

-
	return (EPKG_OK);
+
	return (retcode);
}
modified pkg/upgrade.c
@@ -43,7 +43,7 @@ exec_upgrade(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	if (pkgdb_open(&db, PKGDB_REMOTE, "repo.sqlite") != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
		return (EX_IOERR);
	}

modified pkg/which.c
@@ -33,7 +33,7 @@ exec_which(int argc, char **argv)
		return (EX_USAGE);
	}

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, "local.sqlite") != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		pkgdb_close(db);
		return (EX_IOERR);
	}