2013-11-02 16:04:55 +05:30
|
|
|
/*-
|
2014-01-29 21:28:38 +05:30
|
|
|
* Copyright (c) 2012-2014 Juan Romero Pardines.
|
2013-11-02 16:04:55 +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>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <xbps.h>
|
|
|
|
#include "defs.h"
|
|
|
|
|
2013-11-03 14:46:58 +05:30
|
|
|
struct cbdata {
|
|
|
|
xbps_array_t result;
|
2013-11-25 14:46:38 +05:30
|
|
|
xbps_dictionary_t idx;
|
2014-01-29 21:28:38 +05:30
|
|
|
pthread_mutex_t mtx;
|
2013-11-03 14:46:58 +05:30
|
|
|
const char *repourl;
|
|
|
|
};
|
|
|
|
|
2013-11-02 16:04:55 +05:30
|
|
|
static int
|
|
|
|
idx_cleaner_cb(struct xbps_handle *xhp,
|
|
|
|
xbps_object_t obj,
|
|
|
|
const char *key _unused,
|
|
|
|
void *arg,
|
|
|
|
bool *done _unused)
|
|
|
|
{
|
2013-11-03 14:46:58 +05:30
|
|
|
struct cbdata *cbd = arg;
|
|
|
|
const char *arch, *pkgver, *sha256;
|
2013-11-02 16:04:55 +05:30
|
|
|
char *filen;
|
|
|
|
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
2013-11-03 14:46:58 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "repository", &cbd->repourl);
|
2013-11-02 16:04:55 +05:30
|
|
|
|
|
|
|
xbps_dbg_printf(xhp, "%s: checking %s [%s] ...", pkgver, arch);
|
|
|
|
|
2013-11-03 14:46:58 +05:30
|
|
|
filen = xbps_xasprintf("%s/%s.%s.xbps", cbd->repourl, pkgver, arch);
|
2013-11-02 16:04:55 +05:30
|
|
|
if (access(filen, R_OK) == -1) {
|
|
|
|
/*
|
|
|
|
* File cannot be read, might be permissions,
|
|
|
|
* broken or simply unexistent; either way, remove it.
|
|
|
|
*/
|
2013-11-03 14:46:58 +05:30
|
|
|
xbps_array_add_cstring_nocopy(cbd->result, pkgver);
|
2013-11-02 16:04:55 +05:30
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* File can be read; check its hash.
|
|
|
|
*/
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"filename-sha256", &sha256);
|
2014-01-29 21:28:38 +05:30
|
|
|
if (xbps_file_hash_check(filen, sha256) != 0) {
|
|
|
|
pthread_mutex_lock(&cbd->mtx);
|
2013-11-03 14:46:58 +05:30
|
|
|
xbps_array_add_cstring_nocopy(cbd->result, pkgver);
|
2014-01-29 21:28:38 +05:30
|
|
|
pthread_mutex_unlock(&cbd->mtx);
|
|
|
|
}
|
2013-11-02 16:04:55 +05:30
|
|
|
}
|
|
|
|
free(filen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-25 14:46:38 +05:30
|
|
|
static int
|
2013-11-25 15:15:05 +05:30
|
|
|
idxfiles_cleaner_cb(struct xbps_handle *xhp _unused, xbps_object_t obj _unused,
|
|
|
|
const char *key, void *arg, bool *done _unused)
|
2013-11-25 14:46:38 +05:30
|
|
|
{
|
|
|
|
xbps_dictionary_t pkg;
|
|
|
|
struct cbdata *cbd = arg;
|
|
|
|
char *pkgname;
|
2013-11-25 15:15:05 +05:30
|
|
|
const char *pkgver;
|
2013-11-25 14:46:38 +05:30
|
|
|
|
|
|
|
/* Find out entries on index-files that aren't registered on index */
|
2013-11-25 15:57:11 +05:30
|
|
|
if ((pkgname = xbps_pkg_name(key)) == NULL) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: invalid entry found on index-files: %s\n", cbd->repourl, key);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-25 14:46:38 +05:30
|
|
|
if ((pkg = xbps_dictionary_get(cbd->idx, pkgname))) {
|
2013-11-25 15:15:05 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkg, "pkgver", &pkgver);
|
2014-01-29 21:28:38 +05:30
|
|
|
if (strcmp(pkgver, key)) {
|
|
|
|
pthread_mutex_lock(&cbd->mtx);
|
2013-11-25 15:35:06 +05:30
|
|
|
xbps_array_add_cstring_nocopy(cbd->result, key);
|
2014-01-29 21:28:38 +05:30
|
|
|
pthread_mutex_unlock(&cbd->mtx);
|
|
|
|
}
|
2013-11-25 14:46:38 +05:30
|
|
|
}
|
|
|
|
free(pkgname);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-02 16:04:55 +05:30
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2014-01-31 16:05:31 +05:30
|
|
|
xbps_array_t allkeys;
|
|
|
|
xbps_dictionary_t idx = NULL, idxmeta = NULL, idxfiles = NULL;
|
2013-11-02 16:04:55 +05:30
|
|
|
struct xbps_repo *repo;
|
2013-11-03 14:46:58 +05:30
|
|
|
struct cbdata cbd;
|
2014-01-31 23:14:24 +05:30
|
|
|
struct idxlock *il;
|
2013-11-28 15:59:48 +05:30
|
|
|
char *keyname, *pkgname;
|
2013-11-02 16:04:55 +05:30
|
|
|
int rv = 0;
|
|
|
|
bool flush = false;
|
|
|
|
|
2014-01-31 23:14:24 +05:30
|
|
|
if ((il = index_lock(xhp)) == NULL)
|
2014-01-31 16:05:31 +05:30
|
|
|
return EINVAL;
|
|
|
|
|
2013-11-02 16:04:55 +05:30
|
|
|
repo = xbps_repo_open(xhp, repodir);
|
|
|
|
if (repo == NULL) {
|
|
|
|
if (errno == ENOENT)
|
2014-01-31 16:05:31 +05:30
|
|
|
goto out;
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: cannot read repository data: %s\n",
|
|
|
|
_XBPS_RINDEX, strerror(errno));
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
2013-11-02 16:04:55 +05:30
|
|
|
}
|
|
|
|
xbps_repo_open_idxfiles(repo);
|
|
|
|
idx = xbps_dictionary_copy(repo->idx);
|
2014-01-30 17:37:34 +05:30
|
|
|
idxmeta = xbps_dictionary_copy(repo->idxmeta);
|
2013-11-02 16:04:55 +05:30
|
|
|
idxfiles = xbps_dictionary_copy(repo->idxfiles);
|
|
|
|
xbps_repo_close(repo);
|
|
|
|
if (idx == NULL || idxfiles == NULL) {
|
2014-01-31 16:05:31 +05:30
|
|
|
fprintf(stderr, "%s: incomplete repository data file!\n", _XBPS_RINDEX);
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2013-11-02 16:04:55 +05:30
|
|
|
}
|
|
|
|
printf("Cleaning `%s' index, please wait...\n", repodir);
|
|
|
|
|
2013-11-25 14:46:38 +05:30
|
|
|
/*
|
|
|
|
* First pass: find out obsolete entries on index and index-files.
|
|
|
|
*/
|
2013-11-03 14:46:58 +05:30
|
|
|
cbd.repourl = repodir;
|
|
|
|
cbd.result = xbps_array_create();
|
2014-01-29 21:28:38 +05:30
|
|
|
pthread_mutex_init(&cbd.mtx, NULL);
|
|
|
|
|
2013-11-02 16:04:55 +05:30
|
|
|
allkeys = xbps_dictionary_all_keys(idx);
|
2013-11-25 14:46:38 +05:30
|
|
|
rv = xbps_array_foreach_cb_multi(xhp, allkeys, idx, idx_cleaner_cb, &cbd);
|
2013-11-25 15:15:05 +05:30
|
|
|
for (unsigned int x = 0; x < xbps_array_count(cbd.result); x++) {
|
2013-11-28 15:59:48 +05:30
|
|
|
xbps_array_get_cstring(cbd.result, x, &keyname);
|
2013-11-25 15:15:05 +05:30
|
|
|
printf("index-files: removed entry %s\n", keyname);
|
2013-11-27 19:24:21 +05:30
|
|
|
printf("index: removed entry %s\n", keyname);
|
2013-11-25 15:15:05 +05:30
|
|
|
pkgname = xbps_pkg_name(keyname);
|
2013-11-27 19:30:37 +05:30
|
|
|
xbps_dictionary_remove(idxfiles, keyname);
|
2013-11-28 15:59:48 +05:30
|
|
|
xbps_dictionary_remove(idx, pkgname);
|
2013-11-25 15:15:05 +05:30
|
|
|
free(pkgname);
|
2013-11-28 15:59:48 +05:30
|
|
|
free(keyname);
|
2013-11-25 15:15:05 +05:30
|
|
|
flush = true;
|
|
|
|
}
|
2013-11-25 14:46:38 +05:30
|
|
|
/*
|
|
|
|
* Second pass: find out obsolete entries on index-files.
|
|
|
|
*/
|
2013-11-25 15:15:05 +05:30
|
|
|
xbps_object_release(cbd.result);
|
2013-11-25 14:46:38 +05:30
|
|
|
xbps_object_release(allkeys);
|
2013-11-25 15:15:05 +05:30
|
|
|
cbd.idx = idx;
|
|
|
|
cbd.result = xbps_array_create();
|
2013-11-25 14:46:38 +05:30
|
|
|
allkeys = xbps_dictionary_all_keys(idxfiles);
|
2013-11-25 15:15:05 +05:30
|
|
|
rv = xbps_array_foreach_cb_multi(xhp, allkeys, idxfiles, idxfiles_cleaner_cb, &cbd);
|
2013-11-03 14:46:58 +05:30
|
|
|
for (unsigned int x = 0; x < xbps_array_count(cbd.result); x++) {
|
2013-11-28 15:59:48 +05:30
|
|
|
xbps_array_get_cstring(cbd.result, x, &keyname);
|
2013-11-25 14:46:38 +05:30
|
|
|
printf("index-files: removed entry %s\n", keyname);
|
2013-11-03 15:45:56 +05:30
|
|
|
xbps_dictionary_remove(idxfiles, keyname);
|
2013-11-28 15:59:48 +05:30
|
|
|
free(keyname);
|
2013-11-02 16:04:55 +05:30
|
|
|
flush = true;
|
|
|
|
}
|
2013-11-25 15:15:05 +05:30
|
|
|
|
2014-01-29 21:28:38 +05:30
|
|
|
pthread_mutex_destroy(&cbd.mtx);
|
2013-11-03 14:46:58 +05:30
|
|
|
xbps_object_release(cbd.result);
|
2013-11-02 16:04:55 +05:30
|
|
|
xbps_object_release(allkeys);
|
|
|
|
|
|
|
|
if (flush) {
|
2014-01-30 17:37:34 +05:30
|
|
|
if (!repodata_flush(xhp, repodir, idx, idxfiles, idxmeta)) {
|
2014-01-31 16:05:31 +05:30
|
|
|
rv = errno;
|
2013-11-02 16:04:55 +05:30
|
|
|
fprintf(stderr, "failed to write repodata: %s\n",
|
|
|
|
strerror(errno));
|
2014-01-31 16:05:31 +05:30
|
|
|
goto out;
|
2013-11-02 16:04:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("index: %u packages registered.\n",
|
|
|
|
xbps_dictionary_count(idx));
|
|
|
|
printf("index-files: %u packages registered.\n",
|
|
|
|
xbps_dictionary_count(idxfiles));
|
|
|
|
|
2014-01-31 16:05:31 +05:30
|
|
|
out:
|
2014-01-31 23:14:24 +05:30
|
|
|
index_unlock(il);
|
2014-01-31 16:05:31 +05:30
|
|
|
|
|
|
|
if (idx)
|
|
|
|
xbps_object_release(idx);
|
|
|
|
if (idxmeta)
|
|
|
|
xbps_object_release(idxmeta);
|
|
|
|
if (idxfiles)
|
|
|
|
xbps_object_release(idxfiles);
|
2013-11-02 16:04:55 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|