2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
2019-06-15 21:23:02 +05:30
|
|
|
* Copyright (c) 2009-2019 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):
|
|
|
|
*
|
2013-09-25 14:45:04 +05:30
|
|
|
* @image html images/xbps_pkgdb_dictionary.png
|
2010-01-23 10:59:34 +05:30
|
|
|
*
|
|
|
|
* Legend:
|
2013-09-25 14:45:04 +05:30
|
|
|
* - <b>Salmon filled box</b>: \a pkgdb plist internalized.
|
2010-01-23 10:59:34 +05:30
|
|
|
* - <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
|
|
|
*/
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t
|
2019-06-30 20:03:08 +05:30
|
|
|
xbps_find_pkg_orphans(struct xbps_handle *xhp, xbps_array_t orphans_user)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t rdeps, reqby, array = NULL;
|
|
|
|
xbps_dictionary_t pkgd, deppkgd;
|
|
|
|
xbps_object_t obj;
|
|
|
|
xbps_object_iterator_t iter;
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *curpkgver = NULL, *deppkgver = NULL, *reqbydep = NULL;
|
2009-08-17 22:37:20 +05:30
|
|
|
bool automatic = false;
|
2015-01-10 10:13:35 +05:30
|
|
|
unsigned int i, cnt, reqbycnt;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2013-03-09 14:26:36 +05:30
|
|
|
if (xbps_pkgdb_init(xhp) != 0)
|
|
|
|
return NULL;
|
2013-06-20 13:56:12 +05:30
|
|
|
if ((array = xbps_array_create()) == NULL)
|
2013-03-09 14:26:36 +05:30
|
|
|
return NULL;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2011-01-30 12:38:34 +05:30
|
|
|
/*
|
2013-03-09 14:26:36 +05:30
|
|
|
* Add all packages specified by the client.
|
2011-01-30 12:38:34 +05:30
|
|
|
*/
|
2015-01-10 10:13:35 +05:30
|
|
|
for (i = 0; i < xbps_array_count(orphans_user); i++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get_cstring_nocopy(orphans_user, i, &curpkgver);
|
2013-03-09 14:26:36 +05:30
|
|
|
pkgd = xbps_pkgdb_get_pkg(xhp, curpkgver);
|
2013-03-11 16:43:42 +05:30
|
|
|
if (pkgd == NULL)
|
|
|
|
continue;
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_add(array, pkgd);
|
2011-01-30 12:38:34 +05:30
|
|
|
}
|
2015-05-06 20:51:13 +05:30
|
|
|
if (orphans_user)
|
|
|
|
goto add_orphans;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
iter = xbps_dictionary_iterator(xhp->pkgdb);
|
2013-03-09 14:26:36 +05:30
|
|
|
assert(iter);
|
2011-01-30 12:38:34 +05:30
|
|
|
/*
|
2013-03-09 14:26:36 +05:30
|
|
|
* First pass: track pkgs that were installed manually and
|
|
|
|
* without reverse dependencies.
|
2011-01-30 12:38:34 +05:30
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter))) {
|
|
|
|
pkgd = xbps_dictionary_get_keysym(xhp->pkgdb, obj);
|
2013-03-09 14:26:36 +05:30
|
|
|
/*
|
|
|
|
* Skip packages that were not installed automatically.
|
|
|
|
*/
|
2014-09-11 12:35:57 +05:30
|
|
|
automatic = false;
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_bool(pkgd, "automatic-install", &automatic);
|
2013-03-09 14:26:36 +05:30
|
|
|
if (!automatic)
|
2011-01-30 12:38:34 +05:30
|
|
|
continue;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &curpkgver);
|
2013-03-09 14:26:36 +05:30
|
|
|
reqby = xbps_pkgdb_get_pkg_revdeps(xhp, curpkgver);
|
2013-06-20 13:56:12 +05:30
|
|
|
if (xbps_array_count(reqby) == 0) {
|
2013-03-09 14:26:36 +05:30
|
|
|
/*
|
|
|
|
* Add packages with empty revdeps.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_add(array, pkgd);
|
2013-03-09 14:26:36 +05:30
|
|
|
continue;
|
2011-01-30 12:38:34 +05:30
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
2011-01-22 21:38:49 +05:30
|
|
|
|
2015-05-06 20:51:13 +05:30
|
|
|
add_orphans:
|
2015-01-10 10:13:35 +05:30
|
|
|
for (i = 0; i < xbps_array_count(array); i++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
pkgd = xbps_array_get(array, i);
|
2015-01-10 10:13:35 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &curpkgver);
|
|
|
|
rdeps = xbps_pkgdb_get_pkg_fulldeptree(xhp, curpkgver);
|
2019-06-15 21:23:02 +05:30
|
|
|
if (rdeps == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int x = 0; x < xbps_array_count(rdeps); x++) {
|
2013-03-09 14:26:36 +05:30
|
|
|
cnt = 0;
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get_cstring_nocopy(rdeps, x, &deppkgver);
|
2015-01-10 10:13:35 +05:30
|
|
|
if (xbps_find_pkg_in_array(array, deppkgver, NULL))
|
|
|
|
continue;
|
|
|
|
deppkgd = xbps_pkgdb_get_pkg(xhp, deppkgver);
|
|
|
|
automatic = false;
|
|
|
|
xbps_dictionary_get_bool(deppkgd, "automatic-install", &automatic);
|
|
|
|
if (!automatic)
|
2013-12-08 12:36:28 +05:30
|
|
|
continue;
|
2013-03-09 14:26:36 +05:30
|
|
|
reqby = xbps_pkgdb_get_pkg_revdeps(xhp, deppkgver);
|
|
|
|
if (reqby == NULL)
|
|
|
|
continue;
|
2013-06-20 13:56:12 +05:30
|
|
|
reqbycnt = xbps_array_count(reqby);
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int j = 0; j < reqbycnt; j++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get_cstring_nocopy(reqby, j, &reqbydep);
|
2015-01-10 10:13:35 +05:30
|
|
|
if (xbps_find_pkg_in_array(array, reqbydep, NULL))
|
2013-03-09 14:26:36 +05:30
|
|
|
cnt++;
|
|
|
|
}
|
2015-01-10 10:13:35 +05:30
|
|
|
if (cnt == reqbycnt)
|
|
|
|
xbps_array_add(array, deppkgd);
|
2013-03-09 14:26:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
return array;
|
|
|
|
}
|