2009-11-26 07:52:50 +05:30
|
|
|
/*-
|
2011-01-19 00:51:35 +05:30
|
|
|
* Copyright (c) 2008-2011 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
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
/**
|
2010-11-19 18:10:13 +05:30
|
|
|
* @file lib/regpkgdb_dictionary.c
|
2010-01-23 07:07:19 +05:30
|
|
|
* @brief Package register database routines
|
|
|
|
* @defgroup regpkgdb Package register database functions
|
2010-01-21 07:40:19 +05:30
|
|
|
*
|
2010-01-23 04:29:55 +05:30
|
|
|
* These functions will initialize and release (resources of)
|
2010-01-23 07:07:19 +05:30
|
|
|
* the registered packages database plist file (defined by XBPS_REGPKGDB).
|
2010-01-23 04:29:55 +05:30
|
|
|
*
|
2010-01-23 07:07:19 +05:30
|
|
|
* The returned dictionary by xbps_regpkgs_dictionary_init() uses
|
|
|
|
* the structure as shown in the next graph:
|
2010-01-23 04:29:55 +05:30
|
|
|
*
|
|
|
|
* @image html images/xbps_regpkgdb_dictionary.png
|
|
|
|
*
|
|
|
|
* Legend:
|
2010-01-23 07:07:19 +05:30
|
|
|
* - <b>Salmon filled box</b>: \a XBPS_REGPKGDB_PLIST 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.
|
2010-01-23 04:29:55 +05:30
|
|
|
*
|
|
|
|
* Text inside of white boxes are the key associated with the object, its
|
2010-01-23 07:07:19 +05:30
|
|
|
* data type is specified on its edge, i.e array, bool, integer, string,
|
|
|
|
* dictionary.
|
2010-01-21 07:40:19 +05:30
|
|
|
*/
|
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
int HIDDEN
|
|
|
|
xbps_regpkgdb_dictionary_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
|
|
|
|
2011-12-24 05:35:26 +05:30
|
|
|
if (xhp->regpkgdb != NULL)
|
2011-06-04 17:07:53 +05:30
|
|
|
return 0;
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2011-12-24 05:35:26 +05:30
|
|
|
rv = xbps_regpkgdb_update(xhp, false);
|
|
|
|
if (rv != 0) {
|
|
|
|
if (rv != ENOENT)
|
|
|
|
xbps_dbg_printf("[regpkgdb] cannot internalize "
|
|
|
|
"regpkgdb dictionary: %s\n", strerror(rv));
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
xbps_dbg_printf("[regpkgdb] initialized ok.\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xbps_regpkgdb_update(struct xbps_handle *xhp, bool flush)
|
|
|
|
{
|
|
|
|
char *plist, *metadir;
|
|
|
|
int rv = 0;
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
plist = xbps_xasprintf("%s/%s/%s", xhp->rootdir,
|
2011-01-19 00:48:27 +05:30
|
|
|
XBPS_META_PATH, XBPS_REGPKGDB);
|
|
|
|
if (plist == NULL)
|
2011-06-04 17:07:53 +05:30
|
|
|
return ENOMEM;
|
2011-01-19 00:48:27 +05:30
|
|
|
|
2011-12-24 05:35:26 +05:30
|
|
|
if (xhp->regpkgdb != NULL && flush) {
|
|
|
|
metadir = xbps_xasprintf("%s/%s", xhp->rootdir,
|
|
|
|
XBPS_META_PATH);
|
|
|
|
if (metadir == NULL) {
|
|
|
|
free(plist);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
/* Create metadir if doesn't exist */
|
|
|
|
if (access(metadir, X_OK) == -1) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
if (xbps_mkpath(metadir, 0755) != 0) {
|
|
|
|
xbps_dbg_printf("[regpkgdb] failed to "
|
|
|
|
"create metadir %s: %s\n", metadir,
|
|
|
|
strerror(errno));
|
|
|
|
rv = errno;
|
|
|
|
free(metadir);
|
|
|
|
free(plist);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
free(plist);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(metadir);
|
|
|
|
/* flush dictionary to storage */
|
|
|
|
if (!prop_dictionary_externalize_to_zfile(xhp->regpkgdb,
|
|
|
|
plist)) {
|
|
|
|
free(plist);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
prop_object_release(xhp->regpkgdb);
|
|
|
|
xhp->regpkgdb = NULL;
|
2011-01-19 00:48:27 +05:30
|
|
|
}
|
2011-12-24 05:35:26 +05:30
|
|
|
/* update copy in memory */
|
|
|
|
xhp->regpkgdb = prop_dictionary_internalize_from_zfile(plist);
|
|
|
|
if (xhp->regpkgdb == NULL)
|
|
|
|
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
|
2011-12-22 21:25:35 +05:30
|
|
|
xbps_regpkgdb_dictionary_release(struct xbps_handle *xhp)
|
2009-11-26 07:52:50 +05:30
|
|
|
{
|
2011-12-24 05:35:26 +05:30
|
|
|
assert(xhp != NULL);
|
|
|
|
|
|
|
|
if (xhp->regpkgdb == NULL)
|
2009-11-26 07:52:50 +05:30
|
|
|
return;
|
|
|
|
|
2011-12-24 05:35:26 +05:30
|
|
|
prop_object_release(xhp->regpkgdb);
|
|
|
|
xhp->regpkgdb = NULL;
|
2011-10-29 13:25:09 +05:30
|
|
|
xbps_dbg_printf("[regpkgdb] released ok.\n");
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
2011-12-22 18:55:27 +05:30
|
|
|
|
2011-12-23 12:46:25 +05:30
|
|
|
static int
|
|
|
|
foreach_pkg_cb(int (*fn)(prop_object_t, void *, bool *),
|
|
|
|
void *arg,
|
|
|
|
bool reverse)
|
2011-12-22 18:55:27 +05:30
|
|
|
{
|
|
|
|
struct xbps_handle *xhp = xbps_handle_get();
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
/* initialize regpkgdb */
|
|
|
|
if ((rv = xbps_regpkgdb_dictionary_init(xhp)) != 0)
|
|
|
|
return rv;
|
|
|
|
|
2011-12-23 12:46:25 +05:30
|
|
|
if (reverse) {
|
2011-12-24 05:35:26 +05:30
|
|
|
rv = xbps_callback_array_iter_reverse_in_dict(
|
|
|
|
xhp->regpkgdb, "packages", fn, arg);
|
2011-12-23 12:46:25 +05:30
|
|
|
} else {
|
2011-12-24 05:35:26 +05:30
|
|
|
rv = xbps_callback_array_iter_in_dict(
|
|
|
|
xhp->regpkgdb, "packages", fn, arg);
|
2011-12-22 18:55:27 +05:30
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2011-12-23 12:46:25 +05:30
|
|
|
|
|
|
|
int
|
|
|
|
xbps_regpkgdb_foreach_reverse_pkg_cb(int (*fn)(prop_object_t, void *, bool *),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
return foreach_pkg_cb(fn, arg, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xbps_regpkgdb_foreach_pkg_cb(int (*fn)(prop_object_t, void *, bool *),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
return foreach_pkg_cb(fn, arg, false);
|
|
|
|
}
|
2011-12-24 05:35:26 +05:30
|
|
|
|
|
|
|
prop_dictionary_t
|
|
|
|
xbps_regpkgdb_get_pkgd(const char *pkg, bool bypattern)
|
|
|
|
{
|
|
|
|
struct xbps_handle *xhp = xbps_handle_get();
|
|
|
|
prop_dictionary_t pkgd = NULL;
|
|
|
|
|
|
|
|
if (xbps_regpkgdb_dictionary_init(xhp) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (bypattern)
|
|
|
|
pkgd = xbps_find_pkg_in_dict_by_pattern(xhp->regpkgdb,
|
|
|
|
"packages", pkg);
|
|
|
|
else
|
|
|
|
pkgd = xbps_find_pkg_in_dict_by_name(xhp->regpkgdb,
|
|
|
|
"packages", pkg);
|
|
|
|
|
|
|
|
return prop_dictionary_copy(pkgd);
|
|
|
|
}
|