1998-06-27 20:20:25 +00:00
|
|
|
#!/bin/sh
|
1998-06-27 20:33:22 +00:00
|
|
|
# Test some of cp's options and how cp handles situations in
|
|
|
|
|
# which a naive implementation might overwrite the source file.
|
|
|
|
|
|
2023-01-01 14:50:15 +00:00
|
|
|
# Copyright (C) 1998-2023 Free Software Foundation, Inc.
|
2006-08-17 19:58:17 +00:00
|
|
|
|
2007-07-23 14:35:58 +02:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2006-08-17 19:58:17 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
2007-07-23 14:35:58 +02:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
2006-08-17 19:58:17 +00:00
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2017-09-19 01:13:23 -07:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2006-08-17 19:58:17 +00:00
|
|
|
|
2012-09-02 21:55:12 +02:00
|
|
|
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
|
2010-11-17 21:35:31 +01:00
|
|
|
print_ver_ cp
|
1998-06-27 20:20:25 +00:00
|
|
|
|
2012-01-07 17:47:58 +01:00
|
|
|
# Unset CDPATH. Otherwise, output from the 'cd dir' command
|
2003-03-06 07:43:45 +00:00
|
|
|
# can make this test fail.
|
|
|
|
|
(unset CDPATH) >/dev/null 2>&1 && unset CDPATH
|
|
|
|
|
|
1998-06-27 20:20:25 +00:00
|
|
|
VERSION_CONTROL=numbered; export VERSION_CONTROL
|
|
|
|
|
|
2007-11-01 11:59:08 +01:00
|
|
|
# Determine whether a hard link to a symlink points to the symlink
|
|
|
|
|
# itself or to its referent. For example, the link from FreeBSD6.1
|
|
|
|
|
# does dereference a symlink, but the one from Linux does not.
|
|
|
|
|
ln -s no-such dangling-slink
|
|
|
|
|
ln dangling-slink hard-link > /dev/null 2>&1 \
|
|
|
|
|
&& hard_link_to_symlink_does_the_deref=no \
|
|
|
|
|
|| hard_link_to_symlink_does_the_deref=yes
|
|
|
|
|
rm -f no-such dangling-slink hard-link
|
|
|
|
|
|
|
|
|
|
test $hard_link_to_symlink_does_the_deref = yes \
|
2014-11-29 22:42:04 +00:00
|
|
|
&& remove_these_sed='/^0 -[bf]*l .*sl1 ->/d; /hlsl/d' \
|
2007-11-01 11:59:08 +01:00
|
|
|
|| remove_these_sed='/^ELIDE NO TEST OUTPUT/d'
|
|
|
|
|
|
2011-01-28 19:23:21 +01:00
|
|
|
exec 3>&1 1> actual
|
1998-06-27 20:20:25 +00:00
|
|
|
|
1998-06-28 21:11:20 +00:00
|
|
|
# FIXME: This should be bigger: like more than 8k
|
1998-06-27 20:20:25 +00:00
|
|
|
contents=XYZ
|
1998-06-28 21:11:20 +00:00
|
|
|
|
2014-11-18 20:20:50 +01:00
|
|
|
for args in 'foo symlink' 'symlink foo' 'foo foo' 'sl1 sl2' \
|
|
|
|
|
'foo hardlink' 'hlsl sl2'; do
|
2000-09-04 16:56:09 +00:00
|
|
|
for options in '' -d -f -df --rem -b -bd -bf -bdf \
|
2018-05-14 02:26:05 -07:00
|
|
|
-l -dl -fl -dfl -bl -bdl -bfl -bdfl -s -sf; do
|
1998-07-25 17:52:37 +00:00
|
|
|
case $args$options in
|
|
|
|
|
# These tests are not portable.
|
2000-09-05 12:02:40 +00:00
|
|
|
# They all involve making a hard link to a symbolic link.
|
2007-11-01 11:59:08 +01:00
|
|
|
# In the past, we've skipped the tests that are not portable,
|
|
|
|
|
# by doing "continue" here and eliminating the corresponding
|
|
|
|
|
# expected output lines below. Don't do that anymore.
|
1998-07-25 17:52:37 +00:00
|
|
|
'symlink foo'-dfl)
|
2009-08-22 18:56:06 +02:00
|
|
|
continue;;
|
2000-09-05 12:02:40 +00:00
|
|
|
'symlink foo'-bdl)
|
2009-08-22 18:56:06 +02:00
|
|
|
continue;;
|
1998-07-25 17:52:37 +00:00
|
|
|
'symlink foo'-bdfl)
|
2009-08-22 18:56:06 +02:00
|
|
|
continue;;
|
1998-07-25 17:52:37 +00:00
|
|
|
'sl1 sl2'-dfl)
|
2009-08-22 18:56:06 +02:00
|
|
|
continue;;
|
2000-09-05 11:53:03 +00:00
|
|
|
'sl1 sl2'-bd*l)
|
2009-08-22 18:56:06 +02:00
|
|
|
continue;;
|
2000-09-05 11:53:03 +00:00
|
|
|
'sl1 sl2'-dl)
|
2009-08-22 18:56:06 +02:00
|
|
|
continue;;
|
1998-07-03 20:09:03 +00:00
|
|
|
esac
|
2007-11-01 11:59:08 +01:00
|
|
|
|
|
|
|
|
# cont'd Instead, skip them only on systems for which link does
|
|
|
|
|
# dereference a symlink. Detect and skip such tests here.
|
|
|
|
|
case $hard_link_to_symlink_does_the_deref:$args:$options in
|
2014-11-29 22:42:04 +00:00
|
|
|
'yes:sl1 sl2:-fl')
|
2009-08-22 18:56:06 +02:00
|
|
|
continue ;;
|
2014-11-29 22:42:04 +00:00
|
|
|
'yes:sl1 sl2:-bl')
|
2009-08-22 18:56:06 +02:00
|
|
|
continue ;;
|
2014-11-29 22:42:04 +00:00
|
|
|
'yes:sl1 sl2:-bfl')
|
|
|
|
|
continue ;;
|
|
|
|
|
yes:hlsl*)
|
2009-08-22 18:56:06 +02:00
|
|
|
continue ;;
|
2007-11-01 11:59:08 +01:00
|
|
|
esac
|
|
|
|
|
|
1998-06-27 20:20:25 +00:00
|
|
|
rm -rf dir
|
|
|
|
|
mkdir dir
|
|
|
|
|
cd dir
|
|
|
|
|
echo $contents > foo
|
|
|
|
|
case "$args" in *symlink*) ln -s foo symlink ;; esac
|
1998-06-27 21:03:16 +00:00
|
|
|
case "$args" in *hardlink*) ln foo hardlink ;; esac
|
1998-06-27 20:20:25 +00:00
|
|
|
case "$args" in *sl1*) ln -s foo sl1;; esac
|
|
|
|
|
case "$args" in *sl2*) ln -s foo sl2;; esac
|
2014-11-18 20:20:50 +01:00
|
|
|
case "$args" in *hlsl*) ln sl2 hlsl;; esac
|
1998-06-27 20:20:25 +00:00
|
|
|
(
|
|
|
|
|
(
|
2009-08-22 18:56:06 +02:00
|
|
|
# echo 1>&2 cp $options $args
|
|
|
|
|
cp $options $args 2>_err
|
|
|
|
|
echo $? $options
|
|
|
|
|
|
|
|
|
|
# Normalize the program name and diagnostics in the error output,
|
|
|
|
|
# and put brackets around the output.
|
|
|
|
|
if test -s _err; then
|
|
|
|
|
sed '
|
2018-05-14 02:26:05 -07:00
|
|
|
s/symbolic link/symlink/
|
2009-08-22 18:56:06 +02:00
|
|
|
s/^[^:]*:\([^:]*\).*/cp:\1/
|
|
|
|
|
1s/^/[/
|
|
|
|
|
$s/$/]/
|
|
|
|
|
' _err
|
2007-06-15 22:47:16 +02:00
|
|
|
fi
|
2009-08-22 18:56:06 +02:00
|
|
|
# Strip off all but the file names.
|
2012-04-03 21:42:48 +02:00
|
|
|
ls=$(ls -gG --ignore=_err . \
|
|
|
|
|
| sed \
|
2009-08-22 18:56:06 +02:00
|
|
|
-e '/^total /d' \
|
2012-04-03 21:42:48 +02:00
|
|
|
-e 's/^[^ ]* *[^ ]* *[^ ]* *[^ ]* *[^ ]* *[^ ]* *//')
|
2009-08-22 18:56:06 +02:00
|
|
|
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
|
2012-04-03 20:32:44 +02:00
|
|
|
case "$(cat $f)" in
|
2009-08-22 18:56:06 +02:00
|
|
|
"$contents") ;;
|
|
|
|
|
*) echo cp FAILED;;
|
|
|
|
|
esac
|
|
|
|
|
else
|
|
|
|
|
echo symlink-loop
|
|
|
|
|
fi
|
|
|
|
|
done
|
2003-05-14 06:26:34 +00:00
|
|
|
) | tr '\n' ' '
|
1998-06-27 20:20:25 +00:00
|
|
|
echo
|
|
|
|
|
) | sed 's/ *$//'
|
|
|
|
|
cd ..
|
|
|
|
|
done
|
|
|
|
|
echo
|
|
|
|
|
done
|
|
|
|
|
|
2011-01-28 19:23:21 +01:00
|
|
|
cat <<\EOF | sed "$remove_these_sed" > expected
|
2012-01-07 17:47:58 +01:00
|
|
|
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)
|
|
|
|
|
1 -f [cp: 'foo' and 'symlink' are the same file] (foo symlink -> foo)
|
|
|
|
|
1 -df [cp: 'foo' and 'symlink' are the same file] (foo symlink -> foo)
|
2000-09-04 16:56:09 +00:00
|
|
|
0 --rem (foo symlink)
|
1998-06-27 21:36:28 +00:00
|
|
|
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)
|
2012-01-07 17:47:58 +01:00
|
|
|
1 -l [cp: cannot create hard link 'symlink' to 'foo'] (foo symlink -> foo)
|
ln: replace destination links more atomically
If the file B already exists, commands like 'ln -f A B' and
'cp -fl A B' no longer remove B before creating the new link.
Instead, they arrange for the new link to replace B atomically.
This should fix a race condition reported by Mike Crowe (Bug#25680).
* NEWS, doc/coreutils.texi (cp invocation, ln invocation):
Document this.
* bootstrap.conf (gnulib_modules): Add symlinkat.
* src/copy.c, src/ln.c: Include force-link.h.
* src/copy.c (same_file_ok): It's also OK to remove a destination
symlink when creating symbolic links, or when the source and
destination are on the same file system and when creating hard links.
* src/copy.c (create_hard_link, copy_internal):
* src/ln.c (do_link):
Rewrite using force_linkat and force_symlinkat, to close a window
where the destination temporarily does not exist.
* src/cp.c (main): Do not set x.unlink_dest_before_opening
merely because we are in link-creation mode.
* src/force-link.c, src/force-link.h: New files.
* src/local.mk (copy_sources, src_ln_SOURCES): Add them.
* tests/cp/same-file.sh: Adjust test case to match fixed behavior.
2017-02-11 23:12:31 -08:00
|
|
|
1 -dl [cp: cannot create hard link 'symlink' to 'foo'] (foo symlink -> foo)
|
2007-06-15 22:47:16 +02:00
|
|
|
0 -fl (foo symlink)
|
2000-09-23 19:24:12 +00:00
|
|
|
0 -dfl (foo symlink)
|
2007-06-15 22:47:16 +02:00
|
|
|
0 -bl (foo symlink symlink.~1~ -> foo)
|
1998-06-27 21:36:28 +00:00
|
|
|
0 -bdl (foo symlink symlink.~1~ -> foo)
|
2007-06-15 22:47:16 +02:00
|
|
|
0 -bfl (foo symlink symlink.~1~ -> foo)
|
1998-06-27 21:36:28 +00:00
|
|
|
0 -bdfl (foo symlink symlink.~1~ -> foo)
|
2018-05-14 02:26:05 -07:00
|
|
|
1 -s [cp: cannot create symlink 'symlink' to 'foo'] (foo symlink -> foo)
|
|
|
|
|
0 -sf (foo symlink -> foo)
|
1998-06-27 21:03:16 +00:00
|
|
|
|
2012-01-07 17:47:58 +01:00
|
|
|
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] (foo symlink -> foo)
|
|
|
|
|
1 -df [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
|
|
|
|
|
1 --rem [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
|
|
|
|
|
1 -b [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
|
1998-06-27 21:36:28 +00:00
|
|
|
0 -bd (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop
|
2012-01-07 17:47:58 +01:00
|
|
|
1 -bf [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
|
1998-06-27 21:36:28 +00:00
|
|
|
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 -bfl (foo symlink -> foo)
|
2018-05-14 02:26:05 -07:00
|
|
|
1 -s [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
|
|
|
|
|
1 -sf [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
|
1998-06-27 21:03:16 +00:00
|
|
|
|
2012-01-07 17:47:58 +01:00
|
|
|
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 --rem [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)
|
1998-06-27 21:36:28 +00:00
|
|
|
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~)
|
2018-05-14 02:26:05 -07:00
|
|
|
1 -s [cp: 'foo' and 'foo' are the same file] (foo)
|
|
|
|
|
1 -sf [cp: 'foo' and 'foo' are the same file] (foo)
|
1998-06-27 21:03:16 +00:00
|
|
|
|
2012-01-07 17:47:58 +01:00
|
|
|
1 [cp: 'sl1' and 'sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
|
2000-09-04 16:56:09 +00:00
|
|
|
0 -d (foo sl1 -> foo sl2 -> foo)
|
2012-01-07 17:47:58 +01:00
|
|
|
1 -f [cp: 'sl1' and 'sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
|
1998-07-20 03:59:51 +00:00
|
|
|
0 -df (foo sl1 -> foo sl2 -> foo)
|
2000-09-04 16:56:09 +00:00
|
|
|
0 --rem (foo sl1 -> foo sl2)
|
1998-06-27 21:36:28 +00:00
|
|
|
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)
|
2012-01-07 17:47:58 +01:00
|
|
|
1 -l [cp: cannot create hard link 'sl2' to 'sl1'] (foo sl1 -> foo sl2 -> foo)
|
2013-11-07 23:35:52 +01:00
|
|
|
0 -fl (foo sl1 -> foo sl2)
|
|
|
|
|
0 -bl (foo sl1 -> foo sl2 sl2.~1~ -> foo)
|
|
|
|
|
0 -bfl (foo sl1 -> foo sl2 sl2.~1~ -> foo)
|
2018-05-14 02:26:05 -07:00
|
|
|
1 -s [cp: cannot create symlink 'sl2' to 'sl1'] (foo sl1 -> foo sl2 -> foo)
|
|
|
|
|
0 -sf (foo sl1 -> foo sl2 -> sl1)
|
1998-06-27 21:03:16 +00:00
|
|
|
|
2012-01-07 17:47:58 +01:00
|
|
|
1 [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
|
|
|
|
|
1 -d [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
|
|
|
|
|
1 -f [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
|
|
|
|
|
1 -df [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
|
2000-09-04 16:56:09 +00:00
|
|
|
0 --rem (foo hardlink)
|
1998-06-27 21:36:28 +00:00
|
|
|
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)
|
2018-05-14 02:26:05 -07:00
|
|
|
1 -s [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
|
|
|
|
|
1 -sf [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
|
1998-06-27 20:20:25 +00:00
|
|
|
|
2014-11-18 20:20:50 +01:00
|
|
|
1 [cp: 'hlsl' and 'sl2' are the same file] (foo hlsl -> foo sl2 -> foo)
|
|
|
|
|
0 -d (foo hlsl -> foo sl2 -> foo)
|
|
|
|
|
1 -f [cp: 'hlsl' and 'sl2' are the same file] (foo hlsl -> foo sl2 -> foo)
|
|
|
|
|
0 -df (foo hlsl -> foo sl2 -> foo)
|
|
|
|
|
0 --rem (foo hlsl -> foo sl2)
|
|
|
|
|
0 -b (foo hlsl -> foo sl2 sl2.~1~ -> foo)
|
|
|
|
|
0 -bd (foo hlsl -> foo sl2 -> foo sl2.~1~ -> foo)
|
|
|
|
|
0 -bf (foo hlsl -> foo sl2 sl2.~1~ -> foo)
|
|
|
|
|
0 -bdf (foo hlsl -> foo sl2 -> foo sl2.~1~ -> foo)
|
|
|
|
|
1 -l [cp: cannot create hard link 'sl2' to 'hlsl'] (foo hlsl -> foo sl2 -> foo)
|
|
|
|
|
0 -dl (foo hlsl -> foo sl2 -> foo)
|
|
|
|
|
0 -fl (foo hlsl -> foo sl2)
|
|
|
|
|
0 -dfl (foo hlsl -> foo sl2 -> foo)
|
|
|
|
|
0 -bl (foo hlsl -> foo sl2 sl2.~1~ -> foo)
|
|
|
|
|
0 -bdl (foo hlsl -> foo sl2 -> foo)
|
|
|
|
|
0 -bfl (foo hlsl -> foo sl2 sl2.~1~ -> foo)
|
|
|
|
|
0 -bdfl (foo hlsl -> foo sl2 -> foo)
|
2018-05-14 02:26:05 -07:00
|
|
|
1 -s [cp: cannot create symlink 'sl2' to 'hlsl'] (foo hlsl -> foo sl2 -> foo)
|
|
|
|
|
0 -sf (foo hlsl -> foo sl2 -> hlsl)
|
2014-11-18 20:20:50 +01:00
|
|
|
|
1998-06-27 20:20:25 +00:00
|
|
|
EOF
|
|
|
|
|
|
2010-12-03 11:08:48 -08:00
|
|
|
exec 1>&3 3>&-
|
1998-08-12 17:14:34 +00:00
|
|
|
|
2011-01-28 19:23:21 +01:00
|
|
|
compare expected actual 1>&2 || fail=1
|
2000-09-25 20:23:43 +00:00
|
|
|
|
2008-09-07 10:31:27 +02:00
|
|
|
Exit $fail
|