2008-10-20 16:34:27 +02:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2008 Juan Romero Pardines.
|
|
|
|
* 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>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
2008-12-19 04:18:49 +01:00
|
|
|
#include "xbps_api.h"
|
2008-10-30 06:40:11 +01:00
|
|
|
|
|
|
|
typedef struct pkg_data {
|
|
|
|
const char *pkgname;
|
|
|
|
const char *version;
|
|
|
|
const char *short_desc;
|
|
|
|
} pkg_data_t;
|
2008-10-20 16:34:27 +02:00
|
|
|
|
2008-10-30 06:40:11 +01:00
|
|
|
static prop_dictionary_t make_dict_from_pkg(pkg_data_t *);
|
|
|
|
static void register_pkg(prop_dictionary_t, pkg_data_t *, const char *);
|
|
|
|
static void write_plist_file(prop_dictionary_t, const char *);
|
|
|
|
|
|
|
|
static prop_dictionary_t
|
|
|
|
make_dict_from_pkg(pkg_data_t *pkg)
|
|
|
|
{
|
|
|
|
prop_dictionary_t dict;
|
|
|
|
|
2008-12-21 01:50:19 +01:00
|
|
|
assert(pkg != NULL || pkg->pkgname != NULL);
|
|
|
|
assert(pkg->version != NULL || pkg->short_desc != NULL);
|
2008-10-30 06:40:11 +01:00
|
|
|
|
|
|
|
dict = prop_dictionary_create();
|
2008-12-21 01:50:19 +01:00
|
|
|
assert(dict != NULL);
|
2008-10-30 06:40:11 +01:00
|
|
|
|
|
|
|
prop_dictionary_set_cstring_nocopy(dict, "pkgname", pkg->pkgname);
|
|
|
|
prop_dictionary_set_cstring_nocopy(dict, "version", pkg->version);
|
|
|
|
prop_dictionary_set_cstring_nocopy(dict, "short_desc", pkg->short_desc);
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
register_pkg(prop_dictionary_t dict, pkg_data_t *pkg, const char *dbfile)
|
|
|
|
{
|
|
|
|
prop_dictionary_t pkgdict;
|
|
|
|
prop_array_t array;
|
|
|
|
|
2008-12-21 01:50:19 +01:00
|
|
|
assert(dict != NULL || pkg != NULL || dbfile != NULL);
|
2008-10-30 06:40:11 +01:00
|
|
|
pkgdict = make_dict_from_pkg(pkg);
|
2008-12-21 01:50:19 +01:00
|
|
|
assert(pkgdict != NULL);
|
2008-12-20 03:45:35 +01:00
|
|
|
array = prop_dictionary_get(dict, "packages");
|
2008-12-21 01:50:19 +01:00
|
|
|
assert(array != NULL);
|
|
|
|
assert(prop_object_type(array) == PROP_TYPE_ARRAY);
|
2008-10-30 06:40:11 +01:00
|
|
|
|
2008-12-19 04:18:49 +01:00
|
|
|
if (!xbps_add_obj_to_array(array, pkgdict)) {
|
2008-12-21 01:50:19 +01:00
|
|
|
printf("ERROR: couldn't register '%s-%s' in database!\n",
|
|
|
|
pkg->pkgname, pkg->version);
|
2008-12-19 04:18:49 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2008-10-30 06:40:11 +01:00
|
|
|
write_plist_file(dict, dbfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
perror("=> ERROR: couldn't write to database file");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-19 04:18:49 +01:00
|
|
|
usage(void)
|
2008-10-30 08:47:00 +01:00
|
|
|
{
|
2008-12-19 04:18:49 +01:00
|
|
|
printf("usage: xbps-pkgdb <action> [args]\n\n"
|
|
|
|
" Available actions:\n"
|
|
|
|
" list, register, sanitize-plist, unregister, version\n"
|
|
|
|
" Action arguments:\n"
|
|
|
|
" list\t[none]\n"
|
|
|
|
" register\t[<pkgname> <version> <shortdesc>]\n"
|
|
|
|
" sanitize-plist\t[<plist>]\n"
|
|
|
|
" unregister\t[<pkgname> <version>]\n"
|
|
|
|
" version\t[<pkgname>]\n"
|
|
|
|
" Environment:\n"
|
|
|
|
" XBPS_REGPKGDB_PATH\tPath to xbps pkgdb plist file\n\n"
|
|
|
|
" Examples:\n"
|
|
|
|
" $ xbps-pkgdb list\n"
|
|
|
|
" $ 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"
|
|
|
|
" $ xbps-pkgdb version pkgname\n");
|
|
|
|
exit(1);
|
2008-10-30 08:47:00 +01:00
|
|
|
}
|
|
|
|
|
2008-10-20 16:34:27 +02:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2008-10-30 06:40:11 +01:00
|
|
|
prop_dictionary_t dbdict = NULL, pkgdict;
|
|
|
|
prop_array_t dbarray = NULL;
|
|
|
|
pkg_data_t pkg;
|
|
|
|
const char *version;
|
2008-10-27 07:42:40 +01:00
|
|
|
char dbfile[PATH_MAX], *dbfileenv, *tmppath, *in_chroot_env;
|
|
|
|
bool in_chroot = false;
|
2008-10-20 16:34:27 +02:00
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
|
2008-12-19 04:18:49 +01:00
|
|
|
if ((dbfileenv = getenv("XBPS_REGPKGDB_PATH")) != NULL) {
|
|
|
|
/* Use path as defined by XBPS_REGPKGDB_PATH env var */
|
2008-12-21 02:11:41 +01:00
|
|
|
tmppath = strncpy(dbfile, dbfileenv, sizeof(dbfile) - 1);
|
2008-10-20 16:34:27 +02:00
|
|
|
if (sizeof(*tmppath) >= sizeof(dbfile))
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
/* Use default path */
|
|
|
|
tmppath =
|
2008-12-21 02:11:41 +01:00
|
|
|
strncpy(dbfile, XBPS_REGPKGDB_DEFPATH, sizeof(dbfile) - 1);
|
2008-10-20 16:34:27 +02:00
|
|
|
if (sizeof(*tmppath) >= sizeof(dbfile))
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* nul terminate string */
|
|
|
|
dbfile[sizeof(dbfile) - 1] = '\0';
|
|
|
|
|
2008-10-27 07:42:40 +01:00
|
|
|
in_chroot_env = getenv("in_chroot");
|
|
|
|
if (in_chroot_env != NULL)
|
|
|
|
in_chroot = true;
|
|
|
|
|
2008-12-21 03:45:39 +01:00
|
|
|
if (strcasecmp(argv[1], "register") == 0) {
|
2008-10-20 16:34:27 +02:00
|
|
|
/* Registers a package into the database */
|
2008-10-30 06:40:11 +01:00
|
|
|
if (argc != 5)
|
2008-10-20 16:34:27 +02:00
|
|
|
usage();
|
|
|
|
|
|
|
|
dbdict = prop_dictionary_internalize_from_file(dbfile);
|
|
|
|
if (dbdict == NULL) {
|
2008-10-30 06:40:11 +01:00
|
|
|
/* Create package dictionary and add its objects. */
|
|
|
|
pkg.pkgname = argv[2];
|
|
|
|
pkg.version = argv[3];
|
|
|
|
pkg.short_desc = argv[4];
|
|
|
|
pkgdict = make_dict_from_pkg(&pkg);
|
2008-12-21 01:50:19 +01:00
|
|
|
assert(pkgdict != NULL);
|
2008-10-30 06:40:11 +01:00
|
|
|
|
|
|
|
/* Add pkg dictionary into array. */
|
|
|
|
dbarray = prop_array_create();
|
2008-12-19 04:18:49 +01:00
|
|
|
if (!xbps_add_obj_to_array(dbarray, pkgdict)) {
|
2008-12-21 01:50:19 +01:00
|
|
|
printf("=> ERROR: couldn't register %s-%s\n",
|
|
|
|
pkg.pkgname, pkg.version);
|
2008-12-19 04:18:49 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2008-10-30 06:40:11 +01:00
|
|
|
|
|
|
|
/* Add array into main dictionary. */
|
|
|
|
dbdict = prop_dictionary_create();
|
2008-12-20 03:55:09 +01:00
|
|
|
if (!xbps_add_obj_to_dict(dbdict, dbarray,
|
2008-12-20 08:13:18 +01:00
|
|
|
"packages")) {
|
2008-12-21 01:50:19 +01:00
|
|
|
printf("=> ERROR: couldn't register %s-%s\n",
|
|
|
|
pkg.pkgname, pkg.version);
|
2008-12-19 04:18:49 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2008-10-30 06:40:11 +01:00
|
|
|
|
|
|
|
/* Write main dictionary to file. */
|
|
|
|
write_plist_file(dbdict, dbfile);
|
|
|
|
|
|
|
|
printf("%s==> Package database file not found, "
|
|
|
|
"creating it.\n", in_chroot ? "[chroot] " : "");
|
|
|
|
|
2008-10-20 16:34:27 +02:00
|
|
|
prop_object_release(dbdict);
|
|
|
|
} else {
|
2008-10-30 06:40:11 +01:00
|
|
|
/* Check if pkg is already registered. */
|
2008-12-20 03:45:35 +01:00
|
|
|
pkgdict = xbps_find_pkg_in_dict(dbdict, argv[2]);
|
2008-10-30 06:40:11 +01:00
|
|
|
if (pkgdict != NULL) {
|
2008-12-12 18:12:45 +01:00
|
|
|
printf("%s=> Package %s-%s already registered.\n",
|
|
|
|
in_chroot ? "[chroot] " : "",
|
2008-10-30 06:40:11 +01:00
|
|
|
argv[2], argv[3]);
|
2008-10-20 16:34:27 +02:00
|
|
|
exit(0);
|
|
|
|
}
|
2008-10-30 06:40:11 +01:00
|
|
|
pkg.pkgname = argv[2];
|
|
|
|
pkg.version = argv[3];
|
|
|
|
pkg.short_desc = argv[4];
|
|
|
|
|
|
|
|
register_pkg(dbdict, &pkg, dbfile);
|
2008-10-20 16:34:27 +02:00
|
|
|
}
|
2008-10-27 07:42:40 +01:00
|
|
|
|
2008-10-29 18:33:55 +01:00
|
|
|
printf("%s=> %s-%s registered successfully.\n",
|
2008-10-29 03:20:14 +01:00
|
|
|
in_chroot ? "[chroot] " : "", argv[2], argv[3]);
|
2008-10-20 16:34:27 +02:00
|
|
|
|
2008-12-21 03:45:39 +01:00
|
|
|
} else if (strcasecmp(argv[1], "unregister") == 0) {
|
2008-10-20 16:34:27 +02:00
|
|
|
/* Unregisters a package from the database */
|
|
|
|
if (argc != 4)
|
|
|
|
usage();
|
|
|
|
|
2008-12-21 08:43:57 +01:00
|
|
|
if (!xbps_remove_pkg_dict_from_file(argv[2], dbfile)) {
|
|
|
|
if (errno == ENODEV)
|
|
|
|
printf("=> ERROR: %s not registered "
|
|
|
|
"in database.\n", argv[2]);
|
|
|
|
else
|
|
|
|
printf("=> ERROR: couldn't unregister %s "
|
|
|
|
"from database (%s)\n", argv[2],
|
|
|
|
strerror(errno));
|
|
|
|
exit(EINVAL);
|
|
|
|
}
|
2008-10-30 06:40:11 +01:00
|
|
|
|
|
|
|
printf("%s=> %s-%s unregistered successfully.\n",
|
2008-10-29 03:20:14 +01:00
|
|
|
in_chroot ? "[chroot] " : "", argv[2], argv[3]);
|
2008-10-20 16:34:27 +02:00
|
|
|
|
2008-12-21 03:45:39 +01:00
|
|
|
} else if (strcasecmp(argv[1], "list") == 0) {
|
2008-10-20 16:34:27 +02:00
|
|
|
/* Lists packages currently registered in database */
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
|
2008-12-19 04:18:49 +01:00
|
|
|
dbdict = prop_dictionary_internalize_from_file(dbfile);
|
2008-12-20 04:53:56 +01:00
|
|
|
if (!xbps_callback_array_iter_in_dict(dbdict,
|
2008-12-20 05:49:41 +01:00
|
|
|
"packages", xbps_list_pkgs_in_dict, NULL))
|
2008-12-20 04:53:56 +01:00
|
|
|
exit(EINVAL);
|
2008-10-20 16:34:27 +02:00
|
|
|
|
2008-12-21 03:45:39 +01:00
|
|
|
} else if (strcasecmp(argv[1], "version") == 0) {
|
2008-10-20 16:34:27 +02:00
|
|
|
/* Prints version of an installed package */
|
|
|
|
if (argc != 3)
|
|
|
|
usage();
|
|
|
|
|
2008-12-19 04:18:49 +01:00
|
|
|
pkgdict = xbps_find_pkg_in_dict(
|
2008-12-20 03:45:35 +01:00
|
|
|
prop_dictionary_internalize_from_file(dbfile), argv[2]);
|
2008-10-30 06:40:11 +01:00
|
|
|
if (pkgdict == NULL)
|
|
|
|
exit(1);
|
2008-12-19 04:18:49 +01:00
|
|
|
if (!prop_dictionary_get_cstring_nocopy(pkgdict, "version",
|
|
|
|
&version))
|
2008-10-20 16:34:27 +02:00
|
|
|
exit(1);
|
2008-10-30 06:40:11 +01:00
|
|
|
printf("%s\n", version);
|
2008-10-20 16:34:27 +02:00
|
|
|
|
2008-12-21 03:45:39 +01:00
|
|
|
} else if (strcasecmp(argv[1], "sanitize-plist") == 0) {
|
2008-12-19 04:18:49 +01:00
|
|
|
/* Sanitize a plist file (indent the file properly) */
|
|
|
|
if (argc != 3)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
dbdict = prop_dictionary_internalize_from_file(argv[2]);
|
|
|
|
if (dbdict == NULL) {
|
|
|
|
printf("=> ERROR: couldn't sanitize %s plist file\n",
|
|
|
|
argv[2]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!prop_dictionary_externalize_to_file(dbdict, argv[2])) {
|
2008-12-21 01:50:19 +01:00
|
|
|
printf("=> ERROR: couldn't write new plist file "
|
|
|
|
"(%s)\n", strerror(errno));
|
2008-12-19 04:18:49 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2008-10-20 16:34:27 +02:00
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|