Added script for "cleaning" UPPER relative LOWER
authorRalph Ronnquist <rrq@rrq.au>
Sat, 16 Sep 2023 13:41:38 +0000 (23:41 +1000)
committerRalph Ronnquist <rrq@rrq.au>
Sat, 16 Sep 2023 13:41:38 +0000 (23:41 +1000)
overlay-clean-root [new file with mode: 0755]

diff --git a/overlay-clean-root b/overlay-clean-root
new file mode 100755 (executable)
index 0000000..06c997d
--- /dev/null
@@ -0,0 +1,43 @@
+#!/bin/bash
+#
+# This script "cleans" the UPPER directory tree for a subhost by
+# comparing it with the LOWER tree and remove all files that are equal
+# to content.
+
+PROGRAMDIR="$(dirname $(realpath $0))"
+. $PROGRAMDIR/functions
+
+subhost_name $1
+subhost_config
+
+
+
+: ${LOWER:-/}
+
+if [ ! -d "$UPPER" ] || [ ! -d "$LOWER" ] ; then
+    echo "*** needs a root path" >&2
+    exit 1
+fi
+
+if is_live $NAME ; then
+    echo "** Cannot clean running subhost **" >&2
+    exit 1
+fi
+
+UPPER="${UPPER%/}"
+LOWER="${LOWER%/}"
+
+if [ "$UPPER" = "$LOWER" ] ; then
+    echo "** UPER and LOWER are the same directory **" >&2
+    exit 1
+fi
+
+du -sh $UPPER
+exit 0
+DIFFS=/tmp/clean-$NAME.$$
+rm -f $DIFFS
+find $UPPER -type f -printf '%P\n'| while read X ; do
+    cmp "$UPPER/$X" "$LOWER/$X" >> $DIFFS 2>&1 && rm "$UPPER/$X"
+done 
+du -sh $UPPER
+echo "(See details in $DIFFS)"