Add/fix copyright notices and adjust to latest GNU FDL.
[platform/upstream/coreutils.git] / tests / rm / fail-eperm
1 #!/bin/sh
2 # -*- perl -*-
3 # Ensure that rm gives the expected diagnostic when failing to remove a file
4 # owned by some other user in a directory with the sticky bit set.
5
6 # Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
7
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 # 02110-1301, USA.
22
23 if test "$VERBOSE" = yes; then
24   set -x
25   rm --version
26 fi
27
28 # FIXME-someday: when run as root we don't need all of the
29 # searching below.  root can simply create the required
30 # dir/files and run the test as someone else.
31
32 PRIV_CHECK_ARG=require-non-root . $srcdir/../priv-check
33
34 : ${PERL=perl}
35 : ${srcdir=.}
36
37 $PERL -e 1 > /dev/null 2>&1 || {
38   echo 1>&2 "$0: configure didn't find a usable version of Perl," \
39     "so can't run this test"
40   exit 77
41 }
42
43 ARGV_0=$0
44 export ARGV_0
45
46 exec $PERL -w -- - << \EOP
47 require 5.003;
48 use strict;
49
50 (my $ME = $ENV{ARGV_0}) =~ s|.*/||;
51
52 my $verbose = $ENV{VERBOSE} && $ENV{VERBOSE} eq 'yes';
53
54 # Ensure that the diagnostics are in English.
55 $ENV{LC_ALL} = 'C';
56
57 my @dir_list = qw(/tmp /var/tmp /usr/tmp);
58
59 # Find a directory with the sticky bit set.
60 my $found_dir;
61 my $found_file;
62 foreach my $dir (@dir_list)
63   {
64     if (-d $dir && -k _ && -r _ && -w _ && -x _)
65       {
66         $found_dir = 1;
67
68         # Find a non-directory there that is owned by some other user.
69         opendir DIR_HANDLE, $dir
70           or die "$ME: couldn't open $dir: $!\n";
71
72         foreach my $f (readdir DIR_HANDLE)
73           {
74             my $target_file = "$dir/$f";
75             $verbose
76               and warn "$ME: considering $target_file\n";
77
78             # Skip files owned by self, symlinks, and directories.
79             # It's not technically necessary to skip symlinks, but it's simpler.
80             # SVR4-like systems (e.g., Solaris 9) let you unlink files that
81             # you can write, so skip writable files too.
82             -l $target_file || -o _ || -d _ || -w _
83               and next;
84
85             $found_file = 1;
86
87             # Invoke rm on this file and ensure that we get the
88             # expected exit code and diagnostic.
89             my $cmd = "rm -f $target_file";
90             open RM, "$cmd 2>&1 |"
91               or die "$ME: cannot execute `$cmd'\n";
92
93             my $line = <RM>;
94
95             close RM;
96             my $status = $? >> 8;
97             $status == 1
98               or die "$ME: unexpected exit status from `$cmd';\n"
99                 . "  got $status, expected 1\n";
100
101             my $exp = "rm: cannot remove `$target_file':";
102             $line
103               or die "$ME: no output from `$cmd';\n"
104                 . "expected something like `$exp ...'\n";
105
106             my $regex = quotemeta $exp;
107             $line =~ /^$regex/
108               or die "$ME: unexpected dignostic from `$cmd';\n"
109                 . "  got      $line"
110                 . "  expected $exp ...\n";
111
112             last;
113           }
114
115         closedir DIR_HANDLE;
116         $found_file
117           and last;
118       }
119   }
120
121 if ( ! $found_dir)
122   {
123     warn "$ME: couldn't find a directory with the sticky bit set;"
124       . " skipping this test\n";
125     exit 77;
126   }
127
128 if ( ! $found_file)
129   {
130     warn "$ME: couldn't find a file not owned by you\n"
131       . " in any of the following directories:\n  @dir_list\n"
132       . "...so, skipping this test\n";
133     exit 77;
134   }
135 EOP