xbps-rindex: fix clean mode while removing obsolete entries on index-files with updates.

This commit is contained in:
Juan RP 2013-11-25 10:16:38 +01:00
parent 3cbd3deafe
commit 172c84040c
2 changed files with 79 additions and 20 deletions

View File

@ -40,6 +40,7 @@
struct cbdata { struct cbdata {
xbps_array_t result; xbps_array_t result;
xbps_dictionary_t idx;
const char *repourl; const char *repourl;
}; };
@ -80,6 +81,32 @@ idx_cleaner_cb(struct xbps_handle *xhp,
return 0; return 0;
} }
static int
idxfiles_cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
const char *key _unused, void *arg, bool *done _unused)
{
xbps_dictionary_t pkg;
struct cbdata *cbd = arg;
char *pkgname;
const char *arch, *pkgver, *idxpkgver;
/* Find out entries on index-files that aren't registered on index */
xbps_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
xbps_dbg_printf(xhp, "%s: checking %s [%s] ...", pkgver, arch);
pkgname = xbps_pkg_name(pkgver);
assert(pkgname);
if ((pkg = xbps_dictionary_get(cbd->idx, pkgname))) {
xbps_dictionary_get_cstring_nocopy(pkg, "pkgver", &idxpkgver);
if (strcmp(idxpkgver, pkgver))
xbps_array_add_cstring_nocopy(cbd->result, pkgver);
}
free(pkgname);
return 0;
}
/* /*
* Removes stalled pkg entries in repository's XBPS_REPOIDX file, if any * Removes stalled pkg entries in repository's XBPS_REPOIDX file, if any
* binary package cannot be read (unavailable, not enough perms, etc). * binary package cannot be read (unavailable, not enough perms, etc).
@ -113,17 +140,32 @@ index_clean(struct xbps_handle *xhp, const char *repodir)
} }
printf("Cleaning `%s' index, please wait...\n", repodir); printf("Cleaning `%s' index, please wait...\n", repodir);
/*
* First pass: find out obsolete entries on index and index-files.
*/
cbd.repourl = repodir; cbd.repourl = repodir;
cbd.result = xbps_array_create(); cbd.result = xbps_array_create();
allkeys = xbps_dictionary_all_keys(idx); allkeys = xbps_dictionary_all_keys(idx);
rv = xbps_array_foreach_cb(xhp, allkeys, idx, idx_cleaner_cb, &cbd); rv = xbps_array_foreach_cb_multi(xhp, allkeys, idx, idx_cleaner_cb, &cbd);
/*
* Second pass: find out obsolete entries on index-files.
*/
cbd.idx = idx;
xbps_object_release(allkeys);
allkeys = xbps_dictionary_all_keys(idxfiles);
rv = xbps_array_foreach_cb_multi(xhp, allkeys, idx, idxfiles_cleaner_cb, &cbd);
/*
* Finally remove entries from both dictionaries.
*/
for (unsigned int x = 0; x < xbps_array_count(cbd.result); x++) { for (unsigned int x = 0; x < xbps_array_count(cbd.result); x++) {
xbps_array_get_cstring_nocopy(cbd.result, x, &keyname); xbps_array_get_cstring_nocopy(cbd.result, x, &keyname);
printf("index: removed entry %s\n", keyname); printf("index-files: removed entry %s\n", keyname);
xbps_dictionary_remove(idxfiles, keyname); xbps_dictionary_remove(idxfiles, keyname);
pkgname = xbps_pkg_name(keyname); pkgname = xbps_pkg_name(keyname);
xbps_dictionary_remove(idx, pkgname); xbps_dictionary_remove(idx, pkgname);
printf("index: removed entry %s\n", pkgname);
free(pkgname); free(pkgname);
flush = true; flush = true;
} }

View File

@ -1,12 +1,8 @@
#! /usr/bin/env atf-sh #! /usr/bin/env atf-sh
# Test that xbps-rindex(8) -c (clean mode) works as expected. # Test that xbps-rindex(8) -c (clean mode) works as expected.
common_cleanup() {
rm -rf some_repo
}
# 1st test: make sure that nothing is removed if there are no changes. # 1st test: make sure that nothing is removed if there are no changes.
atf_test_case noremove cleanup atf_test_case noremove
noremove_head() { noremove_head() {
atf_set "descr" "xbps-rindex(8) -c: dont removing anything test" atf_set "descr" "xbps-rindex(8) -c: dont removing anything test"
@ -26,12 +22,8 @@ noremove_body() {
atf_check_equal ${result} 1 atf_check_equal ${result} 1
} }
noremove_cleanup() {
common_cleanup
}
# 2nd test: make sure that entries are also removed from index-files. # 2nd test: make sure that entries are also removed from index-files.
atf_test_case filesclean cleanup atf_test_case filesclean
filesclean_head() { filesclean_head() {
atf_set "descr" "xbps-rindex(8) -c: index-files clean test" atf_set "descr" "xbps-rindex(8) -c: index-files clean test"
@ -54,18 +46,46 @@ filesclean_body() {
atf_check_equal $? 0 atf_check_equal $? 0
} }
filesclean_cleanup() { # 3nd test: make sure that entries are removed from index-files on updates.
common_cleanup atf_test_case filesclean2
filesclean2_head() {
atf_set "descr" "xbps-rindex(8) -c: index-files clean test on updates"
} }
# 3rd test: xbps issue #19. filesclean2_body() {
mkdir -p some_repo pkg_A
touch -f pkg_A/file00
cd some_repo
xbps-create -A noarch -n foo-1.0_1 -s "foo pkg" ../pkg_A
atf_check_equal $? 0
xbps-rindex -a *.xbps
atf_check_equal $? 0
xbps-create -A noarch -n foo-1.1_1 -s "foo pkg" ../pkg_A
atf_check_equal $? 0
xbps-rindex -a *.xbps
atf_check_equal $? 0
cd ..
xbps-rindex -c some_repo
atf_check_equal $? 0
result="$(xbps-query --repository=some_repo -o \*)"
expected="foo-1.1_1: /file00 (some_repo)"
rv=0
if [ "$result" != "$expected" ]; then
rv=1
fi
atf_check_equal $rv 0
}
# 4th test: xbps issue #19.
# How to reproduce it: # How to reproduce it:
# Generate pkg foo-1.0_1. # Generate pkg foo-1.0_1.
# Add it to the index of a local repo. # Add it to the index of a local repo.
# Remove the pkg file from the repo. # Remove the pkg file from the repo.
# Run xbps-rindex -c on the repo. # Run xbps-rindex -c on the repo.
atf_test_case issue19 cleanup atf_test_case issue19
issue19_head() { issue19_head() {
atf_set "descr" "xbps issue #19 (https://github.com/xtraeme/xbps/issues/19)" atf_set "descr" "xbps issue #19 (https://github.com/xtraeme/xbps/issues/19)"
@ -87,12 +107,9 @@ issue19_body() {
atf_check_equal $? 0 atf_check_equal $? 0
} }
issue19_cleanup() {
common_cleanup
}
atf_init_test_cases() { atf_init_test_cases() {
atf_add_test_case noremove atf_add_test_case noremove
atf_add_test_case filesclean atf_add_test_case filesclean
atf_add_test_case filesclean2
atf_add_test_case issue19 atf_add_test_case issue19
} }