Prefix alpm_loglevel_t members with ALPM
[pacman-ng.git] / src / util / testpkg.c
blobcfde486a7e5ab91afa7d85079cdcb1610171783d
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 enum _alpm_errno_t err;
45 alpm_pkg_t *pkg = NULL;
47 if(argc != 2) {
48 fprintf(stderr, "usage: %s <package file>\n", BASENAME);
49 return 1;
52 handle = alpm_initialize(ROOTDIR, DBPATH, &err);
53 if(!handle) {
54 fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(err));
55 return 1;
58 /* let us get log messages from libalpm */
59 alpm_option_set_logcb(handle, output_cb);
61 if(alpm_pkg_load(handle, argv[1], 1, PM_PGP_VERIFY_OPTIONAL, &pkg) == -1
62 || pkg == NULL) {
63 err = alpm_errno(handle);
64 switch(err) {
65 case PM_ERR_PKG_OPEN:
66 printf("Cannot open the given file.\n");
67 break;
68 case PM_ERR_LIBARCHIVE:
69 case PM_ERR_PKG_INVALID:
70 printf("Package is invalid.\n");
71 break;
72 default:
73 printf("libalpm error: %s\n", alpm_strerror(err));
74 break;
76 retval = 1;
77 } else {
78 alpm_pkg_free(pkg);
79 printf("Package is valid.\n");
80 retval = 0;
83 if(alpm_release(handle) == -1) {
84 fprintf(stderr, "error releasing alpm\n");
87 return retval;