2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
2011-01-22 21:38:49 +05:30
|
|
|
* Copyright (c) 2009-2011 Juan Romero Pardines.
|
2009-08-17 22:37:20 +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>
|
|
|
|
|
|
|
|
#include <xbps_api.h>
|
2010-11-19 18:10:13 +05:30
|
|
|
#include "xbps_api_impl.h"
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
/**
|
2011-01-19 05:01:22 +05:30
|
|
|
* @file lib/package_orphans.c
|
2010-01-21 07:40:19 +05:30
|
|
|
* @brief Package orphans handling routines
|
|
|
|
* @defgroup pkg_orphans Package orphans handling functions
|
2010-01-23 10:59:34 +05:30
|
|
|
*
|
|
|
|
* Functions to find installed package orphans.
|
|
|
|
*
|
|
|
|
* Package orphans were installed automatically by another package,
|
|
|
|
* but currently no other packages are depending on.
|
|
|
|
*
|
|
|
|
* The following image shown below shows the registered packages database
|
2011-01-27 16:49:05 +05:30
|
|
|
* dictionary (the array returned by xbps_find_pkg_orphans() will
|
2010-01-23 10:59:34 +05:30
|
|
|
* contain a package dictionary per orphan found):
|
|
|
|
*
|
|
|
|
* @image html images/xbps_regpkgdb_dictionary.png
|
|
|
|
*
|
|
|
|
* Legend:
|
|
|
|
* - <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.
|
|
|
|
*
|
|
|
|
* 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.
|
2010-01-21 07:40:19 +05:30
|
|
|
*/
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
static int
|
|
|
|
find_orphan_pkg(prop_object_t obj, void *arg, bool *loop_done)
|
|
|
|
{
|
2011-01-22 21:38:49 +05:30
|
|
|
prop_array_t reqby, orphans = arg;
|
|
|
|
prop_object_t obj2, obj3;
|
|
|
|
prop_object_iterator_t iter, iter2;
|
|
|
|
const char *orphan_pkgname;
|
2009-08-17 22:37:20 +05:30
|
|
|
char *pkgname;
|
|
|
|
unsigned int ndep = 0, cnt = 0;
|
|
|
|
bool automatic = false;
|
2009-08-22 11:11:23 +05:30
|
|
|
pkg_state_t state = 0;
|
|
|
|
int rv = 0;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
(void)loop_done;
|
|
|
|
|
2010-11-06 11:14:00 +05:30
|
|
|
prop_dictionary_get_bool(obj, "automatic-install", &automatic);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (!automatic)
|
|
|
|
return 0;
|
|
|
|
|
2009-08-22 11:11:23 +05:30
|
|
|
if ((rv = xbps_get_pkg_state_dictionary(obj, &state)) != 0)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (state != XBPS_PKG_STATE_INSTALLED)
|
|
|
|
return 0;
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
reqby = prop_dictionary_get(obj, "requiredby");
|
2011-01-22 21:38:49 +05:30
|
|
|
if (prop_object_type(reqby) != PROP_TYPE_ARRAY)
|
2009-08-17 22:37:20 +05:30
|
|
|
return EINVAL;
|
|
|
|
|
2011-01-22 21:38:49 +05:30
|
|
|
if ((cnt = prop_array_count(reqby)) == 0) {
|
|
|
|
prop_array_add(orphans, obj);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
iter = prop_array_iterator(reqby);
|
|
|
|
if (iter == NULL)
|
2011-01-22 21:38:49 +05:30
|
|
|
return ENOMEM;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
while ((obj2 = prop_object_iterator_next(iter)) != NULL) {
|
|
|
|
pkgname = xbps_get_pkg_name(prop_string_cstring_nocopy(obj2));
|
2011-01-22 21:38:49 +05:30
|
|
|
if (pkgname == NULL) {
|
|
|
|
prop_object_iterator_release(iter);
|
2009-11-23 15:16:51 +05:30
|
|
|
return EINVAL;
|
2011-01-22 21:38:49 +05:30
|
|
|
}
|
2009-11-23 15:16:51 +05:30
|
|
|
|
2011-01-22 21:38:49 +05:30
|
|
|
iter2 = prop_array_iterator(orphans);
|
|
|
|
if (iter == NULL) {
|
|
|
|
free(pkgname);
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
while ((obj3 = prop_object_iterator_next(iter2)) != NULL) {
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj3,
|
|
|
|
"pkgname", &orphan_pkgname);
|
|
|
|
if (strcmp(orphan_pkgname, pkgname) == 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
ndep++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-22 21:38:49 +05:30
|
|
|
prop_object_iterator_release(iter2);
|
2009-08-17 22:37:20 +05:30
|
|
|
free(pkgname);
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
2011-01-22 21:38:49 +05:30
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
if (ndep != cnt)
|
|
|
|
return 0;
|
2011-01-22 21:38:49 +05:30
|
|
|
if (!prop_array_add(orphans, obj))
|
|
|
|
return EINVAL;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
prop_array_t
|
2011-01-27 16:49:05 +05:30
|
|
|
xbps_find_pkg_orphans(void)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
|
|
|
prop_array_t array;
|
|
|
|
prop_dictionary_t dict;
|
|
|
|
int rv = 0;
|
|
|
|
|
2010-11-19 18:10:13 +05:30
|
|
|
if ((dict = xbps_regpkgdb_dictionary_get()) == NULL)
|
2009-08-17 22:37:20 +05:30
|
|
|
return NULL;
|
2011-01-22 21:38:49 +05:30
|
|
|
/*
|
|
|
|
* Prepare an array with all packages previously found.
|
|
|
|
*/
|
|
|
|
if ((array = prop_array_create()) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Find out all orphans by looking at the
|
|
|
|
* regpkgdb dictionary and iterate in reverse order
|
|
|
|
* in which packages were installed.
|
|
|
|
*/
|
|
|
|
rv = xbps_callback_array_iter_reverse_in_dict(dict, "packages",
|
2011-01-22 21:38:49 +05:30
|
|
|
find_orphan_pkg, array);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (rv != 0) {
|
2009-10-23 17:33:16 +05:30
|
|
|
errno = rv;
|
2011-01-22 21:38:49 +05:30
|
|
|
prop_object_release(array);
|
2009-08-17 22:37:20 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_regpkgdb_dictionary_release();
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|