#! /bin/sh
# Test for nftw(3).
# Copyright (C) 1997-2015 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
# .
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
# First create our scenario:
tmp=${objpfx}io
tmpdir=$tmp/ftwtest.d
trap 'chmod -fR a+x $tmpdir; rm -fr $tmpdir $testout' 0 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-tmp.out
$testprogram $tmpdir |
sort > $testout
cat < $testout
cat < $testout
cat < $testout
# perhaps $tmp involves some symlinks...
tmpreal=`cd $tmp; /bin/pwd 2>/dev/null || /usr/bin/pwd`
cat </dev/null || /usr/bin/pwd`
cd "$tmp"
$testprogram --chdir ftwtest.d |
sort > $testout
cd "$curwd"
cat </dev/null || /usr/bin/pwd`
cd "$tmp"
$testprogram --chdir ftwtest.d/. |
sort > $testout
cd "$curwd"
cat </dev/null || /usr/bin/pwd`
cd "$tmp"
$testprogram --chdir ftwtest.d/foo/lvl1/link@1 |
sort > $testout
cd "$curwd"
cat < $testout
cat < $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 < $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 < $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 <