2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2020-04-29 17:42:10 +05:30
|
|
|
* Copyright (c) 2012-2015 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.
|
2012-11-02 19:34:25 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
2013-06-20 16:01:02 +05:30
|
|
|
#include <xbps.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
#include "defs.h"
|
|
|
|
|
2012-12-15 15:43:32 +05:30
|
|
|
static int
|
2013-08-29 14:15:30 +05:30
|
|
|
remove_pkg(const char *repodir, const char *file)
|
2012-12-15 15:43:32 +05:30
|
|
|
{
|
2013-12-24 16:32:33 +05:30
|
|
|
char *filepath, *sigpath;
|
|
|
|
int rv = 0;
|
2012-12-15 15:43:32 +05:30
|
|
|
|
|
|
|
filepath = xbps_xasprintf("%s/%s", repodir, file);
|
2013-12-24 16:32:33 +05:30
|
|
|
sigpath = xbps_xasprintf("%s.sig", filepath);
|
2012-12-15 15:43:32 +05:30
|
|
|
if (remove(filepath) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
rv = errno;
|
2013-01-16 20:55:29 +05:30
|
|
|
fprintf(stderr, "xbps-rindex: failed to remove "
|
2013-12-24 16:32:33 +05:30
|
|
|
"package `%s': %s\n", file, strerror(rv));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (remove(sigpath) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
rv = errno;
|
|
|
|
fprintf(stderr, "xbps-rindex: failed to remove "
|
|
|
|
"package signature `%s': %s\n", sigpath, strerror(rv));
|
2012-12-15 15:43:32 +05:30
|
|
|
}
|
|
|
|
}
|
2013-12-24 16:32:33 +05:30
|
|
|
free(sigpath);
|
2012-12-15 15:43:32 +05:30
|
|
|
free(filepath);
|
|
|
|
|
2013-12-24 16:32:33 +05:30
|
|
|
return rv;
|
2012-12-15 15:43:32 +05:30
|
|
|
}
|
|
|
|
|
2013-07-27 13:17:16 +05:30
|
|
|
static int
|
2018-07-18 07:54:26 +05:30
|
|
|
cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj, const char *key UNUSED, void *arg, bool *done UNUSED)
|
2012-12-14 14:17:47 +05:30
|
|
|
{
|
2016-03-21 15:20:25 +05:30
|
|
|
struct xbps_repo *repo = ((struct xbps_repo **)arg)[0], *stage = ((struct xbps_repo **)arg)[1];
|
2014-09-06 00:37:07 +05:30
|
|
|
const char *binpkg;
|
2014-10-05 11:04:15 +05:30
|
|
|
char *pkgver, *arch = NULL;
|
2012-12-14 14:17:47 +05:30
|
|
|
int rv;
|
|
|
|
|
2013-07-27 13:17:16 +05:30
|
|
|
binpkg = xbps_string_cstring_nocopy(obj);
|
2014-09-06 00:37:07 +05:30
|
|
|
if (access(binpkg, R_OK) == -1) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
if ((rv = remove_pkg(repo->uri, binpkg)) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
printf("Removed broken package `%s'.\n", binpkg);
|
2012-12-14 14:17:47 +05:30
|
|
|
}
|
2013-07-27 13:17:16 +05:30
|
|
|
}
|
2014-09-06 00:37:07 +05:30
|
|
|
arch = xbps_binpkg_arch(binpkg);
|
|
|
|
assert(arch);
|
2013-07-27 13:17:16 +05:30
|
|
|
/* ignore pkgs from other archs */
|
|
|
|
if (!xbps_pkg_arch_match(xhp, arch, NULL)) {
|
2014-09-06 00:37:07 +05:30
|
|
|
free(arch);
|
2013-07-27 13:17:16 +05:30
|
|
|
return 0;
|
2012-12-14 14:17:47 +05:30
|
|
|
}
|
2014-10-05 11:04:15 +05:30
|
|
|
free(arch);
|
2014-09-06 00:37:07 +05:30
|
|
|
|
|
|
|
pkgver = xbps_binpkg_pkgver(binpkg);
|
|
|
|
assert(pkgver);
|
2013-10-10 14:13:01 +05:30
|
|
|
if (xhp->flags & XBPS_FLAG_VERBOSE)
|
|
|
|
printf("checking %s (%s)\n", pkgver, binpkg);
|
2013-07-27 13:17:16 +05:30
|
|
|
/*
|
|
|
|
* If binpkg is not registered in index, remove binpkg.
|
|
|
|
*/
|
2016-03-21 15:20:25 +05:30
|
|
|
if (!xbps_repo_get_pkg(repo, pkgver) && !(stage && xbps_repo_get_pkg(stage, pkgver))) {
|
2014-09-06 00:37:07 +05:30
|
|
|
if ((rv = remove_pkg(repo->uri, binpkg)) != 0) {
|
|
|
|
free(pkgver);
|
2013-07-27 13:17:16 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
printf("Removed obsolete package `%s'.\n", binpkg);
|
|
|
|
}
|
2014-09-06 00:37:07 +05:30
|
|
|
free(pkgver);
|
2013-07-27 13:17:16 +05:30
|
|
|
return 0;
|
2012-12-14 14:17:47 +05:30
|
|
|
}
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
int
|
|
|
|
remove_obsoletes(struct xbps_handle *xhp, const char *repodir)
|
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t array = NULL;
|
2016-03-21 15:20:25 +05:30
|
|
|
struct xbps_repo *repos[2], *repo, *stage;
|
2012-11-02 19:34:25 +05:30
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
2013-06-10 13:58:39 +05:30
|
|
|
char *ext;
|
2013-07-27 13:17:16 +05:30
|
|
|
int rv = 0;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2016-03-21 15:20:25 +05:30
|
|
|
repo = xbps_repo_public_open(xhp, repodir);
|
2013-06-10 13:58:39 +05:30
|
|
|
if (repo == NULL) {
|
2012-11-02 19:34:25 +05:30
|
|
|
if (errno != ENOENT) {
|
2013-06-10 13:58:39 +05:30
|
|
|
fprintf(stderr, "xbps-rindex: cannot read repository data: %s\n",
|
|
|
|
strerror(errno));
|
2012-11-02 19:34:25 +05:30
|
|
|
return -1;
|
2013-06-10 13:58:39 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-21 15:20:25 +05:30
|
|
|
stage = xbps_repo_stage_open(xhp, repodir);
|
2012-11-02 19:34:25 +05:30
|
|
|
if (chdir(repodir) == -1) {
|
2012-11-30 11:41:51 +05:30
|
|
|
fprintf(stderr, "xbps-rindex: cannot chdir to %s: %s\n",
|
2012-11-02 19:34:25 +05:30
|
|
|
repodir, strerror(errno));
|
2020-04-20 14:04:08 +05:30
|
|
|
rv = errno;
|
|
|
|
goto out;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
if ((dirp = opendir(repodir)) == NULL) {
|
2012-11-30 11:41:51 +05:30
|
|
|
fprintf(stderr, "xbps-rindex: failed to open %s: %s\n",
|
2012-11-02 19:34:25 +05:30
|
|
|
repodir, strerror(errno));
|
2020-04-20 14:04:08 +05:30
|
|
|
rv = errno;
|
|
|
|
goto out;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
while ((dp = readdir(dirp))) {
|
|
|
|
if (strcmp(dp->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if ((ext = strrchr(dp->d_name, '.')) == NULL)
|
|
|
|
continue;
|
|
|
|
if (strcmp(ext, ".xbps"))
|
|
|
|
continue;
|
2012-12-14 14:17:47 +05:30
|
|
|
if (array == NULL)
|
2013-06-20 13:56:12 +05:30
|
|
|
array = xbps_array_create();
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-08-07 19:31:59 +05:30
|
|
|
xbps_array_add_cstring(array, dp->d_name);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
(void)closedir(dirp);
|
2012-12-14 14:17:47 +05:30
|
|
|
|
2016-03-21 15:20:25 +05:30
|
|
|
repos[0] = repo;
|
|
|
|
repos[1] = stage;
|
|
|
|
rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, repos);
|
2020-04-20 14:04:08 +05:30
|
|
|
out:
|
2020-04-19 14:39:05 +05:30
|
|
|
xbps_repo_release(repo);
|
2020-04-20 14:04:08 +05:30
|
|
|
xbps_repo_release(stage);
|
2013-07-27 13:17:16 +05:30
|
|
|
xbps_object_release(array);
|
2013-06-10 13:58:39 +05:30
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
return rv;
|
|
|
|
}
|