2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
2014-04-21 14:33:18 +05:30
|
|
|
* Copyright (c) 2008-2014 Juan Romero Pardines.
|
2014-03-04 19:38:14 +05:30
|
|
|
* Copyright (c) 2014 Enno Boland.
|
2009-08-17 22:37:20 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-01-25 17:30:23 +05:30
|
|
|
#include <strings.h>
|
2009-08-17 22:37:20 +05:30
|
|
|
#include <errno.h>
|
2009-11-07 09:26:20 +05:30
|
|
|
#include <ctype.h>
|
2010-11-13 07:48:58 +05:30
|
|
|
#include <assert.h>
|
2010-11-19 18:10:13 +05:30
|
|
|
#include <unistd.h>
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2013-06-20 16:01:02 +05:30
|
|
|
#include <xbps.h>
|
2012-11-06 15:37:31 +05:30
|
|
|
#include "../xbps-install/defs.h"
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2011-01-15 16:29:44 +05:30
|
|
|
static void __attribute__((noreturn))
|
2012-01-17 21:26:17 +05:30
|
|
|
usage(void)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2012-09-24 14:19:45 +05:30
|
|
|
fprintf(stdout,
|
2010-01-15 19:49:16 +05:30
|
|
|
"usage: xbps-uhelper [options] [action] [args]\n"
|
2009-08-17 22:37:20 +05:30
|
|
|
"\n"
|
|
|
|
" Available actions:\n"
|
2009-11-24 17:29:47 +05:30
|
|
|
" cmpver, digest, fetch, getpkgdepname, getpkgname, getpkgrevision,\n"
|
2013-10-22 13:26:55 +05:30
|
|
|
" getpkgversion, pkgmatch, version, real-version.\n"
|
2009-08-17 22:37:20 +05:30
|
|
|
"\n"
|
|
|
|
" Action arguments:\n"
|
2009-11-24 17:29:47 +05:30
|
|
|
" cmpver\t\t<instver> <reqver>\n"
|
|
|
|
" digest\t\t<file> <file1+N>\n"
|
2014-03-04 19:38:14 +05:30
|
|
|
" fetch\t\t<URL[>filename]> <URL1+N[>filename]>\n"
|
2009-11-07 09:26:20 +05:30
|
|
|
" getpkgdepname\t<string>\n"
|
2011-01-24 23:43:11 +05:30
|
|
|
" getpkgdepversion\t<string>\n"
|
2009-08-17 22:37:20 +05:30
|
|
|
" getpkgname\t\t<string>\n"
|
|
|
|
" getpkgrevision\t<string>\n"
|
|
|
|
" getpkgversion\t<string>\n"
|
2009-11-07 09:26:20 +05:30
|
|
|
" pkgmatch\t\t<pkg-version> <pkg-pattern>\n"
|
2009-08-17 22:37:20 +05:30
|
|
|
" version\t\t<pkgname>\n"
|
2014-04-21 14:33:18 +05:30
|
|
|
" real-version\t<pkgname>\n"
|
2009-08-17 22:37:20 +05:30
|
|
|
"\n"
|
|
|
|
" Options shared by all actions:\n"
|
2012-01-20 23:12:00 +05:30
|
|
|
" -C\t\tPath to xbps.conf file.\n"
|
2010-12-18 14:56:12 +05:30
|
|
|
" -d\t\tDebugging messages to stderr.\n"
|
2012-09-25 05:11:49 +05:30
|
|
|
" -r\t\t<rootdir>\n"
|
2009-10-18 14:12:04 +05:30
|
|
|
" -V\t\tPrints the xbps release version\n"
|
2009-08-17 22:37:20 +05:30
|
|
|
"\n"
|
|
|
|
" Examples:\n"
|
2012-09-25 05:11:49 +05:30
|
|
|
" $ xbps-uhelper cmpver 'foo-1.0_1' 'foo-2.1_1'\n"
|
2012-01-20 23:12:00 +05:30
|
|
|
" $ xbps-uhelper digest file ...\n"
|
2010-01-24 21:55:17 +05:30
|
|
|
" $ xbps-uhelper fetch http://www.foo.org/file.blob ...\n"
|
2012-01-20 23:12:00 +05:30
|
|
|
" $ xbps-uhelper getpkgdepname 'foo>=0'\n"
|
|
|
|
" $ xbps-uhelper getpkgdepversion 'foo>=0'\n"
|
2012-09-25 05:11:49 +05:30
|
|
|
" $ xbps-uhelper getpkgname foo-2.0_1\n"
|
2009-11-24 17:29:47 +05:30
|
|
|
" $ xbps-uhelper getpkgrevision foo-2.0_1\n"
|
2012-09-25 05:11:49 +05:30
|
|
|
" $ xbps-uhelper getpkgversion foo-2.0_1\n"
|
|
|
|
" $ xbps-uhelper pkgmatch foo-1.0_1 'foo>=1.0'\n"
|
2009-11-24 17:29:47 +05:30
|
|
|
" $ xbps-uhelper version pkgname\n");
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t dict;
|
2012-01-17 20:47:03 +05:30
|
|
|
struct xbps_handle xh;
|
2011-07-27 21:34:38 +05:30
|
|
|
struct xferstat xfer;
|
2012-09-25 05:11:49 +05:30
|
|
|
const char *version, *rootdir = NULL, *confdir = NULL;
|
2014-03-04 18:52:02 +05:30
|
|
|
char *pkgname, *hash, *sep;
|
2013-09-15 13:36:49 +05:30
|
|
|
int flags = 0, c, rv = 0;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
while ((c = getopt(argc, argv, "C:dr:V")) != -1) {
|
2009-08-17 22:37:20 +05:30
|
|
|
switch (c) {
|
2011-06-04 17:07:53 +05:30
|
|
|
case 'C':
|
2011-10-17 18:41:54 +05:30
|
|
|
confdir = optarg;
|
2011-06-04 17:07:53 +05:30
|
|
|
break;
|
2009-08-17 22:37:20 +05:30
|
|
|
case 'r':
|
|
|
|
/* To specify the root directory */
|
2011-02-21 22:12:47 +05:30
|
|
|
rootdir = optarg;
|
2009-08-17 22:37:20 +05:30
|
|
|
break;
|
2010-12-18 14:56:12 +05:30
|
|
|
case 'd':
|
2012-01-22 15:22:35 +05:30
|
|
|
flags |= XBPS_FLAG_DEBUG;
|
2010-12-18 14:56:12 +05:30
|
|
|
break;
|
2009-10-18 14:12:04 +05:30
|
|
|
case 'V':
|
|
|
|
printf("%s\n", XBPS_RELVER);
|
|
|
|
exit(EXIT_SUCCESS);
|
2009-08-17 22:37:20 +05:30
|
|
|
case '?':
|
|
|
|
default:
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 1)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2012-09-25 04:36:13 +05:30
|
|
|
memset(&xh, 0, sizeof(xh));
|
|
|
|
|
2013-10-22 13:26:55 +05:30
|
|
|
if ((strcmp(argv[0], "version") == 0) ||
|
|
|
|
(strcmp(argv[0], "real-version") == 0) ||
|
|
|
|
(strcmp(argv[0], "fetch") == 0)) {
|
2011-11-05 13:11:29 +05:30
|
|
|
/*
|
2011-12-15 15:49:20 +05:30
|
|
|
* Initialize libxbps.
|
2011-11-05 13:11:29 +05:30
|
|
|
*/
|
2012-01-22 15:22:35 +05:30
|
|
|
xh.flags = flags;
|
2012-01-17 20:47:03 +05:30
|
|
|
xh.fetch_cb = fetch_file_progress_cb;
|
|
|
|
xh.fetch_cb_data = &xfer;
|
2013-12-16 16:16:39 +05:30
|
|
|
if (rootdir)
|
|
|
|
strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
|
2012-01-17 20:47:03 +05:30
|
|
|
xh.conffile = confdir;
|
|
|
|
if ((rv = xbps_init(&xh)) != 0) {
|
2011-11-05 13:11:29 +05:30
|
|
|
xbps_error_printf("xbps-uhelper: failed to "
|
2011-12-15 15:49:20 +05:30
|
|
|
"initialize libxbps: %s.\n", strerror(rv));
|
2011-11-05 13:11:29 +05:30
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
2013-10-22 13:26:55 +05:30
|
|
|
if (strcmp(argv[0], "version") == 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
/* Prints version of an installed package */
|
|
|
|
if (argc != 2)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2013-03-26 01:41:30 +05:30
|
|
|
if ((((dict = xbps_pkgdb_get_pkg(&xh, argv[1])) == NULL)) &&
|
|
|
|
(((dict = xbps_pkgdb_get_virtualpkg(&xh, argv[1])) == NULL)))
|
2012-12-14 16:28:30 +05:30
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(dict, "pkgver", &version);
|
2013-03-05 09:15:32 +05:30
|
|
|
printf("%s\n", xbps_pkg_version(version));
|
2013-10-22 13:26:55 +05:30
|
|
|
} else if (strcmp(argv[0], "real-version") == 0) {
|
|
|
|
/* Prints version of an installed real package, not virtual */
|
2009-08-17 22:37:20 +05:30
|
|
|
if (argc != 2)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2013-10-22 13:26:55 +05:30
|
|
|
if ((dict = xbps_pkgdb_get_pkg(&xh, argv[1])) == NULL)
|
2009-08-17 22:37:20 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2013-10-22 13:26:55 +05:30
|
|
|
|
|
|
|
xbps_dictionary_get_cstring_nocopy(dict, "pkgver", &version);
|
|
|
|
printf("%s\n", xbps_pkg_version(version));
|
|
|
|
} else if (strcmp(argv[0], "getpkgversion") == 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
/* Returns the version of a pkg string */
|
|
|
|
if (argc != 2)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2011-06-01 13:07:32 +05:30
|
|
|
version = xbps_pkg_version(argv[1]);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (version == NULL) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"Invalid string, expected <string>-<version>\n");
|
2009-08-17 22:37:20 +05:30
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
printf("%s\n", version);
|
2013-10-22 13:26:55 +05:30
|
|
|
} else if (strcmp(argv[0], "getpkgname") == 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
/* Returns the name of a pkg string */
|
|
|
|
if (argc != 2)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2011-06-01 13:07:32 +05:30
|
|
|
pkgname = xbps_pkg_name(argv[1]);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (pkgname == NULL) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"Invalid string, expected <string>-<version>\n");
|
2009-08-17 22:37:20 +05:30
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
printf("%s\n", pkgname);
|
|
|
|
free(pkgname);
|
2013-10-22 13:26:55 +05:30
|
|
|
} else if (strcmp(argv[0], "getpkgrevision") == 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
/* Returns the revision of a pkg string */
|
|
|
|
if (argc != 2)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2011-06-01 13:07:32 +05:30
|
|
|
version = xbps_pkg_revision(argv[1]);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (version == NULL)
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
|
|
|
printf("%s\n", version);
|
2013-10-22 13:26:55 +05:30
|
|
|
} else if (strcmp(argv[0], "getpkgdepname") == 0) {
|
2009-11-07 09:26:20 +05:30
|
|
|
/* Returns the pkgname of a dependency */
|
|
|
|
if (argc != 2)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-11-07 09:26:20 +05:30
|
|
|
|
2011-06-01 13:07:32 +05:30
|
|
|
pkgname = xbps_pkgpattern_name(argv[1]);
|
2009-11-07 09:26:20 +05:30
|
|
|
if (pkgname == NULL)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
printf("%s\n", pkgname);
|
|
|
|
free(pkgname);
|
2013-10-22 13:26:55 +05:30
|
|
|
} else if (strcmp(argv[0], "getpkgdepversion") == 0) {
|
2011-01-24 23:43:11 +05:30
|
|
|
/* returns the version of a package pattern dependency */
|
|
|
|
if (argc != 2)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2011-01-24 23:43:11 +05:30
|
|
|
|
2011-06-01 13:07:32 +05:30
|
|
|
version = xbps_pkgpattern_version(argv[1]);
|
2011-01-24 23:43:11 +05:30
|
|
|
if (version == NULL)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
printf("%s\n", version);
|
2013-10-22 13:26:55 +05:30
|
|
|
} else if (strcmp(argv[0], "pkgmatch") == 0) {
|
2009-11-07 09:26:20 +05:30
|
|
|
/* Matches a pkg with a pattern */
|
|
|
|
if (argc != 3)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-11-07 09:26:20 +05:30
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
exit(xbps_pkgpattern_match(argv[1], argv[2]));
|
2013-10-22 13:26:55 +05:30
|
|
|
} else if (strcmp(argv[0], "cmpver") == 0) {
|
2009-11-24 17:29:47 +05:30
|
|
|
/* Compare two version strings, installed vs required */
|
|
|
|
if (argc != 3)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-11-24 17:29:47 +05:30
|
|
|
|
|
|
|
exit(xbps_cmpver(argv[1], argv[2]));
|
2013-10-22 13:26:55 +05:30
|
|
|
} else if (strcmp(argv[0], "digest") == 0) {
|
2009-11-24 17:29:47 +05:30
|
|
|
/* Prints SHA256 hashes for specified files */
|
|
|
|
if (argc < 2)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-11-24 17:29:47 +05:30
|
|
|
|
2013-09-15 13:36:49 +05:30
|
|
|
for (int i = 1; i < argc; i++) {
|
2011-06-01 13:07:32 +05:30
|
|
|
hash = xbps_file_hash(argv[i]);
|
2009-11-24 17:29:47 +05:30
|
|
|
if (hash == NULL) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"E: couldn't get hash for %s (%s)\n",
|
2009-11-24 17:29:47 +05:30
|
|
|
argv[i], strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
printf("%s\n", hash);
|
|
|
|
}
|
2013-10-22 13:26:55 +05:30
|
|
|
} else if (strcmp(argv[0], "fetch") == 0) {
|
2009-11-24 17:29:47 +05:30
|
|
|
/* Fetch a file from specified URL */
|
|
|
|
if (argc != 2)
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-11-24 17:29:47 +05:30
|
|
|
|
2013-09-15 13:36:49 +05:30
|
|
|
for (int i = 1; i < argc; i++) {
|
2014-03-04 18:52:02 +05:30
|
|
|
if( (sep = strrchr(argv[i], '>')) ) {
|
|
|
|
*sep = '\0';
|
|
|
|
rv = xbps_fetch_file_dest(&xh, argv[i], sep+1, "v");
|
|
|
|
} else {
|
|
|
|
rv = xbps_fetch_file(&xh, argv[i], "v");
|
|
|
|
}
|
|
|
|
|
2010-01-24 21:55:17 +05:30
|
|
|
if (rv == -1) {
|
2014-03-04 18:52:02 +05:30
|
|
|
printf("%s: %s\n", argv[i],
|
2010-01-24 21:55:17 +05:30
|
|
|
xbps_fetch_error_string());
|
|
|
|
} else if (rv == 0) {
|
|
|
|
printf("%s: file is identical than remote.\n",
|
2014-03-04 18:52:02 +05:30
|
|
|
argv[i]);
|
2011-12-24 21:26:45 +05:30
|
|
|
} else
|
|
|
|
rv = 0;
|
2009-11-24 17:29:47 +05:30
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
} else {
|
2012-01-17 21:26:17 +05:30
|
|
|
usage();
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2012-12-14 16:28:30 +05:30
|
|
|
|
2011-12-24 21:07:42 +05:30
|
|
|
exit(rv ? EXIT_FAILURE : EXIT_SUCCESS);
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|