Add/fix copyright notices and adjust to latest GNU FDL.
[platform/upstream/coreutils.git] / tests / ls / stat-vs-dirent
1 #!/bin/sh
2 # Ensure that d_ino (from ls -di) and st_ino (from stat --format=%i) match.
3
4 # Copyright (C) 2006 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 2 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, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 if test "$VERBOSE" = yes; then
22   set -x
23   ls --version
24 fi
25
26 . $srcdir/../envvar-check
27
28 pwd=`pwd`
29 t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$
30 trap 'status=$?; cd $pwd; chmod -R u+rwx $t0; rm -rf $t0 && exit $status' 0
31 trap '(exit $?); exit $?' 1 2 13 15
32
33 framework_failure=0
34 mkdir -p $tmp || framework_failure=1
35 cd $tmp || framework_failure=1
36
37 if test $framework_failure = 1; then
38   echo "$0: failure in testing framework" 1>&2
39   (exit 1); exit 1
40 fi
41
42 fail=0
43
44 root_dev_ino=`stat --format=%d-%i /`
45 t=`pwd`
46 while :; do
47   ls -i1 "$t" > tmp
48   if test $? = 0; then
49     # Extract the inode number from the first line of output from ls -i1.
50     # This value comes from dirent.d_ino, on systems with d_ino support.
51     d_ino=`sed -n '1s/^ *\([0-9][0-9]*\) .*/\1/p;q' tmp`
52
53     # Extract the name of the corresponding directory entry.
54     file=`sed -n '1s/^ *[0-9][0-9]*  *//p;q' tmp`
55
56     # Get its inode number (stat.st_ino) via stat(1)'s call to lstat.
57     st_ino=`stat --format=%i "$t/$file"`
58
59     # Make sure that they are the same.
60     # We know from experience that there may be mismatches on some
61     # buggy file systems, at mount points.
62     if test "$d_ino" != "$st_ino"; then
63       echo "$0: test failed: $t/$file: d_ino($d_ino) != st_ino($st_ino)" 1>&2
64       echo "$0: This may indicate a flaw in your kernel or" \
65              "file system implementation." 1>&2
66       echo "$0: This flaw won't impact coreutils, but it may well"
67              "affect other tools,"
68       echo "$0: so you should report it to your operating system vendor." 1>&2
69
70       # This test fails too often, and we don't want to be distracted
71       # with reports, since the code that could be affected by the losing
72       # behavior (pwd and getcwd) works around any mismatch.
73       # So do continue to issue the warning, but don't count it as a
74       # real failure.
75       # fail=1
76       break
77     fi
78   fi
79
80   t=`(cd "$t/.."; pwd)`
81   dev_ino=`stat --format=%d-%i "$t"`
82   test $dev_ino = $root_dev_ino && break
83 done
84
85 (exit $fail); exit $fail