Merge pull request #192 from jantatje/clean-cache-dry-run

xbps-remove: fix --dry-run for --clean-cache
This commit is contained in:
Enno Boland 2016-09-07 22:03:28 +02:00 committed by GitHub
commit 1ffac73f54
3 changed files with 12 additions and 7 deletions

View File

@ -38,12 +38,17 @@
static int static int
cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj, cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
const char *key _unused, void *arg _unused, const char *key _unused, void *arg,
bool *done _unused) bool *done _unused)
{ {
xbps_dictionary_t repo_pkgd; xbps_dictionary_t repo_pkgd;
const char *binpkg, *rsha256; const char *binpkg, *rsha256;
char *binpkgsig, *pkgver, *arch; char *binpkgsig, *pkgver, *arch;
bool drun = false;
/* Extract drun (dry-run) flag from arg*/
if (arg != NULL)
drun = *(bool*)arg;
/* Internalize props.plist dictionary from binary pkg */ /* Internalize props.plist dictionary from binary pkg */
binpkg = xbps_string_cstring_nocopy(obj); binpkg = xbps_string_cstring_nocopy(obj);
@ -73,13 +78,13 @@ cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
} }
} }
binpkgsig = xbps_xasprintf("%s.sig", binpkg); binpkgsig = xbps_xasprintf("%s.sig", binpkg);
if (unlink(binpkg) == -1) { if (!drun && unlink(binpkg) == -1) {
fprintf(stderr, "Failed to remove `%s': %s\n", fprintf(stderr, "Failed to remove `%s': %s\n",
binpkg, strerror(errno)); binpkg, strerror(errno));
} else { } else {
printf("Removed %s from cachedir (obsolete)\n", binpkg); printf("Removed %s from cachedir (obsolete)\n", binpkg);
} }
if (unlink(binpkgsig) == -1) { if (!drun && unlink(binpkgsig) == -1) {
if (errno != ENOENT) { if (errno != ENOENT) {
fprintf(stderr, "Failed to remove `%s': %s\n", fprintf(stderr, "Failed to remove `%s': %s\n",
binpkgsig, strerror(errno)); binpkgsig, strerror(errno));
@ -91,7 +96,7 @@ cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
} }
int int
clean_cachedir(struct xbps_handle *xhp) clean_cachedir(struct xbps_handle *xhp, bool drun)
{ {
xbps_array_t array = NULL; xbps_array_t array = NULL;
DIR *dirp; DIR *dirp;
@ -123,7 +128,7 @@ clean_cachedir(struct xbps_handle *xhp)
(void)closedir(dirp); (void)closedir(dirp);
if (xbps_array_count(array)) { if (xbps_array_count(array)) {
rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, NULL); rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&drun);
xbps_object_release(array); xbps_object_release(array);
} }
return rv; return rv;

View File

@ -27,6 +27,6 @@
#define _XBPS_REMOVE_DEFS_H_ #define _XBPS_REMOVE_DEFS_H_
/* From clean-cache.c */ /* From clean-cache.c */
int clean_cachedir(struct xbps_handle *); int clean_cachedir(struct xbps_handle *, bool drun);
#endif /* !_XBPS_REMOVE_DEFS_H_ */ #endif /* !_XBPS_REMOVE_DEFS_H_ */

View File

@ -262,7 +262,7 @@ main(int argc, char **argv)
maxcols = get_maxcols(); maxcols = get_maxcols();
if (clean_cache) { if (clean_cache) {
rv = clean_cachedir(&xh); rv = clean_cachedir(&xh, drun);
if (!orphans || rv) if (!orphans || rv)
exit(rv);; exit(rv);;
} }