Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Merge remote branch 'pkgng/master'
Will Andrews committed 14 years ago
commit d2afb0fe11f6b349d8ce81762d6c172f574f778b
parent 37345a2
7 files changed +112 -2
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
@@ -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,79 @@
+
#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"
+

+
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);
+
	}
+

+
	cleanup:
+
	
+
	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
@@ -12,6 +12,7 @@
#include "info.h"
#include "which.h"
#include "add.h"
+
#include "autoremove.h"
#include "version.h"
#include "update.h"
#include "upgrade.h"
@@ -28,6 +29,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}