2014-05-06 15:47:18 +05:30
|
|
|
/*-
|
2020-04-24 15:14:19 +05:30
|
|
|
* Licensed under the SPDX BSD-2-Clause identifier.
|
|
|
|
* Use is subject to license terms, as specified in the LICENSE file.
|
2014-05-06 15:47:18 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include <xbps.h>
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
static int
|
|
|
|
cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
|
2018-07-18 07:54:26 +05:30
|
|
|
const char *key UNUSED, void *arg,
|
|
|
|
bool *done UNUSED)
|
2014-05-06 15:47:18 +05:30
|
|
|
{
|
2014-09-06 00:39:27 +05:30
|
|
|
xbps_dictionary_t repo_pkgd;
|
|
|
|
const char *binpkg, *rsha256;
|
|
|
|
char *binpkgsig, *pkgver, *arch;
|
2016-09-02 19:25:09 +05:30
|
|
|
bool drun = false;
|
|
|
|
|
|
|
|
/* Extract drun (dry-run) flag from arg*/
|
|
|
|
if (arg != NULL)
|
|
|
|
drun = *(bool*)arg;
|
2014-05-06 15:47:18 +05:30
|
|
|
|
|
|
|
/* Internalize props.plist dictionary from binary pkg */
|
|
|
|
binpkg = xbps_string_cstring_nocopy(obj);
|
2014-09-06 00:39:27 +05:30
|
|
|
arch = xbps_binpkg_arch(binpkg);
|
|
|
|
assert(arch);
|
|
|
|
|
2014-05-06 15:47:18 +05:30
|
|
|
if (!xbps_pkg_arch_match(xhp, arch, NULL)) {
|
2015-01-10 15:43:26 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: ignoring binpkg with unmatched arch (%s)\n", binpkg, arch);
|
2014-10-05 11:30:31 +05:30
|
|
|
free(arch);
|
2014-05-06 15:47:18 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2014-09-06 00:39:27 +05:30
|
|
|
free(arch);
|
2014-05-06 15:47:18 +05:30
|
|
|
/*
|
|
|
|
* Remove binary pkg if it's not registered in any repository
|
|
|
|
* or if hash doesn't match.
|
|
|
|
*/
|
2015-01-10 15:43:26 +05:30
|
|
|
pkgver = xbps_binpkg_pkgver(binpkg);
|
|
|
|
assert(pkgver);
|
2014-05-06 15:47:18 +05:30
|
|
|
repo_pkgd = xbps_rpool_get_pkg(xhp, pkgver);
|
2014-09-06 00:39:27 +05:30
|
|
|
free(pkgver);
|
2014-05-06 15:47:18 +05:30
|
|
|
if (repo_pkgd) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(repo_pkgd,
|
|
|
|
"filename-sha256", &rsha256);
|
2020-02-10 06:24:52 +05:30
|
|
|
if (xbps_file_sha256_check(binpkg, rsha256) == 0) {
|
2015-01-10 15:43:26 +05:30
|
|
|
/* hash matched */
|
|
|
|
return 0;
|
2014-05-06 15:47:18 +05:30
|
|
|
}
|
|
|
|
}
|
2015-01-10 15:43:26 +05:30
|
|
|
binpkgsig = xbps_xasprintf("%s.sig", binpkg);
|
2016-09-02 19:25:09 +05:30
|
|
|
if (!drun && unlink(binpkg) == -1) {
|
2014-05-06 15:47:18 +05:30
|
|
|
fprintf(stderr, "Failed to remove `%s': %s\n",
|
|
|
|
binpkg, strerror(errno));
|
|
|
|
} else {
|
|
|
|
printf("Removed %s from cachedir (obsolete)\n", binpkg);
|
|
|
|
}
|
2016-09-02 19:25:09 +05:30
|
|
|
if (!drun && unlink(binpkgsig) == -1) {
|
2014-10-08 13:39:58 +05:30
|
|
|
if (errno != ENOENT) {
|
|
|
|
fprintf(stderr, "Failed to remove `%s': %s\n",
|
|
|
|
binpkgsig, strerror(errno));
|
|
|
|
}
|
2014-05-06 15:47:18 +05:30
|
|
|
}
|
|
|
|
free(binpkgsig);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-09-02 19:25:09 +05:30
|
|
|
clean_cachedir(struct xbps_handle *xhp, bool drun)
|
2014-05-06 15:47:18 +05:30
|
|
|
{
|
|
|
|
xbps_array_t array = NULL;
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
|
|
|
char *ext;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
if (chdir(xhp->cachedir) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((dirp = opendir(xhp->cachedir)) == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2014-09-06 00:39:27 +05:30
|
|
|
array = xbps_array_create();
|
2014-05-06 15:47:18 +05:30
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
|
|
|
if ((strcmp(dp->d_name, ".") == 0) ||
|
|
|
|
(strcmp(dp->d_name, "..") == 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* only process xbps binary packages, ignore something else */
|
|
|
|
if ((ext = strrchr(dp->d_name, '.')) == NULL)
|
|
|
|
continue;
|
|
|
|
if (strcmp(ext, ".xbps")) {
|
|
|
|
xbps_dbg_printf(xhp, "ignoring unknown file: %s\n", dp->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
xbps_array_add_cstring(array, dp->d_name);
|
|
|
|
}
|
|
|
|
(void)closedir(dirp);
|
|
|
|
|
2014-08-02 13:30:54 +05:30
|
|
|
if (xbps_array_count(array)) {
|
2016-09-02 19:25:09 +05:30
|
|
|
rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&drun);
|
2014-08-02 13:30:54 +05:30
|
|
|
xbps_object_release(array);
|
|
|
|
}
|
2014-05-06 15:47:18 +05:30
|
|
|
return rv;
|
|
|
|
}
|