Rename pmpkg_t to alpm_pkg_t
[pacman-ng.git] / src / util / cleanupdelta.c
blobdcd849bb76b260d1cc02649f2e57ed392b2899a6
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 "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <limits.h> /* PATH_MAX */
27 #include <alpm.h>
28 #include <alpm_list.h>
30 #define BASENAME "cleanupdelta"
32 alpm_handle_t *handle = NULL;
34 static void cleanup(int signum) {
35 if(handle && alpm_release(handle) == -1) {
36 fprintf(stderr, "error releasing alpm\n");
39 exit(signum);
42 static void output_cb(pmloglevel_t level, const char *fmt, va_list args)
44 if(strlen(fmt)) {
45 switch(level) {
46 case PM_LOG_ERROR: printf("error: "); break;
47 case PM_LOG_WARNING: printf("warning: "); break;
48 //case PM_LOG_DEBUG: printf("debug: "); break;
49 default: return;
51 vprintf(fmt, args);
56 static void checkpkgs(alpm_list_t *pkglist)
58 alpm_list_t *i, *j;
59 for(i = pkglist; i; i = alpm_list_next(i)) {
60 alpm_pkg_t *pkg = alpm_list_getdata(i);
61 alpm_list_t *unused = alpm_pkg_unused_deltas(pkg);
62 for(j = unused; j; j = alpm_list_next(j)) {
63 char *delta = alpm_list_getdata(j);
64 printf("%s\n", delta);
66 alpm_list_free(unused);
70 static void checkdbs(const char *dbpath, alpm_list_t *dbnames) {
71 char syncdbpath[PATH_MAX];
72 alpm_db_t *db = NULL;
73 alpm_list_t *i;
75 for(i = dbnames; i; i = alpm_list_next(i)) {
76 char *dbname = alpm_list_getdata(i);
77 snprintf(syncdbpath, PATH_MAX, "%s/sync/%s", dbpath, dbname);
78 db = alpm_db_register_sync(handle, dbname, PM_PGP_VERIFY_OPTIONAL);
79 if(db == NULL) {
80 fprintf(stderr, "error: could not register sync database (%s)\n",
81 alpm_strerror(alpm_errno(handle)));
82 return;
84 checkpkgs(alpm_db_get_pkgcache(db));
89 static void usage(void) {
90 fprintf(stderr, "usage:\n");
91 fprintf(stderr,
92 "\t%s [-b <pacman db>] core extra ... : check the listed sync databases\n", BASENAME);
93 exit(1);
96 int main(int argc, char *argv[])
98 const char *dbpath = DBPATH;
99 enum _pmerrno_t err;
100 int a = 1;
101 alpm_list_t *dbnames = NULL;
103 while(a < argc) {
104 if(strcmp(argv[a], "-b") == 0) {
105 if(++a < argc) {
106 dbpath = argv[a];
107 } else {
108 usage();
110 } else if(strcmp(argv[a], "-h") == 0 ||
111 strcmp(argv[a], "--help") == 0 ) {
112 usage();
113 } else {
114 dbnames = alpm_list_add(dbnames, argv[a]);
116 a++;
119 if(!dbnames) {
120 usage();
123 handle = alpm_initialize(ROOTDIR, dbpath, &err);
124 if(!handle) {
125 fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(err));
126 return 1;
129 /* let us get log messages from libalpm */
130 alpm_option_set_logcb(handle, output_cb);
132 checkdbs(dbpath,dbnames);
133 alpm_list_free(dbnames);
135 cleanup(0);
138 /* vim: set ts=2 sw=2 noet: */