Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New autoremove command (currently only list files to be autoremoved
- Loading branch information
Showing
6 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
PROG= pkg | ||
SRCS= add.c \ | ||
autoremove.c \ | ||
create.c \ | ||
delete.c \ | ||
info.c \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#ifndef _AUTOREMOVE_H | ||
#define _AUTOREMOVE_H | ||
|
||
int exec_autoremove(int, char **); | ||
void usage_autoremove(void); | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters