mirror of
https://git.savannah.gnu.org/git/coreutils.git
synced 2026-11-17 18:02:09 +03:00
cp, mv: use linkat to guarantee semantics
* src/copy.c (copy_internal): Use linkat, not link.
This commit is contained in:
1 parent
efcee783e4
commit
ca9e212cf8
1 file changed
+11
-18
+11
-18
@@ -1687,7 +1687,9 @@ copy_internal (char const *src_name, char const *dst_name,
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bool link_failed = (link (earlier_file, dst_name) != 0);
|
/* We want to guarantee that symlinks are not followed. */
|
||||||
|
bool link_failed = (linkat (AT_FDCWD, earlier_file, AT_FDCWD,
|
||||||
|
dst_name, 0) != 0);
|
||||||
|
|
||||||
/* If the link failed because of an existing destination,
|
/* If the link failed because of an existing destination,
|
||||||
remove that file and then call link again. */
|
remove that file and then call link again. */
|
||||||
@@ -1700,7 +1702,8 @@ copy_internal (char const *src_name, char const *dst_name,
|
|||||||
}
|
}
|
||||||
if (x->verbose)
|
if (x->verbose)
|
||||||
printf (_("removed %s\n"), quote (dst_name));
|
printf (_("removed %s\n"), quote (dst_name));
|
||||||
link_failed = (link (earlier_file, dst_name) != 0);
|
link_failed = (linkat (AT_FDCWD, earlier_file, AT_FDCWD,
|
||||||
|
dst_name, 0) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (link_failed)
|
if (link_failed)
|
||||||
@@ -1990,25 +1993,15 @@ copy_internal (char const *src_name, char const *dst_name,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* POSIX 2008 states that it is implementation-defined whether
|
/* cp, invoked with `--link --no-dereference', should not follow the
|
||||||
link() on a symlink creates a hard-link to the symlink, or only
|
link; we guarantee this with gnulib's linkat module (on systems
|
||||||
to the referent (effectively dereferencing the symlink) (POSIX
|
where link(2) follows the link, gnulib creates a symlink with
|
||||||
2001 required the latter behavior, although many systems provided
|
identical contents, which is good enough for our purposes). */
|
||||||
the former). Yet cp, invoked with `--link --no-dereference',
|
|
||||||
should not follow the link. We can approximate the desired
|
|
||||||
behavior by skipping this hard-link creating block and instead
|
|
||||||
copying the symlink, via the `S_ISLNK'- copying code below.
|
|
||||||
LINK_FOLLOWS_SYMLINKS is tri-state; if it is -1, we don't know
|
|
||||||
how link() behaves, so we use the fallback case for safety.
|
|
||||||
|
|
||||||
FIXME - use a gnulib linkat emulation for more fine-tuned
|
|
||||||
emulation, particularly when LINK_FOLLOWS_SYMLINKS is -1. */
|
|
||||||
else if (x->hard_link
|
else if (x->hard_link
|
||||||
&& (!LINK_FOLLOWS_SYMLINKS
|
&& (!S_ISLNK (src_mode)
|
||||||
|| !S_ISLNK (src_mode)
|
|
||||||
|| x->dereference != DEREF_NEVER))
|
|| x->dereference != DEREF_NEVER))
|
||||||
{
|
{
|
||||||
if (link (src_name, dst_name))
|
if (linkat (AT_FDCWD, src_name, AT_FDCWD, dst_name, 0))
|
||||||
{
|
{
|
||||||
error (0, errno, _("cannot create link %s"), quote (dst_name));
|
error (0, errno, _("cannot create link %s"), quote (dst_name));
|
||||||
goto un_backup;
|
goto un_backup;
|
||||||
|
|||||||
Reference in new issue
Block a user