Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xbps-rindex: remove mode #76

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/xbps-rindex/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ TOPDIR = ../..
-include $(TOPDIR)/config.mk

BIN = xbps-rindex
OBJS = main.o index-add.o index-clean.o remove-obsoletes.o repoflush.o sign.o
OBJS = main.o index-add.o index-clean.o index-remove.o remove-obsoletes.o repoflush.o sign.o

include $(TOPDIR)/mk/prog.mk

Expand Down
3 changes: 3 additions & 0 deletions bin/xbps-rindex/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ int index_clean(struct xbps_handle *, const char *, bool, const char *);
/* From remove-obsoletes.c */
int remove_obsoletes(struct xbps_handle *, const char *);

/* From index-remove.c */
int index_remove(struct xbps_handle *, int, int, char **, const char *);

/* From sign.c */
int sign_repo(struct xbps_handle *, const char *, const char *,
const char *, const char *);
Expand Down
157 changes: 157 additions & 0 deletions bin/xbps-rindex/index-remove.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*-
* Copyright (c) 2012-2015 Juan Romero Pardines.
* Copyright (c) 2019-2020 Piotr Wójcik
* 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 <sys/stat.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <libgen.h>
#include <assert.h>
#include <fcntl.h>

#include <xbps.h>
#include "defs.h"

static xbps_dictionary_t idx;

int
index_remove(struct xbps_handle *xhp, int args, int argmax, char **argv, const char *compression)
{
struct xbps_repo *repo = NULL;
char *tmprepodir = NULL;
const char *repodir = NULL;
char *rlockfname = NULL;
int rv = 0, rlockfd = -1;

assert(argv);

if ((tmprepodir = strdup(argv[args])) == NULL)
return ENOMEM;
repodir = dirname(tmprepodir);
if (!xbps_repo_lock(xhp, repodir, &rlockfd, &rlockfname)) {
rv = errno;
fprintf(stderr, "%s: cannot lock repository: %s\n",
_XBPS_RINDEX, strerror(rv));
goto earlyout;
}
repo = xbps_repo_public_open(xhp, repodir);
if (repo == NULL) {
rv = errno;
fprintf(stderr, "%s: cannot read repository %s data: %s\n",
_XBPS_RINDEX, repodir, strerror(errno));
goto earlyout;
}
if (repo->idx == NULL) {
fprintf(stderr, "%s: incomplete repository data file!\n", _XBPS_RINDEX);
rv = EINVAL;
goto earlyout;
}
idx = xbps_dictionary_copy_mutable(repo->idx);

for (int i = args; i < argmax; i++) {
xbps_dictionary_t curpkgd = NULL;
const char *pkg = argv[i], *opkgver = NULL;
char *pkgver = NULL, *arch = NULL;
char pkgname[XBPS_NAME_SIZE];

/*
* Take package properties from passed path.
*/
assert(pkg);
pkgver = xbps_binpkg_pkgver(pkg);
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
fprintf(stderr, "%s: argument %s doesn't look like path to binary package\n", _XBPS_RINDEX, pkg);
free(pkgver);
goto out;
}
arch = xbps_binpkg_arch(pkg);
if (!xbps_pkg_arch_match(xhp, arch, NULL)) {
fprintf(stderr, "%s: ignoring %s, unmatched arch (%s)\n", _XBPS_RINDEX, pkgver, arch);
goto again;
}

/*
* Check if this package exists already in the index
*/
curpkgd = xbps_dictionary_get(idx, pkgname);
if (!curpkgd) {
if (errno == ENOENT) {
xbps_dbg_printf(xhp, "Package %s isn't indexed in %s, skipping.\n",
pkgname, repodir);
goto again;
}
rv = errno;
fprintf(stderr, "%s: cannot read metadata of %s from repodata\n", _XBPS_RINDEX, pkgname);
free(arch);
free(pkgver);
goto out;
}


/*
* Unindex.
*/
xbps_dictionary_get_cstring_nocopy(curpkgd, "pkgver", &opkgver);
if (opkgver) {
printf("index: unindexing %s\n", opkgver);
} else {
printf("index: unindexing some version of %s\n", pkgname);
}
xbps_dictionary_remove(idx, pkgname);

again:
free(arch);
free(pkgver);
}

/*
* Generate repository data files.
*/
if (!xbps_dictionary_equals(idx, repo->idx)) {
if (!repodata_flush(xhp, repodir, "repodata", idx, repo->idxmeta, compression)) {
rv = errno;
fprintf(stderr, "%s: failed to write repodata: %s\n",
_XBPS_RINDEX, strerror(errno));
goto out;
}
}
printf("index: %u packages in index.\n", xbps_dictionary_count(idx));


out:
xbps_object_release(idx);

earlyout:
if (repo)
xbps_repo_close(repo);
xbps_repo_unlock(rlockfd, rlockfname);
free(tmprepodir);

