Move important information up in -Si output
[pacman-ng.git] / src / util / testpkg.c
blob96400a754f58913df64a041e6db3df9f5901044b
1 /*
2 * testpkg.c : Test a pacman package for validity
4 * Copyright (c) 2007 by Aaron Griffin <aaronmgriffin@gmail.com>
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> /* printf */
21 #include <stdarg.h> /* va_list */
23 #include <alpm.h>
25 #define BASENAME "testpkg"
27 static void output_cb(alpm_loglevel_t level, const char *fmt, va_list args)
29 if(fmt[0] == '\0') {
30 return;
32 switch(level) {
33 case ALPM_LOG_ERROR: printf("error: "); break;
34 case ALPM_LOG_WARNING: printf("warning: "); break;
35 default: return; /* skip other messages */
37 vprintf(fmt, args);
40 int main(int argc, char *argv[])
42 int retval = 1; /* default = false */
43 alpm_handle_t *handle;
44 alpm_errno_t err;
45 alpm_pkg_t *pkg = NULL;
46 const alpm_siglevel_t level = ALPM_SIG_PACKAGE | ALPM_SIG_PACKAGE_OPTIONAL;
48 if(argc != 2) {
49 fprintf(stderr, "usage: %s <package file>\n", BASENAME);
50 return 1;
53 handle = alpm_initialize(ROOTDIR, DBPATH, &err);
54 if(!handle) {
55 fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(err));
56 return 1;
59 /* let us get log messages from libalpm */
60 alpm_option_set_logcb(handle, output_cb);
62 /* set gpgdir to default */
63 alpm_option_set_gpgdir(handle, GPGDIR);
65 if(alpm_pkg_load(handle, argv[1], 1, level, &pkg) == -1
66 || pkg == NULL) {
67 err = alpm_errno(handle);
68 switch(err) {
69 case ALPM_ERR_PKG_NOT_FOUND:
70 printf("Cannot find the given file.\n");
71 break;
72 case ALPM_ERR_PKG_OPEN:
73 printf("Cannot open the given file.\n");
74 break;
75 case ALPM_ERR_LIBARCHIVE:
76 case ALPM_ERR_PKG_INVALID:
77 printf("Package is invalid.\n");
78 break;
79 default:
80 printf("libalpm error: %s\n", alpm_strerror(err));
81 break;
83 retval = 1;
84 } else {
85 alpm_pkg_free(pkg);
86 printf("Package is valid.\n");
87 retval = 0;
90 if(alpm_release(handle) == -1) {
91 fprintf(stderr, "error releasing alpm\n");
94 return retval;