Skip Menu |
 

This queue is for tickets about the File-Slurp CPAN distribution.

Report information
The Basics
Id: 7825
Status: resolved
Priority: 0/
Queue: File-Slurp

People
Owner: uri [...] sysarch.com
Requestors: pagaltzis [...] gmx.de
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 9999.06
Fixed in: (no value)

Attachments


Subject: Support for non-overwriting use of write_file
Download (untitled) / with headers
text/plain 112b
The attached patch adds an option "noclobber" to write_file() which prevents it from overwriting existing files.
Download Slurp.pm.diff
text/x-diff 2.1k
--- Slurp.pm.orig 2004-09-30 11:45:48.000000000 +0200 +++ Slurp.pm 2004-09-30 12:45:17.000000000 +0200 @@ -200,6 +200,7 @@ my $mode = O_WRONLY | O_CREAT ; $mode |= O_BINARY if $args->{'binmode'} ; $mode |= O_APPEND if $args->{'append'} ; + $mode |= O_EXCL if $args->{'noclobber'} ; # open the file and handle any error. @@ -248,7 +249,21 @@ # handle the atomic mode - move the temp file to the original filename. - rename( $file_name, $orig_file_name ) if $args->{'atomic'} ; + if ( $args->{'atomic'} ) { + +# *sigh* lame because no atomic non-clobbering rename() operation is +# possible; while a stupid fallback to link() might get us a little +# closer, rename() on two links both to the same file is undefined (in +# practice mostly a nop, it seems) and crippled filesystems like FAT +# don't support link() to begin with + + if ( $args->{'noclobber'} and -e $orig_file_name ) { + @_ = ( $args, "write_file '$file_name' - rename: File exists"); + goto &error ; + } + + rename( $file_name, $orig_file_name ); + } return 1 ; } @@ -523,6 +538,13 @@ write_file( $bin_file, \$buffer ) ; write_file( $bin_file, $buffer ) ; +=head3 noclobber + +If you set this boolean option, C<write_file> will not overwrite +existing files. + + write_file( $file, {noclobber => 1}, @data ) ; + =head3 atomic If you set this boolean option, the file will be written to in an @@ -531,7 +553,10 @@ file is closed it is renamed to the original file name (and rename is an atomic operation on most OS's). If the program using this were to crash in the middle of this, then the file with the pid suffix could -be left behind. +be left behind. Note that a race condition exists when combining +C<noclobber> with C<atomic> since due to inadequate intefaces, it is not +possible to be absolutely certain that the desired filename does not +already exist at the time of renaming. =head3 append @@ -545,6 +570,8 @@ error. (Yes, I know if it croaks it can't return anything but that is for when I add the options to select the error handling mode). +C<append> has no appreciable effect when combined with C<noclobber>. + =head3 err_mode You can use this option to control how C<write_file> behaves when an


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.