return rv;
}
40 changes: 24 additions & 16 deletions bin/xbps-rindex/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ usage(bool fail)
"MODE\n"
" -a, --add <repodir/file.xbps> ... Add package(s) to repository index\n"
" -c, --clean <repodir> Clean repository index\n"
" -R --remove <pkg> ... Removes package(s) from repository\n"
" -r, --remove-obsoletes <repodir> Removes obsolete packages from repository\n"
" -s, --sign <repodir> Initialize repository metadata signature\n"
" -S, --sign-pkg <file.xbps> ... Sign binary package archive\n");
Expand All @@ -58,13 +59,14 @@ usage(bool fail)
int
main(int argc, char **argv)
{
const char *shortopts = "acdfhrsCSVv";
const char *shortopts = "acdfhRrsCSVv";
struct option longopts[] = {
{ "add", no_argument, NULL, 'a' },
{ "clean", no_argument, NULL, 'c' },
{ "debug", no_argument, NULL, 'd' },
{ "force", no_argument, NULL, 'f' },
{ "help", no_argument, NULL, 'h' },
{ "remove", no_argument, NULL, 'R' },
{ "remove-obsoletes", no_argument, NULL, 'r' },
{ "version", no_argument, NULL, 'V' },
{ "verbose", no_argument, NULL, 'v' },
Expand All @@ -79,12 +81,12 @@ main(int argc, char **argv)
struct xbps_handle xh;
const char *compression = NULL;
const char *privkey = NULL, *signedby = NULL;
int rv, c, flags = 0;
bool add_mode, clean_mode, rm_mode, sign_mode, sign_pkg_mode, force,
hashcheck;
int rv, c, flags = 0, modes_count = 0;
bool add_mode, clean_mode, obsoletes_mode, remove_mode, sign_mode, sign_pkg_mode,
force, hashcheck;

add_mode = clean_mode = rm_mode = sign_mode = sign_pkg_mode = force =
hashcheck = false;
add_mode = clean_mode = obsoletes_mode = remove_mode = sign_mode = sign_pkg_mode =
force = hashcheck = false;

while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch (c) {
Expand All @@ -99,9 +101,11 @@ main(int argc, char **argv)
break;
case 'a':
add_mode = true;
modes_count++;
break;
case 'c':
clean_mode = true;
modes_count++;
break;
case 'd':
flags |= XBPS_FLAG_DEBUG;
Expand All @@ -112,17 +116,24 @@ main(int argc, char **argv)
case 'h':
usage(false);
/* NOTREACHED */
case 'R':
remove_mode = true;
modes_count++;
break;
case 'r':
rm_mode = true;
obsoletes_mode = true;
modes_count++;
break;
case 's':
sign_mode = true;
modes_count++;
break;
case 'C':
hashcheck = true;
break;
case 'S':
sign_pkg_mode = true;
modes_count++;
break;
case 'v':
flags |= XBPS_FLAG_VERBOSE;
Expand All @@ -136,17 +147,12 @@ main(int argc, char **argv)
/* NOTREACHED */
}
}
if ((argc == optind) ||
(!add_mode && !clean_mode && !rm_mode && !sign_mode && !sign_pkg_mode)) {
if ((argc == optind) || (modes_count == 0)) {
usage(true);
/* NOTREACHED */
} else if ((add_mode && (clean_mode || rm_mode || sign_mode || sign_pkg_mode)) ||
(clean_mode && (add_mode || rm_mode || sign_mode || sign_pkg_mode)) ||
(rm_mode && (add_mode || clean_mode || sign_mode || sign_pkg_mode)) ||
(sign_mode && (add_mode || clean_mode || rm_mode || sign_pkg_mode)) ||
(sign_pkg_mode && (add_mode || clean_mode || rm_mode || sign_mode))) {
} else if (modes_count > 1) {
fprintf(stderr, "Only one mode can be specified: add, clean, "
"remove-obsoletes, sign or sign-pkg.\n");
"remove, remove-obsoletes, sign or sign-pkg.\n");
exit(EXIT_FAILURE);
}

Expand All @@ -163,8 +169,10 @@ main(int argc, char **argv)
rv = index_add(&xh, optind, argc, argv, force, compression);
else if (clean_mode)
rv = index_clean(&xh, argv[optind], hashcheck, compression);
else if (rm_mode)
else if (obsoletes_mode)
rv = remove_obsoletes(&xh, argv[optind]);
else if (remove_mode)
rv = index_remove(&xh, optind, argc, argv, compression);
else if (sign_mode)
rv = sign_repo(&xh, argv[optind], privkey, signedby, compression);
else if (sign_pkg_mode)
Expand Down
6 changes: 5 additions & 1 deletion bin/xbps-rindex/xbps-rindex.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.Dd February 21, 2020
.Dd April 14, 2020
.Dt XBPS-RINDEX 1
.Sh NAME
.Nm xbps-rindex
Expand Down Expand Up @@ -56,6 +56,10 @@ Absolute path to the local repository is expected.
.It Sy -c, --clean Ar /path/to/repository
Removes obsolete entries found in the local repository.
Absolute path to the local repository is expected.
.It Sy -R, --remove Ar /path/to/repository/binpkg.xbps ...
Removes specified packages from repository.
Absolute paths to existing binpkgs are expected.
Binpkg is not read and does not need to exist on disk.
.It Sy -r, --remove-obsoletes Ar /path/to/repository
Removes obsolete packages from
.Ar repository .
Expand Down
1 change: 1 addition & 0 deletions data/_xbps
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ _xbps_rindex() {
- '(mode)' \
{-a,--add}'[Add package to repository index]' \
{-c,--clean}'[Clean repository index]' \
{-R,--remove}'[Remove packages from repository index]' \
{-r,--remove-obsoletes}'[Removes obsolete packages from repository]' \
{-s,--sign}'[Sign repository index]' \
{-S,--sign-pkg}'[Sign binary package archive]' \
Expand Down
1 change: 1 addition & 0 deletions tests/xbps/xbps-rindex/Kyuafile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ test_suite("xbps-rindex")
atf_test_program{name="add_test"}
atf_test_program{name="clean_test"}
atf_test_program{name="remove_test"}
atf_test_program{name="unindex_test"}
2 changes: 1 addition & 1 deletion tests/xbps/xbps-rindex/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TOPDIR = ../../..
-include $(TOPDIR)/config.mk

TESTSHELL = add_test clean_test remove_test
TESTSHELL = add_test clean_test remove_test unindex_test
TESTSSUBDIR = xbps/xbps-rindex
EXTRA_FILES = Kyuafile

Expand Down
Loading