Files
git/cpio.sh
Johannes Sixt 009ab81675 Avoid spawning 'ls' for each directory that is passed to the cpio emulator.
We assume that the file names are passed in using find -depth so that a
directory is listed after its contents.
2007-08-17 15:16:33 +02:00

61 lines
955 B
Bash

#!/bin/sh
#
# Emulates some cpio behavior using GNU tar
die() {
echo >&2 "$@"
exit 1
}
null=
while test $# -gt 0; do
case "$1" in
-0) null=--null;;
-o) mode=o;;
-iuv) ;;
-pumd|-pumdl)
mode=p
dir="$2"
shift
;;
*) die "cpio emulation supports only -0, -o, -iuv, -pumdl";;
esac
shift
done
prev=
filterdirs() {
while read f; do
if test -d "$f"; then
# list only empty directories
# here we assume that directories are listed after
# its files (aka 'find -depth'), hence, a directory
# that is not empty will be a leading sub-string
# of the preceding entry
case "$prev" in
"$f"/* ) ;;
*) echo "$f";;
esac
else
echo "$f"
fi
prev="$f"
done
}
case $mode in
o)
tar --create --file=- $null --files-from=-
;;
p)
test -z "$null" || die "cpio: cannot use -0 in pass-through mode"
filterdirs |
tar --create --file=- --files-from=- |
tar --extract --directory="$dir" --file=-
;;
*)
tar xvf -
esac