2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2013-03-05 08:38:42 +05:30
|
|
|
* Copyright (c) 2012-2013 Juan Romero Pardines.
|
2012-11-02 19:34:25 +05:30
|
|
|
* 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>
|
2012-12-14 14:17:47 +05:30
|
|
|
#include <pthread.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
#include "defs.h"
|
|
|
|
|
2012-12-14 14:17:47 +05:30
|
|
|
struct thread_data {
|
|
|
|
pthread_t thread;
|
|
|
|
prop_array_t array;
|
2013-06-10 13:58:39 +05:30
|
|
|
struct xbps_repo *repo;
|
2012-12-14 14:17:47 +05:30
|
|
|
unsigned int start;
|
|
|
|
unsigned int end;
|
|
|
|
int thread_num;
|
|
|
|
};
|
|
|
|
|
2012-12-15 15:43:32 +05:30
|
|
|
static int
|
|
|
|
remove_pkg(const char *repodir, const char *arch, const char *file)
|
|
|
|
{
|
|
|
|
char *filepath;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
filepath = xbps_xasprintf("%s/%s/%s", repodir, arch, file);
|
|
|
|
if (remove(filepath) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
rv = errno;
|
2013-01-16 20:55:29 +05:30
|
|
|
fprintf(stderr, "xbps-rindex: failed to remove "
|
|
|
|
"package `%s': %s\n", file,
|
|
|
|
strerror(rv));
|
2012-12-15 15:43:32 +05:30
|
|
|
free(filepath);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(filepath);
|
|
|
|
|
|
|
|
filepath = xbps_xasprintf("%s/%s", repodir, file);
|
|
|
|
if (remove(filepath) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
rv = errno;
|
2013-01-16 20:55:29 +05:30
|
|
|
fprintf(stderr, "xbps-rindex: failed to remove "
|
|
|
|
"package `%s': %s\n", file,
|
|
|
|
strerror(rv));
|
2012-12-15 15:43:32 +05:30
|
|
|
free(filepath);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(filepath);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-14 14:17:47 +05:30
|
|
|
static void *
|
|
|
|
cleaner_thread(void *arg)
|
|
|
|
{
|
|
|
|
prop_dictionary_t pkgd;
|
|
|
|
struct thread_data *thd = arg;
|
|
|
|
const char *binpkg, *pkgver, *arch;
|
|
|
|
unsigned int i;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
/* process pkgs from start until end */
|
|
|
|
for (i = thd->start; i < thd->end; i++) {
|
|
|
|
prop_array_get_cstring_nocopy(thd->array, i, &binpkg);
|
|
|
|
pkgd = xbps_get_pkg_plist_from_binpkg(binpkg, "./props.plist");
|
|
|
|
if (pkgd == NULL) {
|
2013-06-10 13:58:39 +05:30
|
|
|
rv = remove_pkg(thd->repo->uri, arch, binpkg);
|
2012-12-14 14:17:47 +05:30
|
|
|
if (rv != 0) {
|
|
|
|
prop_object_release(pkgd);
|
2013-01-16 20:55:29 +05:30
|
|
|
continue;
|
2012-12-14 14:17:47 +05:30
|
|
|
}
|
|
|
|
printf("Removed broken package `%s'.\n", binpkg);
|
|
|
|
}
|
|
|
|
prop_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
|
|
|
|
prop_dictionary_get_cstring_nocopy(pkgd, "architecture", &arch);
|
|
|
|
/* ignore pkgs from other archs */
|
2013-06-10 13:58:39 +05:30
|
|
|
if (!xbps_pkg_arch_match(thd->repo->xhp, arch, NULL)) {
|
2012-12-14 14:17:47 +05:30
|
|
|
prop_object_release(pkgd);
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
xbps_dbg_printf(thd->repo->xhp, "thread[%d] checking %s (%s)\n",
|
2012-12-14 14:17:47 +05:30
|
|
|
thd->thread_num, pkgver, binpkg);
|
|
|
|
/*
|
|
|
|
* If binpkg is not registered in index, remove binpkg.
|
|
|
|
*/
|
2013-06-10 13:58:39 +05:30
|
|
|
if (!xbps_repo_get_pkg(thd->repo, pkgver)) {
|
|
|
|
rv = remove_pkg(thd->repo->uri, arch, binpkg);
|
2012-12-14 14:17:47 +05:30
|
|
|
if (rv != 0) {
|
|
|
|
prop_object_release(pkgd);
|
2013-01-16 20:55:29 +05:30
|
|
|
continue;
|
2012-12-14 14:17:47 +05:30
|
|
|
}
|
|
|
|
printf("Removed obsolete package `%s'.\n", binpkg);
|
|
|
|
}
|
|
|
|
prop_object_release(pkgd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
int
|
|
|
|
remove_obsoletes(struct xbps_handle *xhp, const char *repodir)
|
|
|
|
{
|
2012-12-14 14:17:47 +05:30
|
|
|
prop_array_t array = NULL;
|
2013-06-10 13:58:39 +05:30
|
|
|
struct xbps_repo *repo;
|
2012-12-14 14:17:47 +05:30
|
|
|
struct thread_data *thd;
|
2012-11-02 19:34:25 +05:30
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
2013-06-10 13:58:39 +05:30
|
|
|
char *ext;
|
2012-12-14 14:17:47 +05:30
|
|
|
int i, maxthreads, rv = 0;
|
|
|
|
size_t slicecount, pkgcount;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
repo = xbps_repo_open(xhp, repodir);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if ((repo->idx = xbps_repo_get_plist(repo, XBPS_PKGINDEX)) == NULL) {
|
|
|
|
xbps_repo_close(repo);
|
|
|
|
return -1;
|
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));
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
array = prop_array_create();
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2012-12-14 14:17:47 +05:30
|
|
|
prop_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
|
|
|
|
|
|
|
maxthreads = (int)sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
thd = calloc(maxthreads, sizeof(*thd));
|
|
|
|
|
|
|
|
slicecount = prop_array_count(array) / maxthreads;
|
|
|
|
pkgcount = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < maxthreads; i++) {
|
|
|
|
thd[i].thread_num = i;
|
|
|
|
thd[i].array = array;
|
2013-06-10 13:58:39 +05:30
|
|
|
thd[i].repo = repo;
|
2012-12-14 14:17:47 +05:30
|
|
|
thd[i].start = pkgcount;
|
|
|
|
if (i + 1 >= maxthreads)
|
|
|
|
thd[i].end = prop_array_count(array);
|
|
|
|
else
|
|
|
|
thd[i].end = pkgcount + slicecount;
|
|
|
|
pthread_create(&thd[i].thread, NULL, cleaner_thread, &thd[i]);
|
|
|
|
pkgcount += slicecount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for all threads */
|
|
|
|
for (i = 0; i < maxthreads; i++)
|
|
|
|
pthread_join(thd[i].thread, NULL);
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
xbps_repo_close(repo);
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
return rv;
|
|
|
|
}
|