libalpm
Arch Linux Package Manager Library
testpkg.c
Go to the documentation of this file.
00001 /*
00002  *  testpkg.c : Test a pacman package for validity
00003  *
00004  *  Copyright (c) 2007 by Aaron Griffin <aaronmgriffin@gmail.com>
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> /* printf */
00021 #include <stdarg.h> /* va_list */
00022 
00023 #include <alpm.h>
00024 
00025 #define BASENAME "testpkg"
00026 
00027 static void output_cb(alpm_loglevel_t level, const char *fmt, va_list args)
00028 {
00029     if(fmt[0] == '\0') {
00030         return;
00031     }
00032     switch(level) {
00033         case ALPM_LOG_ERROR: printf("error: "); break;
00034         case ALPM_LOG_WARNING: printf("warning: "); break;
00035         default: return; /* skip other messages */
00036     }
00037     vprintf(fmt, args);
00038 }
00039 
00040 int main(int argc, char *argv[])
00041 {
00042     int retval = 1; /* default = false */
00043     alpm_handle_t *handle;
00044     alpm_errno_t err;
00045     alpm_pkg_t *pkg = NULL;
00046     const alpm_siglevel_t level = ALPM_SIG_PACKAGE | ALPM_SIG_PACKAGE_OPTIONAL;
00047 
00048     if(argc != 2) {
00049         fprintf(stderr, "usage: %s <package file>\n", BASENAME);
00050         return 1;
00051     }
00052 
00053     handle = alpm_initialize(ROOTDIR, DBPATH, &err);
00054     if(!handle) {
00055         fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(err));
00056         return 1;
00057     }
00058 
00059     /* let us get log messages from libalpm */
00060     alpm_option_set_logcb(handle, output_cb);
00061 
00062     if(alpm_pkg_load(handle, argv[1], 1, level, &pkg) == -1
00063             || pkg == NULL) {
00064         err = alpm_errno(handle);
00065         switch(err) {
00066             case ALPM_ERR_PKG_NOT_FOUND:
00067                 printf("Cannot find the given file.\n");
00068                 break;
00069             case ALPM_ERR_PKG_OPEN:
00070                 printf("Cannot open the given file.\n");
00071                 break;
00072             case ALPM_ERR_LIBARCHIVE:
00073             case ALPM_ERR_PKG_INVALID:
00074                 printf("Package is invalid.\n");
00075                 break;
00076             default:
00077                 printf("libalpm error: %s\n", alpm_strerror(err));
00078                 break;
00079         }
00080         retval = 1;
00081     } else {
00082         alpm_pkg_free(pkg);
00083         printf("Package is valid.\n");
00084         retval = 0;
00085     }
00086 
00087     if(alpm_release(handle) == -1) {
00088         fprintf(stderr, "error releasing alpm\n");
00089     }
00090 
00091     return retval;
00092 }