1#!/bin/sh
2# Exercise the fix for https://bugs.gnu.org/8419
3
4# Copyright (C) 2011-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ cp
21
22same_inode()
23{
24  local u v
25  u=$(stat --format %i "$1") &&
26    v=$(stat --format %i "$2") && test "$u" = "$v"
27}
28
29create_source_tree()
30{
31  rm -Rf s
32  mkdir s || framework_failure_
33
34  # a missing link in dest will be created
35  touch s/f || framework_failure_
36  ln s/f s/linkm || framework_failure_
37
38  # an existing link in dest will be maintained
39  ln s/f s/linke || framework_failure_
40
41  # a separate older file in dest will be overwritten
42  ln s/f s/fileo || framework_failure_
43
44  # a separate newer file in dest will be overwritten!
45  ln s/f s/fileu || framework_failure_
46}
47
48create_target_tree()
49{
50  f=$1 # which of f or linkm to create in t/
51
52  rm -Rf t
53  mkdir -p t/s/ || framework_failure_
54
55  # a missing link in dest must be created
56  touch t/s/$f || framework_failure_
57
58  # an existing link must be maintained
59  ln t/s/$f t/s/linke || framework_failure_
60
61  # a separate older file in dest will be overwritten
62  touch -d '-1 hour' t/s/fileo || framework_failure_
63
64  # a separate newer file in dest will be overwritten!
65  touch -d '+1 hour' t/s/fileu || framework_failure_
66}
67
68
69# Note we repeat this, creating either one of
70# two hard linked files from source in the dest, so as to
71# test both paths in $(cp) for creating the hard links.
72# The path taken by cp is dependent on which cp encounters
73# first in the source, which is non deterministic currently
74# (I'm guessing that results are sorted by inode and
75# because they're the same here, and due to the sort
76# being unstable, either can be processed first).
77create_source_tree
78
79for f in f linkm; do
80  create_target_tree $f
81
82  # Copy all the hard links across.  With cp from coreutils-8.12
83  # and prior, it would sometimes mistakenly copy rather than link.
84  cp -au s t || fail=1
85
86  same_inode t/s/f t/s/linkm || fail=1
87  same_inode t/s/f t/s/linke || fail=1
88  same_inode t/s/f t/s/fileo || fail=1
89  same_inode t/s/f t/s/fileu || fail=1
90done
91
92Exit $fail
93