From 376967889ed7ed561e46ff6d88a66779db62737a Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 11 Feb 2017 23:12:31 -0800 Subject: 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. --- doc/coreutils.texi | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'doc/coreutils.texi') diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 3eac96b7d..2dbfccecc 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -8125,9 +8125,11 @@ Equivalent to @option{--no-dereference --preserve=links}. When copying without this option and an existing destination file cannot be opened for writing, the copy fails. However, with @option{--force}, when a destination file cannot be opened, @command{cp} then removes it and -tries to open it again. Contrast this behavior with that enabled by -@option{--link} and @option{--symbolic-link}, whereby the destination file -is never opened but rather is removed unconditionally. Also see the +tries to open it again. When this option is combined with +@option{--link} (@option{-l}) or @option{--symbolic-link} +(@option{-s}), the destination link is replaced, and unless +@option{--backup} (@option{-b}) is also given there is no brief +moment when the destination does not exist. Also see the description of @option{--remove-destination}. This option is independent of the @option{--interactive} or @@ -9825,11 +9827,13 @@ directory, using the @var{target}s' names. @end itemize -Normally @command{ln} does not remove existing files. Use the -@option{--force} (@option{-f}) option to remove them unconditionally, -the @option{--interactive} (@option{-i}) option to remove them +Normally @command{ln} does not replace existing files. Use the +@option{--force} (@option{-f}) option to replace them unconditionally, +the @option{--interactive} (@option{-i}) option to replace them conditionally, and the @option{--backup} (@option{-b}) option to -rename them. +rename them. Unless the @option{--backup} (@option{-b}) option is +used there is no brief moment when the destination does not exist; +this is an extension to POSIX. @cindex hard link, defined @cindex inode, and hard links -- cgit v1.2.3-54-g00ecf