1#!/bin/sh
2# Ensure that d_ino (from ls -di) and st_ino (from stat --format=%i) match.
3
4# Copyright (C) 2006-2023 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 <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ ls
21
22
23root_dev_ino=$(stat --format=%d-%i /)
24t=$(pwd)
25while :; do
26  if ls -i1 "$t" > tmp; then
27    # Extract the inode number from the first line of output from ls -i1.
28    # This value comes from dirent.d_ino, on systems with d_ino support.
29    d_ino=$(sed -n '1s/^ *\([0-9][0-9]*\) .*/\1/p;q' tmp)
30
31    # Extract the name of the corresponding directory entry.
32    file=$(sed -n '1s/^ *[0-9][0-9]* //p;q' tmp)
33
34    # Get its inode number (stat.st_ino) via stat(1)'s call to lstat.
35    st_ino=$(stat --format=%i "$t/$file") ||
36        skip_ "error stating: $t/$file"  # removed or newlines in name etc.
37
38    # Make sure that they are the same.
39    # We know from experience that there may be mismatches on some
40    # buggy file systems, at mount points.
41    # Note that when a directory contains only entries whose names
42    # start with ".", d_ino and file will both be empty.  In that case,
43    # skip the test.
44    if test -n "$d_ino" && test "$d_ino" != "$st_ino"; then
45      echo "$0: test failed: $t/$file: d_ino($d_ino) != st_ino($st_ino)
46        This may indicate a flaw in your kernel or file system implementation.
47        The flaw isn't serious for coreutils, but it might break other tools,
48        so you should report it to your operating system vendor." 1>&2
49
50      fail=1
51      break
52    fi
53  fi
54
55  t=$(cd "$t/.."; pwd)
56  dev_ino=$(stat --format=%d-%i "$t")
57  test $dev_ino = $root_dev_ino && break
58done
59
60Exit $fail
61