linux-firmware/dedup-firmware.sh
Emil Velikov ee8c336ab3 copy-firmware.sh: flesh out and fix dedup-firmware.sh
Flesh out the de-duplication logic in separate script. The copy-firmware.sh is
already complex enough and de-duplication doesn't really fit in there.

In the process we migrate away from the open-coded `ln --relative`. We also
avoid touching symlinks, which are not created by rdfind. Otherwise we end up
"fixing" the folder to folder symlinks (created earlier in the process) and
things explode.

As result we also get a few bonuses:
 - the COPYOPTS shell injection is gone - the variable was never used
 - people can dedup as separate step if/when they choose to do so

Aside: based on the noise in git log and around distros ... I'm wondering if
having the de-duplication as opt-in, would have been better. Is it too late to
change or the ship has sailed?

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2024-10-10 14:33:32 +00:00

53 lines
1.0 KiB
Bash
Executable File

#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
#
# Deduplicate files in a given destdir
#
err() {
echo "ERROR: $*"
exit 1
}
verbose=:
destdir=
while test $# -gt 0; do
case $1 in
-v | --verbose)
# shellcheck disable=SC2209
verbose=echo
;;
*)
if test -n "$destdir"; then
err "unknown command-line options: $*"
fi
destdir="$1"
shift
;;
esac
done
if test -z "$destdir"; then
err "destination directory was not specified."
fi
if ! test -d "$destdir"; then
err "provided directory does not exit."
fi
if ! command -v rdfind >/dev/null; then
err "rdfind is not installed."
fi
$verbose "Finding duplicate files"
rdfind -makesymlinks true -makeresultsfile true "$destdir" >/dev/null
grep DUPTYPE_WITHIN_SAME_TREE results.txt | grep -o "$destdir.*" | while read -r l; do
target="$(realpath "$l")"
$verbose "Correcting path for $l"
ln --force --symbolic --relative "$target" "$l"
done
rm results.txt