libalpm
Arch Linux Package Manager Library
cleanupdelta.c
Go to the documentation of this file.
00001 /*
00002  *  cleanupdelta.c : return list of unused delta in a given sync database
00003  *
00004  *  Copyright (c) 2011 Pacman Development Team <pacman-dev@archlinux.org>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 
00024 #include <alpm.h>
00025 #include <alpm_list.h>
00026 
00027 #define BASENAME "cleanupdelta"
00028 
00029 alpm_handle_t *handle = NULL;
00030 
00031 static void cleanup(int signum) {
00032     if(handle && alpm_release(handle) == -1) {
00033         fprintf(stderr, "error releasing alpm\n");
00034     }
00035 
00036     exit(signum);
00037 }
00038 
00039 static void output_cb(alpm_loglevel_t level, const char *fmt, va_list args)
00040 {
00041     if(strlen(fmt)) {
00042         switch(level) {
00043             case ALPM_LOG_ERROR: printf("error: "); break;
00044             case ALPM_LOG_WARNING: printf("warning: "); break;
00045             //case ALPM_LOG_DEBUG: printf("debug: "); break;
00046             default: return;
00047         }
00048         vprintf(fmt, args);
00049     }
00050 }
00051 
00052 
00053 static void checkpkgs(alpm_list_t *pkglist)
00054 {
00055     alpm_list_t *i, *j;
00056     for(i = pkglist; i; i = alpm_list_next(i)) {
00057         alpm_pkg_t *pkg = i->data;
00058         alpm_list_t *unused = alpm_pkg_unused_deltas(pkg);
00059         for(j = unused; j; j = alpm_list_next(j)) {
00060             const char *delta = j->data;
00061             printf("%s\n", delta);
00062         }
00063         alpm_list_free(unused);
00064     }
00065 }
00066 
00067 static void checkdbs(alpm_list_t *dbnames) {
00068     alpm_db_t *db = NULL;
00069     alpm_list_t *i;
00070     const alpm_siglevel_t level = ALPM_SIG_DATABASE | ALPM_SIG_DATABASE_OPTIONAL;
00071 
00072     for(i = dbnames; i; i = alpm_list_next(i)) {
00073         const char *dbname = i->data;
00074         db = alpm_db_register_sync(handle, dbname, level);
00075         if(db == NULL) {
00076             fprintf(stderr, "error: could not register sync database '%s' (%s)\n",
00077                     dbname, alpm_strerror(alpm_errno(handle)));
00078             continue;
00079         }
00080         checkpkgs(alpm_db_get_pkgcache(db));
00081     }
00082 
00083 }
00084 
00085 static void usage(void) {
00086     fprintf(stderr, "usage:\n");
00087     fprintf(stderr,
00088             "\t%s [-b <pacman db>] core extra ... : check the listed sync databases\n", BASENAME);
00089     exit(1);
00090 }
00091 
00092 int main(int argc, char *argv[])
00093 {
00094     const char *dbpath = DBPATH;
00095     alpm_errno_t err;
00096     int a = 1;
00097     alpm_list_t *dbnames = NULL;
00098 
00099     while(a < argc) {
00100         if(strcmp(argv[a], "-b") == 0) {
00101             if(++a < argc) {
00102                 dbpath = argv[a];
00103             } else {
00104                 usage();
00105             }
00106         }   else if(strcmp(argv[a], "-h") == 0 ||
00107                 strcmp(argv[a], "--help") == 0 ) {
00108             usage();
00109         } else {
00110             dbnames = alpm_list_add(dbnames, argv[a]);
00111         }
00112         a++;
00113     }
00114 
00115     if(!dbnames) {
00116         usage();
00117     }
00118 
00119     handle = alpm_initialize(ROOTDIR, dbpath, &err);
00120     if(!handle) {
00121         fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(err));
00122         return 1;
00123     }
00124 
00125     /* let us get log messages from libalpm */
00126     alpm_option_set_logcb(handle, output_cb);
00127 
00128     checkdbs(dbnames);
00129     alpm_list_free(dbnames);
00130 
00131     cleanup(0);
00132 }
00133 
00134 /* vim: set ts=2 sw=2 noet: */