2014-05-06 15:47:18 +05:30
|
|
|
/*-
|
2020-04-29 17:42:10 +05:30
|
|
|
* Copyright (c) 2008-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.
|
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>
|
2021-12-25 20:35:46 +05:30
|
|
|
#include <limits.h>
|
2014-05-06 15:47:18 +05:30
|
|
|
|
|
|
|
#include <xbps.h>
|
|
|
|
#include "defs.h"
|
|
|
|
|
2021-12-25 20:35:46 +05:30
|
|
|
static int
|
|
|
|
binpkg_parse(char *buf, size_t bufsz, const char *path, const char **pkgver, const char **arch)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
size_t n = xbps_strlcpy(buf, path, bufsz);
|
|
|
|
if (n >= bufsz)
|
|
|
|
return -ENOBUFS;
|
|
|
|
|
|
|
|
/* remove .xbps file extension */
|
|
|
|
p = buf+n-sizeof(".xbps")+1;
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
/* find the previous dot that separates the architecture */
|
|
|
|
for (p = p-1; p > buf && *p != '.'; p--);
|
|
|
|
if (*p != '.')
|
|
|
|
return -EINVAL;
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
/* make sure the pkgver part is valid */
|
|
|
|
if (!xbps_pkg_version(buf))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*arch = p+1;
|
|
|
|
*pkgver = buf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-30 20:27:01 +05:30
|
|
|
struct cleaner_data {
|
|
|
|
bool dry;
|
|
|
|
bool uninstalled;
|
|
|
|
};
|
|
|
|
|
2014-05-06 15:47:18 +05:30
|
|
|
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
|
|
|
{
|
2021-12-25 20:35:46 +05:30
|
|
|
char buf[PATH_MAX];
|
2023-01-30 20:27:01 +05:30
|
|
|
xbps_dictionary_t pkgd;
|
2014-09-06 00:39:27 +05:30
|
|
|
const char *binpkg, *rsha256;
|
2021-12-25 20:35:46 +05:30
|
|
|
const char *binpkgver, *binpkgarch;
|
2023-01-30 20:27:01 +05:30
|
|
|
struct cleaner_data *data = arg;
|
2021-12-25 20:35:46 +05:30
|
|
|
int r;
|
2016-09-02 19:25:09 +05:30
|
|
|
|
2014-05-06 15:47:18 +05:30
|
|
|
binpkg = xbps_string_cstring_nocopy(obj);
|
2021-12-25 20:35:46 +05:30
|
|
|
r = binpkg_parse(buf, sizeof(buf), binpkg, &binpkgver, &binpkgarch);
|
|
|
|
if (r < 0) {
|
|
|
|
xbps_error_printf("Binary package filename: %s: %s\n", binpkg, strerror(-r));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (strcmp(binpkgarch, xhp->target_arch ? xhp->target_arch : xhp->native_arch) != 0 &&
|
|
|
|
strcmp(binpkgarch, "noarch") != 0) {
|
2022-12-24 18:16:33 +05:30
|
|
|
xbps_dbg_printf("%s: ignoring binpkg with unmatched arch\n", binpkg);
|
2014-05-06 15:47:18 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2021-12-25 20:35:46 +05:30
|
|
|
|
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.
|
|
|
|
*/
|
2023-01-30 20:27:01 +05:30
|
|
|
if (data->uninstalled) {
|
|
|
|
pkgd = xbps_pkgdb_get_pkg(xhp, binpkgver);
|
|
|
|
} else {
|
|
|
|
pkgd = xbps_rpool_get_pkg(xhp, binpkgver);
|
|
|
|
}
|
|
|
|
if (pkgd) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd,
|
2014-05-06 15:47:18 +05:30
|
|
|
"filename-sha256", &rsha256);
|
2021-12-25 19:46:02 +05:30
|
|
|
r = xbps_file_sha256_check(binpkg, rsha256);
|
|
|
|
if (r == 0) {
|
2015-01-10 15:43:26 +05:30
|
|
|
/* hash matched */
|
|
|
|
return 0;
|
2014-05-06 15:47:18 +05:30
|
|
|
}
|
2021-12-25 19:46:02 +05:30
|
|
|
if (r != ERANGE) {
|
|
|
|
xbps_error_printf("Failed to checksum `%s': %s\n", binpkg, strerror(r));
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-06 15:47:18 +05:30
|
|
|
}
|
2021-12-25 20:35:46 +05:30
|
|
|
snprintf(buf, sizeof(buf), "%s.sig", binpkg);
|
2023-01-30 20:27:01 +05:30
|
|
|
if (!data->dry && unlink(binpkg) == -1) {
|
2021-12-25 19:46:02 +05:30
|
|
|
xbps_error_printf("Failed to remove `%s': %s\n",
|
2014-05-06 15:47:18 +05:30
|
|
|
binpkg, strerror(errno));
|
|
|
|
} else {
|
|
|
|
printf("Removed %s from cachedir (obsolete)\n", binpkg);
|
|
|
|
}
|
2023-01-30 20:27:01 +05:30
|
|
|
if (!data->dry && unlink(buf) == -1 && errno != ENOENT) {
|
2021-12-25 20:35:46 +05:30
|
|
|
xbps_error_printf("Failed to remove `%s': %s\n",
|
|
|
|
buf, strerror(errno));
|
2014-05-06 15:47:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-01-30 20:27:01 +05:30
|
|
|
clean_cachedir(struct xbps_handle *xhp, bool uninstalled, 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")) {
|
2022-12-24 18:16:33 +05:30
|
|
|
xbps_dbg_printf("ignoring unknown file: %s\n", dp->d_name);
|
2014-05-06 15:47:18 +05:30
|
|
|
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)) {
|
2023-01-30 20:27:01 +05:30
|
|
|
struct cleaner_data data = {
|
|
|
|
.dry = drun,
|
|
|
|
.uninstalled = uninstalled,
|
|
|
|
};
|
|
|
|
rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&data);
|
2014-08-02 13:30:54 +05:30
|
|
|
xbps_object_release(array);
|
|
|
|
}
|
2014-05-06 15:47:18 +05:30
|
|
|
return rv;
|
|
|
|
}
|