Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Split add into add and install commands.
jlaffaye committed 14 years ago
commit 0f9e6eef80766ee5649fdd5aad81008cc740bc05
parent 929b50a
8 files changed +133 -137
modified libpkg/pkg.h
@@ -498,10 +498,9 @@ int pkgdb_compact(struct pkgdb *db);
 * Install and register a new package.
 * @param db An opened pkgdb
 * @param path The path to the package archive file on the local disk
-
 * @param pkg A pointer to pkg pointer (allocates a new pkg).
 * @return An error code.
 */
-
int pkg_add(struct pkgdb *db, const char *path, struct pkg **pkg);
+
int pkg_add(struct pkgdb *db, const char *path);

/**
 * Allocate a new pkg_jobs.
@@ -532,7 +531,7 @@ int pkg_jobs(struct pkg_jobs *jobs, struct pkg **pkg);
 * Apply the jobs in the queue (fetch and install).
 * @return An error code.
 */
-
int pkg_jobs_apply(struct pkg_jobs *jobs, status_cb scb);
+
int pkg_jobs_apply(struct pkg_jobs *jobs);

/**
 * Archive formats options.
modified libpkg/pkg_add.c
@@ -78,7 +78,7 @@ do_extract(struct archive *a, struct archive_entry *ae)
}

int
-
pkg_add(struct pkgdb *db, const char *path, struct pkg **pkg_p)
+
pkg_add(struct pkgdb *db, const char *path)
{
	struct archive *a;
	struct archive_entry *ae;
@@ -147,7 +147,7 @@ pkg_add(struct pkgdb *db, const char *path, struct pkg **pkg_p)
					 ext);

			if (access(dpath, F_OK) == 0) {
-
				if (pkg_add(db, dpath, NULL) != EPKG_OK) {
+
				if (pkg_add(db, dpath) != EPKG_OK) {
					retcode = EPKG_FATAL;
					goto cleanup;
				}
@@ -197,10 +197,7 @@ pkg_add(struct pkgdb *db, const char *path, struct pkg **pkg_p)
	if (p != NULL)
		pkg_free(p);

-
	if (pkg_p != NULL)
-
		*pkg_p = (retcode == EPKG_OK) ? pkg : NULL;
-
	else
-
		pkg_free(pkg);
+
	pkg_free(pkg);

	return (retcode);
}
modified libpkg/pkg_jobs.c
@@ -73,10 +73,9 @@ pkg_jobs(struct pkg_jobs *j, struct pkg **pkg)
}

int
-
pkg_jobs_apply(struct pkg_jobs *j, status_cb scb)
+
pkg_jobs_apply(struct pkg_jobs *j)
{
	struct pkg *p = NULL;
-
	struct pkg *pfile = NULL;
	const char *cachedir;
	char path[MAXPATHLEN];

@@ -93,17 +92,11 @@ pkg_jobs_apply(struct pkg_jobs *j, status_cb scb)
		snprintf(path, sizeof(path), "%s/%s", cachedir,
				 pkg_get(p, PKG_REPOPATH));

-
		if (scb != NULL)
-
			scb(NULL, p);
-
		if (pkg_add(j->db, path, &pfile) != EPKG_OK) {
-
			pkg_free(pfile);
+
		if (pkg_add(j->db, path) != EPKG_OK) {
			return (EPKG_FATAL);
		}
-
		if (scb != NULL)
-
			scb(NULL, pfile);
	}

-
	pkg_free(pfile);
	return (EPKG_OK);
}

modified pkg/Makefile
@@ -5,6 +5,7 @@ SRCS= add.c \
		delete.c \
		event.c \
		info.c \
+
		install.c \
		main.c \
		register.c \
		repo.c \
modified pkg/add.c
@@ -1,3 +1,4 @@
+
#include <sys/param.h>
#include <sys/types.h>

#include <err.h>
@@ -23,99 +24,6 @@ is_url(const char *pattern)
	return (EPKG_FATAL);
}

-
static void
-
install_status(__unused void *data, struct pkg *pkg)
-
{
-
	const char *message;
-
	pkg_t type = pkg_type(pkg);
-

-
	if (type == PKG_REMOTE)
-
		printf("Installing %s-%s...", pkg_get(pkg, PKG_NAME),
-
			   pkg_get(pkg, PKG_VERSION));
-
	if (type == PKG_FILE) {
-
		printf(" Done!\n");
-
		message = pkg_get(pkg, PKG_MESSAGE);
-
		if (message != NULL)
-
			printf("----\n%s----\n", message);
-
	}
-
	fflush(stdout);
-
}
-

-
static int
-
add_from_repo(const char *name)
-
{
-
	struct pkg *pkg = NULL;
-
	struct pkgdb *db = NULL;
-
	struct pkg_jobs *jobs = NULL;
-
	int retcode = EPKG_OK;
-

-
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
-
		pkg_error_warn("can not open database");
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	if (pkg_jobs_new(&jobs, db) != EPKG_OK) {
-
		pkg_error_warn("pkg_jobs_new()");
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	if ((pkg = pkgdb_query_remote(db, name)) == NULL) {
-
		retcode = pkg_error_number();
-
		pkg_error_warn("can query the database");
-
		goto cleanup;
-
	}
-

-
	pkg_jobs_add(jobs, pkg);
-

-
	/* print a summary before applying the jobs */
