2008-10-20 16:34:27 +02:00
|
|
|
/*-
|
2009-02-10 02:52:12 +01:00
|
|
|
* Copyright (c) 2008-2009 Juan Romero Pardines.
|
2008-10-20 16:34:27 +02:00
|
|
|
* 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>
|
2009-03-13 00:21:55 +01:00
|
|
|
#include <strings.h>
|
2008-10-20 16:34:27 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
2008-12-21 23:57:02 +01:00
|
|
|
#include <xbps_api.h>
|
2008-10-30 06:40:11 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
write_plist_file(prop_dictionary_t dict, const char *file)
|
|
|
|
{
|
2008-12-21 01:50:19 +01:00
|
|
|
assert(dict != NULL || file != NULL);
|
2008-10-30 06:40:11 +01:00
|
|
|
|
|
|
|
if (!prop_dictionary_externalize_to_file(dict, file)) {
|
|
|
|
prop_object_release(dict);
|
2008-12-21 09:00:55 +01:00
|
|
|
printf("=> ERROR: couldn't write to %s (%s)",
|
|
|
|
file, strerror(errno));
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-10-30 06:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-19 04:18:49 +01:00
|
|
|
usage(void)
|
2008-10-30 08:47:00 +01:00
|
|
|
{
|
2009-02-17 23:29:13 +01:00
|
|
|
printf("usage: xbps-pkgdb [options] [action] [args]\n"
|
|
|
|
"\n"
|
2008-12-19 04:18:49 +01:00
|
|
|
" Available actions:\n"
|
2009-04-18 16:45:10 +02:00
|
|
|
" getpkgname, getpkgrevision, getpkgversion, register\n"
|
|
|
|
" sanitize-plist, unregister, version\n"
|
2009-02-17 23:29:13 +01:00
|
|
|
"\n"
|
2008-12-19 04:18:49 +01:00
|
|
|
" Action arguments:\n"
|
2009-02-17 23:29:13 +01:00
|
|
|
" getpkgname\t\t<string>\n"
|
2009-04-18 16:45:10 +02:00
|
|
|
" getpkgrevision\t<string>\n"
|
2009-02-17 23:29:13 +01:00
|
|
|
" getpkgversion\t<string>\n"
|
2008-12-27 12:56:51 +01:00
|
|
|
" register\t\t<pkgname> <version> <shortdesc>\n"
|
|
|
|
" sanitize-plist\t<plist>\n"
|
|
|
|
" unregister\t\t<pkgname> <version>\n"
|
|
|
|
" version\t\t<pkgname>\n"
|
2009-02-17 23:29:13 +01:00
|
|
|
"\n"
|
2008-12-27 12:56:51 +01:00
|
|
|
" Options shared by all actions:\n"
|
|
|
|
" -r\t\t\t<rootdir>\n"
|
|
|
|
"\n"
|
2009-02-18 00:28:35 +01:00
|
|
|
" Options used by the register action:\n"
|
|
|
|
" -a\t\t\tSet automatic installation flag.\n"
|
|
|
|
"\n"
|
2008-12-19 04:18:49 +01:00
|
|
|
" Examples:\n"
|
2009-02-17 23:29:13 +01:00
|
|
|
" $ xbps-pkgdb getpkgname foo-2.0\n"
|
2009-04-18 16:45:10 +02:00
|
|
|
" $ xbps-pkgdb getpkgrevision foo-2.0_1\n"
|
2009-02-17 23:29:13 +01:00
|
|
|
" $ xbps-pkgdb getpkgversion foo-2.0\n"
|
2008-12-19 04:18:49 +01:00
|
|
|
" $ xbps-pkgdb register pkgname 2.0 \"A short description\"\n"
|
|
|
|
" $ xbps-pkgdb sanitize-plist /blah/foo.plist\n"
|
|
|
|
" $ xbps-pkgdb unregister pkgname 2.0\n"
|
2009-02-17 23:29:13 +01:00
|
|
|
" $ xbps-pkgdb version pkgname\n");
|
|
|
|
|
2009-02-12 18:59:43 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-10-30 08:47:00 +01:00
|
|
|
}
|
|
|
|
|
2008-10-20 16:34:27 +02:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2009-02-17 23:29:13 +01:00
|
|
|
prop_dictionary_t dict;
|
2008-10-30 06:40:11 +01:00
|
|
|
const char *version;
|
2009-02-17 23:29:13 +01:00
|
|
|
char *plist, *pkgname, *in_chroot_env, *root = NULL;
|
2009-02-18 00:28:35 +01:00
|
|
|
bool automatic = false, in_chroot = false;
|
2008-12-27 12:56:51 +01:00
|
|
|
int c, rv = 0;
|
|
|
|
|
2009-02-18 00:28:35 +01:00
|
|
|
while ((c = getopt(argc, argv, "ar:")) != -1) {
|
2008-12-27 12:56:51 +01:00
|
|
|
switch (c) {
|
2009-02-18 00:28:35 +01:00
|
|
|
case 'a':
|
|
|
|
/* Set automatic install flag */
|
|
|
|
automatic = true;
|
|
|
|
break;
|
2008-12-27 12:56:51 +01:00
|
|
|
case 'r':
|
|
|
|
/* To specify the root directory */
|
|
|
|
root = strdup(optarg);
|
|
|
|
if (root == NULL)
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-27 12:56:51 +01:00
|
|
|
xbps_set_rootdir(root);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
2008-10-20 16:34:27 +02:00
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 1)
|
2008-10-20 16:34:27 +02:00
|
|
|
usage();
|
|
|
|
|
2009-04-04 19:32:39 +02:00
|
|
|
plist = xbps_xasprintf("%s/%s/%s", root, XBPS_META_PATH, XBPS_REGPKGDB);
|
2009-02-17 23:29:13 +01:00
|
|
|
if (plist == NULL) {
|
2008-12-23 12:28:21 +01:00
|
|
|
printf("=> ERROR: couldn't find regpkdb file (%s)\n",
|
|
|
|
strerror(errno));
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-10-20 16:34:27 +02:00
|
|
|
}
|
|
|
|
|
2008-10-27 07:42:40 +01:00
|
|
|
in_chroot_env = getenv("in_chroot");
|
|
|
|
if (in_chroot_env != NULL)
|
|
|
|
in_chroot = true;
|
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
if (strcasecmp(argv[0], "register") == 0) {
|
2008-10-20 16:34:27 +02:00
|
|
|
/* Registers a package into the database */
|
2008-12-27 12:56:51 +01:00
|
|
|
if (argc != 4)
|
2008-10-20 16:34:27 +02:00
|
|
|
usage();
|
|
|
|
|
2009-04-02 02:46:36 +02:00
|
|
|
dict = prop_dictionary_create();
|
|
|
|
if (dict == NULL)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
prop_dictionary_set_cstring_nocopy(dict, "pkgname", argv[1]);
|
|
|
|
prop_dictionary_set_cstring_nocopy(dict, "version", argv[2]);
|
|
|
|
prop_dictionary_set_cstring_nocopy(dict, "short_desc", argv[3]);
|
|
|
|
|
2009-08-08 22:29:48 +02:00
|
|
|
rv = xbps_set_pkg_state_installed(argv[1],
|
|
|
|
XBPS_PKG_STATE_INSTALLED);
|
|
|
|
if (rv != 0)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
rv = xbps_register_pkg(dict, automatic);
|
2008-12-26 11:30:19 +01:00
|
|
|
if (rv == EEXIST) {
|
|
|
|
printf("%s=> %s-%s already registered.\n",
|
2008-12-27 12:56:51 +01:00
|
|
|
in_chroot ? "[chroot] " : "", argv[1], argv[2]);
|
2008-12-26 11:30:19 +01:00
|
|
|
} else if (rv != 0) {
|
|
|
|
printf("%s=> couldn't register %s-%s (%s).\n",
|
2008-12-27 12:56:51 +01:00
|
|
|
in_chroot ? "[chroot] " : "" , argv[1], argv[2],
|
2008-12-26 11:30:19 +01:00
|
|
|
strerror(rv));
|
2008-10-20 16:34:27 +02:00
|
|
|
} else {
|
2008-12-26 11:30:19 +01:00
|
|
|
printf("%s=> %s-%s registered successfully.\n",
|
2008-12-27 12:56:51 +01:00
|
|
|
in_chroot ? "[chroot] " : "", argv[1], argv[2]);
|
2008-10-20 16:34:27 +02:00
|
|
|
}
|
2008-10-27 07:42:40 +01:00
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
} else if (strcasecmp(argv[0], "unregister") == 0) {
|
2008-10-20 16:34:27 +02:00
|
|
|
/* Unregisters a package from the database */
|
2008-12-27 12:56:51 +01:00
|
|
|
if (argc != 3)
|
2008-10-20 16:34:27 +02:00
|
|
|
usage();
|
|
|
|
|
2009-04-06 16:39:14 +02:00
|
|
|
rv = xbps_remove_pkg_dict_from_file(argv[1], plist);
|
|
|
|
if (rv == ENOENT) {
|
|
|
|
printf("=> ERROR: %s not registered in database.\n",
|
|
|
|
argv[1]);
|
2009-04-06 16:58:13 +02:00
|
|
|
} else if (rv != 0) {
|
2009-04-06 16:39:14 +02:00
|
|
|
printf("=> ERROR: couldn't unregister %s "
|
|
|
|
"from database (%s)\n", argv[1], strerror(rv));
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-21 08:43:57 +01:00
|
|
|
}
|
2008-10-30 06:40:11 +01:00
|
|
|
|
|
|
|
printf("%s=> %s-%s unregistered successfully.\n",
|
2008-12-27 12:56:51 +01:00
|
|
|
in_chroot ? "[chroot] " : "", argv[1], argv[2]);
|
2008-10-20 16:34:27 +02:00
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
} else if (strcasecmp(argv[0], "version") == 0) {
|
2008-10-20 16:34:27 +02:00
|
|
|
/* Prints version of an installed package */
|
2008-12-27 12:56:51 +01:00
|
|
|
if (argc != 2)
|
2008-10-20 16:34:27 +02:00
|
|
|
usage();
|
|
|
|
|
2009-02-17 23:29:13 +01:00
|
|
|
dict = xbps_find_pkg_from_plist(plist, argv[1]);
|
|
|
|
if (dict == NULL)
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-27 08:43:13 +01:00
|
|
|
|
2009-02-17 23:29:13 +01:00
|
|
|
if (!prop_dictionary_get_cstring_nocopy(dict, "version",
|
2008-12-19 04:18:49 +01:00
|
|
|
&version))
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-27 08:43:13 +01:00
|
|
|
|
2008-10-30 06:40:11 +01:00
|
|
|
printf("%s\n", version);
|
2009-02-17 23:29:13 +01:00
|
|
|
prop_object_release(dict);
|
2008-10-20 16:34:27 +02:00
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
} else if (strcasecmp(argv[0], "sanitize-plist") == 0) {
|
2009-02-17 23:29:13 +01:00
|
|
|
/* Sanitize a plist file (properly indent the file) */
|
2008-12-27 16:19:11 +01:00
|
|
|
if (argc != 2)
|
2008-12-19 04:18:49 +01:00
|
|
|
usage();
|
|
|
|
|
2009-02-17 23:29:13 +01:00
|
|
|
dict = prop_dictionary_internalize_from_file(argv[1]);
|
|
|
|
if (dict == NULL) {
|
2008-12-21 09:12:51 +01:00
|
|
|
printf("=> ERROR: couldn't sanitize %s plist file "
|
2008-12-27 12:56:51 +01:00
|
|
|
"(%s)\n", argv[1], strerror(errno));
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-19 04:18:49 +01:00
|
|
|
}
|
2009-02-17 23:29:13 +01:00
|
|
|
write_plist_file(dict, argv[1]);
|
2008-12-19 04:18:49 +01:00
|
|
|
|
2009-02-17 13:57:51 +01:00
|
|
|
} else if (strcasecmp(argv[0], "getpkgversion") == 0) {
|
|
|
|
/* Returns the version of a pkg string */
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
|
2009-02-17 23:29:13 +01:00
|
|
|
version = xbps_get_pkg_version(argv[1]);
|
|
|
|
if (version == NULL) {
|
|
|
|
printf("Invalid string, expected <string>-<version>\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
printf("%s\n", version);
|
2009-02-17 13:57:51 +01:00
|
|
|
|
|
|
|
} else if (strcasecmp(argv[0], "getpkgname") == 0) {
|
|
|
|
/* Returns the name of a pkg string */
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
pkgname = xbps_get_pkg_name(argv[1]);
|
2009-02-17 23:29:13 +01:00
|
|
|
if (pkgname == NULL) {
|
|
|
|
printf("Invalid string, expected <string>-<version>\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2009-02-17 13:57:51 +01:00
|
|
|
printf("%s\n", pkgname);
|
|
|
|
free(pkgname);
|
|
|
|
|
2009-04-18 16:45:10 +02:00
|
|
|
} else if (strcasecmp(argv[0], "getpkgrevision") == 0) {
|
|
|
|
/* Returns the revision of a pkg string */
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
version = xbps_get_pkg_revision(argv[1]);
|
|
|
|
if (version == NULL)
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
|
|
|
printf("%s\n", version);
|
|
|
|
|
2008-10-20 16:34:27 +02:00
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2008-10-20 16:34:27 +02:00
|
|
|
}
|