summaryrefslogtreecommitdiff
path: root/tests/CuTmpdir.pm
diff options
context:
space:
mode:
authorJim Meyering <meyering@redhat.com>2008-04-25 10:41:42 +0200
committerJim Meyering <meyering@redhat.com>2008-04-27 21:31:32 +0200
commitbbafdc3c8f97cc1df1b4cd0f17280ea5f5295cfa (patch)
treeaadc44e4afdedfb6218edb87e3d2666fc1fd1cb8 /tests/CuTmpdir.pm
parentdfdb532fd3472304da08fb35859cdfee3caaf5c6 (diff)
downloadcoreutils-bbafdc3c8f97cc1df1b4cd0f17280ea5f5295cfa.tar.xz
tests: improve perl-based tempdir handling
Before, upon interrupt, directories would be left behind. * tests/CuTmpdir.pm: Remove temporary directory on interrupt.
Diffstat (limited to 'tests/CuTmpdir.pm')
-rw-r--r--tests/CuTmpdir.pm65
1 files changed, 42 insertions, 23 deletions
diff --git a/tests/CuTmpdir.pm b/tests/CuTmpdir.pm
index 60eec90c0..f9d2c00f0 100644
--- a/tests/CuTmpdir.pm
+++ b/tests/CuTmpdir.pm
@@ -1,7 +1,7 @@
package CuTmpdir;
# create, then chdir into a temporary sub-directory
-# Copyright (C) 2007 Free Software Foundation, Inc.
+# Copyright (C) 2007-2008 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
@@ -26,12 +26,44 @@ our $ME = $0 || "<???>";
my $dir;
-sub skip_test
+sub skip_test($)
{
- warn "$ME: skipping test: unsafe working directory name\n";
+ warn "$ME: skipping test: unsafe working directory name: `$_[0]'\n";
exit 77;
}
+sub chmod_1
+{
+ my $name = $_;
+
+ # Skip symlinks and non-directories.
+ -l $name || !-d _
+ and return;
+
+ chmod 0700, $name;
+}
+
+sub chmod_tree
+{
+ chdir $dir
+ or warn "$ME: failed to chdir to $dir: $!\n";
+ # Perform the equivalent of find . -type d -print0|xargs -0 chmod -R 700.
+ my $options = {untaint => 1, wanted => \&chmod_1};
+ find ($options, '.');
+}
+
+sub on_sig_remove_tmpdir
+{
+ my ($sig) = @_;
+ if (defined $dir)
+ {
+ chmod_tree;
+ File::Temp::cleanup;
+ }
+ $SIG{$sig} = 'DEFAULT';
+ kill $sig, $$;
+}
+
sub import {
my $prefix = $_[1];
@@ -47,35 +79,22 @@ sub import {
# Untaint for the upcoming mkdir.
$prefix =~ m!^([-+\@\w./]+)$!
- or skip_test;
+ or skip_test $prefix;
$prefix = $1;
+ foreach my $sig (qw (INT TERM HUP))
+ {
+ $SIG{$sig} = \&on_sig_remove_tmpdir;
+ }
+
$dir = File::Temp::tempdir("$prefix.tmp-XXXX", CLEANUP => 1 );
chdir $dir
or warn "$ME: failed to chdir to $dir: $!\n";
}
-sub wanted
-{
- my $name = $_;
-
- # Skip symlinks and non-directories.
- -l $name || !-d _
- and return;
-
- chmod 0700, $name;
-}
-
END {
my $saved_errno = $?;
- if (defined $dir)
- {
- chdir $dir
- or warn "$ME: failed to chdir to $dir: $!\n";
- # Perform the equivalent of find . -type d -print0|xargs -0 chmod -R 700.
- my $options = {untaint => 1, wanted => \&wanted};
- find ($options, '.');
- }
+ chmod_tree;
$? = $saved_errno;
}