Skip to content

Instantly share code, notes, and snippets.

@jessicah
Created October 3, 2017 03:48
Show Gist options
  • Save jessicah/2daa3a1ee0a7974590185f825facb1b7 to your computer and use it in GitHub Desktop.
Save jessicah/2daa3a1ee0a7974590185f825facb1b7 to your computer and use it in GitHub Desktop.
Haiku package deduplication
#!/bin/bash
PACKAGE=../rust/package.sh
TOP=$(dirname $0)
CACHEDIR=$TOP/cache
TMPDIR=$TOP/tmp
PACKAGEDIR=$TOP/packages
INCOMING=$TOP/incoming
COMPLETED=$TOP/completed
# clean up temporary directory
rm -rf $TMPDIR/*
# extract packages in incoming, and set up links to cache
for filename in ./incoming/*.hpkg; do
echo "Extracting $(basename $filename)..."
$PACKAGE extract -C $TMPDIR $filename
mkdir -p $PACKAGEDIR/$(basename $filename)
echo "Generating shasums, and linking to cache..."
while IFS= read -r -d '' file; do
echo $file
sum=$(sha1sum "$file" | awk '{ print $1 }')
#echo $file-$sum
clean_name="$(echo "$file" | sed -e s,$TMPDIR,,)"
unique_name="$(basename "$file")-$sum"
dir_name="$CACHEDIR/$(dirname $clean_name)"
mkdir -p "$dir_name"
if [ ! -e "$dir_name/$unique_name" ]; then
# create our first copy
cp "$file" "$dir_name/$unique_name"
fi
# create hardlinks from PACKAGEDIR to CACHEDIR
output_dir="$PACKAGEDIR/$(basename $filename)"
sub_dir="$(dirname $clean_name)"
mkdir -p "$output_dir/$sub_dir"
if [ -h "$file" ]; then
# just copy as is
cp "$file" "$output_dir/$sub_dir/$(basename "$file")"
else
ln "$dir_name/$unique_name" "$output_dir/$sub_dir/$(basename "$file")"
fi
done < <(find $TMPDIR -type f -print0)
# clean up extracted package
rm -rf $TMPDIR/*
# and move completed package
mkdir -p $COMPLETED
mv $filename $COMPLETED/
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment