blob: 478171db78e9023cc19b849c04ada6a8ba0bc47c [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#! /bin/sh
2# Test for nftw(3).
3# Copyright (C) 1997-2015 Free Software Foundation, Inc.
4# This file is part of the GNU C Library.
5
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.
10
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.
15
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# <http://www.gnu.org/licenses/>.
19
20set -e
21
22# The common objpfx, used to find libraries and the dynamic loader.
23objpfx=$1
24
25# We expect one parameter which is the test program. This must understand
26# a number options:
27# --phys use the FTW_PHYS flag
28# --chdir use the FTW_CHDIR and print the current directory
29# in the callback
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
33testprogram=$2
34
35# We cannot test this as root.
36if test `id | sed "s/uid=\([0-9]*\).*/\1/"` = 0; then
37 exit 0
38fi
39
40# Since we use `sort' we must make sure to use the same locale everywhere.
41LC_ALL=C
42export LC_ALL
43
44# First create our scenario:
45tmp=${objpfx}io
46tmpdir=$tmp/ftwtest.d
47
48trap 'chmod -fR a+x $tmpdir; rm -fr $tmpdir $testout' 1 2 3 15
49
50if test -d $tmpdir; then
51 chmod -fR a+x $tmpdir
52 rm -fr $tmpdir
53fi
54mkdir $tmpdir
55mkdir $tmpdir/foo
56mkdir $tmpdir/bar
57echo > $tmpdir/baz
58mkdir $tmpdir/foo/lvl1
59echo > $tmpdir/foo/lvl1/file@1
60mkdir $tmpdir/foo/lvl1/lvl2
61echo > $tmpdir/foo/lvl1/lvl2/file@2
62mkdir $tmpdir/foo/lvl1/lvl2/lvl3
63echo > $tmpdir/foo/lvl1/lvl2/lvl3/file@3
64ln -s $tmpdir $tmpdir/foo/lvl1/lvl2/lvl3/link@3
65ln -s $tmpdir/foo/lvl1/lvl2 $tmpdir/foo/lvl1/lvl2/link@2
66ln -s $tmpdir/foo/lvl1/lvl2/lvl3/lvl4 $tmpdir/foo/lvl1/link@1
67echo > $tmpdir/bar/xo
68chmod a-x,a+r $tmpdir/bar
69
70testout=$tmp/ftwtest-tmp.out
71
72$testprogram $tmpdir |
73 sort > $testout
74
75cat <<EOF | cmp $testout - || exit 1
76base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
77base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
78base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
79base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
80base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_NS, level = 2
81base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
82base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
83base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, level = 3
84base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, level = 3
85base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
86base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, level = 4
87base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
88EOF
89rm $testout
90
91$testprogram --depth $tmpdir |
92 sort > $testout
93
94cat <<EOF | cmp $testout - || exit 1
95base = "$tmp/", file = "ftwtest.d", flag = FTW_DP, level = 0
96base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_DP, level = 1
97base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
98base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_DP, level = 1
99base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_NS, level = 2
100base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_DP, level = 2
101base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
102base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, level = 3
103base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_DP, level = 3
104base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
105base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_DP, level = 4
106base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
107EOF
108rm $testout
109
110$testprogram --phys $tmpdir |
111 sort > $testout
112
113cat <<EOF | cmp $testout - || exit 1
114base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
115base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
116base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
117base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
118base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_NS, level = 2
119base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
120base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
121base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SL, level = 3
122base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, level = 3
123base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
124base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "link@2", flag = FTW_SL, level = 4
125base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, level = 4
126base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
127base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "link@3", flag = FTW_SL, level = 5
128EOF
129rm $testout
130
131# For the next test everything must be readable.
132chmod -fR a+x $tmpdir
133
134$testprogram --chdir $tmpdir |
135 sort > $testout
136
137# perhaps $tmp involves some symlinks...
138tmpreal=`cd $tmp; /bin/pwd 2>/dev/null || /usr/bin/pwd`
139
140cat <<EOF | cmp $testout - || exit 1
141base = "$tmp/", file = "ftwtest.d", flag = FTW_D, cwd = $tmpreal, level = 0
142base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
143base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, cwd = $tmpreal/ftwtest.d, level = 1
144base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
145base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, cwd = $tmpreal/ftwtest.d/bar, level = 2
146base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo, level = 2
147base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
148base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
149base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
150base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
151base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
152base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2/lvl3, level = 5
153EOF
154rm $testout
155
156curwd=`/bin/pwd 2>/dev/null || /usr/bin/pwd`
157cd "$tmp"
158$testprogram --chdir ftwtest.d |
159 sort > $testout
160cd "$curwd"
161
162cat <<EOF | diff -u $testout - || exit 1
163base = "", file = "ftwtest.d", flag = FTW_D, cwd = $tmpreal, level = 0
164base = "ftwtest.d/", file = "bar", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
165base = "ftwtest.d/", file = "baz", flag = FTW_F, cwd = $tmpreal/ftwtest.d, level = 1
166base = "ftwtest.d/", file = "foo", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
167base = "ftwtest.d/bar/", file = "xo", flag = FTW_F, cwd = $tmpreal/ftwtest.d/bar, level = 2
168base = "ftwtest.d/foo/", file = "lvl1", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo, level = 2
169base = "ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
170base = "ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
171base = "ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
172base = "ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
173base = "ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
174base = "ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2/lvl3, level = 5
175EOF
176rm $testout
177
178curwd=`/bin/pwd 2>/dev/null || /usr/bin/pwd`
179cd "$tmp"
180$testprogram --chdir ftwtest.d/. |
181 sort > $testout
182cd "$curwd"
183
184cat <<EOF | diff -u $testout - || exit 1
185base = "ftwtest.d/", file = ".", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 0
186base = "ftwtest.d/./", file = "bar", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
187base = "ftwtest.d/./", file = "baz", flag = FTW_F, cwd = $tmpreal/ftwtest.d, level = 1
188base = "ftwtest.d/./", file = "foo", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
189base = "ftwtest.d/./bar/", file = "xo", flag = FTW_F, cwd = $tmpreal/ftwtest.d/bar, level = 2
190base = "ftwtest.d/./foo/", file = "lvl1", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo, level = 2
191base = "ftwtest.d/./foo/lvl1/", file = "file@1", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
192base = "ftwtest.d/./foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
193base = "ftwtest.d/./foo/lvl1/", file = "lvl2", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
194base = "ftwtest.d/./foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
195base = "ftwtest.d/./foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
196base = "ftwtest.d/./foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2/lvl3, level = 5
197EOF
198rm $testout
199
200curwd=`/bin/pwd 2>/dev/null || /usr/bin/pwd`
201cd "$tmp"
202$testprogram --chdir ftwtest.d/foo/lvl1/link@1 |
203 sort > $testout
204cd "$curwd"
205
206cat <<EOF | diff -u $testout - || exit 1
207base = "ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 0
208EOF
209rm $testout
210
211$testprogram --early-exit $tmpdir |
212 sort > $testout
213
214cat <<EOF | cmp $testout - || exit 1
215base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
216succeeded
217EOF
218rm $testout
219
220mkdir $tmpdir/foo/lvl1b
221echo > $tmpdir/foo/lvl1b/file@1b
222echo > $tmpdir/foo/lvl1b/file2@1b
223echo > $tmpdir/foo/lvl1b/file3@1b
224
225$testprogram --skip-subtree=lvl1 $tmpdir |
226 sort > $testout
227
228cat <<EOF | diff -u $testout - || exit 1
229base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
230base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
231base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
232base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
233base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, level = 2
234base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
235base = "$tmp/ftwtest.d/foo/", file = "lvl1b", flag = FTW_D, level = 2
236base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file2@1b", flag = FTW_F, level = 3
237base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file3@1b", flag = FTW_F, level = 3
238base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file@1b", flag = FTW_F, level = 3
239EOF
240rm $testout
241
242$testprogram --skip-siblings=lvl1 $tmpdir |
243 sort > $testout
244
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.
248catcmd=cat
249[ -n "`ls -U $tmpdir/foo/ | sed -n '/lvl1$/,${/lvl1b$/p;}'`" ] \
250 && catcmd="grep -v lvl1b"
251
252$catcmd <<EOF | diff -u $testout - || exit 1
253base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
254base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
255base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
256base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
257base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, level = 2
258base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
259base = "$tmp/ftwtest.d/foo/", file = "lvl1b", flag = FTW_D, level = 2
260base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file2@1b", flag = FTW_F, level = 3
261base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file3@1b", flag = FTW_F, level = 3
262base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file@1b", flag = FTW_F, level = 3
263EOF
264rm $testout
265
266$testprogram --skip-siblings=file@1b $tmpdir |
267 sort > $testout
268
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.
272regexp=`echo $(ls -U $tmp/ftwtest.d/foo/lvl1b \
273 | sed -n '/file@1b$/,${/file[23]@1b$/p;}') | sed 's, ,|,'`
274catcmd=cat
275[ -n "$regexp" ] && catcmd="egrep -v $regexp"
276
277$catcmd <<EOF | diff -u $testout - || exit 1
278base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
279base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
280base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
281base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
282base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, level = 2
283base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
284base = "$tmp/ftwtest.d/foo/", file = "lvl1b", flag = FTW_D, level = 2
285base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
286base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, level = 3
287base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, level = 3
288base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
289base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, level = 4
290base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
291base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file2@1b", flag = FTW_F, level = 3
292base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file3@1b", flag = FTW_F, level = 3
293base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file@1b", flag = FTW_F, level = 3
294EOF
295rm $testout
296
297rm -fr $tmpdir
298
299exit 0