diff options
Diffstat (limited to 'MDK/Common/File.pm')
-rw-r--r-- | MDK/Common/File.pm | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/MDK/Common/File.pm b/MDK/Common/File.pm index 86fb98b..5df7b4f 100644 --- a/MDK/Common/File.pm +++ b/MDK/Common/File.pm @@ -38,6 +38,11 @@ array context it returns the lines creates a file and outputs the list (if the file exists, it is clobbered) +=item secured_output(FILENAME, LIST) + +likes output() but prevents insecured usage (it dies if somebody try +to exploit the race window between unlink() and creat()) + =item append_to_file(FILENAME, LIST) add the LIST at the end of the file @@ -136,6 +141,8 @@ sub output_with_perm { my ($f, $perm, @l) = @_; mkdir_p(dirname($f)); output($f, sub linkf { unlink $_[1]; link $_[0], $_[1] } sub symlinkf { unlink $_[1]; symlink $_[0], $_[1] } sub renamef { unlink $_[1]; rename $_[0], $_[1] } +use Fcntl; +sub secured_output { my $f = shift; unlink($f); sysopen(my $F, $f, O_CREAT|O_EXCL|O_RDWR) or die "secure output in file $f failed: $! $@\n"; print $F $_ foreach @_; 1 } sub mkdir_p { |