tests: factor 350 fail=0 initializations into test-lib.sh
[platform/upstream/coreutils.git] / tests / ls / readdir-mountpoint-inode
1 #!/bin/sh
2 # ensure that ls -i works also for mount points
3
4 # Copyright (C) 2009 Free Software Foundation, Inc.
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program 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
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 if test "$VERBOSE" = yes; then
20   set -x
21   ls --version
22 fi
23
24 . $srcdir/test-lib.sh
25
26
27 mount_points=$(df --local -P 2>&1 | sed -n 's,.*[0-9]% \(/.\),\1,p')
28 test -z "$mount_points" && skip_test_ "this test requires a non-root mount point"
29
30 # Given e.g., /dev/shm, produce the list of GNU ls options that
31 # let us list just that entry using readdir data from its parent:
32 # ls -i -I '[^s]*' -I 's[^h]*' -I 'sh[^m]*' -I 'shm?*' -I '.?*' \
33 # -I '?' -I '??' /dev
34
35 ls_ignore_options()
36 {
37   name=$1
38   opts="-I '.?*' -I '$name?*'"
39   while :; do
40     glob=$(echo "$name"|sed 's/\(.*\)\(.\)$/\1[^\2]*/')
41     opts="$opts -I '$glob'"
42     name=$(echo "$name"|sed 's/.$//')
43     test -z "$name" && break
44     glob=$(echo "$name"|sed 's/./?/g')
45     opts="$opts -I '$glob'"
46   done
47   echo "$opts"
48 }
49
50 inode_via_readdir()
51 {
52   mount_point=$1
53   base=$(basename $mount_point)
54   case $base in
55     .*) skip_test_ 'mount point component starts with "."' ;;
56     *[*?]*) skip_test_ 'mount point component contains "?" or "*"' ;;
57   esac
58   opts=$(ls_ignore_options "$base")
59   parent_dir=$(dirname $mount_point)
60   eval "ls -i $opts $parent_dir" | sed 's/ .*//'
61 }
62
63 # FIXME: use a timeout, in case stat'ing mount points takes too long.
64
65 for dir in $mount_points; do
66   readdir_inode=$(inode_via_readdir $dir)
67   stat_inode=$(env stat --format=%i $dir)
68   test "$readdir_inode" = "$stat_inode" || fail=1
69 done
70
71 Exit $fail