Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Cleanup redundant code for updating into pkgcli_update()
Bryan Drewery committed 13 years ago
commit 51ac2eed0f6c59606ab7240ac6c790341984e401
parent 99a9e54
5 files changed +53 -171
modified pkg/fetch.c
@@ -25,7 +25,6 @@
 */

#include <sys/types.h>
-
#include <sys/param.h>

#include <err.h>
#include <libgen.h>
@@ -50,19 +49,14 @@ usage_fetch(void)
int
exec_fetch(int argc, char **argv)
{
-
	char url[MAXPATHLEN];
	struct pkg *pkg = NULL;
	struct pkgdb_it *it = NULL;
	struct pkgdb *db = NULL;
	struct pkg_jobs *jobs = NULL;
	const char *reponame = NULL;
-
	const char *repo_name = NULL;
-
	const char *packagesite = NULL;
-
	struct pkg_config_kv *repokv = NULL;
	int retcode = EXIT_FAILURE;
	int ch;
	bool yes = false;
-
	bool multi_repos = false;
	match_t match = MATCH_EXACT;

	while ((ch = getopt(argc, argv, "ygxXr:qa")) != -1) {
@@ -108,45 +102,9 @@ exec_fetch(int argc, char **argv)
	}

	/* first update the remote repositories if needed */
-
	pkg_config_bool(PKG_CONFIG_MULTIREPOS, &multi_repos);
-
	if (!multi_repos) {
-
		/*
-
		 * Single remote database
-
		 */
-
		pkg_config_string(PKG_CONFIG_REPO, &packagesite);
-
		if (packagesite == NULL) {
-
			warnx("PACKAGESITE is not defined.");
-
			return (1);
-
		}
-

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

-
		retcode = pkg_update("repo", url);
-
		if (retcode == EPKG_UPTODATE)
-
			retcode = EPKG_OK;
-
	} else {
-
		/* multiple repositories */
-
		while (pkg_config_list(PKG_CONFIG_REPOS, &repokv) == EPKG_OK) {
-
			repo_name = pkg_config_kv_get(repokv, PKG_CONFIG_KV_KEY);
-
			packagesite = pkg_config_kv_get(repokv, PKG_CONFIG_KV_VALUE);
-

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

-
			retcode = pkg_update(repo_name, url);
-
			if (retcode == EPKG_UPTODATE) {
-
				retcode = EPKG_OK;
-
			}
-
		}
-
	}
-

-
	if (retcode != EPKG_OK)
+
	if ((retcode = pkgcli_update()) != EPKG_OK)
		return (retcode);
+

	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
		return (EX_IOERR);
	}
modified pkg/install.c
@@ -27,7 +27,6 @@
 */

#include <sys/types.h>
-
#include <sys/param.h>

#include <err.h>
#include <libgen.h>
@@ -52,19 +51,14 @@ usage_install(void)
int
exec_install(int argc, char **argv)
{
-
	char url[MAXPATHLEN];
	struct pkg *pkg = NULL;
	struct pkgdb_it *it = NULL;
	struct pkgdb *db = NULL;
	struct pkg_jobs *jobs = NULL;
	const char *reponame = NULL;
-
	const char *repo_name = NULL;
-
	const char *packagesite = NULL;
-
	struct pkg_config_kv *repokv = NULL;
	int retcode = 1;
	int ch;
	bool yes = false;
-
	bool multi_repos = false;

	match_t match = MATCH_EXACT;
	bool force = false;
@@ -111,43 +105,7 @@ exec_install(int argc, char **argv)
	}

	/* first update the remote repositories if needed */
-
	pkg_config_bool(PKG_CONFIG_MULTIREPOS, &multi_repos);
-
	if (!multi_repos) {
-
		/*
-
		 * Single remote database
-
		 */
-
		pkg_config_string(PKG_CONFIG_REPO, &packagesite);
-
		if (packagesite == NULL) {
-
			warnx("PACKAGESITE is not defined.");
-
			return (1);
-
		}
-

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

-
		retcode = pkg_update("repo", url);
-
		if (retcode == EPKG_UPTODATE)
-
			retcode = EPKG_OK;
-
	} else {
-
		/* multiple repositories */
-
		while (pkg_config_list(PKG_CONFIG_REPOS, &repokv) == EPKG_OK) {
-
			repo_name = pkg_config_kv_get(repokv, PKG_CONFIG_KV_KEY);
-
			packagesite = pkg_config_kv_get(repokv, PKG_CONFIG_KV_VALUE);
-

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

-
			retcode = pkg_update(repo_name, url);
-
			if (retcode == EPKG_UPTODATE) {
-
				retcode = EPKG_OK;
-
			}
-
		}
-
	}
-
	if (retcode != EPKG_OK)
+
	if ((retcode = pkgcli_update()) != EPKG_OK)
		return (retcode);

	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
modified pkg/pkgcli.h
@@ -102,6 +102,7 @@ char *sanitize(char *, const char *, size_t);
/* pkg update */
int exec_update(int, char **);
void usage_update(void);
+
int pkgcli_update(void);

