From 172c84040c6e79834636f99a0411125f1429f614 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Mon, 25 Nov 2013 10:16:38 +0100 Subject: [PATCH] xbps-rindex: fix clean mode while removing obsolete entries on index-files with updates. --- bin/xbps-rindex/index-clean.c | 46 ++++++++++++++++++++++-- tests/xbps/xbps-rindex/clean_test.sh | 53 ++++++++++++++++++---------- 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/bin/xbps-rindex/index-clean.c b/bin/xbps-rindex/index-clean.c index 047bd073..37a85e77 100644 --- a/bin/xbps-rindex/index-clean.c +++ b/bin/xbps-rindex/index-clean.c @@ -40,6 +40,7 @@ struct cbdata { xbps_array_t result; + xbps_dictionary_t idx; const char *repourl; }; @@ -80,6 +81,32 @@ idx_cleaner_cb(struct xbps_handle *xhp, 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 * 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); + /* + * First pass: find out obsolete entries on index and index-files. + */ cbd.repourl = repodir; cbd.result = xbps_array_create(); 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++) { 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); pkgname = xbps_pkg_name(keyname); xbps_dictionary_remove(idx, pkgname); + printf("index: removed entry %s\n", pkgname); free(pkgname); flush = true; } diff --git a/tests/xbps/xbps-rindex/clean_test.sh b/tests/xbps/xbps-rindex/clean_test.sh index e4235387..303ff989 100644 --- a/tests/xbps/xbps-rindex/clean_test.sh +++ b/tests/xbps/xbps-rindex/clean_test.sh @@ -1,12 +1,8 @@ #! /usr/bin/env atf-sh # 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. -atf_test_case noremove cleanup +atf_test_case noremove noremove_head() { atf_set "descr" "xbps-rindex(8) -c: dont removing anything test" @@ -26,12 +22,8 @@ noremove_body() { atf_check_equal ${result} 1 } -noremove_cleanup() { - common_cleanup -} - # 2nd test: make sure that entries are also removed from index-files. -atf_test_case filesclean cleanup +atf_test_case filesclean filesclean_head() { atf_set "descr" "xbps-rindex(8) -c: index-files clean test" @@ -54,18 +46,46 @@ filesclean_body() { atf_check_equal $? 0 } -filesclean_cleanup() { - common_cleanup +# 3nd test: make sure that entries are removed from index-files on updates. +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: # Generate pkg foo-1.0_1. # Add it to the index of a local repo. # Remove the pkg file from the repo. # Run xbps-rindex -c on the repo. -atf_test_case issue19 cleanup +atf_test_case issue19 issue19_head() { atf_set "descr" "xbps issue #19 (https://github.com/xtraeme/xbps/issues/19)" @@ -87,12 +107,9 @@ issue19_body() { atf_check_equal $? 0 } -issue19_cleanup() { - common_cleanup -} - atf_init_test_cases() { atf_add_test_case noremove atf_add_test_case filesclean + atf_add_test_case filesclean2 atf_add_test_case issue19 }