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
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)
}
}
- /* 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;
}
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. */
#endif
)
{
- preserve_metadata = false;
if (link (src_name, dst_name))
{
error (0, errno, _("cannot create link %s"), quote (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));
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'
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;
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)
}
}
+ /* 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)))
--- /dev/null
+#!/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