2009-11-28 10:13:21 +05:30
|
|
|
/*-
|
2011-01-22 17:10:19 +05:30
|
|
|
* Copyright (c) 2009-2011 Juan Romero Pardines.
|
2009-11-28 10:13:21 +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.
|
|
|
|
*/
|
|
|
|
|
2010-05-18 11:16:27 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2009-11-28 10:13:21 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2010-05-18 11:16:27 +05:30
|
|
|
#include <unistd.h>
|
2010-10-27 03:45:59 +05:30
|
|
|
#include <libgen.h>
|
2009-11-28 10:13:21 +05:30
|
|
|
|
|
|
|
#include <xbps_api.h>
|
2010-11-13 07:48:58 +05:30
|
|
|
#include "xbps_api_impl.h"
|
2009-11-28 10:13:21 +05:30
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
int HIDDEN
|
2009-11-28 10:13:21 +05:30
|
|
|
xbps_remove_obsoletes(prop_dictionary_t oldd, prop_dictionary_t newd)
|
|
|
|
{
|
2011-02-21 22:12:47 +05:30
|
|
|
const struct xbps_handle *xhp;
|
2010-11-19 18:10:13 +05:30
|
|
|
prop_object_iterator_t iter, iter2;
|
|
|
|
prop_object_t obj, obj2;
|
|
|
|
prop_string_t oldstr, newstr;
|
2010-05-18 11:16:27 +05:30
|
|
|
struct stat st;
|
2009-11-28 10:13:21 +05:30
|
|
|
const char *array_str = "files";
|
2010-11-19 18:10:13 +05:30
|
|
|
const char *oldhash;
|
|
|
|
char *file;
|
2011-02-21 22:12:47 +05:30
|
|
|
int rv = 0;
|
2010-11-19 18:10:13 +05:30
|
|
|
bool found, dodirs = false, dolinks = false;
|
2009-11-28 10:13:21 +05:30
|
|
|
|
2011-01-25 08:44:33 +05:30
|
|
|
assert(oldd != NULL);
|
|
|
|
assert(newd != NULL);
|
2011-02-21 22:12:47 +05:30
|
|
|
xhp = xbps_handle_get();
|
2011-01-25 08:44:33 +05:30
|
|
|
|
2009-11-28 10:13:21 +05:30
|
|
|
again:
|
|
|
|
iter = xbps_get_array_iter_from_dict(oldd, array_str);
|
|
|
|
if (iter == NULL)
|
|
|
|
return errno;
|
|
|
|
iter2 = xbps_get_array_iter_from_dict(newd, array_str);
|
|
|
|
if (iter2 == NULL) {
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
/*
|
2010-11-19 18:10:13 +05:30
|
|
|
* Check for obsolete files, i.e files/links/dirs available in
|
2009-11-28 10:13:21 +05:30
|
|
|
* the old package list not found in new package list.
|
|
|
|
*/
|
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
2010-05-18 11:16:27 +05:30
|
|
|
rv = 0;
|
2009-11-28 10:13:21 +05:30
|
|
|
found = false;
|
|
|
|
oldstr = prop_dictionary_get(obj, "file");
|
|
|
|
if (oldstr == NULL) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-05-18 11:16:27 +05:30
|
|
|
file = xbps_xasprintf(".%s",
|
|
|
|
prop_string_cstring_nocopy(oldstr));
|
|
|
|
if (file == NULL) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (strcmp(array_str, "files") == 0) {
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"sha256", &oldhash);
|
|
|
|
rv = xbps_check_file_hash(file, oldhash);
|
|
|
|
if (rv == ENOENT || rv == ERANGE) {
|
|
|
|
/*
|
|
|
|
* Skip unexistent and files that do not
|
|
|
|
* match the hash.
|
|
|
|
*/
|
|
|
|
free(file);
|
2010-05-18 19:03:27 +05:30
|
|
|
rv = 0;
|
2010-05-18 11:16:27 +05:30
|
|
|
continue;
|
|
|
|
}
|
2010-11-19 18:10:13 +05:30
|
|
|
} else if (strcmp(array_str, "links") == 0) {
|
2010-05-18 11:16:27 +05:30
|
|
|
/*
|
|
|
|
* Only remove dangling symlinks.
|
|
|
|
*/
|
|
|
|
if (stat(file, &st) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
free(file);
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
free(file);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-28 10:13:21 +05:30
|
|
|
while ((obj2 = prop_object_iterator_next(iter2))) {
|
|
|
|
newstr = prop_dictionary_get(obj2, "file");
|
|
|
|
if (newstr == NULL) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (prop_string_equals(oldstr, newstr)) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prop_object_iterator_reset(iter2);
|
2010-05-18 11:16:27 +05:30
|
|
|
if (found) {
|
|
|
|
free(file);
|
2009-11-28 10:13:21 +05:30
|
|
|
continue;
|
2010-05-18 11:16:27 +05:30
|
|
|
}
|
2009-11-28 10:13:21 +05:30
|
|
|
/*
|
2010-11-19 18:10:13 +05:30
|
|
|
* Obsolete obj found, remove it.
|
2009-11-28 10:13:21 +05:30
|
|
|
*/
|
2010-05-18 11:16:27 +05:30
|
|
|
if (remove(file) == -1) {
|
2011-02-25 15:21:43 +05:30
|
|
|
if (errno != EEXIST &&
|
|
|
|
errno != ENOTEMPTY &&
|
|
|
|
errno != EBUSY) {
|
|
|
|
xbps_warn_printf("couldn't remove obsole entry "
|
|
|
|
"`%s': %s\n",
|
|
|
|
prop_string_cstring_nocopy(oldstr),
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2010-05-18 11:16:27 +05:30
|
|
|
free(file);
|
2009-11-28 10:13:21 +05:30
|
|
|
continue;
|
|
|
|
}
|
2011-02-25 15:21:43 +05:30
|
|
|
xbps_printf("Removed obsolete entry: %s\n",
|
|
|
|
prop_string_cstring_nocopy(oldstr));
|
2010-12-25 07:07:25 +05:30
|
|
|
free(file);
|
2009-11-28 10:13:21 +05:30
|
|
|
}
|
|
|
|
if (!dolinks) {
|
|
|
|
/*
|
|
|
|
* Now look for obsolete links.
|
|
|
|
*/
|
|
|
|
dolinks = true;
|
|
|
|
array_str = "links";
|
|
|
|
prop_object_iterator_release(iter2);
|
|
|
|
prop_object_iterator_release(iter);
|
2010-04-29 03:00:56 +05:30
|
|
|
iter2 = NULL;
|
2009-11-28 10:13:21 +05:30
|
|
|
goto again;
|
|
|
|
}
|
2010-11-19 18:10:13 +05:30
|
|
|
if (!dodirs) {
|
|
|
|
/*
|
|
|
|
* Look for obsolete dirs.
|
|
|
|
*/
|
|
|
|
dodirs = true;
|
|
|
|
array_str = "dirs";
|
|
|
|
prop_object_iterator_release(iter2);
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
iter2 = NULL;
|
|
|
|
goto again;
|
|
|
|
}
|
2009-11-28 10:13:21 +05:30
|
|
|
|
|
|
|
out:
|
|
|
|
prop_object_iterator_release(iter2);
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
|
|
|
|
return rv;
|
2010-05-18 11:16:27 +05:30
|
|
|
}
|