3 # Copyright (C) 1997-2020 Free Software Foundation, Inc.
4 # This file is part of the GNU C Library.
6 # The GNU C Library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # The GNU C Library 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 GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with the GNU C Library; if not, see
18 # <https://www.gnu.org/licenses/>.
22 # The common objpfx, used to find libraries and the dynamic loader.
25 # We expect one parameter which is the test program. This must understand
27 # --phys use the FTW_PHYS flag
28 # --chdir use the FTW_CHDIR and print the current directory
30 # --depth use the FTW_DEPTH flag
31 # --early-exit print file@2 item only and return non-zero from the
32 # callback when it is seen
35 # We cannot test this as root.
36 if test `id | sed "s/uid=\([0-9]*\).*/\1/"` = 0; then
40 # Since we use `sort' we must make sure to use the same locale everywhere.
44 # First create our scenario:
48 trap 'chmod -fR a+x $tmpdir; rm -fr $tmpdir $testout' 0 1 2 3 15
50 if test -d $tmpdir; then
58 mkdir $tmpdir/foo/lvl1
59 echo > $tmpdir/foo/lvl1/file@1
60 mkdir $tmpdir/foo/lvl1/lvl2
61 echo > $tmpdir/foo/lvl1/lvl2/file@2
62 mkdir $tmpdir/foo/lvl1/lvl2/lvl3
63 echo > $tmpdir/foo/lvl1/lvl2/lvl3/file@3
64 ln -s $tmpdir $tmpdir/foo/lvl1/lvl2/lvl3/link@3
65 ln -s $tmpdir/foo/lvl1/lvl2 $tmpdir/foo/lvl1/lvl2/link@2
66 ln -s $tmpdir/foo/lvl1/lvl2/lvl3/lvl4 $tmpdir/foo/lvl1/link@1
68 chmod a-x,a+r $tmpdir/bar
70 testout=$tmp/ftwtest-tmp.out
72 $testprogram $tmpdir |
75 cat <<EOF | cmp $testout - || exit 1
76 base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
77 base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
78 base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
79 base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
80 base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_NS, level = 2
81 base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
82 base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
83 base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, level = 3
84 base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, level = 3
85 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
86 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, level = 4
87 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
91 $testprogram --depth $tmpdir |
94 cat <<EOF | cmp $testout - || exit 1
95 base = "$tmp/", file = "ftwtest.d", flag = FTW_DP, level = 0
96 base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_DP, level = 1
97 base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
98 base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_DP, level = 1
99 base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_NS, level = 2
100 base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_DP, level = 2
101 base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
102 base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, level = 3
103 base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_DP, level = 3
104 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
105 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_DP, level = 4
106 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
110 $testprogram --phys $tmpdir |
113 cat <<EOF | cmp $testout - || exit 1
114 base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
115 base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
116 base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
117 base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
118 base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_NS, level = 2
119 base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
120 base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
121 base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SL, level = 3
122 base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, level = 3
123 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
124 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "link@2", flag = FTW_SL, level = 4
125 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, level = 4
126 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
127 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "link@3", flag = FTW_SL, level = 5
131 # For the next test everything must be readable.
132 chmod -fR a+x $tmpdir
134 $testprogram --chdir $tmpdir |
137 # perhaps $tmp involves some symlinks...
138 tmpreal=`cd $tmp; pwd -P 2>/dev/null`
140 cat <<EOF | cmp $testout - || exit 1
141 base = "$tmp/", file = "ftwtest.d", flag = FTW_D, cwd = $tmpreal, level = 0
142 base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
143 base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, cwd = $tmpreal/ftwtest.d, level = 1
144 base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
145 base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, cwd = $tmpreal/ftwtest.d/bar, level = 2
146 base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo, level = 2
147 base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
148 base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
149 base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
150 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
151 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
152 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2/lvl3, level = 5
156 curwd=`pwd -P 2>/dev/null`
158 $testprogram --chdir ftwtest.d |
162 cat <<EOF | diff -u $testout - || exit 1
163 base = "", file = "ftwtest.d", flag = FTW_D, cwd = $tmpreal, level = 0
164 base = "ftwtest.d/", file = "bar", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
165 base = "ftwtest.d/", file = "baz", flag = FTW_F, cwd = $tmpreal/ftwtest.d, level = 1
166 base = "ftwtest.d/", file = "foo", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
167 base = "ftwtest.d/bar/", file = "xo", flag = FTW_F, cwd = $tmpreal/ftwtest.d/bar, level = 2
168 base = "ftwtest.d/foo/", file = "lvl1", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo, level = 2
169 base = "ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
170 base = "ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
171 base = "ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
172 base = "ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
173 base = "ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
174 base = "ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2/lvl3, level = 5
180 $testprogram --chdir ftwtest.d/. |
184 cat <<EOF | diff -u $testout - || exit 1
185 base = "ftwtest.d/", file = ".", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 0
186 base = "ftwtest.d/./", file = "bar", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
187 base = "ftwtest.d/./", file = "baz", flag = FTW_F, cwd = $tmpreal/ftwtest.d, level = 1
188 base = "ftwtest.d/./", file = "foo", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
189 base = "ftwtest.d/./bar/", file = "xo", flag = FTW_F, cwd = $tmpreal/ftwtest.d/bar, level = 2
190 base = "ftwtest.d/./foo/", file = "lvl1", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo, level = 2
191 base = "ftwtest.d/./foo/lvl1/", file = "file@1", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
192 base = "ftwtest.d/./foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
193 base = "ftwtest.d/./foo/lvl1/", file = "lvl2", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
194 base = "ftwtest.d/./foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
195 base = "ftwtest.d/./foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
196 base = "ftwtest.d/./foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2/lvl3, level = 5
200 curwd=`pwd -P 2>/dev/null`
202 $testprogram --chdir ftwtest.d/foo/lvl1/link@1 |
206 cat <<EOF | diff -u $testout - || exit 1
207 base = "ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 0
211 $testprogram --early-exit $tmpdir |
214 cat <<EOF | cmp $testout - || exit 1
215 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
220 mkdir $tmpdir/foo/lvl1b
221 echo > $tmpdir/foo/lvl1b/file@1b
222 echo > $tmpdir/foo/lvl1b/file2@1b
223 echo > $tmpdir/foo/lvl1b/file3@1b
225 $testprogram --skip-subtree=lvl1 $tmpdir |
228 cat <<EOF | diff -u $testout - || exit 1
229 base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
230 base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
231 base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
232 base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
233 base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, level = 2
234 base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
235 base = "$tmp/ftwtest.d/foo/", file = "lvl1b", flag = FTW_D, level = 2
236 base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file2@1b", flag = FTW_F, level = 3
237 base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file3@1b", flag = FTW_F, level = 3
238 base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file@1b", flag = FTW_F, level = 3
242 $testprogram --skip-siblings=lvl1 $tmpdir |
245 # The filesystem is not required to put lvl1 before lvl1b.
246 # If lvl1b comes after lvl1, it shouldn't be printed, while if it
247 # comes before, it should.
249 [ -n "`ls -U $tmpdir/foo/ | sed -n '/lvl1$/,${/lvl1b$/p;}'`" ] \
250 && catcmd="grep -v lvl1b"
252 $catcmd <<EOF | diff -u $testout - || exit 1
253 base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
254 base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
255 base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
256 base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
257 base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, level = 2
258 base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
259 base = "$tmp/ftwtest.d/foo/", file = "lvl1b", flag = FTW_D, level = 2
260 base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file2@1b", flag = FTW_F, level = 3
261 base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file3@1b", flag = FTW_F, level = 3
262 base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file@1b", flag = FTW_F, level = 3
266 $testprogram --skip-siblings=file@1b $tmpdir |
269 # The filesystem is not required to put file2@1b and file3@1b after file@1b.
270 # If file[23]@1b come after file@1b, it shouldn't be printed, while if they
271 # come before, they should.
272 regexp=`echo $(ls -U $tmp/ftwtest.d/foo/lvl1b \
273 | sed -n '/file@1b$/,${/file[23]@1b$/p;}') | sed 's, ,|,'`
275 [ -n "$regexp" ] && catcmd="egrep -v $regexp"
277 $catcmd <<EOF | diff -u $testout - || exit 1
278 base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
279 base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
280 base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
281 base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
282 base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, level = 2
283 base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
284 base = "$tmp/ftwtest.d/foo/", file = "lvl1b", flag = FTW_D, level = 2
285 base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
286 base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, level = 3
287 base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, level = 3
288 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
289 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, level = 4
290 base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
291 base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file2@1b", flag = FTW_F, level = 3
292 base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file3@1b", flag = FTW_F, level = 3
293 base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file@1b", flag = FTW_F, level = 3