Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Bring back pkg add
Baptiste Daroussin committed 12 years ago
commit 4c7603b4f476608706da04d943613a55b6dc21c6
parent 28992e2
5 files changed +260 -11
modified docs/pkg-add.8
@@ -15,22 +15,83 @@
.\"     @(#)pkg.8
.\" $FreeBSD$
.\"
-
.Dd March 20, 2014
+
.Dd September 22, 2013
.Dt PKG-ADD 8
.Os
.Sh NAME
.Nm "pkg add"
-
.Nd alias on
-
.Xr pkg-install 8
+
.Nd Registers a package and installs it on the system
.Sh SYNOPSIS
-
See
-
.Xr pkg-install 8
+
.Nm
+
.Op Fl IAfq
+
.Ar pkg-name ...
+
.Nm
+
.Op Fl IAfq
+
.Ar <protocol>://<path>/<pkg-name> ...
.Sh DESCRIPTION
.Nm
-
is an alias on:
-
.Bd -literal -offset indent
-
pkg install -ylq
-
.Ed
+
installs packages from either a local source or a remote one.
+
.Pp
+
When installing from a remote source you need to specify
+
the protocol to use when fetching the package.
+
.Pp
+
Currently supported protocols are FTP, HTTP and HTTPS.
+
.Pp
+
Otherwise,
+
.Nm
+
will read the file named on the command line.
+
.Pp
+
If this is a regular file, and the package to be installed has
+
unmet dependencies,
+
.Nm
+
will search the directory containing
+
.Ar pkg-name
+
for suitable pkg archive files to fulfill those dependencies.
+
If
+
.Ar pkg-name
+
is literally
+
.Pa -
+
then it will read the package data from stdin.
+
.Nm
+
will automatically detect and unpack most common compression formats
+
based on the content of the data stream it reads, ignoring any
+
extension the filename may have.
+
.Pp
+
If this involves reading from a pipe (including
+
.Pa stdin ) ,
+
fifo, socket or some other non-regular form of input stream then
+
.Nm
+
will immediately emit an error if
+
.Ar pkg-name
+
has unmet dependencies.
+
.Sh OPTIONS
+
The following options are supported by
+
.Nm :
+
.Bl -tag -width I1
+
.It Fl I
+
If any installation scripts (pre-install or post-install) exist for given
+
packages, do not execute them.
+
.It Fl A
+
Mark the installed packages as orphan.
+
Will be automatically removed if no other packages depend on them.
+
For more information please refer to
+
.Xr pkg-autoremove 8
+
.It Fl f
+
Force the reinstallation of the package if already installed.
+
.It Fl q
+
Force quiet output.
+
.El
+
.Sh ENVIRONMENT
+
The following environment variables affect the execution of
+
.Nm .
+
See
+
.Xr pkg.conf 5
+
for further description.
+
.Bl -tag -width ".Ev NO_DESCRIPTIONS"
+
.It Ev ASSUME_ALWAYS_YES
+
.It Ev HANDLE_RC_SCRIPTS
+
.It Ev PKG_DBDIR
+
.El
.Sh FILES
See
.Xr pkg.conf 5 .
modified src/Makefile.am
@@ -1,6 +1,7 @@
noinst_HEADERS=	pkgcli.h progressmeter.h

-
pkg_SOURCES=		annotate.c \
+
pkg_SOURCES=		add.c \
+
			annotate.c \
			audit.c \
			autoremove.c \
			backup.c \
added src/add.c
@@ -0,0 +1,183 @@
+
/*-
+
 * Copyright (c) 2011-2012 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
+
 * All rights reserved.
+
 * 
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer
+
 *    in this position and unchanged.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 * 
+
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
 */
+

+
#include <sys/param.h>
+

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

+
#include <pkg.h>
+

+
#include "pkgcli.h"
+

+
static int
+
is_url(const char * const pattern)
+
{
+
	if (strncmp(pattern, "http://", 7) == 0 ||
+
		strncmp(pattern, "https://", 8) == 0 ||
+
		strncmp(pattern, "ftp://", 6) == 0 ||
+
		strncmp(pattern, "file://", 7) == 0)
+
		return (EPKG_OK);
+

+
	return (EPKG_FATAL);
+
}
+

+
void
+
usage_add(void)
+
{
+
	fprintf(stderr, "Usage: pkg add [-IAfq] <pkg-name> ...\n");
+
	fprintf(stderr, "       pkg add [-IAfq] <protocol>://<path>/<pkg-name> ...\n\n");
+
	fprintf(stderr, "For more information see 'pkg help add'.\n");
+
}
+

+
int
+
exec_add(int argc, char **argv)
+
{
+
	struct pkgdb *db = NULL;
+
	struct sbuf *failedpkgs = NULL;
+
	char path[MAXPATHLEN];
+
	char *file;
+
	int retcode;
+
	int ch;
+
	int i;
+
	int failedpkgcount = 0;
+
	pkg_flags f = PKG_FLAG_NONE;
+
	struct pkg_manifest_key *keys = NULL;
+

+
	while ((ch = getopt(argc, argv, "IAfq")) != -1) {
+
		switch (ch) {
+
		case 'I':
+
			f |= PKG_ADD_NOSCRIPT;
+
			break;
+
		case 'A':
+
			f |= PKG_ADD_AUTOMATIC;
+
			break;
+
		case 'f':
+
			f |= PKG_FLAG_FORCE;
+
			break;
+
		case 'q':
+
			quiet = true;
+
			break;
+
		default:
+
			usage_add();
+
			return (EX_USAGE);
+
		}
+
	}
+
	argc -= optind;
+
	argv += optind;
+

+
	if (argc < 1) {
+
		usage_add();
+
		return (EX_USAGE);
+
	}
+

+
	retcode = pkgdb_access(PKGDB_MODE_READ  |
+
			       PKGDB_MODE_WRITE |
+
			       PKGDB_MODE_CREATE,
+
			       PKGDB_DB_LOCAL);
+
	if (retcode == EPKG_ENOACCESS) {
+
		warnx("Insufficient privileges to add packages");
+
		return (EX_NOPERM);
+
	} else if (retcode != EPKG_OK)
+
		return (EX_IOERR);
+

+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK)
+
		return (EX_IOERR);
+

+
	if (pkgdb_obtain_lock(db, PKGDB_LOCK_EXCLUSIVE, 0, 0) != EPKG_OK) {
+
		pkgdb_close(db);
+
		warnx("Cannot get an exclusive lock on a database, it is locked by another process");
+
		return (EX_TEMPFAIL);
+
	}
+

+
	failedpkgs = sbuf_new_auto();
+
	pkg_manifest_keys_new(&keys);
+
	for (i = 0; i < argc; i++) {
+
		if (is_url(argv[i]) == EPKG_OK) {
+
			snprintf(path, sizeof(path), "%s/%s.XXXXX",
+
			    getenv("TMPDIR") != NULL ? getenv("TMPDIR") : "/tmp", basename(argv[i]));
+
			if ((retcode = pkg_fetch_file(NULL, argv[i], path, 0)) != EPKG_OK)
+
				break;
+

+
			file = path;
+
		} else {
+
			file = argv[i];
+

+
			/* Special case: treat a filename of "-" as
+
			   meaning 'read from stdin.'  It doesn't make
+
			   sense to have a filename of "-" more than
+
			   once per command line, but we aren't
+
			   testing for that at the moment */
+

+
			if (strcmp(file, "-") != 0 && access(file, F_OK) != 0) {
+
				warn("%s", file);
+
				if (errno == ENOENT)
+
					warnx("Was 'pkg install %s' meant?", file);
+
				sbuf_cat(failedpkgs, argv[i]);
+
				if (i != argc - 1)
+
					sbuf_printf(failedpkgs, ", ");
+
				failedpkgcount++;
+
				continue;
+
			}
+

+
		}
+

+
		if ((retcode = pkg_add(db, file, f, keys)) != EPKG_OK) {
+
			sbuf_cat(failedpkgs, argv[i]);
+
			if (i != argc - 1)
+
				sbuf_printf(failedpkgs, ", ");
+
			failedpkgcount++;
+
		}
+

+
		if (is_url(argv[i]) == EPKG_OK)
+
			unlink(file);
+

+
	}
+
	pkg_manifest_keys_free(keys);
+
	pkgdb_release_lock(db, PKGDB_LOCK_EXCLUSIVE);
+
	pkgdb_close(db);
+
	
+
	if(failedpkgcount > 0) {
+
		sbuf_finish(failedpkgs);
+
		printf("\nFailed to install the following %d package(s): %s\n", failedpkgcount, sbuf_data(failedpkgs));
+
		retcode = EPKG_FATAL;
+
	}
+
	sbuf_delete(failedpkgs);
+

+
	if (messages != NULL) {
+
		sbuf_finish(messages);
+
		printf("%s", sbuf_data(messages));
+
	}
+

+
	return (retcode == EPKG_OK ? EX_OK : EX_SOFTWARE);
+
}
+

modified src/main.c
@@ -71,7 +71,7 @@ static struct commands {
	int (*exec)(int argc, char **argv);
	void (* const usage)(void);
} cmd[] = {
-
	{ "add", "Compatibility alias for install", exec_install, usage_install},
+
	{ "add", "Compatibility interface to install a package", exec_add, usage_add},
	{ "annotate", "Add, modify or delete tag-value style annotations on packages", exec_annotate, usage_annotate},
	{ "audit", "Reports vulnerable packages", exec_audit, usage_audit},
	{ "autoremove", "Removes orphan packages", exec_autoremove, usage_autoremove},
modified src/pkgcli.h
@@ -35,6 +35,10 @@ extern int nbdone;
extern bool newpkgversion;
int nbdone;

+
/* pkg add */
+
int exec_add(int, char **);
+
void usage_add(void);
+

/* pkg annotate */
int exec_annotate(int, char **);
void usage_annotate(void);