2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
2012-01-20 15:40:52 +05:30
|
|
|
* Copyright (c) 2009-2012 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>
|
|
|
|
|
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):
|
|
|
|
*
|
2012-01-20 15:46:07 +05:30
|
|
|
* @image html images/xbps_pkgdb_array.png
|
2010-01-23 10:59:34 +05:30
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2011-01-30 12:38:34 +05:30
|
|
|
struct orphan_data {
|
|
|
|
prop_array_t array;
|
|
|
|
prop_array_t orphans_user;
|
|
|
|
};
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
static int
|
2012-06-14 11:52:11 +05:30
|
|
|
find_orphan_pkg(struct xbps_handle *xhp,
|
|
|
|
prop_object_t obj,
|
|
|
|
void *arg,
|
|
|
|
bool *loop_done)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2011-01-30 12:38:34 +05:30
|
|
|
struct orphan_data *od = arg;
|
|
|
|
prop_array_t reqby;
|
2011-01-29 23:04:50 +05:30
|
|
|
prop_object_t obj2;
|
|
|
|
prop_object_iterator_t iter;
|
2011-01-30 12:38:34 +05:30
|
|
|
const char *pkgdep, *curpkgname;
|
|
|
|
char *pkgdepname;
|
2009-08-17 22:37:20 +05:30
|
|
|
unsigned int ndep = 0, cnt = 0;
|
|
|
|
bool automatic = false;
|
2011-01-30 12:38:34 +05:30
|
|
|
size_t i;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
(void)xhp;
|
2009-08-17 22:37:20 +05:30
|
|
|
(void)loop_done;
|
2011-01-29 23:04:50 +05:30
|
|
|
/*
|
|
|
|
* Skip packages that were not installed automatically.
|
|
|
|
*/
|
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;
|
|
|
|
|
|
|
|
reqby = prop_dictionary_get(obj, "requiredby");
|
2011-02-26 15:38:58 +05:30
|
|
|
if (reqby == NULL || ((cnt = prop_array_count(reqby)) == 0)) {
|
|
|
|
/*
|
|
|
|
* Add packages with empty or missing "requiredby" arrays.
|
|
|
|
*/
|
2011-01-30 12:38:34 +05:30
|
|
|
prop_array_add(od->array, obj);
|
2011-01-22 21:38:49 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2011-01-30 12:38:34 +05:30
|
|
|
/*
|
|
|
|
* Add packages that only have 1 entry matching any pkgname
|
|
|
|
* object in the user supplied array of strings.
|
|
|
|
*/
|
|
|
|
if (od->orphans_user != NULL && cnt == 1) {
|
|
|
|
for (i = 0; i < prop_array_count(od->orphans_user); i++) {
|
|
|
|
prop_array_get_cstring_nocopy(od->orphans_user,
|
|
|
|
i, &curpkgname);
|
2011-07-15 21:52:58 +05:30
|
|
|
if (xbps_match_pkgname_in_array(reqby, curpkgname)) {
|
2011-01-30 12:38:34 +05:30
|
|
|
prop_array_add(od->array, 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;
|
2011-01-30 12:38:34 +05:30
|
|
|
/*
|
|
|
|
* Iterate over the requiredby array and add current pkg
|
|
|
|
* when all pkg dependencies are already in requiredby
|
|
|
|
* or any pkgname object in the user supplied array of
|
|
|
|
* strings match.
|
|
|
|
*/
|
2009-08-17 22:37:20 +05:30
|
|
|
while ((obj2 = prop_object_iterator_next(iter)) != NULL) {
|
2011-01-29 23:04:50 +05:30
|
|
|
pkgdep = prop_string_cstring_nocopy(obj2);
|
|
|
|
if (pkgdep == NULL) {
|
2011-01-22 21:38:49 +05:30
|
|
|
prop_object_iterator_release(iter);
|
2009-11-23 15:16:51 +05:30
|
|
|
return EINVAL;
|
2011-01-22 21:38:49 +05:30
|
|
|
}
|
2012-11-30 11:41:51 +05:30
|
|
|
if (xbps_find_pkg_in_array(od->array, pkgdep))
|
2011-01-29 23:04:50 +05:30
|
|
|
ndep++;
|
2011-01-30 12:38:34 +05:30
|
|
|
if (od->orphans_user == NULL)
|
|
|
|
continue;
|
|
|
|
|
2011-06-01 13:07:32 +05:30
|
|
|
pkgdepname = xbps_pkg_name(pkgdep);
|
2011-12-22 12:53:11 +05:30
|
|
|
if (pkgdepname == NULL) {
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2011-01-30 12:38:34 +05:30
|
|
|
for (i = 0; i < prop_array_count(od->orphans_user); i++) {
|
|
|
|
prop_array_get_cstring_nocopy(od->orphans_user,
|
|
|
|
i, &curpkgname);
|
|
|
|
if (strcmp(curpkgname, pkgdepname) == 0) {
|
|
|
|
ndep++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 20:43:54 +05:30
|
|
|
free(pkgdepname);
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
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-30 12:38:34 +05:30
|
|
|
if (!prop_array_add(od->array, obj))
|
2011-01-22 21:38:49 +05:30
|
|
|
return EINVAL;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
prop_array_t
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_find_pkg_orphans(struct xbps_handle *xhp, prop_array_t orphans_user)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2011-01-29 23:04:50 +05:30
|
|
|
prop_array_t array = NULL;
|
2011-01-30 12:38:34 +05:30
|
|
|
struct orphan_data od;
|
2009-08-17 22:37:20 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
2011-01-22 21:38:49 +05:30
|
|
|
/*
|
|
|
|
* Prepare an array with all packages previously found.
|
|
|
|
*/
|
2011-01-30 12:38:34 +05:30
|
|
|
if ((od.array = prop_array_create()) == NULL)
|
2011-06-04 17:07:53 +05:30
|
|
|
return NULL;
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
2012-01-20 15:40:52 +05:30
|
|
|
* Find out all orphans by looking at pkgdb and iterating in reverse
|
|
|
|
* order in which packages were installed.
|
2009-08-17 22:37:20 +05:30
|
|
|
*/
|
2011-01-30 12:38:34 +05:30
|
|
|
od.orphans_user = orphans_user;
|
2012-06-14 11:52:11 +05:30
|
|
|
rv = xbps_pkgdb_foreach_reverse_cb(xhp, find_orphan_pkg, &od);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (rv != 0) {
|
2009-10-23 17:33:16 +05:30
|
|
|
errno = rv;
|
2011-01-30 12:38:34 +05:30
|
|
|
prop_object_release(od.array);
|
2011-06-04 17:07:53 +05:30
|
|
|
return NULL;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2011-01-30 12:38:34 +05:30
|
|
|
array = prop_array_copy(od.array);
|
2012-01-21 14:51:01 +05:30
|
|
|
prop_array_make_immutable(array);
|
2011-07-27 20:43:54 +05:30
|
|
|
prop_object_release(od.array);
|
2009-08-17 22:37:20 +05:30
|
|
|
return array;
|
|
|
|
}
|