maint: update all copyright year number ranges
[coreutils.git] / tests / tail-2 / symlink.sh
blobe3a9a60b7916a0b10652a48c81398a058068e90b
1 #!/bin/sh
2 # Ensure tail tracks symlinks properly.
4 # Copyright (C) 2013-2017 Free Software Foundation, Inc.
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.
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.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ tail
22 # Function to count number of lines from tail
23 # while ignoring transient errors due to resource limits
24 countlines_ ()
26 grep -Ev 'inotify (resources exhausted|cannot be used)' out | wc -l
29 # Function to check the expected line count in 'out'.
30 # Called via retry_delay_(). Sleep some time - see retry_delay_() - if the
31 # line count is still smaller than expected.
32 wait4lines_ ()
34 local delay=$1
35 local elc=$2 # Expected line count.
36 [ "$(countlines_)" -ge "$elc" ] || { sleep $delay; return 1; }
39 # Terminate any background tail process
40 cleanup_() { kill $pid 2>/dev/null && wait $pid; }
42 # speedup non inotify case
43 fastpoll='-s.1 --max-unchanged-stats=1'
45 # Ensure changing targets of cli specified symlinks are handled.
46 # Prior to v8.22, inotify would fail to recognize changes in the targets.
47 # Clear 'out' so that we can check its contents without races.
48 >out || framework_failure_
49 ln -nsf target symlink || framework_failure_
50 timeout 10 tail $fastpoll -F symlink >out 2>&1 & pid=$!
51 # Wait for "cannot open..."
52 retry_delay_ wait4lines_ .1 6 1 || { cat out; fail=1; }
53 echo "X" > target || framework_failure_
54 # Wait for the expected output.
55 retry_delay_ wait4lines_ .1 6 3 || { cat out; fail=1; }
56 cleanup_
57 # Expect 3 lines in the output file.
58 [ "$(countlines_)" = 3 ] || { fail=1; cat out; }
59 grep -F 'cannot open' out || { fail=1; cat out; }
60 grep -F 'has appeared' out || { fail=1; cat out; }
61 grep '^X$' out || { fail=1; cat out; }
62 rm -f target out || framework_failure_
64 # Ensure we correctly handle the source symlink itself changing.
65 # I.e., that we don't operate solely on the targets.
66 # Clear 'out' so that we can check its contents without races.
67 >out || framework_failure_
68 echo "X1" > target1 || framework_failure_
69 ln -nsf target1 symlink || framework_failure_
70 timeout 10 tail $fastpoll -F symlink >out 2>&1 & pid=$!
71 # Wait for the expected output.
72 retry_delay_ wait4lines_ .1 6 1 || { cat out; fail=1; }
73 ln -nsf target2 symlink || framework_failure_
74 # Wait for "become inaccess..."
75 retry_delay_ wait4lines_ .1 6 2 || { cat out; fail=1; }
76 echo "X2" > target2 || framework_failure_
77 # Wait for the expected output.
78 retry_delay_ wait4lines_ .1 6 4 || { cat out; fail=1; }
79 cleanup_
80 # Expect 4 lines in the output file.
81 [ "$(countlines_)" = 4 ] || { fail=1; cat out; }
82 grep -F 'become inacce' out || { fail=1; cat out; }
83 grep -F 'has appeared' out || { fail=1; cat out; }
84 grep '^X1$' out || { fail=1; cat out; }
85 grep '^X2$' out || { fail=1; cat out; }
86 rm -f target1 target2 out || framework_failure_
88 # Note other symlink edge cases are currently just diagnosed
89 # rather than being handled. I.e., if you specify a missing item,
90 # or existing file that later change to a symlink, if inotify
91 # is in use, you'll get a diagnostic saying that link will
92 # no longer be tailed.
94 Exit $fail