1#!/bin/sh
2# readlink from 6.9 would fail with a false-positive symlink loop error
3
4# Copyright (C) 2007-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_ readlink pwd
21cwd=$(env pwd -P)
22
23# To trigger this bug, we have to construct a name/situation during
24# the resolution of which the code dereferences the same symlink (S)
25# two different times with no actual loop.  In addition, arrange
26# so that the second and fourth calls to readlink operate on S.
27
28ln -s s p        || framework_failure_
29ln -s d s        || framework_failure_
30mkdir d          || framework_failure_
31echo 2 > d/2     || framework_failure_
32ln -s ../s/2 d/1 || framework_failure_
33
34# With coreutils-6.9, this would fail with ELOOP.
35readlink -v -e p/1 > out || fail=1
36# readlink -e d/2 > exp || fail=1
37echo "$cwd/d/2" > exp || framework_failure_
38compare exp out || fail=1
39
40# Construct a real loop and make sure readlink still detects it.
41ln -sf ../s/1 d/2 || framework_failure_
42readlink -v -e p/1 2> out && fail=1
43readlink_msg=$(cat out)
44case $readlink_msg in
45  "readlink: p/1: "*) ;;
46  *) fail=1;;
47esac
48symlink_loop_msg=${readlink_msg#"readlink: p/1: "}
49
50# Exercise the hash table code.
51ln -nsf ../s/3 d/2 || framework_failure_
52ln -nsf ../p/4 d/3 || framework_failure_
53ln -nsf ../p/5 d/4 || framework_failure_
54ln -nsf ../p/6 d/5 || framework_failure_
55ln -nsf ../p/7 d/6 || framework_failure_
56ln -nsf ../p/8 d/7 || framework_failure_
57echo x > d/8       || framework_failure_
58readlink -v -e p/1 > out || fail=1
59echo "$cwd/d/8" > exp || framework_failure_
60compare exp out || fail=1
61
62# A trivial loop
63ln -s loop loop
64readlink -v -e loop 2> out && fail=1
65echo "readlink: loop: $symlink_loop_msg" > exp || framework_failure_
66compare exp out || fail=1
67
68Exit $fail
69