]>
Commit | Line | Data |
---|---|---|
9803ec30 AG |
1 | /* |
2 | * testpkg.c : Test a pacman package for validity | |
3 | * | |
4 | * Copyright (c) 2007 by Aaron Griffin <aaronmgriffin@gmail.com> | |
23229097 | 5 | * |
9803ec30 AG |
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. | |
10 | * | |
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. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
9781d0d6 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
9803ec30 AG |
18 | */ |
19 | ||
f4dbb204 DM |
20 | #include <stdio.h> /* printf */ |
21 | #include <stdarg.h> /* va_list */ | |
9803ec30 AG |
22 | |
23 | #include <alpm.h> | |
24 | ||
3fa28308 | 25 | __attribute__((format(printf, 2, 0))) |
0aef91bc | 26 | static void output_cb(alpm_loglevel_t level, const char *fmt, va_list args) |
9803ec30 | 27 | { |
17eca54b NG |
28 | if(fmt[0] == '\0') { |
29 | return; | |
30 | } | |
31 | switch(level) { | |
ca43fdd9 AM |
32 | case ALPM_LOG_ERROR: printf("error: "); break; |
33 | case ALPM_LOG_WARNING: printf("warning: "); break; | |
17eca54b NG |
34 | default: return; /* skip other messages */ |
35 | } | |
36 | vprintf(fmt, args); | |
9803ec30 AG |
37 | } |
38 | ||
0ff2a914 | 39 | int main(int argc, char *argv[]) |
9803ec30 | 40 | { |
17eca54b | 41 | int retval = 1; /* default = false */ |
64c1cf79 | 42 | alpm_handle_t *handle; |
a3f93992 | 43 | alpm_errno_t err; |
8a04bc25 | 44 | alpm_pkg_t *pkg = NULL; |
7af0ab1c | 45 | const alpm_siglevel_t level = ALPM_SIG_PACKAGE | ALPM_SIG_PACKAGE_OPTIONAL; |
9803ec30 | 46 | |
17eca54b | 47 | if(argc != 2) { |
a86015f7 JSJ |
48 | fprintf(stderr, "testpkg (pacman) v" PACKAGE_VERSION "\n\n" |
49 | "Test a pacman package for validity.\n\n" | |
50 | "Usage: testpkg <package file>\n"); | |
0303b26b | 51 | return 1; |
9803ec30 AG |
52 | } |
53 | ||
19755b64 DM |
54 | handle = alpm_initialize(ROOTDIR, DBPATH, &err); |
55 | if(!handle) { | |
56 | fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(err)); | |
0303b26b | 57 | return 1; |
9803ec30 AG |
58 | } |
59 | ||
17eca54b | 60 | /* let us get log messages from libalpm */ |
17a6ac56 | 61 | alpm_option_set_logcb(handle, output_cb); |
9803ec30 | 62 | |
e5309933 AM |
63 | /* set gpgdir to default */ |
64 | alpm_option_set_gpgdir(handle, GPGDIR); | |
65 | ||
7af0ab1c | 66 | if(alpm_pkg_load(handle, argv[1], 1, level, &pkg) == -1 |
4d63ebe2 | 67 | || pkg == NULL) { |
f01c6f81 | 68 | err = alpm_errno(handle); |
e2aa9526 | 69 | switch(err) { |
2cfcc874 DM |
70 | case ALPM_ERR_PKG_NOT_FOUND: |
71 | printf("Cannot find the given file.\n"); | |
72 | break; | |
afc96f2a | 73 | case ALPM_ERR_PKG_OPEN: |
17eca54b NG |
74 | printf("Cannot open the given file.\n"); |
75 | break; | |
afc96f2a AM |
76 | case ALPM_ERR_LIBARCHIVE: |
77 | case ALPM_ERR_PKG_INVALID: | |
17eca54b NG |
78 | printf("Package is invalid.\n"); |
79 | break; | |
80 | default: | |
e2aa9526 | 81 | printf("libalpm error: %s\n", alpm_strerror(err)); |
17eca54b NG |
82 | break; |
83 | } | |
84 | retval = 1; | |
9803ec30 AG |
85 | } else { |
86 | alpm_pkg_free(pkg); | |
17eca54b NG |
87 | printf("Package is valid.\n"); |
88 | retval = 0; | |
9803ec30 | 89 | } |
23229097 | 90 | |
19755b64 DM |
91 | if(alpm_release(handle) == -1) { |
92 | fprintf(stderr, "error releasing alpm\n"); | |
9803ec30 AG |
93 | } |
94 | ||
0303b26b | 95 | return retval; |
9803ec30 | 96 | } |