From cdec7e6e93db547a80525c24167345a090a00273 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 15 Jun 2007 22:47:16 +0200 Subject: Correct cp's handling of destination symlinks in some cases. * NEWS: "cp" no longer considers a destination symlink to be the same as the referenced file when copying links or making backups. * src/copy.c (copy_reg): When following a symlink, use the followed name in later chown etc. requests, so that the created file is affected, rather than the symlink. Use O_NOFOLLOW on source when not dereferencing symlinks; this avoids a race. Preserve errno correctly when doing multiple open attempts on the destination. (copy_internal): Follow destination symlinks only when copying a regular file and only when we don't intend to remove or rename the destination first, regardless of whether following source symlinks; this is because since POSIX and tradition (e.g., FreeBSD) say we should ordinarily follow destination symlinks if the system calls would ordinarily do so. * src/copy.h (struct cp_options): Add comment that 'dereference' is only for source files. * src/cp.c (usage): Note that --derereference etc. are only for source files. (make_dir_parents_private): Follow symlinks, regardless of whether --dereference is specified, because these are destination symlinks. * tests/cp/same-file: Adjust tests to match revised behavior. Filter out perror output since it might vary from host to host. Use sed alone instead of also using echo. * doc/coreutils.texi (cp invocation): Document the behavior better when the destination is a symlink. Clarify source versus destination symlinks. Describe the new behavior for destination symlinks. 2007-06-15 Jim Meyering * src/copy.c: Include "canonicalize.h". (copy_reg): Use canonicalize_filename_mode to follow the symlink, so that we can always open with O_EXCL and avoid a race. --- tests/cp/same-file | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'tests/cp/same-file') diff --git a/tests/cp/same-file b/tests/cp/same-file index 44d5dd74c..8e0593e40 100755 --- a/tests/cp/same-file +++ b/tests/cp/same-file @@ -89,9 +89,15 @@ for args in 'foo symlink' 'symlink foo' 'foo foo' 'sl1 sl2' 'foo hardlink'; do cp $options $args 2>_err echo $? $options - # Normalize the program name in the error output, + # Normalize the program name and diagnostics in the error output, # and put brackets around the output. - test -s _err && echo "[`sed 's/^[^:][^:]*:/cp:/' _err`]" + if test -s _err; then + sed ' + s/^[^:]*:\([^:]*\).*/cp:\1/ + 1s/^/[/ + $s/$/]/ + ' _err + fi # Strip off all but the file names. ls="`ls -gG --ignore=_err . \ | sed \ @@ -128,13 +134,13 @@ cat <<\EOF > $expected 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) +1 -l [cp: cannot create link `symlink'] (foo symlink -> foo) 0 -dl (foo symlink -> foo) -0 -fl (foo symlink -> foo) +0 -fl (foo symlink) 0 -dfl (foo symlink) -0 -bl (foo symlink -> foo) +0 -bl (foo symlink symlink.~1~ -> foo) 0 -bdl (foo symlink symlink.~1~ -> foo) -0 -bfl (foo symlink -> foo) +0 -bfl (foo symlink symlink.~1~ -> foo) 0 -bdfl (foo symlink symlink.~1~ -> foo) 1 [cp: `symlink' and `foo' are the same file] (foo symlink -> foo) @@ -179,10 +185,10 @@ cat <<\EOF > $expected 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) +1 -l [cp: cannot create link `sl2'] (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) +0 -bl (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo) +0 -bfl (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo) 1 [cp: `foo' and `hardlink' are the same file] (foo hardlink) 1 -d [cp: `foo' and `hardlink' are the same file] (foo hardlink) -- cgit v1.2.3-54-g00ecf