vp-build/lib/orphans.c
Juan RP 0d8f08e8ea Added support to list and remove orphaned packages.
A new target has been added to xbps-bin: autoremove. It works in
the same way than 'apt-get', but there's no stdin input. By default
it will list you all orphaned packages, and -f flag must be set
to really remove them.

The following is a real example for the git package, which requires
directly perl and curl-libs, and indirectly libidn and openssl.

$ xbps-bin -r ~/testing-xbps remove git
Removing package git-1.6.1.3 ... done.
$

$ xbps-bin -r ~/testing-xbps autoremove
The following packages were installed automatically
(as dependencies) and aren't needed anymore:

	perl-5.10.0 curl-libs-7.19.0 libidn-1.10 openssl-0.9.8j

If you are really sure you don't need them, use -f to confirm.
$

--HG--
extra : convert_revision : 70eebaa3d99be27753b94f580c8ec86330c7c2d3
2009-03-02 02:26:20 +01:00

172 lines
4.4 KiB
C

/*-
* Copyright (c) 2009 Juan Romero Pardines.
* 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 <unistd.h>
#include <xbps_api.h>
struct orphan {
prop_array_t array;
const char *pkgname;
const char *version;
};
static int
find_orphan_pkg(prop_object_t obj, void *arg, bool *loop_done)
{
struct orphan *orphan = arg;
prop_array_t reqby;
bool automatic = false;
(void)loop_done;
reqby = prop_dictionary_get(obj, "requiredby");
if (reqby != NULL && prop_array_count(reqby) > 0)
return 0;
if (!prop_dictionary_get_bool(obj, "automatic-install", &automatic))
return EINVAL;
if (automatic)
if (!prop_array_add(orphan->array, obj))
return EINVAL;
return 0;
}
static int
find_indirect_orphan_pkg(prop_object_t obj, void *arg, bool *loop_done)
{
struct orphan *orphan = arg;
prop_array_t reqby;
size_t len = 0;
char *pkg;
bool automatic = false;
(void)loop_done;
if (!prop_dictionary_get_bool(obj, "automatic-install", &automatic))
return EINVAL;
if (!automatic)
return 0;
reqby = prop_dictionary_get(obj, "requiredby");
if (reqby == NULL || prop_array_count(reqby) != 1)
return 0;
len = strlen(orphan->pkgname) + strlen(orphan->version) + 2;
pkg = malloc(len);
if (pkg == NULL)
return ENOMEM;
(void)snprintf(pkg, len, "%s-%s", orphan->pkgname, orphan->version);
if (xbps_find_string_in_array(reqby, pkg)) {
if (!prop_array_add(orphan->array, obj)) {
free(pkg);
return EINVAL;
}
}
free(pkg);
return 0;
}
prop_array_t
xbps_find_orphan_packages(void)
{
prop_dictionary_t dict;
prop_object_t obj;
prop_object_iterator_t iter;
struct orphan orphan;
char *plist;
int rv = 0;
plist = xbps_append_full_path(true, NULL, XBPS_REGPKGDB);
if (plist == NULL)
return NULL;
dict = prop_dictionary_internalize_from_file(plist);
if (dict == NULL) {
free(plist);
return NULL;
}
free(plist);
orphan.array = prop_array_create();
if (orphan.array == NULL) {
prop_object_release(dict);
return NULL;
}
/*
* First look for direct orphan packages, i.e the ones
* that were required directly by a previous removed package.
*/
rv = xbps_callback_array_iter_in_dict(dict, "packages",
find_orphan_pkg, (void *)&orphan);
if (rv != 0) {
prop_object_release(dict);
prop_object_release(orphan.array);
return NULL;
}
/*
* Now look if any of these packages have dependencies that
* were installed indirectly by some removed package.
*/
iter = prop_array_iterator(orphan.array);
if (iter == NULL) {
prop_object_release(dict);
prop_object_release(orphan.array);
return NULL;
}
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname",
&orphan.pkgname);
prop_dictionary_get_cstring_nocopy(obj, "version",
&orphan.version);
rv = xbps_callback_array_iter_in_dict(dict, "packages",
find_indirect_orphan_pkg, (void *)&orphan);
if (rv != 0) {
prop_object_iterator_release(iter);
prop_object_release(dict);
prop_object_release(orphan.array);
return NULL;
}
}
prop_object_iterator_release(iter);
prop_object_release(dict);
return prop_array_copy(orphan.array);
}