2009-11-26 07:52:50 +05:30
|
|
|
/*-
|
2012-01-20 15:40:52 +05:30
|
|
|
* Copyright (c) 2008-2012 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-01-20 15:40:52 +05:30
|
|
|
rv = xbps_pkgdb_update(xhp, false);
|
2011-12-24 05:35:26 +05:30
|
|
|
if (rv != 0) {
|
|
|
|
if (rv != ENOENT)
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_dbg_printf("[pkgdb] cannot internalize "
|
|
|
|
"pkgdb array: %s\n", strerror(rv));
|
2011-12-24 05:35:26 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_dbg_printf("[pkgdb] initialized ok.\n");
|
2011-12-24 05:35:26 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_pkgdb_update(struct xbps_handle *xhp, bool flush)
|
2011-12-24 05:35:26 +05:30
|
|
|
{
|
|
|
|
char *plist, *metadir;
|
|
|
|
int rv = 0;
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
plist = xbps_xasprintf("%s/%s/%s", xhp->rootdir,
|
2012-01-20 15:40:52 +05:30
|
|
|
XBPS_META_PATH, XBPS_PKGDB);
|
2011-01-19 00:48:27 +05:30
|
|
|
if (plist == NULL)
|
2011-06-04 17:07:53 +05:30
|
|
|
return ENOMEM;
|
2011-01-19 00:48:27 +05:30
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
if (xhp->pkgdb != NULL && flush) {
|
2011-12-24 05:35:26 +05:30
|
|
|
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) {
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_dbg_printf("[pkgdb] failed to "
|
2011-12-24 05:35:26 +05:30
|
|
|
"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 */
|
2012-01-20 15:40:52 +05:30
|
|
|
if (!prop_array_externalize_to_zfile(xhp->pkgdb, plist)) {
|
2011-12-24 05:35:26 +05:30
|
|
|
free(plist);
|
|
|
|
return errno;
|
|
|
|
}
|
2012-01-20 15:40:52 +05:30
|
|
|
prop_object_release(xhp->pkgdb);
|
|
|
|
xhp->pkgdb = NULL;
|
2011-01-19 00:48:27 +05:30
|
|
|
}
|
2011-12-24 05:35:26 +05:30
|
|
|
/* update copy in memory */
|
2012-01-20 15:40:52 +05:30
|
|
|
xhp->pkgdb = prop_array_internalize_from_zfile(plist);
|
|
|
|
if (xhp->pkgdb == NULL)
|
2011-12-24 05:35:26 +05:30
|
|
|
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-01-20 15:40:52 +05:30
|
|
|
prop_object_release(xhp->pkgdb);
|
|
|
|
xhp->pkgdb = NULL;
|
|
|
|
xbps_dbg_printf("[pkgdb] 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;
|
|
|
|
|
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;
|
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
if (reverse)
|
|
|
|
rv = xbps_callback_array_iter_reverse(xhp->pkgdb, fn, arg);
|
|
|
|
else
|
|
|
|
rv = xbps_callback_array_iter(xhp->pkgdb, fn, arg);
|
|
|
|
|
2011-12-22 18:55:27 +05:30
|
|
|
return rv;
|
|
|
|
}
|
2011-12-23 12:46:25 +05:30
|
|
|
|
|
|
|
int
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_pkgdb_foreach_reverse_pkg_cb(int (*fn)(prop_object_t, void *, bool *),
|
|
|
|
void *arg)
|
2011-12-23 12:46:25 +05:30
|
|
|
{
|
|
|
|
return foreach_pkg_cb(fn, arg, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_pkgdb_foreach_pkg_cb(int (*fn)(prop_object_t, void *, bool *),
|
|
|
|
void *arg)
|
2011-12-23 12:46:25 +05:30
|
|
|
{
|
|
|
|
return foreach_pkg_cb(fn, arg, false);
|
|
|
|
}
|
2011-12-24 05:35:26 +05:30
|
|
|
|
|
|
|
prop_dictionary_t
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_pkgdb_get_pkgd(const char *pkg, bool bypattern)
|
2011-12-24 05:35:26 +05:30
|
|
|
{
|
|
|
|
struct xbps_handle *xhp = xbps_handle_get();
|
|
|
|
prop_dictionary_t pkgd = NULL;
|
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
if (xbps_pkgdb_init(xhp) != 0)
|
2011-12-24 05:35:26 +05:30
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (bypattern)
|
2012-01-20 15:40:52 +05:30
|
|
|
pkgd = xbps_find_pkg_in_array_by_pattern(xhp->pkgdb, pkg);
|
2011-12-24 05:35:26 +05:30
|
|
|
else
|
2012-01-20 15:40:52 +05:30
|
|
|
pkgd = xbps_find_pkg_in_array_by_name(xhp->pkgdb, pkg);
|
|
|
|
|
|
|
|
if (pkgd != NULL)
|
|
|
|
return prop_dictionary_copy(pkgd);
|
2011-12-24 05:35:26 +05:30
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
return NULL;
|
2011-12-24 05:35:26 +05:30
|
|
|
}
|
2011-12-24 20:39:30 +05:30
|
|
|
|
|
|
|
bool
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_pkgdb_remove_pkgd(const char *pkg, bool bypattern, bool flush)
|
2011-12-24 20:39:30 +05:30
|
|
|
{
|
2012-01-20 15:40:52 +05:30
|
|
|
struct xbps_handle *xhp = xbps_handle_get();
|
|
|
|
bool rv = false;
|
|
|
|
|
|
|
|
if (xbps_pkgdb_init(xhp) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (bypattern)
|
|
|
|
rv = xbps_remove_pkg_from_array_by_pattern(xhp->pkgdb, pkg);
|
|
|
|
else
|
|
|
|
rv = xbps_remove_pkg_from_array_by_name(xhp->pkgdb, pkg);
|
2011-12-24 20:39:30 +05:30
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
if (!flush || !rv)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if ((xbps_pkgdb_update(xhp, true)) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
xbps_pkgdb_replace_pkgd(prop_dictionary_t pkgd,
|
|
|
|
const char *pkg,
|
|
|
|
bool bypattern,
|
|
|
|
bool flush)
|
|
|
|
{
|
2011-12-24 20:39:30 +05:30
|
|
|
struct xbps_handle *xhp = xbps_handle_get();
|
2012-01-20 15:40:52 +05:30
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (xbps_pkgdb_init(xhp) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (bypattern)
|
|
|
|
rv = xbps_array_replace_dict_by_pattern(xhp->pkgdb, pkgd, pkg);
|
|
|
|
else
|
|
|
|
rv = xbps_array_replace_dict_by_name(xhp->pkgdb, pkgd, pkg);
|
|
|
|
|
2012-01-20 17:08:24 +05:30
|
|
|
if (!flush)
|
|
|
|
return rv != 0 ? false : true;
|
2011-12-24 20:39:30 +05:30
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
if ((xbps_pkgdb_update(xhp, true)) != 0)
|
2011-12-24 20:39:30 +05:30
|
|
|
return false;
|
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
return true;
|
2011-12-24 20:39:30 +05:30
|
|
|
}
|