2013-11-02 16:04:55 +05:30
|
|
|
/*-
|
2019-06-17 10:54:05 +05:30
|
|
|
* Copyright (c) 2012-2019 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"
|
|
|
|
|
2015-04-09 15:44:03 +05:30
|
|
|
static xbps_dictionary_t dest;
|
2013-11-03 14:46:58 +05:30
|
|
|
|
2018-07-03 11:37:23 +05:30
|
|
|
struct CleanerCbInfo {
|
|
|
|
const char *repourl;
|
|
|
|
bool hashcheck;
|
|
|
|
};
|
2019-06-17 10:54:05 +05:30
|
|
|
|
2013-11-02 16:04:55 +05:30
|
|
|
static int
|
|
|
|
idx_cleaner_cb(struct xbps_handle *xhp,
|
|
|
|
xbps_object_t obj,
|
2018-07-18 07:54:26 +05:30
|
|
|
const char *key UNUSED,
|
2013-11-02 16:04:55 +05:30
|
|
|
void *arg,
|
2018-07-18 07:54:26 +05:30
|
|
|
bool *done UNUSED)
|
2013-11-02 16:04:55 +05:30
|
|
|
{
|
2018-07-03 11:37:23 +05:30
|
|
|
struct CleanerCbInfo *info = arg;
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *arch = NULL, *pkgver = NULL, *sha256 = NULL;
|
2020-02-09 00:01:29 +05:30
|
|
|
char *filen, pkgname[XBPS_NAME_SIZE];
|
2013-11-02 16:04:55 +05:30
|
|
|
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
|
|
|
|
2018-07-03 11:37:23 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: checking %s [%s] ...\n", info->repourl, pkgver, arch);
|
2013-11-02 16:04:55 +05:30
|
|
|
|
2018-07-03 11:37:23 +05:30
|
|
|
filen = xbps_xasprintf("%s/%s.%s.xbps", info->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.
|
|
|
|
*/
|
2020-02-09 00:01:29 +05:30
|
|
|
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver))
|
2015-07-26 11:36:38 +05:30
|
|
|
goto out;
|
2015-04-09 15:44:03 +05:30
|
|
|
xbps_dictionary_remove(dest, pkgname);
|
|
|
|
printf("index: removed pkg %s\n", pkgver);
|
2018-07-03 11:37:23 +05:30
|
|
|
} else if (info->hashcheck) {
|
2013-11-02 16:04:55 +05:30
|
|
|
/*
|
|
|
|
* 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) {
|
2020-02-09 00:01:29 +05:30
|
|
|
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver))
|
2015-07-26 11:36:38 +05:30
|
|
|
goto out;
|
2015-04-09 15:44:03 +05:30
|
|
|
xbps_dictionary_remove(dest, pkgname);
|
|
|
|
printf("index: removed pkg %s\n", pkgver);
|
2014-01-29 21:28:38 +05:30
|
|
|
}
|
2013-11-02 16:04:55 +05:30
|
|
|
}
|
2015-07-26 11:36:38 +05:30
|
|
|
out:
|
2013-11-02 16:04:55 +05:30
|
|
|
free(filen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-21 15:52:32 +05:30
|
|
|
static int
|
|
|
|
cleanup_repo(struct xbps_handle *xhp, const char *repodir, struct xbps_repo *repo,
|
2020-01-06 19:01:40 +05:30
|
|
|
const char *reponame, bool hashcheck, const char *compression)
|
2019-06-17 10:54:05 +05:30
|
|
|
{
|
2016-03-21 15:52:32 +05:30
|
|
|
int rv = 0;
|
|
|
|
xbps_array_t allkeys;
|
2018-07-03 11:37:23 +05:30
|
|
|
struct CleanerCbInfo info = {
|
|
|
|
.hashcheck = hashcheck,
|
|
|
|
.repourl = repodir
|
|
|
|
};
|
2016-03-21 15:52:32 +05:30
|
|
|
/*
|
|
|
|
* First pass: find out obsolete entries on index and index-files.
|
|
|
|
*/
|
|
|
|
dest = xbps_dictionary_copy_mutable(repo->idx);
|
|
|
|
allkeys = xbps_dictionary_all_keys(dest);
|
2018-07-03 11:37:23 +05:30
|
|
|
(void)xbps_array_foreach_cb_multi(xhp, allkeys, repo->idx, idx_cleaner_cb, &info);
|
2016-03-21 15:52:32 +05:30
|
|
|
xbps_object_release(allkeys);
|
|
|
|
|
2019-06-17 10:54:05 +05:30
|
|
|
if (strcmp("stagedata", reponame) == 0 && xbps_dictionary_count(dest) == 0) {
|
2016-03-22 14:09:07 +05:30
|
|
|
char *stagefile = xbps_repo_path_with_name(xhp, repodir, "stagedata");
|
|
|
|
unlink(stagefile);
|
|
|
|
free(stagefile);
|
|
|
|
}
|
2016-03-21 15:52:32 +05:30
|
|
|
if (!xbps_dictionary_equals(dest, repo->idx)) {
|
2020-01-06 19:01:40 +05:30
|
|
|
if (!repodata_flush(xhp, repodir, reponame, dest, repo->idxmeta, compression)) {
|
2016-03-21 15:52:32 +05:30
|
|
|
rv = errno;
|
|
|
|
fprintf(stderr, "failed to write repodata: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 10:54:05 +05:30
|
|
|
if (strcmp("stagedata", reponame) == 0)
|
2016-03-21 15:52:32 +05:30
|
|
|
printf("stage: %u packages registered.\n", xbps_dictionary_count(dest));
|
|
|
|
else
|
|
|
|
printf("index: %u packages registered.\n", xbps_dictionary_count(dest));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
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
|
2020-01-06 19:01:40 +05:30
|
|
|
index_clean(struct xbps_handle *xhp, const char *repodir, const bool hashcheck, const char *compression)
|
2013-11-02 16:04:55 +05:30
|
|
|
{
|
2016-03-21 15:52:32 +05:30
|
|
|
struct xbps_repo *repo, *stage;
|
2015-03-25 16:30:59 +05:30
|
|
|
char *rlockfname = NULL;
|
|
|
|
int rv = 0, rlockfd = -1;
|
2013-11-02 16:04:55 +05:30
|
|
|
|
2015-03-25 16:30:59 +05:30
|
|
|
if (!xbps_repo_lock(xhp, repodir, &rlockfd, &rlockfname)) {
|
|
|
|
rv = errno;
|
|
|
|
fprintf(stderr, "%s: cannot lock repository: %s\n",
|
|
|
|
_XBPS_RINDEX, strerror(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
2016-03-21 15:52:32 +05:30
|
|
|
repo = xbps_repo_public_open(xhp, repodir);
|
2013-11-02 16:04:55 +05:30
|
|
|
if (repo == NULL) {
|
2014-09-05 15:56:42 +05:30
|
|
|
rv = errno;
|
2015-07-26 12:08:12 +05:30
|
|
|
if (rv == ENOENT) {
|
|
|
|
xbps_repo_unlock(rlockfd, rlockfname);
|
2014-09-05 15:56:42 +05:30
|
|
|
return 0;
|
2015-07-26 12:08:12 +05:30
|
|
|
}
|
2014-01-31 16:05:31 +05:30
|
|
|
fprintf(stderr, "%s: cannot read repository data: %s\n",
|
|
|
|
_XBPS_RINDEX, strerror(errno));
|
2015-07-26 11:47:17 +05:30
|
|
|
xbps_repo_unlock(rlockfd, rlockfname);
|
2014-09-05 15:56:42 +05:30
|
|
|
return rv;
|
2013-11-02 16:04:55 +05:30
|
|
|
}
|
2016-03-21 15:52:32 +05:30
|
|
|
stage = xbps_repo_stage_open(xhp, repodir);
|
|
|
|
if (repo->idx == NULL || (stage && stage->idx == 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);
|
|
|
|
|
2020-01-06 19:01:40 +05:30
|
|
|
if ((rv = cleanup_repo(xhp, repodir, repo, "repodata", hashcheck, compression))) {
|
2016-03-21 15:52:32 +05:30
|
|
|
goto out;
|
2019-06-17 10:54:05 +05:30
|
|
|
}
|
|
|
|
if (stage) {
|
2020-01-06 19:01:40 +05:30
|
|
|
cleanup_repo(xhp, repodir, stage, "stagedata", hashcheck, compression);
|
2013-11-02 16:04:55 +05:30
|
|
|
}
|
|
|
|
|
2014-01-31 16:05:31 +05:30
|
|
|
out:
|
2015-01-11 13:41:38 +05:30
|
|
|
xbps_repo_close(repo);
|
2016-03-21 15:52:32 +05:30
|
|
|
if(stage)
|
|
|
|
xbps_repo_close(stage);
|
2015-03-25 16:30:59 +05:30
|
|
|
xbps_repo_unlock(rlockfd, rlockfname);
|
|
|
|
|
2013-11-02 16:04:55 +05:30
|
|
|
return rv;
|
|
|
|
}
|