Skip to content

Commit

Permalink
New autoremove command (currently only list files to be autoremoved
Browse files Browse the repository at this point in the history
  • Loading branch information
bapt committed May 24, 2011
1 parent 3a9aff3 commit 4b36a03
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions libpkg/pkg.h
Expand Up @@ -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?
Expand Down
20 changes: 20 additions & 0 deletions libpkg/pkgdb.c
Expand Up @@ -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));
}
1 change: 1 addition & 0 deletions pkg/Makefile
@@ -1,5 +1,6 @@
PROG= pkg
SRCS= add.c \
autoremove.c \
create.c \
delete.c \
info.c \
Expand Down
79 changes: 79 additions & 0 deletions 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);
}
7 changes: 7 additions & 0 deletions pkg/autoremove.h
@@ -0,0 +1,7 @@
#ifndef _AUTOREMOVE_H
#define _AUTOREMOVE_H

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

2 changes: 2 additions & 0 deletions pkg/main.c
Expand Up @@ -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"
Expand All @@ -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},
Expand Down

0 comments on commit 4b36a03

Please sign in to comment.