1#!/bin/sh
2# Demonstrate that tail -F works when renaming the tailed files.
3# Between coreutils 7.5 and 8.2 inclusive, 'tail -F a b' would
4# stop tracking additions to b after 'mv a b'.
5
6# Copyright (C) 2009-2023 Free Software Foundation, Inc.
7
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
21. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
22print_ver_ tail
23
24check_tail_output() {
25  local delay="$1"
26  if [ "$tail_re" ]; then
27    grep "$tail_re" err ||
28      { sleep $delay; return 1; }
29  else
30    local tail_re="==> $file <==@$data@"
31    tr '\n' @ < out | grep "$tail_re" > /dev/null ||
32      { sleep $delay; return 1; }
33  fi
34  unset tail_re
35}
36
37# Terminate any background tail process
38cleanup_() { kill $pid 2>/dev/null && wait $pid; }
39
40# Speedup the non inotify case
41fastpoll='-s.1 --max-unchanged-stats=1'
42
43for mode in '' '---disable-inotify'; do
44  rm -f a b out err
45  touch a b || framework_failure_
46
47  tail $mode -F $fastpoll a b >out 2>err & pid=$!
48
49  # Wait up to 12.7s for tail to start.
50  echo x > a
51  file=a data=x retry_delay_ check_tail_output .1 7 || { cat out; fail=1; }
52
53  mv a b || framework_failure_
54
55  # Wait 12.7s for this diagnostic:
56  # tail: 'a' has become inaccessible: No such file or directory
57  tail_re='inaccessible' retry_delay_ check_tail_output .1 7 ||
58    { cat out; fail=1; }
59
60  # Wait 12.7s for this diagnostic:
61  # tail: 'b' has been replaced;  following new file
62  tail_re='replaced' retry_delay_ check_tail_output .1 7 ||
63    { cat out; fail=1; }
64  # Wait up to 12.7s for "x" to be displayed:
65  file='b' data='x' retry_delay_ check_tail_output .1 7 ||
66    { echo "$0: b: unexpected delay?"; cat out; fail=1; }
67
68  echo x2 > a
69  # Wait up to 12.7s for this to appear in the output:
70  # "tail: '...' has appeared;  following new file"
71  tail_re='has appeared' retry_delay_ check_tail_output .1 7 ||
72    { echo "$0: a: unexpected delay?"; cat out; fail=1; }
73  # Wait up to 12.7s for "x2" to be displayed:
74  file='a' data='x2' retry_delay_ check_tail_output .1 7 ||
75    { echo "$0: a: unexpected delay 2?"; cat out; fail=1; }
76
77  echo y >> b
78  # Wait up to 12.7s for "y" to appear in the output:
79  file='b' data='y' retry_delay_ check_tail_output .1 7 ||
80    { echo "$0: b: unexpected delay 2?"; cat out; fail=1; }
81
82  echo z >> a
83  # Wait up to 12.7s for "z" to appear in the output:
84  file='a' data='z' retry_delay_ check_tail_output .1 7 ||
85    { echo "$0: a: unexpected delay 3?"; cat out; fail=1; }
86
87  cleanup_
88done
89
90Exit $fail
91