2007-02-11 20:30:02 -05:00
|
|
|
/*
|
|
|
|
* testpkg.c : Test a pacman package for validity
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 by Aaron Griffin <aaronmgriffin@gmail.com>
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2007-02-11 20:30:02 -05:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-12-10 23:55:22 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-02-11 20:30:02 -05:00
|
|
|
*/
|
|
|
|
|
2007-08-15 20:16:46 -04:00
|
|
|
#include <stdio.h> /* printf */
|
|
|
|
#include <stdarg.h> /* va_list */
|
2007-02-11 20:30:02 -05:00
|
|
|
|
|
|
|
#include <alpm.h>
|
|
|
|
|
2007-10-31 12:43:04 -04:00
|
|
|
#define BASENAME "testpkg"
|
|
|
|
|
2011-06-28 00:41:07 -04:00
|
|
|
static void output_cb(alpm_loglevel_t level, const char *fmt, va_list args)
|
2007-02-11 20:30:02 -05:00
|
|
|
{
|
2008-03-01 08:01:40 -05:00
|
|
|
if(fmt[0] == '\0') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch(level) {
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_ERROR: printf("error: "); break;
|
|
|
|
case ALPM_LOG_WARNING: printf("warning: "); break;
|
2008-03-01 08:01:40 -05:00
|
|
|
default: return; /* skip other messages */
|
|
|
|
}
|
|
|
|
vprintf(fmt, args);
|
2007-02-11 20:30:02 -05:00
|
|
|
}
|
|
|
|
|
2010-10-11 21:20:06 -04:00
|
|
|
int main(int argc, char *argv[])
|
2007-02-11 20:30:02 -05:00
|
|
|
{
|
2008-03-01 08:01:40 -05:00
|
|
|
int retval = 1; /* default = false */
|
2011-06-28 00:04:00 -04:00
|
|
|
alpm_handle_t *handle;
|
2011-10-28 22:03:24 -04:00
|
|
|
alpm_errno_t err;
|
2011-06-28 09:26:39 -04:00
|
|
|
alpm_pkg_t *pkg = NULL;
|
2011-06-27 17:29:49 -04:00
|
|
|
const alpm_siglevel_t level = ALPM_SIG_PACKAGE | ALPM_SIG_PACKAGE_OPTIONAL;
|
2007-02-11 20:30:02 -05:00
|
|
|
|
2008-03-01 08:01:40 -05:00
|
|
|
if(argc != 2) {
|
2007-10-31 12:43:04 -04:00
|
|
|
fprintf(stderr, "usage: %s <package file>\n", BASENAME);
|
2011-03-20 20:45:57 -04:00
|
|
|
return 1;
|
2007-02-11 20:30:02 -05:00
|
|
|
}
|
|
|
|
|
2011-06-03 16:46:06 -04:00
|
|
|
handle = alpm_initialize(ROOTDIR, DBPATH, &err);
|
|
|
|
if(!handle) {
|
|
|
|
fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(err));
|
2011-03-20 20:45:57 -04:00
|
|
|
return 1;
|
2007-02-11 20:30:02 -05:00
|
|
|
}
|
|
|
|
|
2008-03-01 08:01:40 -05:00
|
|
|
/* let us get log messages from libalpm */
|
2011-06-07 14:15:43 -04:00
|
|
|
alpm_option_set_logcb(handle, output_cb);
|
2007-02-11 20:30:02 -05:00
|
|
|
|
2012-02-11 01:56:02 -05:00
|
|
|
/* set gpgdir to default */
|
|
|
|
alpm_option_set_gpgdir(handle, GPGDIR);
|
|
|
|
|
2011-06-27 17:29:49 -04:00
|
|
|
if(alpm_pkg_load(handle, argv[1], 1, level, &pkg) == -1
|
2011-04-22 00:39:01 -04:00
|
|
|
|| pkg == NULL) {
|
2011-06-27 11:10:08 -04:00
|
|
|
err = alpm_errno(handle);
|
2011-06-07 17:06:16 -04:00
|
|
|
switch(err) {
|
2011-08-25 19:29:00 -04:00
|
|
|
case ALPM_ERR_PKG_NOT_FOUND:
|
|
|
|
printf("Cannot find the given file.\n");
|
|
|
|
break;
|
2011-07-01 12:01:39 -04:00
|
|
|
case ALPM_ERR_PKG_OPEN:
|
2008-03-01 08:01:40 -05:00
|
|
|
printf("Cannot open the given file.\n");
|
|
|
|
break;
|
2011-07-01 12:01:39 -04:00
|
|
|
case ALPM_ERR_LIBARCHIVE:
|
|
|
|
case ALPM_ERR_PKG_INVALID:
|
2008-03-01 08:01:40 -05:00
|
|
|
printf("Package is invalid.\n");
|
|
|
|
break;
|
|
|
|
default:
|
2011-06-07 17:06:16 -04:00
|
|
|
printf("libalpm error: %s\n", alpm_strerror(err));
|
2008-03-01 08:01:40 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
retval = 1;
|
2007-02-11 20:30:02 -05:00
|
|
|
} else {
|
|
|
|
alpm_pkg_free(pkg);
|
2008-03-01 08:01:40 -05:00
|
|
|
printf("Package is valid.\n");
|
|
|
|
retval = 0;
|
2007-02-11 20:30:02 -05:00
|
|
|
}
|
2007-11-16 21:18:45 -05:00
|
|
|
|
2011-06-03 16:46:06 -04:00
|
|
|
if(alpm_release(handle) == -1) {
|
|
|
|
fprintf(stderr, "error releasing alpm\n");
|
2007-02-11 20:30:02 -05:00
|
|
|
}
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return retval;
|
2007-02-11 20:30:02 -05:00
|
|
|
}
|