summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Meyering <meyering@redhat.com>2009-07-27 17:08:02 +0200
committerJim Meyering <meyering@redhat.com>2009-08-04 17:38:54 +0200
commiteae535e1a322e244248bd31ed7e21ac95ecaa16d (patch)
treeecc8956558befd65ef146eb233d46e16dba7c735
parent1762092901adf040445d59931d14837ef7f2ebe1 (diff)
downloadcoreutils-eae535e1a322e244248bd31ed7e21ac95ecaa16d.tar.xz
cp -pP (and e.g., -a): preserve time stamps on symlinks, too
* src/copy.c (utimensat_if_possible): New function. (copy_internal): Remove variable, "preserve_metadata". Replace with "dest_is_symlink". That covers all cases but one: the one in which cp --link has created hard links to non-directories. In that case, there is no need to update attributes of the links. Use utimensat_if_possible, to preserve timestamps of symlinks. * NEWS (New features): Mention this. * tests/Makefile.am (TESTS): Add cp/preserve-slink-time. * tests/cp/preserve-slink-time: New file. * m4/jm-macros.m4 (coreutils_MACROS): Test for utimensat. Reported in http://bugzilla.redhat.com/230866
-rw-r--r--NEWS2
-rw-r--r--m4/jm-macros.m43
-rw-r--r--src/copy.c42
-rw-r--r--tests/Makefile.am1
-rwxr-xr-xtests/cp/preserve-slink-time44
5 files changed, 76 insertions, 16 deletions
diff --git a/NEWS b/NEWS
index 5058b5783..0a0d4a719 100644
--- a/NEWS
+++ b/NEWS
@@ -38,6 +38,8 @@ GNU coreutils NEWS -*- outline -*-
chroot now accepts the options --userspec and --groups.
+ cp now preserves time stamps on symbolic links, when possible
+
cp, install, mv: take advantage of btrfs' O(1) copy-on-write feature
when both the source and destination are on the same btrfs partition.
diff --git a/m4/jm-macros.m4 b/m4/jm-macros.m4
index f862984cc..32a0954a7 100644
--- a/m4/jm-macros.m4
+++ b/m4/jm-macros.m4
@@ -89,6 +89,9 @@ AC_DEFUN([coreutils_MACROS],
tcgetpgrp \
)
+ # for cp.c
+ AC_CHECK_FUNCS_ONCE([utimensat])
+
dnl This can't use AC_REQUIRE; I'm not quite sure why.
cu_PREREQ_STAT_PROG
diff --git a/src/copy.c b/src/copy.c
index bbed336b1..24b5f6b46 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -118,6 +118,20 @@ static bool owner_failure_ok (struct cp_options const *x);
static char const *top_level_src_name;
static char const *top_level_dst_name;
+/* Wrap utimensat-with-AT_FDCWD and utimens, to keep these
+ cpp directives out of the main code. */
+static inline int
+utimensat_if_possible (char const *file, struct timespec const *timespec)
+{
+ return
+#if HAVE_UTIMENSAT
+ utimensat (AT_FDCWD, file, timespec, AT_SYMLINK_NOFOLLOW)
+#else
+ utimens (file, timespec)
+#endif
+ ;
+}
+
/* Perform the O(1) btrfs clone operation, if possible.
Upon success, return 0. Otherwise, return -1 and set errno. */
static inline int
@@ -1211,7 +1225,7 @@ copy_internal (char const *src_name, char const *dst_name,
bool backup_succeeded = false;
bool delayed_ok;
bool copied_as_regular = false;
- bool preserve_metadata;
+ bool dest_is_symlink = false;
bool have_dst_lstat = false;
if (x->move_mode && rename_succeeded)
@@ -1810,12 +1824,6 @@ copy_internal (char const *src_name, char const *dst_name,
}
}
- /* In certain modes (cp's --symbolic-link), and for certain file types
- (symlinks and hard links) it doesn't make sense to preserve metadata,
- or it's possible to preserve only some of it.
- In such cases, set this variable to zero. */
- preserve_metadata = true;
-
if (S_ISDIR (src_mode))
{
struct dir_list *dir;
@@ -1909,8 +1917,7 @@ copy_internal (char const *src_name, char const *dst_name,
}
else if (x->symbolic_link)
{
- preserve_metadata = false;
-
+ dest_is_symlink = true;
if (*src_name != '/')
{
/* Check that DST_NAME denotes a file in the current directory. */
@@ -1962,7 +1969,6 @@ copy_internal (char const *src_name, char const *dst_name,
#endif
)
{
- preserve_metadata = false;
if (link (src_name, dst_name))
{
error (0, errno, _("cannot create link %s"), quote (dst_name));
@@ -2007,6 +2013,7 @@ copy_internal (char const *src_name, char const *dst_name,
else if (S_ISLNK (src_mode))
{
char *src_link_val = areadlink_with_size (src_name, src_sb.st_size);
+ dest_is_symlink = true;
if (src_link_val == NULL)
{
error (0, errno, _("cannot read symbolic link %s"), quote (src_name));
@@ -2045,9 +2052,6 @@ copy_internal (char const *src_name, char const *dst_name,
if (x->preserve_security_context)
restore_default_fscreatecon_or_die ();
- /* There's no need to preserve timestamps or permissions. */
- preserve_metadata = false;
-
if (x->preserve_ownership)
{
/* Preserve the owner and group of the just-`copied'
@@ -2084,8 +2088,10 @@ copy_internal (char const *src_name, char const *dst_name,
record_file (x->dest_info, dst_name, &sb);
}
- if ( ! preserve_metadata)
- return true;
+ /* If we've just created a hard-link due to cp's --link option,
+ we're done. */
+ if (x->hard_link && ! S_ISDIR (src_mode))
+ return delayed_ok;
if (copied_as_regular)
return delayed_ok;
@@ -2108,7 +2114,7 @@ copy_internal (char const *src_name, char const *dst_name,
timespec[0] = get_stat_atime (&src_sb);
timespec[1] = get_stat_mtime (&src_sb);
- if (utimens (dst_name, timespec) != 0)
+ if (utimensat_if_possible (dst_name, timespec) != 0)
{
error (0, errno, _("preserving times for %s"), quote (dst_name));
if (x->require_preserve)
@@ -2116,6 +2122,10 @@ copy_internal (char const *src_name, char const *dst_name,
}
}
+ /* The operations beyond this point may dereference a symlink. */
+ if (dest_is_symlink)
+ return delayed_ok;
+
/* Avoid calling chown if we know it's not necessary. */
if (x->preserve_ownership
&& (new_dst || !SAME_OWNER_AND_GROUP (src_sb, dst_sb)))
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 59737a05d..ad19d0264 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -287,6 +287,7 @@ TESTS = \
cp/parent-perm-race \
cp/perm \
cp/preserve-2 \
+ cp/preserve-slink-time \
cp/proc-short-read \
cp/proc-zero-len \
cp/r-vs-symlink \
diff --git a/tests/cp/preserve-slink-time b/tests/cp/preserve-slink-time
new file mode 100755
index 000000000..c5c21a5e1
--- /dev/null
+++ b/tests/cp/preserve-slink-time
@@ -0,0 +1,44 @@
+#!/bin/sh
+# Verify that cp -Pp preserves times even on symlinks.
+
+# Copyright (C) 2009 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (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
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ cp --version
+fi
+
+. $srcdir/test-lib.sh
+
+grep '^#define HAVE_UTIMENSAT' "$CONFIG_HEADER" > /dev/null ||
+ skip_test_ 'this system lacks the utimensat function'
+
+ln -s no-such dangle || framework_failure
+
+# If the current file system lacks sub-second resolution, sleep for 2s to
+# ensure that the times on the copy are different from those of the original.
+case $(stat --format=%y dangle) in
+ ??:??:??.000000000) sleep 2;;
+esac
+
+cp -Pp dangle d2 || framework_failure
+stat --format=%y dangle > t1 || framework_failure
+stat --format=%y d2 > t2 || framework_failure
+
+fail=0
+compare t1 t2 || fail=1
+
+Exit $fail