Skip to content

Instantly share code, notes, and snippets.

@Crest
Created March 27, 2024 20:45
Show Gist options
  • Save Crest/7a3228d991d371e938f09e80d45e2778 to your computer and use it in GitHub Desktop.
Save Crest/7a3228d991d371e938f09e80d45e2778 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -Cefu -o pipefail
cleanup() {
if [ -d "${TMP_DIR:?}" ]; then
echo rm -rf -- "${TMP_DIR:?}"
fi
trap - EXIT
}
: "${BLOB_DIR:=/var/db/blobs}"
[ -d "${BLOB_DIR:?}" ] || mkdir -p "${BLOB_DIR:?}"
readonly BLOB_DIR TMP_DIR="${BLOB_DIR:?}/.tmp.$$"
trap cleanup EXIT INT PIPE ALRM TERM XCPU XFSZ VTALRM PROF USR1 USR2
TAG='' SRC=''
while getopts 't:s:' ARG; do
case "$ARG" in
t) TAG="$OPTARG" ;;
s) SRC="$OPTARG" ;;
*) exit 64;;
esac
done
readonly TAG SRC TAG_DIR="${BLOB_DIR:?}/$TAG"
case "$TAG" in
'') echo "Tag (-t <tag>) is missing (or empty)." >&2; exit 64;;
.*) echo "Tags must not start with a dot." >&2; exit 64;;
*[$'/ \t\n']*) echo "Tags must not contain a slashes, spaces, tabs or newlines." >&2; exit 64;;
esac
if [ -z "$SRC" ]; then
echo "Source (-s <source>) is missing." >&2; exit 64
elif [ ! -d "$SRC" ]; then
echo "The source must be an existing directory." >&2; exit 64
fi
if [ -d "${TAG_DIR:?}" ]; then
echo "The tag already exists."
exit 0
fi
rm -rf -- "${TMP_DIR:?}/.tmp.$$"
mkdir -p "${TMP_DIR:?}"
cp -an -- "${SRC:?}/" "${TMP_DIR:?}/content"
cd "${TMP_DIR:?}/content"
umask 377
mtree -c -n -K flags,gid,mode,sha512,size,time,type,uid >../mtree
umask 022
IFS=' '
<../mtree mtree -D -R type,uid,gid,mode,nlink,time,size -K sha512,flags | while read -r HASH FLAGS FILE; do
if [ -z "$FILE" ] || [ -L "$FILE" ] || [ ! -f "$FILE" ] || [ ! -s "$FILE" ]; then
continue
fi
HASH="${HASH#sha512=}"
FLAGS="${FLAGS#flags=}"
DIR_XX="${HASH%??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????}"
DIR_XXXX="${HASH%????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????}"
INFIX=".blobs/${DIR_XX:?}/${DIR_XXXX:?}"
[ -d "../../${INFIX:?}" ] || mkdir -p "../../${INFIX:?}"
[ -s "../../${INFIX:?}/${HASH:?}" ] || install -S -C -m 444 -f uarch -- "$FILE" "../../${INFIX:?}/${HASH:?}"
[ -d "../${INFIX:?}" ] || mkdir -p "../${INFIX:?}"
[ -s "../${INFIX:?}/${HASH:?}" ] || ln -hn -- "../../${INFIX:?}/${HASH:?}" "../${INFIX:?}/${HASH:?}"
case ",$FLAGS," in
*,schg,*)
chflags -- noschg "$FILE"
chmod -- u+w "$FILE"
cat "../${INFIX:?}/${HASH:?}" >| "$FILE"
chmod -- u-w "$FILE"
chflags -- schg "$FILE"
;;
*)
cat "../${INFIX:?}/${HASH:?}" >| "$FILE"
;;
esac
done
cd -
mv -nh -- "${TMP_DIR:?}" "${TAG_DIR:?}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment