#!/bin/sh # Test some of cp's options and how cp handles situations in # which a naive implementation might overwrite the source file. if test "$VERBOSE" = yes; then set -x cp --version fi LANGUAGE=C; export LANGUAGE LANG=C; export LANG VERSION_CONTROL=numbered; export VERSION_CONTROL pwd=`pwd` actual=actual-$$ expected=expected-$$ trap "cd $pwd; rm -rf $actual $expected dir" 0 1 2 3 15 exec 1> $actual # FIXME: This should be bigger: like more than 8k contents=XYZ for args in 'foo symlink' 'symlink foo' 'foo foo' 'sl1 sl2' 'foo hardlink'; do for options in '' -d -f -df -b -bd -bf -bdf \ -l -dl -fl -dfl -bl -bdl -bfl -bdfl; do case $args$options in # These tests are not portable. 'sl1 sl2'-bd*l) continue;; 'symlink foo'-dfl) continue;; 'symlink foo'-bdfl) continue;; 'sl1 sl2'-dfl) continue;; esac rm -rf dir mkdir dir cd dir echo $contents > foo case "$args" in *symlink*) ln -s foo symlink ;; esac case "$args" in *hardlink*) ln foo hardlink ;; esac case "$args" in *sl1*) ln -s foo sl1;; esac case "$args" in *sl2*) ln -s foo sl2;; esac ( ( # echo 1>&2 cp $options $args cp $options $args 2>.err echo $? $options # Normalize the program name in the error output, # and put brackets around the output. test -s .err && echo "[`sed 's/^[^:][^:]*:/cp:/' .err`]" # Strip off all but the file names. ls="`ls -lG --ignore=.err . \ | sed \ -e '/^total /d' \ -e 's/^...............................................//'`" echo "($ls)" # Make sure the original is unchanged and that # the destination is a copy. for f in $args; do if test -f $f; then case "`cat $f`" in "$contents") ;; *) echo cp FAILED;; esac else echo symlink-loop fi done ) | tr '\012' ' ' echo ) | sed 's/ *$//' cd .. done echo done cat <<\EOF > $expected 1 [cp: `foo' and `symlink' are the same file] (foo symlink -> foo) 1 -d [cp: `foo' and `symlink' are the same file] (foo symlink -> foo) 0 -f (foo symlink) 0 -df (foo symlink) 0 -b (foo symlink symlink.~1~ -> foo) 0 -bd (foo symlink symlink.~1~ -> foo) 0 -bf (foo symlink symlink.~1~ -> foo) 0 -bdf (foo symlink symlink.~1~ -> foo) 0 -l (foo symlink -> foo) 0 -dl (foo symlink -> foo) 0 -fl (foo symlink -> foo) 0 -dfl (foo symlink) 0 -bl (foo symlink -> foo) 0 -bdl (foo symlink symlink.~1~ -> foo) 0 -bfl (foo symlink -> foo) 0 -bdfl (foo symlink symlink.~1~ -> foo) 1 [cp: `symlink' and `foo' are the same file] (foo symlink -> foo) 1 -d [cp: `symlink' and `foo' are the same file] (foo symlink -> foo) 1 -f [cp: `symlink' and `foo' are the same file] (symlink -> foo) symlink-loop symlink-loop 0 -df (foo -> foo symlink -> foo) symlink-loop symlink-loop 1 -b [cp: `symlink' and `foo' are the same file] (foo symlink -> foo) 0 -bd (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop 1 -bf [cp: `symlink' and `foo' are the same file] (foo symlink -> foo) 0 -bdf (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop 0 -l (foo symlink -> foo) 0 -dl (foo symlink -> foo) 0 -fl (foo symlink -> foo) 0 -bl (foo symlink -> foo) 0 -bdl (foo symlink -> foo) 0 -bfl (foo symlink -> foo) 1 [cp: `foo' and `foo' are the same file] (foo) 1 -d [cp: `foo' and `foo' are the same file] (foo) 1 -f [cp: `foo' and `foo' are the same file] (foo) 1 -df [cp: `foo' and `foo' are the same file] (foo) 1 -b [cp: `foo' and `foo' are the same file] (foo) 1 -bd [cp: `foo' and `foo' are the same file] (foo) 0 -bf (foo foo.~1~) 0 -bdf (foo foo.~1~) 0 -l (foo) 0 -dl (foo) 0 -fl (foo) 0 -dfl (foo) 0 -bl (foo) 0 -bdl (foo) 0 -bfl (foo foo.~1~) 0 -bdfl (foo foo.~1~) 1 [cp: `sl1' and `sl2' are the same file] (foo sl1 -> foo sl2 -> foo) 1 -d [cp: `sl1' and `sl2' are the same file] (foo sl1 -> foo sl2 -> foo) 0 -f (foo sl1 -> foo sl2) 0 -df (foo sl1 -> foo sl2 -> foo) 0 -b (foo sl1 -> foo sl2 sl2.~1~ -> foo) 0 -bd (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo) 0 -bf (foo sl1 -> foo sl2 sl2.~1~ -> foo) 0 -bdf (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo) 0 -l (foo sl1 -> foo sl2 -> foo) 0 -dl (foo sl1 -> foo sl2 -> foo) 0 -fl (foo sl1 -> foo sl2 -> foo) 0 -bl (foo sl1 -> foo sl2 -> foo) 0 -bfl (foo sl1 -> foo sl2 -> foo) 1 [cp: `foo' and `hardlink' are the same file] (foo hardlink) 1 -d [cp: `foo' and `hardlink' are the same file] (foo hardlink) 0 -f (foo hardlink) 0 -df (foo hardlink) 0 -b (foo hardlink hardlink.~1~) 0 -bd (foo hardlink hardlink.~1~) 0 -bf (foo hardlink hardlink.~1~) 0 -bdf (foo hardlink hardlink.~1~) 0 -l (foo hardlink) 0 -dl (foo hardlink) 0 -fl (foo hardlink) 0 -dfl (foo hardlink) 0 -bl (foo hardlink) 0 -bdl (foo hardlink) 0 -bfl (foo hardlink) 0 -bdfl (foo hardlink) EOF # Uncomment this if you see a failure and want to try to diagnose it. # diff -u $expected $actual 1>&2 cmp $expected $actual exit $?