From 616d4420d238e1bd1fdb7dbd8eb7a906cd736485 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Sat, 2 Nov 2013 11:34:55 +0100 Subject: [PATCH] xbps-rindex(8): fix #19. An ATF test-case will be imported later. --- NEWS | 5 +- bin/xbps-rindex/Makefile | 2 +- bin/xbps-rindex/defs.h | 3 + bin/xbps-rindex/index-clean.c | 143 ++++++++++++++++++++++++++++++++++ bin/xbps-rindex/main.c | 24 ++++-- bin/xbps-rindex/xbps-rindex.8 | 4 +- 6 files changed, 168 insertions(+), 13 deletions(-) create mode 100644 bin/xbps-rindex/index-clean.c diff --git a/NEWS b/NEWS index 26e86a4d..576f8d22 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,7 @@ xbps-0.27 (???): + * Fixed issue #19: https://github.com/xtraeme/xbps/issues/19 + * Fixed issue #18: https://github.com/xtraeme/xbps/issues/18 * xbps-rindex(8): also understands the XBPS_PASSPHRASE environmental variable to @@ -7,9 +9,6 @@ xbps-0.27 (???): * xbps-rindex(8): added -v --verbose option. - * xbps-rindex(8): -c --clean mode has been removed. Generating a local repository - is almost as fast as cleaning up the repository data. - * xbps-rkeys(8): new utility to manage RSA public keys from remote signed repositories. * Support for RSA signed repositories. A repository can be signed with your diff --git a/bin/xbps-rindex/Makefile b/bin/xbps-rindex/Makefile index cf876649..2f17c90a 100644 --- a/bin/xbps-rindex/Makefile +++ b/bin/xbps-rindex/Makefile @@ -2,6 +2,6 @@ TOPDIR = ../.. -include $(TOPDIR)/config.mk BIN = xbps-rindex -OBJS = main.o index-add.o remove-obsoletes.o repoflush.o sign.o +OBJS = main.o index-add.o index-clean.o remove-obsoletes.o repoflush.o sign.o include $(TOPDIR)/mk/prog.mk diff --git a/bin/xbps-rindex/defs.h b/bin/xbps-rindex/defs.h index e9eb2c32..ade5440c 100644 --- a/bin/xbps-rindex/defs.h +++ b/bin/xbps-rindex/defs.h @@ -67,6 +67,9 @@ /* From index-add.c */ int index_add(struct xbps_handle *, int, char **, bool); +/* From index-clean.c */ +int index_clean(struct xbps_handle *, const char *); + /* From remove-obsoletes.c */ int remove_obsoletes(struct xbps_handle *, const char *); diff --git a/bin/xbps-rindex/index-clean.c b/bin/xbps-rindex/index-clean.c new file mode 100644 index 00000000..047c1dfc --- /dev/null +++ b/bin/xbps-rindex/index-clean.c @@ -0,0 +1,143 @@ +/*- + * Copyright (c) 2012-2013 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "defs.h" + +static int +idx_cleaner_cb(struct xbps_handle *xhp, + xbps_object_t obj, + const char *key _unused, + void *arg, + bool *done _unused) +{ + + xbps_array_t result = arg; + const char *arch, *pkgver, *sha256, *repoloc; + char *filen; + + xbps_dictionary_get_cstring_nocopy(obj, "architecture", &arch); + xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); + xbps_dictionary_get_cstring_nocopy(obj, "repository", &repoloc); + + xbps_dbg_printf(xhp, "%s: checking %s [%s] ...", pkgver, arch); + + filen = xbps_xasprintf("%s/%s.%s.xbps", repoloc, pkgver, arch); + if (access(filen, R_OK) == -1) { + /* + * File cannot be read, might be permissions, + * broken or simply unexistent; either way, remove it. + */ + xbps_array_add_cstring_nocopy(result, pkgver); + } else { + /* + * File can be read; check its hash. + */ + xbps_dictionary_get_cstring_nocopy(obj, + "filename-sha256", &sha256); + if (xbps_file_hash_check(filen, sha256) != 0) + xbps_array_add_cstring_nocopy(result, pkgver); + } + free(filen); + return 0; +} + +/* + * Removes stalled pkg entries in repository's XBPS_REPOIDX file, if any + * binary package cannot be read (unavailable, not enough perms, etc). + */ +int +index_clean(struct xbps_handle *xhp, const char *repodir) +{ + struct xbps_repo *repo; + xbps_array_t result = NULL, allkeys; + xbps_dictionary_t idx, idxfiles; + const char *keyname; + char *pkgname; + int rv = 0; + bool flush = false; + + repo = xbps_repo_open(xhp, repodir); + if (repo == NULL) { + if (errno == ENOENT) + return 0; + fprintf(stderr, "index: cannot read repository data: %s\n", strerror(errno)); + return -1; + } + xbps_repo_open_idxfiles(repo); + idx = xbps_dictionary_copy(repo->idx); + idxfiles = xbps_dictionary_copy(repo->idxfiles); + xbps_repo_close(repo); + if (idx == NULL || idxfiles == NULL) { + fprintf(stderr, "incomplete repository data file!"); + return -1; + } + printf("Cleaning `%s' index, please wait...\n", repodir); + + result = xbps_array_create(); + allkeys = xbps_dictionary_all_keys(idx); + rv = xbps_array_foreach_cb(xhp, allkeys, idx, idx_cleaner_cb, result); + + for (unsigned int x = 0; x < xbps_array_count(result); x++) { + xbps_array_get_cstring_nocopy(result, x, &keyname); + printf("index: removed entry %s\n", keyname); + pkgname = xbps_pkg_name(keyname); + xbps_dictionary_remove(idx, pkgname); + xbps_dictionary_remove(idxfiles, keyname); + free(pkgname); + flush = true; + } + xbps_object_release(result); + xbps_object_release(allkeys); + + if (flush) { + if (!repodata_flush(xhp, repodir, idx, idxfiles, NULL)) { + fprintf(stderr, "failed to write repodata: %s\n", + strerror(errno)); + return -1; + } + } + printf("index: %u packages registered.\n", + xbps_dictionary_count(idx)); + printf("index-files: %u packages registered.\n", + xbps_dictionary_count(idxfiles)); + + xbps_object_release(idx); + xbps_object_release(idxfiles); + + return rv; +} diff --git a/bin/xbps-rindex/main.c b/bin/xbps-rindex/main.c index 516f3abd..3998560c 100644 --- a/bin/xbps-rindex/main.c +++ b/bin/xbps-rindex/main.c @@ -45,6 +45,7 @@ usage(bool fail) " --signedby Signature details, i.e \"name \"\n\n" "MODE\n" " -a --add ... Add package(s) to repository index\n" + " -c --clean Clean repository index\n" " -r --remove-obsoletes Removes obsolete packages from repository\n" " -s --sign Sign repository index\n\n"); exit(fail ? EXIT_FAILURE : EXIT_SUCCESS); @@ -53,9 +54,10 @@ usage(bool fail) int main(int argc, char **argv) { - const char *shortopts = "afhrVv"; + const char *shortopts = "acfhrVv"; struct option longopts[] = { { "add", no_argument, NULL, 'a' }, + { "clean", no_argument, NULL, 'c' }, { "force", no_argument, NULL, 'f' }, { "help", no_argument, NULL, 'h' }, { "remove-obsoletes", no_argument, NULL, 'r' }, @@ -69,9 +71,9 @@ main(int argc, char **argv) struct xbps_handle xh; const char *privkey = NULL, *signedby = NULL; int rv, c, flags = 0; - bool add_mode, rm_mode, sign_mode, force; + bool add_mode, clean_mode, rm_mode, sign_mode, force; - add_mode = rm_mode = sign_mode = force = false; + add_mode = clean_mode = rm_mode = sign_mode = force = false; while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { switch (c) { @@ -84,6 +86,9 @@ main(int argc, char **argv) case 'a': add_mode = true; break; + case 'c': + clean_mode = true; + break; case 'f': force = true; break; @@ -104,12 +109,13 @@ main(int argc, char **argv) exit(EXIT_SUCCESS); } } - if ((argc == optind) || (!add_mode && !rm_mode && !sign_mode)) { + if ((argc == optind) || (!add_mode && !clean_mode && !rm_mode && !sign_mode)) { usage(true); - } else if ((add_mode && (rm_mode || sign_mode)) || - (rm_mode && (add_mode || sign_mode)) || - (sign_mode && (add_mode || rm_mode))) { - fprintf(stderr, "Only one mode can be specified: add, " + } else if ((add_mode && (clean_mode || rm_mode || sign_mode)) || + (clean_mode && (add_mode || rm_mode || sign_mode)) || + (rm_mode && (add_mode || clean_mode || sign_mode)) || + (sign_mode && (add_mode || clean_mode || rm_mode))) { + fprintf(stderr, "Only one mode can be specified: add, clean, " "remove-obsoletes or sign.\n"); exit(EXIT_FAILURE); } @@ -125,6 +131,8 @@ main(int argc, char **argv) if (add_mode) rv = index_add(&xh, argc - optind, argv + optind, force); + else if (clean_mode) + rv = index_clean(&xh, argv[optind]); else if (rm_mode) rv = remove_obsoletes(&xh, argv[optind]); else if (sign_mode) diff --git a/bin/xbps-rindex/xbps-rindex.8 b/bin/xbps-rindex/xbps-rindex.8 index 2629d8b9..74da83cf 100644 --- a/bin/xbps-rindex/xbps-rindex.8 +++ b/bin/xbps-rindex/xbps-rindex.8 @@ -1,4 +1,4 @@ -.Dd October 12, 2013 +.Dd November 2, 2013 .Os Void Linux .Dt xbps-rindex 8 .Sh NAME @@ -37,6 +37,8 @@ Path to the private RSA key to sign the repository. If unset, defaults to Registers the binary package into the local repository. The specified binary package is only added to the index if its version is greater than the one currently stored. Multiple binary packages can be specified as arguments. +.It Sy -c, --clean Ar repository +Removes obsolete entries found in the local repository. .It Sy -r, --remove-obsoletes Ar repository Removes obsolete packages from .Ar repository .