3 # Ensure that pwd works even when run from a very deep directory.
5 # Copyright (C) 2006-2008 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 . $top_srcdir/tests/require-perl
23 . $top_srcdir/tests/test-lib.sh
24 require_readable_root_
29 # Don't use CuTmpdir here, since File::Temp's use of rmtree can't
30 # remove the deep tree we create.
33 # Show that pwd works even when the length of the resulting
34 # directory name is longer than PATH_MAX.
37 (my $ME = $ENV{ARGV_0}) =~ s|.*/||;
39 sub normalize_to_cwd_relative ($$$)
41 my ($dir, $dev, $ino) = @_;
46 $slash = index $dir, '/', $slash + 1;
48 and die "$ME: $dir does not contain old CWD\n";
49 my $dir_prefix = $slash ? substr ($dir, 0, $slash) : '/';
50 my ($d, $i) = (stat $dir_prefix)[0, 1];
51 $d == $dev && $i == $ino
52 and return substr $dir, $slash + 1;
56 # Set up a safe, well-known environment
57 delete @ENV{qw(BASH_ENV CDPATH ENV PATH)};
60 # Save CWD's device and inode numbers.
61 my ($dev, $ino) = (stat '.')[0, 1];
63 # Construct the expected "."-relative part of pwd's output.
66 my $expected = "/$z" x $n;
67 # Remove the leading "/".
68 substr ($expected, 0, 1) = '';
74 or die "$ME: at depth $i: $!\n";
79 my $abs_top_builddir = $ENV{abs_top_builddir};
81 or die "$ME: envvar abs_top_builddir not defined\n";
82 my $build_src_dir = "$abs_top_builddir/src";
83 if ($build_src_dir !~ m!^([-+.:/\w]+)$!)
85 warn "$0: skipping this test; odd build source directory name:\n"
91 my $pwd_binary = "$build_src_dir/pwd";
94 or die "$ME: $pwd_binary is not an executable file\n";
95 chomp (my $actual = `$pwd_binary`);
97 # Convert the absolute name from pwd into a $CWD-relative name.
98 # This is necessary in order to avoid a spurious failure when run
99 # from a directory in a bind-mounted partition. What happens is
100 # pwd reads a ".." that contains two or more entries with identical
101 # dev,ino that match the ones we're looking for, and it chooses a
102 # name that does not correspond to the one already recorded in $CWD.
103 $actual = normalize_to_cwd_relative $actual, $dev, $ino;
105 if ($expected ne $actual)
107 my $e_len = length $expected;
108 my $a_len = length $actual;
109 warn "expected len: $e_len\n";
110 warn "actual len: $a_len\n";
111 warn "expected: $expected\n";
112 warn "actual: $actual\n";
119 (exit $fail); exit $fail