2009-11-26 07:52:50 +05:30
|
|
|
/*-
|
2013-03-05 08:38:42 +05:30
|
|
|
* Copyright (c) 2012-2013 Juan Romero Pardines.
|
2009-11-26 07:52:50 +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>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2010-11-13 07:48:58 +05:30
|
|
|
#include "xbps_api_impl.h"
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2012-01-20 16:47:27 +05:30
|
|
|
/**
|
|
|
|
* @file lib/pkgdb.c
|
|
|
|
* @brief Package database handling routines
|
|
|
|
* @defgroup pkgdb Package database handling functions
|
|
|
|
*
|
|
|
|
* Functions to manipulate the main package database plist file (pkgdb).
|
|
|
|
*
|
|
|
|
* The following image shown below shows the proplib structure used
|
|
|
|
* by the main package database plist:
|
|
|
|
*
|
|
|
|
* @image html images/xbps_pkgdb_array.png
|
|
|
|
*
|
|
|
|
* Legend:
|
|
|
|
* - <b>Salmon filled box</b>: \a XBPS_PKGDB file internalized.
|
|
|
|
* - <b>White filled box</b>: mandatory objects.
|
|
|
|
* - <b>Grey filled box</b>: optional objects.
|
|
|
|
* - <b>Green filled box</b>: possible value set in the object, only one
|
|
|
|
* of them is set.
|
|
|
|
*
|
|
|
|
* Text inside of white boxes are the key associated with the object, its
|
|
|
|
* data type is specified on its edge, i.e array, bool, integer, string,
|
|
|
|
* dictionary.
|
|
|
|
*/
|
2011-06-04 17:07:53 +05:30
|
|
|
int HIDDEN
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_pkgdb_init(struct xbps_handle *xhp)
|
2009-11-26 07:52:50 +05:30
|
|
|
{
|
2011-12-24 05:35:26 +05:30
|
|
|
int rv;
|
|
|
|
|
|
|
|
assert(xhp != NULL);
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
if (xhp->pkgdb != NULL)
|
2011-06-04 17:07:53 +05:30
|
|
|
return 0;
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
if ((rv = xbps_pkgdb_update(xhp, false)) != 0) {
|
2011-12-24 05:35:26 +05:30
|
|
|
if (rv != ENOENT)
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp, "[pkgdb] cannot internalize "
|
2012-01-20 15:40:52 +05:30
|
|
|
"pkgdb array: %s\n", strerror(rv));
|
2011-12-24 05:35:26 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp, "[pkgdb] initialized ok.\n");
|
2011-12-24 05:35:26 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_pkgdb_update(struct xbps_handle *xhp, bool flush)
|
2011-12-24 05:35:26 +05:30
|
|
|
{
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_dictionary_t pkgdb_storage;
|
2012-03-13 14:52:35 +05:30
|
|
|
char *plist;
|
2012-06-15 19:03:11 +05:30
|
|
|
static int cached_rv;
|
2011-12-24 05:35:26 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
2012-06-15 19:03:11 +05:30
|
|
|
if (cached_rv && !flush)
|
|
|
|
return cached_rv;
|
|
|
|
|
2012-03-13 14:52:35 +05:30
|
|
|
plist = xbps_xasprintf("%s/%s", xhp->metadir, XBPS_PKGDB);
|
2012-06-15 19:03:11 +05:30
|
|
|
if (xhp->pkgdb && flush) {
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgdb_storage = prop_dictionary_internalize_from_file(plist);
|
2012-10-26 14:35:17 +05:30
|
|
|
if (pkgdb_storage == NULL ||
|
2013-03-05 08:38:42 +05:30
|
|
|
!prop_dictionary_equals(xhp->pkgdb, pkgdb_storage)) {
|
2012-10-26 13:55:14 +05:30
|
|
|
/* flush dictionary to storage */
|
2013-03-05 08:38:42 +05:30
|
|
|
if (!prop_dictionary_externalize_to_file(xhp->pkgdb, plist)) {
|
2012-10-26 13:55:14 +05:30
|
|
|
free(plist);
|
|
|
|
return errno;
|
|
|
|
}
|
2011-12-24 05:35:26 +05:30
|
|
|
}
|
2012-10-26 14:35:17 +05:30
|
|
|
if (pkgdb_storage)
|
|
|
|
prop_object_release(pkgdb_storage);
|
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
prop_object_release(xhp->pkgdb);
|
|
|
|
xhp->pkgdb = NULL;
|
2012-06-15 19:03:11 +05:30
|
|
|
cached_rv = 0;
|
2011-01-19 00:48:27 +05:30
|
|
|
}
|
2011-12-24 05:35:26 +05:30
|
|
|
/* update copy in memory */
|
2013-03-05 08:38:42 +05:30
|
|
|
if ((xhp->pkgdb = prop_dictionary_internalize_from_file(plist)) == NULL) {
|
2012-11-11 14:33:14 +05:30
|
|
|
if (errno == ENOENT)
|
2013-03-05 08:38:42 +05:30
|
|
|
xhp->pkgdb = prop_dictionary_create();
|
2013-01-15 17:44:30 +05:30
|
|
|
else
|
|
|
|
xbps_error_printf("cannot access to pkgdb: %s\n", strerror(errno));
|
2011-12-24 05:35:26 +05:30
|
|
|
|
2012-11-11 14:33:14 +05:30
|
|
|
cached_rv = rv = errno;
|
|
|
|
}
|
2011-01-19 00:48:27 +05:30
|
|
|
free(plist);
|
|
|
|
|
2011-12-24 05:35:26 +05:30
|
|
|
return rv;
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
void HIDDEN
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_pkgdb_release(struct xbps_handle *xhp)
|
2009-11-26 07:52:50 +05:30
|
|
|
{
|
2011-12-24 05:35:26 +05:30
|
|
|
assert(xhp != NULL);
|
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
if (xhp->pkgdb == NULL)
|
2009-11-26 07:52:50 +05:30
|
|
|
return;
|
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
if (prop_object_type(xhp->pkg_metad) == PROP_TYPE_DICTIONARY)
|
|
|
|
prop_object_release(xhp->pkg_metad);
|
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
prop_object_release(xhp->pkgdb);
|
|
|
|
xhp->pkgdb = NULL;
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp, "[pkgdb] released ok.\n");
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
2011-12-22 18:55:27 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
int
|
|
|
|
xbps_pkgdb_foreach_reverse_cb(struct xbps_handle *xhp,
|
|
|
|
int (*fn)(struct xbps_handle *, prop_object_t, void *, bool *),
|
|
|
|
void *arg)
|
2011-12-22 18:55:27 +05:30
|
|
|
{
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_array_t allkeys;
|
|
|
|
prop_object_t obj;
|
|
|
|
prop_dictionary_t pkgd;
|
|
|
|
size_t i;
|
2011-12-22 18:55:27 +05:30
|
|
|
int rv;
|
2013-03-05 08:38:42 +05:30
|
|
|
bool done = false;
|
2011-12-22 18:55:27 +05:30
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
if ((rv = xbps_pkgdb_init(xhp)) != 0)
|
2011-12-22 18:55:27 +05:30
|
|
|
return rv;
|
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
allkeys = prop_dictionary_all_keys(xhp->pkgdb);
|
|
|
|
assert(allkeys);
|
2012-01-20 15:40:52 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
for (i = prop_array_count(allkeys); i > 0; i--) {
|
|
|
|
obj = prop_array_get(allkeys, i);
|
|
|
|
pkgd = prop_dictionary_get_keysym(xhp->pkgdb, obj);
|
|
|
|
rv = (*fn)(xhp, pkgd, arg, &done);
|
|
|
|
if (rv != 0 || done)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prop_object_release(allkeys);
|
2011-12-22 18:55:27 +05:30
|
|
|
return rv;
|
|
|
|
}
|
2011-12-23 12:46:25 +05:30
|
|
|
|
|
|
|
int
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_pkgdb_foreach_cb(struct xbps_handle *xhp,
|
|
|
|
int (*fn)(struct xbps_handle *, prop_object_t, void *, bool *),
|
2012-01-21 15:00:20 +05:30
|
|
|
void *arg)
|
2011-12-23 12:46:25 +05:30
|
|
|
{
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_object_t obj;
|
|
|
|
prop_object_iterator_t iter;
|
|
|
|
prop_dictionary_t pkgd;
|
|
|
|
int rv;
|
|
|
|
bool done = false;
|
|
|
|
|
|
|
|
if ((rv = xbps_pkgdb_init(xhp)) != 0)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
iter = prop_dictionary_iterator(xhp->pkgdb);
|
|
|
|
assert(iter);
|
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
|
|
|
pkgd = prop_dictionary_get_keysym(xhp->pkgdb, obj);
|
|
|
|
rv = (*fn)(xhp, pkgd, arg, &done);
|
|
|
|
if (rv != 0 || done)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
return rv;
|
2011-12-23 12:46:25 +05:30
|
|
|
}
|
2011-12-24 05:35:26 +05:30
|
|
|
|
|
|
|
prop_dictionary_t
|
2012-11-30 11:41:51 +05:30
|
|
|
xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
|
2011-12-24 05:35:26 +05:30
|
|
|
{
|
2012-01-20 15:40:52 +05:30
|
|
|
if (xbps_pkgdb_init(xhp) != 0)
|
2011-12-24 05:35:26 +05:30
|
|
|
return NULL;
|
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
return xbps_find_pkg_in_dict(xhp->pkgdb, pkg);
|
2011-12-24 05:35:26 +05:30
|
|
|
}
|
2011-12-24 20:39:30 +05:30
|
|
|
|
2012-11-20 02:16:54 +05:30
|
|
|
prop_dictionary_t
|
2012-11-30 11:41:51 +05:30
|
|
|
xbps_pkgdb_get_virtualpkg(struct xbps_handle *xhp, const char *vpkg)
|
2012-11-20 02:16:54 +05:30
|
|
|
{
|
|
|
|
if (xbps_pkgdb_init(xhp) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
return xbps_find_virtualpkg_in_dict(xhp, xhp->pkgdb, vpkg);
|
2012-11-20 02:16:54 +05:30
|
|
|
}
|
|
|
|
|
2012-11-30 22:10:52 +05:30
|
|
|
static prop_dictionary_t
|
|
|
|
get_pkg_metadata(struct xbps_handle *xhp, prop_dictionary_t pkgd)
|
2012-04-05 14:27:15 +05:30
|
|
|
{
|
2012-11-30 22:10:52 +05:30
|
|
|
prop_dictionary_t pkg_metad;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver;
|
|
|
|
char *pkgname, *plist;
|
2012-11-30 11:41:51 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
|
|
|
|
pkgname = xbps_pkg_name(pkgver);
|
|
|
|
assert(pkgname);
|
2012-11-30 11:41:51 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
if ((pkg_metad = prop_dictionary_get(xhp->pkg_metad, pkgname)) != NULL) {
|
|
|
|
free(pkgname);
|
2012-11-30 11:41:51 +05:30
|
|
|
return pkg_metad;
|
2013-03-05 08:38:42 +05:30
|
|
|
}
|
2012-11-30 11:41:51 +05:30
|
|
|
plist = xbps_xasprintf("%s/.%s.plist", xhp->metadir, pkgname);
|
2013-03-05 08:38:42 +05:30
|
|
|
pkg_metad = prop_dictionary_internalize_from_file(plist);
|
2012-11-30 11:41:51 +05:30
|
|
|
free(plist);
|
|
|
|
|
|
|
|
if (pkg_metad == NULL) {
|
|
|
|
xbps_dbg_printf(xhp, "[pkgdb] cannot read %s metadata: %s\n",
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgver, strerror(errno));
|
|
|
|
free(pkgname);
|
2012-11-30 11:41:51 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xhp->pkg_metad == NULL)
|
|
|
|
xhp->pkg_metad = prop_dictionary_create();
|
|
|
|
|
|
|
|
prop_dictionary_set(xhp->pkg_metad, pkgname, pkg_metad);
|
|
|
|
prop_object_release(pkg_metad);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgname);
|
2012-11-30 11:41:51 +05:30
|
|
|
|
|
|
|
return pkg_metad;
|
2012-04-05 14:27:15 +05:30
|
|
|
}
|
|
|
|
|
2012-12-15 12:03:49 +05:30
|
|
|
static void
|
|
|
|
generate_full_revdeps_tree(struct xbps_handle *xhp)
|
2012-11-30 22:10:52 +05:30
|
|
|
{
|
2012-12-15 12:03:49 +05:30
|
|
|
prop_array_t rundeps, pkg;
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_dictionary_t pkgmetad, pkgd;
|
2012-11-30 22:10:52 +05:30
|
|
|
prop_object_t obj;
|
|
|
|
prop_object_iterator_t iter;
|
2012-12-15 12:03:49 +05:30
|
|
|
const char *pkgver, *pkgdep, *vpkgname;
|
|
|
|
char *curpkgname;
|
|
|
|
unsigned int i;
|
|
|
|
bool alloc;
|
2012-11-30 22:10:52 +05:30
|
|
|
|
2012-12-15 12:03:49 +05:30
|
|
|
if (xhp->pkgdb_revdeps)
|
|
|
|
return;
|
2012-11-30 22:10:52 +05:30
|
|
|
|
2012-12-15 12:03:49 +05:30
|
|
|
xhp->pkgdb_revdeps = prop_dictionary_create();
|
2012-11-30 22:10:52 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
iter = prop_dictionary_iterator(xhp->pkgdb);
|
2012-11-30 22:10:52 +05:30
|
|
|
assert(iter);
|
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgd = prop_dictionary_get_keysym(xhp->pkgdb, obj);
|
2012-11-30 22:10:52 +05:30
|
|
|
/*
|
|
|
|
* If run_depends is in pkgdb use it, otherwise fallback to
|
|
|
|
* the slower pkg metadata method.
|
|
|
|
*/
|
2013-03-05 08:38:42 +05:30
|
|
|
rundeps = prop_dictionary_get(pkgd, "run_depends");
|
2012-11-30 22:10:52 +05:30
|
|
|
if (rundeps == NULL) {
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgmetad = get_pkg_metadata(xhp, pkgd);
|
2012-12-15 12:03:49 +05:30
|
|
|
assert(pkgmetad);
|
|
|
|
rundeps = prop_dictionary_get(pkgmetad, "run_depends");
|
2012-11-30 22:10:52 +05:30
|
|
|
}
|
|
|
|
if (rundeps == NULL || !prop_array_count(rundeps))
|
|
|
|
continue;
|
|
|
|
|
2012-12-15 12:03:49 +05:30
|
|
|
for (i = 0; i < prop_array_count(rundeps); i++) {
|
|
|
|
alloc = false;
|
|
|
|
prop_array_get_cstring_nocopy(rundeps, i, &pkgdep);
|
|
|
|
curpkgname = xbps_pkgpattern_name(pkgdep);
|
|
|
|
if (curpkgname == NULL)
|
|
|
|
curpkgname = xbps_pkg_name(pkgdep);
|
|
|
|
assert(curpkgname);
|
|
|
|
vpkgname = vpkg_user_conf(xhp, curpkgname, false);
|
|
|
|
if (vpkgname == NULL)
|
|
|
|
vpkgname = curpkgname;
|
|
|
|
|
|
|
|
pkg = prop_dictionary_get(xhp->pkgdb_revdeps, vpkgname);
|
|
|
|
if (pkg == NULL) {
|
|
|
|
alloc = true;
|
|
|
|
pkg = prop_array_create();
|
|
|
|
}
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
|
2012-12-15 12:03:49 +05:30
|
|
|
prop_array_add_cstring_nocopy(pkg, pkgver);
|
|
|
|
prop_dictionary_set(xhp->pkgdb_revdeps, vpkgname, pkg);
|
|
|
|
free(curpkgname);
|
|
|
|
if (alloc)
|
|
|
|
prop_object_release(pkg);
|
2012-11-30 22:10:52 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
2012-12-15 12:03:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
prop_array_t
|
|
|
|
xbps_pkgdb_get_pkg_revdeps(struct xbps_handle *xhp, const char *pkg)
|
|
|
|
{
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_array_t res;
|
2012-12-15 12:03:49 +05:30
|
|
|
prop_dictionary_t pkgd;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver;
|
|
|
|
char *pkgname;
|
2012-12-15 12:03:49 +05:30
|
|
|
|
|
|
|
if ((pkgd = xbps_pkgdb_get_pkg(xhp, pkg)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
generate_full_revdeps_tree(xhp);
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
|
|
|
|
pkgname = xbps_pkg_name(pkgver);
|
|
|
|
res = prop_dictionary_get(xhp->pkgdb_revdeps, pkgname);
|
|
|
|
free(pkgname);
|
2012-11-30 22:10:52 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
return res;
|
2012-11-30 22:10:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
prop_dictionary_t
|
|
|
|
xbps_pkgdb_get_pkg_metadata(struct xbps_handle *xhp, const char *pkg)
|
|
|
|
{
|
|
|
|
prop_dictionary_t pkgd;
|
|
|
|
|
|
|
|
pkgd = xbps_pkgdb_get_pkg(xhp, pkg);
|
|
|
|
if (pkgd == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return get_pkg_metadata(xhp, pkgd);
|
|
|
|
}
|