summaryrefslogtreecommitdiff
path: root/tests/ls/readdir-mountpoint-inode
diff options
context:
space:
mode:
authorJim Meyering <meyering@redhat.com>2008-07-02 18:01:43 +0200
committerJim Meyering <meyering@redhat.com>2009-09-01 07:07:45 +0200
commita977dbbe78f4dcb64e008f41c8a46c4972af834b (patch)
tree3be345b10aa1da67137c6071bfb1d18251c94c88 /tests/ls/readdir-mountpoint-inode
parenta4e123abd3dab42f48cc79d9e2c1cbc72e77d18b (diff)
downloadcoreutils-a977dbbe78f4dcb64e008f41c8a46c4972af834b.tar.xz
ls -i: print consistent inode numbers also for mount points
On most unix- and linux-based kernels, ls -i DIR_CONTAINING_MOUNT_POINT would print the wrong inode number for any entry that is a mount point. It would do that by relying on readdir's dirent.d_ino values, while most readdir implementations return the inode number of the underlying, inaccessible directory. Thus, it is not consistent with what you'd get when applying stat to the same entry. This bug led to surprising results like "ls -i" and "ls -i --color" printing different numbers (ls must usually "stat" a file to colorize its name). This change makes it so that on offending systems, ls must stat non-command-line-arguments for which otherwise it would be able to use "for free" dirent.d_ino values. Regardless of this change, ls is already required to stat every command-line argument. Note: versions of GNU ls prior to coreutils-6.0 did not perform the invalid optimization, and hence always printed correct inode numbers. Thus, for the sake of correctness, ls -i is forgoing the readdir optimization, for any kernel (including linux!) with POSIX-nonconforming readdir. Note that currently, only Cygwin has been agile enough to conform. * src/ls.c (RELIABLE_D_INO): Define. (print_dir): Use it. For plenty of discussion, see this long thread: http://thread.gmane.org/gmane.comp.gnu.coreutils.bugs/14020 This bug was introduced by the 2006-02-26 commit, 33eb3efe: "In ls, avoid calling stat for --inode (-i), when possible." * tests/ls/readdir-mountpoint-inode: New test. * tests/Makefile.am (TESTS): Add it. * tests/ls/stat-vs-dirent: Don't suppress failure of this test, now that ls -i is fixed. Though note that it doesn't test well, since it compares only the always-stat'd command-line arguments. * NEWS (Bug fixes): Mention it.
Diffstat (limited to 'tests/ls/readdir-mountpoint-inode')
-rwxr-xr-xtests/ls/readdir-mountpoint-inode72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/ls/readdir-mountpoint-inode b/tests/ls/readdir-mountpoint-inode
new file mode 100755
index 000000000..763cab186
--- /dev/null
+++ b/tests/ls/readdir-mountpoint-inode
@@ -0,0 +1,72 @@
+#!/bin/sh
+# ensure that ls -i works also for mount points
+
+# Copyright (C) 2009 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program 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 General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ ls --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+
+mount_points=$(df --local -P 2>&1 | sed -n 's,.*[0-9]% \(/.\),\1,p')
+test -z "$mount_points" && skip_test_ "this test requires a non-root mount point"
+
+# Given e.g., /dev/shm, produce the list of GNU ls options that
+# let us list just that entry using readdir data from its parent:
+# ls -i -I '[^s]*' -I 's[^h]*' -I 'sh[^m]*' -I 'shm?*' -I '.?*' \
+# -I '?' -I '??' /dev
+
+ls_ignore_options()
+{
+ name=$1
+ opts="-I '.?*' -I '$name?*'"
+ while :; do
+ glob=$(echo "$name"|sed 's/\(.*\)\(.\)$/\1[^\2]*/')
+ opts="$opts -I '$glob'"
+ name=$(echo "$name"|sed 's/.$//')
+ test -z "$name" && break
+ glob=$(echo "$name"|sed 's/./?/g')
+ opts="$opts -I '$glob'"
+ done
+ echo "$opts"
+}
+
+inode_via_readdir()
+{
+ mount_point=$1
+ base=$(basename $mount_point)
+ case $base in
+ .*) skip_test_ 'mount point component starts with "."' ;;
+ *[*?]*) skip_test_ 'mount point component contains "?" or "*"' ;;
+ esac
+ opts=$(ls_ignore_options "$base")
+ parent_dir=$(dirname $mount_point)
+ eval "ls -i $opts $parent_dir" | sed 's/ .*//'
+}
+
+# FIXME: use a timeout, in case stat'ing mount points takes too long.
+
+for dir in $mount_points; do
+ readdir_inode=$(inode_via_readdir $dir)
+ stat_inode=$(env stat --format=%i $dir)
+ test "$readdir_inode" = "$stat_inode" || fail=1
+done
+
+Exit $fail