-
	pkg = NULL;
-
	printf("The following packages will be installed:\n");
-
	while (pkg_jobs(jobs, &pkg) == EPKG_OK) {
-
		printf("%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
-
	}
-

-
	if (pkg_jobs_apply(jobs, install_status) != EPKG_OK)
-
		pkg_error_warn("can not install");
-

-
	cleanup:
-
	pkgdb_close(db);
-
	pkg_jobs_free(jobs);
-

-
	return (retcode);
-
}
-

-
static int
-
add_from_file(const char *file)
-
{
-
	struct pkgdb *db = NULL;
-
	struct pkg *pkg = NULL;
-
	const char *message;
-
	int retcode = EPKG_OK;
-

-
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
-
		pkg_error_warn("can not open database");
-
		retcode = EPKG_FATAL;
-
	}
-

-
	if (pkg_add(db, file, &pkg) != EPKG_OK) {
-
		pkg_error_warn("can not install %s", file);
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	message = pkg_get(pkg, PKG_MESSAGE);
-
	if (message != NULL && message[0] != '\0')
-
		printf("%s", message);
-

-
	cleanup:
-
	pkgdb_close(db);
-
	pkg_free(pkg);
-

-
	return (retcode);
-
}
-

void
usage_add(void)
{
@@ -127,10 +35,13 @@ usage_add(void)
int
exec_add(int argc, char **argv)
{
-
	char *name;
-
	int retcode = 0;
+
	struct pkgdb *db = NULL;
+
	char path[MAXPATHLEN];
+
	char *file;
+
	int retcode = EPKG_OK;
+
	int i;

-
	if (argc != 2) {
+
	if (argc < 2) {
		usage_add();
		return (-1);
	}
@@ -140,26 +51,32 @@ exec_add(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	if (is_url(argv[1]) == EPKG_OK) {
-
		asprintf(&name, "./%s", basename(argv[1]));
-
		if (pkg_fetch_file(argv[1], name) != EPKG_OK) {
-
			pkg_error_warn("can not fetch %s", argv[1]);
-
			return (1);
-
		}
-
	} else
-
		name = argv[1];
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
+
		pkg_error_warn("can not open database");
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
+
	}

-
	/* if it is a file  */
-
	if (name[0] == '/' || name[0] == '.') {
-
		if (access(name, F_OK) == 0) {
-
			retcode = add_from_file(name);
-
		} else {
-
			warn("%s", name);
+
	for (i = 1; i < argc; i++) {
+
		if (is_url(argv[i]) == EPKG_OK) {
+
			snprintf(path, sizeof(path), "./%s", basename(argv[i]));
+
			if ((retcode = pkg_fetch_file(argv[i], path)) != EPKG_OK) {
+
				pkg_error_warn("can not fetch %s", argv[i]);
+
				continue;
+
			}
+
			file = path;
+
		} else
+
			file = argv[i];
+

+
		if (pkg_add(db, file) != EPKG_OK) {
+
			pkg_error_warn("can not install %s", file);
+
			continue;
		}
-
	} else {
-
		retcode = add_from_repo(name);
	}

+
	cleanup:
+
	pkgdb_close(db);
+

	return (retcode == EPKG_OK ? 0 : 1);
}

added pkg/install.c
@@ -0,0 +1,79 @@
+
#include <sys/types.h>
+

+
#include <err.h>
+
#include <libgen.h>
+
#include <stdbool.h>
+
#include <stdio.h>
+
#include <string.h>
+
#include <sysexits.h>
+
#include <unistd.h>
+

+
#include <pkg.h>
+

+
#include "install.h"
+

+
void
+
usage_install(void)
+
{
+
	fprintf(stderr, "usage: pkg install <pkg-name>\n");
+
	fprintf(stderr, "For more information see 'pkg help install'.\n");
+
}
+

+
int
+
exec_install(int argc, char **argv)
+
{
+
	struct pkg *pkg = NULL;
+
	struct pkgdb *db = NULL;
+
	struct pkg_jobs *jobs = NULL;
+
	int retcode = EPKG_OK;
+
	int i;
+

+
	if (argc < 2) {
+
		usage_install();
+
		return (-1);
+
	}
+

+
	if (geteuid() != 0) {
+
		warnx("installing packages can only be done as root");
+
		return (EX_NOPERM);
+
	}
+

+
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
+
		pkg_error_warn("can not open database");
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

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

+
	for (i = 1; i < argc; i++) {
+
		if ((pkg = pkgdb_query_remote(db, argv[i])) == NULL) {
+
			retcode = pkg_error_number();
+
			pkg_error_warn("can query the database");
+
			goto cleanup;
+
		}
+

+
		pkg_jobs_add(jobs, pkg);
+
	}
+

+
	/* print a summary before applying the jobs */
+
	pkg = NULL;
+
	printf("The following packages will be installed:\n");
+
	while (pkg_jobs(jobs, &pkg) == EPKG_OK) {
+
		printf("%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
+
	}
+

+
	if (pkg_jobs_apply(jobs) != EPKG_OK)
+
		pkg_error_warn("can not install");
+

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

+
	return (retcode == EPKG_OK ? 0 : 1);
+
}
+

added pkg/install.h
@@ -0,0 +1,7 @@
+
#ifndef _INSTALL_H
+
#define _INSTALL_H
+

+
int exec_install(int, char **);
+
void usage_install(void);
+
#endif
+

modified pkg/main.c
@@ -6,20 +6,22 @@
#include <sysexits.h>
#include <unistd.h>

-
#include "pkg.h"
+
#include <pkg.h>
+

+
#include "add.h"
+
#include "autoremove.h"
#include "create.h"
#include "delete.h"
#include "event.h"
#include "info.h"
-
#include "which.h"
-
#include "add.h"
-
#include "autoremove.h"
-
#include "version.h"
+
#include "install.h"
+
#include "register.h"
+
#include "repo.h"
#include "search.h"
#include "update.h"
#include "upgrade.h"
-
#include "register.h"
-
#include "repo.h"
+
#include "version.h"
+
#include "which.h"

static void usage(void);
static void usage_help(void);
@@ -36,6 +38,7 @@ static struct commands {
	{ "delete", exec_delete, usage_delete},
	{ "help", exec_help, usage_help},
	{ "info", exec_info, usage_info},
+
	{ "install", exec_install, usage_install},
	{ "search", exec_search, usage_search},
	{ "register", exec_register, usage_register},
	{ "repo", exec_repo, usage_repo},