From 283fb4bc064340e6ce39bd4036c6f544ac6144aa Mon Sep 17 00:00:00 2001
From: Juan RP <xtraeme@gmail.com>
Date: Sat, 10 Jan 2015 11:13:26 +0100
Subject: [PATCH] xbps-remove: misc changes to the clean-cache mode.

- If orphans mode is not set, exit directly.
- Simplify the clean-cache code to avoid unnecessary allocs in some cases.
---
 bin/xbps-remove/clean-cache.c | 29 ++++++++---------------------
 bin/xbps-remove/main.c        |  4 ++--
 2 files changed, 10 insertions(+), 23 deletions(-)

diff --git a/bin/xbps-remove/clean-cache.c b/bin/xbps-remove/clean-cache.c
index 85dacc63..b89fea24 100644
--- a/bin/xbps-remove/clean-cache.c
+++ b/bin/xbps-remove/clean-cache.c
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2008-2014 Juan Romero Pardines.
+ * Copyright (c) 2008-2015 Juan Romero Pardines.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -47,14 +47,11 @@ cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
 
 	/* Internalize props.plist dictionary from binary pkg */
 	binpkg = xbps_string_cstring_nocopy(obj);
-	pkgver = xbps_binpkg_pkgver(binpkg);
-	assert(pkgver);
 	arch = xbps_binpkg_arch(binpkg);
 	assert(arch);
 
 	if (!xbps_pkg_arch_match(xhp, arch, NULL)) {
-		xbps_dbg_printf(xhp, "%s: ignoring pkg with unmatched arch (%s)\n", pkgver, arch);
-		free(pkgver);
+		xbps_dbg_printf(xhp, "%s: ignoring binpkg with unmatched arch (%s)\n", binpkg, arch);
 		free(arch);
 		return 0;
 	}
@@ -63,29 +60,19 @@ cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
 	 * Remove binary pkg if it's not registered in any repository
 	 * or if hash doesn't match.
 	 */
-	binpkgsig = xbps_xasprintf("%s.sig", binpkg);
+	pkgver = xbps_binpkg_pkgver(binpkg);
+	assert(pkgver);
 	repo_pkgd = xbps_rpool_get_pkg(xhp, pkgver);
 	free(pkgver);
 	if (repo_pkgd) {
 		xbps_dictionary_get_cstring_nocopy(repo_pkgd,
 		    "filename-sha256", &rsha256);
-		if (xbps_file_hash_check(binpkg, rsha256) == ERANGE) {
-			if (unlink(binpkg) == -1) {
-				fprintf(stderr, "Failed to remove "
-				    "`%s': %s\n", binpkg, strerror(errno));
-			} else {
-				printf("Removed %s from cachedir (sha256 mismatch)\n", binpkg);
-			}
-			if (unlink(binpkgsig) == -1) {
-				if (errno != ENOENT) {
-					fprintf(stderr, "Failed to remove "
-					    "`%s': %s\n", binpkgsig, strerror(errno));
-				}
-			}
+		if (xbps_file_hash_check(binpkg, rsha256) == 0) {
+			/* hash matched */
+			return 0;
 		}
-		free(binpkgsig);
-		return 0;
 	}
+	binpkgsig = xbps_xasprintf("%s.sig", binpkg);
 	if (unlink(binpkg) == -1) {
 		fprintf(stderr, "Failed to remove `%s': %s\n",
 		    binpkg, strerror(errno));
diff --git a/bin/xbps-remove/main.c b/bin/xbps-remove/main.c
index 831b02aa..74c4a0a2 100644
--- a/bin/xbps-remove/main.c
+++ b/bin/xbps-remove/main.c
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2008-2014 Juan Romero Pardines.
+ * Copyright (c) 2008-2015 Juan Romero Pardines.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -269,7 +269,7 @@ main(int argc, char **argv)
 
 	if (clean_cache) {
 		rv = clean_cachedir(&xh);
-		if (rv != 0)
+		if (!orphans || rv)
 			exit(rv);;
 	}