From dfd77a5ef5d5443794045fbb24357edcb40202b2 Mon Sep 17 00:00:00 2001 From: Pascal Rigaux Date: Thu, 18 Sep 2008 14:19:04 +0000 Subject: create unwind_protect() and with_private_tmp_file() (for future use) --- perl-install/common.pm | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'perl-install/common.pm') diff --git a/perl-install/common.pm b/perl-install/common.pm index 32854daed..3eb223cd5 100644 --- a/perl-install/common.pm +++ b/perl-install/common.pm @@ -271,6 +271,29 @@ sub secured_file { $f; } +sub unwind_protect { + my ($to_do, $cleanup) = @_; + my @l = eval { $to_do->() }; + my $err = $@; + $cleanup->(); + $err and die $err; + wantarray() ? @l : $l[0]; +} + +sub with_private_tmp_file { + my ($file, $content, $f) = @_; + + my $prev_umask = umask 077; + + unwind_protect(sub { + MDK::Common::File::secured_output($file, $content); + $f->($file); + }, sub { + umask $prev_umask; + unlink $file; + }); +} + sub chown_ { my ($b_recursive, $name, $group, @files) = @_; -- cgit v1.2.1