stat-vs-dirent.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/sh
  2. # Ensure that d_ino (from ls -di) and st_ino (from stat --format=%i) match.
  3. # Copyright (C) 2006-2018 Free Software Foundation, Inc.
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
  15. print_ver_ ls
  16. root_dev_ino=$(stat --format=%d-%i /)
  17. t=$(pwd)
  18. while :; do
  19. if ls -i1 "$t" > tmp; then
  20. # Extract the inode number from the first line of output from ls -i1.
  21. # This value comes from dirent.d_ino, on systems with d_ino support.
  22. d_ino=$(sed -n '1s/^ *\([0-9][0-9]*\) .*/\1/p;q' tmp)
  23. # Extract the name of the corresponding directory entry.
  24. file=$(sed -n '1s/^ *[0-9][0-9]* //p;q' tmp)
  25. # Get its inode number (stat.st_ino) via stat(1)'s call to lstat.
  26. st_ino=$(stat --format=%i "$t/$file")
  27. # Make sure that they are the same.
  28. # We know from experience that there may be mismatches on some
  29. # buggy file systems, at mount points.
  30. # Note that when a directory contains only entries whose names
  31. # start with ".", d_ino and file will both be empty. In that case,
  32. # skip the test.
  33. if test -n "$d_ino" && test "$d_ino" != "$st_ino"; then
  34. echo "$0: test failed: $t/$file: d_ino($d_ino) != st_ino($st_ino)
  35. This may indicate a flaw in your kernel or file system implementation.
  36. The flaw isn't serious for coreutils, but it might break other tools,
  37. so you should report it to your operating system vendor." 1>&2
  38. fail=1
  39. break
  40. fi
  41. fi
  42. t=$(cd "$t/.."; pwd)
  43. dev_ino=$(stat --format=%d-%i "$t")
  44. test $dev_ino = $root_dev_ino && break
  45. done
  46. Exit $fail