aboutsummaryrefslogtreecommitdiff
path: root/io/ftwtest-sh
blob: 7341c1f65480e55af59757e0a6a912370bbdffa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#! /bin/sh
# Test for nftw(3).
# Copyright (C) 1997-2014 Free Software Foundation, Inc.
# This file is part of the GNU C Library.

# The GNU C Library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# The GNU C Library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with the GNU C Library; if not, see
# <http://www.gnu.org/licenses/>.

set -e

# The common objpfx, used to find libraries and the dynamic loader.
objpfx=$1

# We expect one parameter which is the test program.  This must understand
# a number options:
#   --phys		use the FTW_PHYS flag
#   --chdir		use the FTW_CHDIR and print the current directory
#			in the callback
#   --depth		use the FTW_DEPTH flag
#   --early-exit 	print file@2 item only and return non-zero from the
#			callback when it is seen
testprogram=$2

# We cannot test this as root.
if test `id | sed "s/uid=\([0-9]*\).*/\1/"` = 0; then
  exit 0
fi

# Since we use `sort' we must make sure to use the same locale everywhere.
LC_ALL=C
export LC_ALL
LANG=C
export LANG

# First create our scenario:
tmp=${objpfx}io
tmpdir=$tmp/ftwtest.d

trap 'chmod -fR a+x $tmpdir; rm -fr $tmpdir $testout' 1 2 3 15

if test -d $tmpdir; then
  chmod -fR a+x $tmpdir
  rm -fr $tmpdir
fi
mkdir $tmpdir
mkdir $tmpdir/foo
mkdir $tmpdir/bar
echo > $tmpdir/baz
mkdir $tmpdir/foo/lvl1
echo > $tmpdir/foo/lvl1/file@1
mkdir $tmpdir/foo/lvl1/lvl2
echo > $tmpdir/foo/lvl1/lvl2/file@2
mkdir $tmpdir/foo/lvl1/lvl2/lvl3
echo > $tmpdir/foo/lvl1/lvl2/lvl3/file@3
ln -s $tmpdir $tmpdir/foo/lvl1/lvl2/lvl3/link@3
ln -s $tmpdir/foo/lvl1/lvl2 $tmpdir/foo/lvl1/lvl2/link@2
ln -s $tmpdir/foo/lvl1/lvl2/lvl3/lvl4 $tmpdir/foo/lvl1/link@1
echo > $tmpdir/bar/xo
chmod a-x,a+r $tmpdir/bar

testout=$tmp/ftwtest.out

$testprogram $tmpdir |
    sort > $testout

cat <<EOF | cmp $testout - || exit 1
base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_NS, level = 2
base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, level = 4
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
EOF
rm $testout

$testprogram --depth $tmpdir |
    sort > $testout

cat <<EOF | cmp $testout - || exit 1
base = "$tmp/", file = "ftwtest.d", flag = FTW_DP, level = 0
base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_DP, level = 1
base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_DP, level = 1
base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_NS, level = 2
base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_DP, level = 2
base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_DP, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_DP, level = 4
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
EOF
rm $testout

$testprogram --phys $tmpdir |
    sort > $testout

cat <<EOF | cmp $testout - || exit 1
base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_NS, level = 2
base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SL, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "link@2", flag = FTW_SL, level = 4
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, level = 4
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "link@3", flag = FTW_SL, level = 5
EOF
rm $testout

# For the next test everything must be readable.
chmod -fR a+x $tmpdir

$testprogram --chdir $tmpdir |
    sort > $testout

# perhaps $tmp involves some symlinks...
tmpreal=`cd $tmp; /bin/pwd 2>/dev/null || /usr/bin/pwd`

cat <<EOF | cmp $testout - || exit 1
base = "$tmp/", file = "ftwtest.d", flag = FTW_D, cwd = $tmpreal, level = 0
base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, cwd = $tmpreal/ftwtest.d, level = 1
base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, cwd = $tmpreal/ftwtest.d/bar, level = 2
base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo, level = 2
base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2/lvl3, level = 5
EOF
rm $testout

curwd=`/bin/pwd 2>/dev/null || /usr/bin/pwd`
cd "$tmp"
$testprogram --chdir ftwtest.d |
    sort > $testout
cd "$curwd"

cat <<EOF | diff -u $testout - || exit 1
base = "", file = "ftwtest.d", flag = FTW_D, cwd = $tmpreal, level = 0
base = "ftwtest.d/", file = "bar", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
base = "ftwtest.d/", file = "baz", flag = FTW_F, cwd = $tmpreal/ftwtest.d, level = 1
base = "ftwtest.d/", file = "foo", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
base = "ftwtest.d/bar/", file = "xo", flag = FTW_F, cwd = $tmpreal/ftwtest.d/bar, level = 2
base = "ftwtest.d/foo/", file = "lvl1", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo, level = 2
base = "ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
base = "ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
base = "ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
base = "ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
base = "ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
base = "ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2/lvl3, level = 5
EOF
rm $testout

curwd=`/bin/pwd 2>/dev/null || /usr/bin/pwd`
cd "$tmp"
$testprogram --chdir ftwtest.d/. |
    sort > $testout
cd "$curwd"

