From 7e5c3585b8da01782e6d55af2b7fb9b722a76bd6 Mon Sep 17 00:00:00 2001
From: Daniel Baumann <daniel@debian.org>
Date: Tue, 15 Apr 2008 17:16:28 +0200
Subject: Improving .deb caching by using hardlinks (if possible).

---
 functions/cache.sh | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/functions/cache.sh b/functions/cache.sh
index 06ec5a2..1b04368 100755
--- a/functions/cache.sh
+++ b/functions/cache.sh
@@ -18,7 +18,14 @@ Restore_cache ()
 		if [ -d "${DIRECTORY}" ]
 		then
 			# Restore old cache
-			cp "${DIRECTORY}"/*.deb chroot/var/cache/apt/archives
+			if [ "$(stat --printf %d ${DIRECTORY})" = "$(stat --printf %d chroot/var/cache/apt/archives)" ]
+			then
+				# with hardlinks
+				cp -fl "${DIRECTORY}"/*.deb chroot/var/cache/apt/archives
+			else
+				# without hardlinks
+				cp "${DIRECTORY}"/*.deb chroot/var/cache/apt/archives
+			fi
 		fi
 	fi
 }
@@ -38,7 +45,14 @@ Save_cache ()
 			mkdir -p "${DIRECTORY}"
 
 			# Saving new cache
-			mv -f chroot/var/cache/apt/archives/*.deb "${DIRECTORY}"
+			if [ "$(stat --printf %d ${DIRECTORY})" = "$(stat --printf %d chroot/var/cache/apt/archives)" ]
+			then
+				# with hardlinks
+				cp --force -l chroot/var/cache/apt/archives/*.deb "${DIRECTORY}"
+			else
+				# without hardlinks
+				mv -f chroot/var/cache/apt/archives/*.deb "${DIRECTORY}"
+			fi
 		fi
 	else
 		# Purging current cache
-- 
cgit v1.0