Port tests/rmdir/ignore away from GNU/Linux.
[coreutils/ericb.git] / tests / CuTmpdir.pm
blobf8d43d5ff7d565051c2d8d871bba073ae64503d1
1 package CuTmpdir;
2 # create, then chdir into a temporary sub-directory
4 # Copyright (C) 2007 Free Software Foundation, Inc.
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.
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.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 use strict;
20 use warnings;
22 use File::Temp;
23 use File::Find;
25 our $ME = $0 || "<???>";
27 my $dir;
29 sub skip_test
31 warn "$ME: skipping test: unsafe working directory name\n";
32 exit 77;
35 sub import {
36 my $prefix = $_[1];
37 if ($prefix !~ /^\//)
39 eval 'use Cwd';
40 my $cwd = $@ ? '.' : Cwd::getcwd();
41 $prefix = "$cwd/$prefix";
44 # Untaint for the upcoming mkdir.
45 $prefix =~ m!^([-+\@\w./]+)$!
46 or skip_test;
47 $prefix = $1;
49 $dir = File::Temp::tempdir("$prefix.tmp-XXXX", CLEANUP => 1 );
50 chdir $dir
51 or warn "$ME: failed to chdir to $dir: $!\n";
54 sub wanted
56 my $name = $_;
58 # Skip symlinks and non-directories.
59 -l $name || !-d _
60 and return;
62 chmod 0700, $name;
65 END {
66 my $saved_errno = $?;
67 chdir $dir
68 or warn "$ME: failed to chdir to $dir: $!\n";
69 # Perform the equivalent of find . -type d -print0|xargs -0 chmod -R 700.
70 my $options = {untaint => 1, wanted => \&wanted};
71 find ($options, '.');
72 $? = $saved_errno;