cat <<EOF | diff -u $testout - || exit 1
base = "ftwtest.d/", file = ".", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 0
base = "ftwtest.d/./", file = "bar", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
base = "ftwtest.d/./", file = "baz", flag = FTW_F, cwd = $tmpreal/ftwtest.d, level = 1
base = "ftwtest.d/./", file = "foo", flag = FTW_D, cwd = $tmpreal/ftwtest.d, level = 1
base = "ftwtest.d/./bar/", file = "xo", flag = FTW_F, cwd = $tmpreal/ftwtest.d/bar, level = 2
base = "ftwtest.d/./foo/", file = "lvl1", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo, level = 2
base = "ftwtest.d/./foo/lvl1/", file = "file@1", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
base = "ftwtest.d/./foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
base = "ftwtest.d/./foo/lvl1/", file = "lvl2", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 3
base = "ftwtest.d/./foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
base = "ftwtest.d/./foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2, level = 4
base = "ftwtest.d/./foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, cwd = $tmpreal/ftwtest.d/foo/lvl1/lvl2/lvl3, level = 5
EOF
rm $testout

curwd=`/bin/pwd 2>/dev/null || /usr/bin/pwd`
cd "$tmp"
$testprogram --chdir ftwtest.d/foo/lvl1/link@1 |
    sort > $testout
cd "$curwd"

cat <<EOF | diff -u $testout - || exit 1
base = "ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, cwd = $tmpreal/ftwtest.d/foo/lvl1, level = 0
EOF
rm $testout

$testprogram --early-exit $tmpdir |
    sort > $testout

cat <<EOF | cmp $testout - || exit 1
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
succeeded
EOF
rm $testout

mkdir $tmpdir/foo/lvl1b
echo > $tmpdir/foo/lvl1b/file@1b
echo > $tmpdir/foo/lvl1b/file2@1b
echo > $tmpdir/foo/lvl1b/file3@1b

$testprogram --skip-subtree=lvl1 $tmpdir |
    sort > $testout

cat <<EOF | diff -u $testout - || exit 1
base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, level = 2
base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
base = "$tmp/ftwtest.d/foo/", file = "lvl1b", flag = FTW_D, level = 2
base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file2@1b", flag = FTW_F, level = 3
base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file3@1b", flag = FTW_F, level = 3
base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file@1b", flag = FTW_F, level = 3
EOF
rm $testout

$testprogram --skip-siblings=lvl1 $tmpdir |
    sort > $testout

# The filesystem is not required to put lvl1 before lvl1b.
# If lvl1b comes after lvl1, it shouldn't be printed, while if it
# comes before, it should.
catcmd=cat
[ -n "`ls -U $tmpdir/foo/ | sed -n '/lvl1$/,${/lvl1b$/p;}'`" ] \
  && catcmd="grep -v lvl1b"

$catcmd <<EOF | diff -u $testout - || exit 1
base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, level = 2
base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
base = "$tmp/ftwtest.d/foo/", file = "lvl1b", flag = FTW_D, level = 2
base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file2@1b", flag = FTW_F, level = 3
base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file3@1b", flag = FTW_F, level = 3
base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file@1b", flag = FTW_F, level = 3
EOF
rm $testout

$testprogram --skip-siblings=file@1b $tmpdir |
    sort > $testout

# The filesystem is not required to put file2@1b and file3@1b after file@1b.
# If file[23]@1b come after file@1b, it shouldn't be printed, while if they
# come before, they should.
regexp=`echo $(ls -U $tmp/ftwtest.d/foo/lvl1b \
	       | sed -n '/file@1b$/,${/file[23]@1b$/p;}') | sed 's, ,|,'`
catcmd=cat
[ -n "$regexp" ] && catcmd="egrep -v $regexp"

$catcmd <<EOF | diff -u $testout - || exit 1
base = "$tmp/", file = "ftwtest.d", flag = FTW_D, level = 0
base = "$tmp/ftwtest.d/", file = "bar", flag = FTW_D, level = 1
base = "$tmp/ftwtest.d/", file = "baz", flag = FTW_F, level = 1
base = "$tmp/ftwtest.d/", file = "foo", flag = FTW_D, level = 1
base = "$tmp/ftwtest.d/bar/", file = "xo", flag = FTW_F, level = 2
base = "$tmp/ftwtest.d/foo/", file = "lvl1", flag = FTW_D, level = 2
base = "$tmp/ftwtest.d/foo/", file = "lvl1b", flag = FTW_D, level = 2
base = "$tmp/ftwtest.d/foo/lvl1/", file = "file@1", flag = FTW_F, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/", file = "link@1", flag = FTW_SLN, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/", file = "lvl2", flag = FTW_D, level = 3
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "file@2", flag = FTW_F, level = 4
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/", file = "lvl3", flag = FTW_D, level = 4
base = "$tmp/ftwtest.d/foo/lvl1/lvl2/lvl3/", file = "file@3", flag = FTW_F, level = 5
base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file2@1b", flag = FTW_F, level = 3
base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file3@1b", flag = FTW_F, level = 3
base = "$tmp/ftwtest.d/foo/lvl1b/", file = "file@1b", flag = FTW_F, level = 3
EOF
rm $testout

rm -fr $tmpdir

exit 0