Undo /usr/bin/sort and /usr/bin/find workarounds.
[git/dscho.git] / cpio.sh
blobdf5f64c14c786265d82a583902c64ddeabcd9f5b
1 #!/bin/sh
3 # Emulates some cpio behavior using GNU tar
5 die() {
6 echo >&2 "$@"
7 exit 1
10 null=
12 while test $# -gt 0; do
13 case "$1" in
14 -0) null=--null;;
15 -o) mode=o;;
16 -iuv) ;;
17 -pumd|-pumdl)
18 mode=p
19 dir="$2"
20 shift
22 *) die "cpio emulation supports only -0, -o, -iuv, -pumdl";;
23 esac
24 shift
25 done
27 prev=
29 filterdirs() {
30 while read f; do
31 if test -d "$f"; then
32 # list only empty directories
33 # here we assume that directories are listed after
34 # its files (aka 'find -depth'), hence, a directory
35 # that is not empty will be a leading sub-string
36 # of the preceding entry
37 case "$prev" in
38 "$f"/* ) ;;
39 *) echo "$f";;
40 esac
41 else
42 echo "$f"
44 prev="$f"
45 done
48 case $mode in
50 tar --create --file=- $null --files-from=-
53 test -z "$null" || die "cpio: cannot use -0 in pass-through mode"
54 filterdirs |
55 tar --create --file=- --files-from=- |
56 tar --extract --directory="$dir" --file=-
59 tar xvf -
60 esac