/* pkg updating */
int exec_updating(int, char **);
modified pkg/update.c
@@ -40,50 +40,17 @@

#include "pkgcli.h"

-
void
-
usage_update(void)
-
{
-
	fprintf(stderr, "usage: pkg update [-q]\n\n");
-
	fprintf(stderr, "For more information see 'pkg help update'.\n");
-
}
-

+
/**
+
 * Fetch remote databases.
+
 */
int
-
exec_update(int argc, char **argv)
-
{
+
pkgcli_update(void) {
	char url[MAXPATHLEN];
	const char *packagesite = NULL;
	const char *repo_name;
-
	struct pkg_config_kv *repokv = NULL;
-
	int retcode = EPKG_OK;
	bool multi_repos = false;
-
	int ch;
-

-
	while ((ch = getopt(argc, argv, "q")) != -1) {
-
		switch (ch) {
-
			case 'q':
-
				quiet = true;
-
				break;
-
			default:
-
				usage_update();
-
				return (EX_USAGE);
-
		}
-
	}
-
	argc -= optind;
-
	argv += optind;
-

-
	if (argc != 0) {
-
		usage_update();
-
		return (EX_USAGE);
-
	}
-

-
	if (geteuid() != 0) {
-
		warnx("updating the remote database can only be done as root");
-
		return (EX_NOPERM);
-
	}
-

-
	/* 
-
	 * Fetch remote databases.
-
	 */
+
	struct pkg_config_kv *repokv = NULL;
+
	int retcode;

	pkg_config_bool(PKG_CONFIG_MULTIREPOS, &multi_repos);

@@ -133,3 +100,45 @@ exec_update(int argc, char **argv)

	return (retcode);
}
+

+

+
void
+
usage_update(void)
+
{
+
	fprintf(stderr, "usage: pkg update [-q]\n\n");
+
	fprintf(stderr, "For more information see 'pkg help update'.\n");
+
}
+

+
int
+
exec_update(int argc, char **argv)
+
{
+
	int retcode = EPKG_OK;
+
	int ch;
+

+
	while ((ch = getopt(argc, argv, "q")) != -1) {
+
		switch (ch) {
+
			case 'q':
+
				quiet = true;
+
				break;
+
			default:
+
				usage_update();
+
				return (EX_USAGE);
+
		}
+
	}
+
	argc -= optind;
+
	argv += optind;
+

+
	if (argc != 0) {
+
		usage_update();
+
		return (EX_USAGE);
+
	}
+

+
	if (geteuid() != 0) {
+
		warnx("updating the remote database can only be done as root");
+
		return (EX_NOPERM);
+
	}
+

+
	retcode = pkgcli_update();
+

+
	return (retcode);
+
}
modified pkg/upgrade.c
@@ -24,11 +24,8 @@
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

-
#include <sys/param.h>
-

#include <err.h>
#include <stdio.h>
-
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <libutil.h>
@@ -47,20 +44,15 @@ usage_upgrade(void)
int
exec_upgrade(int argc, char **argv)
{
-
	char url[MAXPATHLEN];
	struct pkgdb *db = NULL;
	struct pkgdb_it *it = NULL;
	struct pkg *pkg = NULL;
	struct pkg_jobs *jobs = NULL;
	const char *reponame = NULL;
-
	const char *repo_name = NULL;
-
	const char *packagesite = NULL;
-
	struct pkg_config_kv *repokv = NULL;
	int retcode = 1;
	int ch;
	bool yes = false;
	bool all = false;
-
	bool multi_repos = false;

	if (geteuid() != 0) {
		warnx("upgrading can only be done as root");
@@ -96,43 +88,7 @@ exec_upgrade(int argc, char **argv)
	}

	/* first update the remote repositories if needed */
-
	pkg_config_bool(PKG_CONFIG_MULTIREPOS, &multi_repos);
-
	if (!multi_repos) {
-
		/*
-
		 * Single remote database
-
		 */
-
		pkg_config_string(PKG_CONFIG_REPO, &packagesite);
-
		if (packagesite == NULL) {
-
			warnx("PACKAGESITE is not defined.");
-
			return (1);
-
		}
-

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

-
		retcode = pkg_update("repo", url);
-
		if (retcode == EPKG_UPTODATE)
-
			retcode = EPKG_OK;
-
	} else {
-
		/* multiple repositories */
-
		while (pkg_config_list(PKG_CONFIG_REPOS, &repokv) == EPKG_OK) {
-
			repo_name = pkg_config_kv_get(repokv, PKG_CONFIG_KV_KEY);
-
			packagesite = pkg_config_kv_get(repokv, PKG_CONFIG_KV_VALUE);
-

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

-
			retcode = pkg_update(repo_name, url);
-
			if (retcode == EPKG_UPTODATE) {
-
				retcode = EPKG_OK;
-
			}
-
		}
-
	}
-
	if (retcode != EPKG_OK)
+
	if ((retcode = pkgcli_update()) != EPKG_OK)
		return (retcode);

	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {