Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Merge branch 'master' of github.com:pkgng/pkgng
jlaffaye committed 14 years ago
commit c531a214cf7e5756f4c6b6898ee5732a3a0ebd4f
parent 00a885c
7 files changed +150 -4
modified libpkg/pkg.h
@@ -431,6 +431,7 @@ struct pkgdb_it * pkgdb_query(struct pkgdb *db, const char *pattern,
 */
struct pkgdb_it *pkgdb_query_upgrades(struct pkgdb *db);
struct pkgdb_it *pkgdb_query_downgrades(struct pkgdb *db);
+
struct pkgdb_it *pkgdb_query_autoremove(struct pkgdb *db);

/**
 * @todo Return directly the struct pkg?
modified libpkg/pkgdb.c
@@ -162,7 +162,7 @@ pkgdb_init(sqlite3 *sdb)
		"VALUES (NEW.origin, NEW.name, NEW.version, NEW.comment, NEW.desc, "
		"(SELECT id FROM mtree WHERE content = NEW.mtree), "
		"NEW.message, NEW.arch, NEW.osversion, NEW.maintainer, NEW.www, NEW.prefix, "
-
		"NEW.flatsize NEW.automatic);"
+
		"NEW.flatsize, NEW.automatic);"
	"END;"
	"CREATE TABLE scripts ("
		"package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
@@ -1334,3 +1334,23 @@ pkgdb_query_downgrades(struct pkgdb *db)

	return (pkgdb_it_new(db, stmt, IT_UPGRADE));
}
+

+
struct pkgdb_it *
+
pkgdb_query_autoremove(struct pkgdb *db)
+
{
+
	sqlite3_stmt *stmt;
+

+
	const char sql[] = ""
+
		"SELECT id, origin, name, version, comment, desc, "
+
		"message, arch, osversion, maintainer, www, prefix, "
+
		"flatsize FROM packages WHERE automatic=1 AND "
+
		"(SELECT deps.origin FROM deps where deps.origin = packages.origin) "
+
		"IS NULL";
+

+
	if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) {
+
		ERROR_SQLITE(db->sqlite);
+
		return (NULL);
+
	}
+

+
	return (pkgdb_it_new(db, stmt, IT_LOCAL));
+
}
modified pkg/Makefile
@@ -1,5 +1,6 @@
PROG=		pkg
SRCS=		add.c \
+
		autoremove.c \
		create.c \
		delete.c \
		info.c \
added pkg/autoremove.c
@@ -0,0 +1,115 @@
+
#include <sys/param.h>
+
#include <sys/types.h>
+

+
#include <err.h>
+
#include <stdlib.h>
+
#include <stdio.h>
+
#include <sysexits.h>
+
#include <unistd.h>
+
#include <string.h>
+
#include <fcntl.h>
+
#include <libutil.h>
+

+
#include <pkg.h>
+

+
#include "autoremove.h"
+

+
static int
+
query_yesno(const char *msg)
+
{
+
	int c, r = 0;
+

+
	printf(msg);
+

+
	c = getchar();
+
	if (c == 'y' || c == 'Y')
+
		r = 1;
+
	else if (c == '\n' || c == EOF)
+
		return 0;
+

+
	while((c = getchar()) != '\n' && c != EOF)
+
		continue;
+

+
	return r;
+
}
+

+
void
+
usage_autoremove(void)
+
{
+
	fprintf(stderr, "usage pkg autoremove\n\n");
+
	fprintf(stderr, "For more information see 'pkg help autoremove'.\n");
+
}
+

+
int
+
exec_autoremove(int argc, char **argv)
+
{
+
	struct pkgdb *db = NULL;
+
	struct pkgdb_it *it;
+
	struct pkg *pkg = NULL;
+
	int retcode = 0;
+
	int64_t oldsize = 0, newsize = 0;
+
	char size[7];
+

+
	(void) argv;
+
	if (argc != 1) {
+
		usage_autoremove();
+
		return (-1);
+
	}
+

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

+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
+
		pkg_error_warn("can not open database");
+
		return (1);
+
	}
+

+
	if ((it = pkgdb_query_autoremove(db)) == NULL) {
+
		pkg_error_warn("can not query database");
+
		goto cleanup;
+
	}
+

+
	printf("Packages to be autoremoved: \n");
+
	while ((retcode = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
+
		oldsize += pkg_flatsize(pkg);
+
		newsize += pkg_new_flatsize(pkg);
+
		printf("\t%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
+
	}
+
	printf("\n");
+
	pkgdb_it_free(it);
+

+
	if (oldsize > newsize) {
+
		newsize *= -1;
+
		humanize_number(size, sizeof(size), oldsize - newsize, "B", HN_AUTOSCALE, 0);
+
		printf("the autoremove will save %s\n", size);
+
	} else {
+
		humanize_number(size, sizeof(size), newsize - oldsize, "B", HN_AUTOSCALE, 0);
+
		printf("the autoremove will require %s more space\n", size);
+
	}
+

+
	if (query_yesno("Proceed (y|N): ")) {
+
		it = pkgdb_query_autoremove(db);
+
		while ((retcode = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
+
			if (pkg_delete(pkg, db, 0) != EPKG_OK) {
+
				retcode++;
+
				pkg_error_warn("can not delete %s-%s", pkg_get(pkg, PKG_ORIGIN));
+
			}
+
		}
+

+
	} else {
+
		printf("Aborted");
+
	}
+

+
	if (pkgdb_compact(db) != EPKG_OK)
+
		pkg_error_warn("can not compact database");
+

+
	cleanup:
+
	pkg_free(pkg);
+
	
+
	if (db != NULL)
+
		pkgdb_close(db);
+

+
	return (retcode);
+
}
added pkg/autoremove.h
@@ -0,0 +1,7 @@
+
#ifndef _AUTOREMOVE_H
+
#define _AUTOREMOVE_H
+

+
int exec_autoremove(int, char **);
+
void usage_autoremove(void);
+
#endif
+

modified pkg/main.c
@@ -10,6 +10,7 @@
#include "info.h"
#include "which.h"
#include "add.h"
+
#include "autoremove.h"
#include "version.h"
#include "update.h"
#include "upgrade.h"
@@ -26,6 +27,7 @@ static struct commands {
	void (* const usage)(void);
} cmd[] = {
	{ "add", exec_add, usage_add},
+
	{ "autoremove", exec_autoremove, usage_autoremove},
	{ "create", exec_create, usage_create},
	{ "delete", exec_delete, usage_delete},
	{ "help", exec_help, usage_help},
modified ports/bsd.pkgng.mk
@@ -25,7 +25,7 @@ ACTUAL-PACKAGE-DEPENDS?= \
			${ECHO_CMD} @dep $${pkgname%-*} $${dir\#\#${PORTSDIR}/} $${pkgname\#\#*-}; \
			for pkg in $$(${PKG_INFO} -qd $${dir\#\#${PORTSDIR}/}); do\
				origin=$$(${PKG_INFO} -qo $${pkg%-*}); \
-
				${ECHO_CMD} $${pkg%-*} $$origin $${pkg\#\#*}; \
+
				${ECHO_CMD} @dep $${pkg%-*} $$origin $${pkg\#\#*-}; \
			done; \
		done; \
	fi
@@ -45,7 +45,7 @@ fake-pkg:
.if defined(WWW)
	@${ECHO_CMD} "@www ${WWW}" >> ${MANIFESTF}
.endif
-
	@${MAKE} -C ${.CURDIR} actual-package-depends | ${GREP} -v -E ${PKG_IGNORE_DEPENDS} | ${SORT} -u -t : -k 2 >> ${MANIFESTF}
+
	@${MAKE} -C ${.CURDIR} actual-package-depends | ${GREP} -v -E ${PKG_IGNORE_DEPENDS} | ${SORT} -u >> ${MANIFESTF}
.if !defined(DISABLE_CONFLICTS)
.for conflicts in ${CONFLICTS}
	@${ECHO_CMD} "@conflict ${conflicts}" >> ${MANIFESTF}
@@ -89,7 +89,7 @@ fake-pkg:
	@${CP} ${MTREE_FILE} ${METADIR}/+MTREE_DIRS
.endif
.if defined(INSTALLS_DEPENDS)
-
	@${PKG_CMD} -a -l -m ${METADIR} -f ${TMPPLIST}
+
	@${PKG_CMD} -d -l -m ${METADIR} -f ${TMPPLIST}
.else
	@${PKG_CMD} -l -m ${METADIR} -f ${TMPPLIST}
.endif