Move important information up in -Si output
[pacman-ng.git] / src / util / cleanupdelta.c
blob2f7720b51dc770f27412b635b0681bee4f3e3901
1 /*
2 * cleanupdelta.c : return list of unused delta in a given sync database
4 * Copyright (c) 2011 Pacman Development Team <pacman-dev@archlinux.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include <alpm.h>
25 #include <alpm_list.h>
27 #define BASENAME "cleanupdelta"
29 alpm_handle_t *handle = NULL;
31 static void cleanup(int signum) {
32 if(handle && alpm_release(handle) == -1) {
33 fprintf(stderr, "error releasing alpm\n");
36 exit(signum);
39 static void output_cb(alpm_loglevel_t level, const char *fmt, va_list args)
41 if(strlen(fmt)) {
42 switch(level) {
43 case ALPM_LOG_ERROR: printf("error: "); break;
44 case ALPM_LOG_WARNING: printf("warning: "); break;
45 //case ALPM_LOG_DEBUG: printf("debug: "); break;
46 default: return;
48 vprintf(fmt, args);
53 static void checkpkgs(alpm_list_t *pkglist)
55 alpm_list_t *i, *j;
56 for(i = pkglist; i; i = alpm_list_next(i)) {
57 alpm_pkg_t *pkg = i->data;
58 alpm_list_t *unused = alpm_pkg_unused_deltas(pkg);
59 for(j = unused; j; j = alpm_list_next(j)) {
60 const char *delta = j->data;
61 printf("%s\n", delta);
63 alpm_list_free(unused);
67 static void checkdbs(alpm_list_t *dbnames) {
68 alpm_db_t *db = NULL;
69 alpm_list_t *i;
70 const alpm_siglevel_t level = ALPM_SIG_DATABASE | ALPM_SIG_DATABASE_OPTIONAL;
72 for(i = dbnames; i; i = alpm_list_next(i)) {
73 const char *dbname = i->data;
74 db = alpm_register_syncdb(handle, dbname, level);
75 if(db == NULL) {
76 fprintf(stderr, "error: could not register sync database '%s' (%s)\n",
77 dbname, alpm_strerror(alpm_errno(handle)));
78 continue;
80 checkpkgs(alpm_db_get_pkgcache(db));
85 static void usage(void) {
86 fprintf(stderr, "usage:\n");
87 fprintf(stderr,
88 "\t%s [-b <pacman db>] core extra ... : check the listed sync databases\n", BASENAME);
89 exit(1);
92 int main(int argc, char *argv[])
94 const char *dbpath = DBPATH;
95 alpm_errno_t err;
96 int a = 1;
97 alpm_list_t *dbnames = NULL;
99 while(a < argc) {
100 if(strcmp(argv[a], "-b") == 0) {
101 if(++a < argc) {
102 dbpath = argv[a];
103 } else {
104 usage();
106 } else if(strcmp(argv[a], "-h") == 0 ||
107 strcmp(argv[a], "--help") == 0 ) {
108 usage();
109 } else {
110 dbnames = alpm_list_add(dbnames, argv[a]);
112 a++;
115 if(!dbnames) {
116 usage();
119 handle = alpm_initialize(ROOTDIR, dbpath, &err);
120 if(!handle) {
121 fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(err));
122 return 1;
125 /* let us get log messages from libalpm */
126 alpm_option_set_logcb(handle, output_cb);
128 checkdbs(dbnames);
129 alpm_list_free(dbnames);
131 cleanup(0);
134 /* vim: set ts=2 sw=2 noet: */