Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Safe way to open files

by L0rdPhi1 (Sexton)
on Jun 15, 2002 at 14:03 UTC ( [id://174840]=note: print w/replies, xml ) Need Help??


in reply to Safe way to open files

Thanks, That's a good idea :) I just cannot figure out the zero bytes bug... I've managed to stump quite a few people with it, by the way. But I think it has something to do with sysopen and truncate but I can't be sure.


Update:

I totally recoded the old sysopen part but I'm not sure if what I did will work...
sub safe_open { my $fn = shift; my $fh = do { local *FH }; my ($mode, $result); if ($fn =~ /^>[^>]/) { $mode = 1; } elsif ($fn =~ /^>>/) { $mode = 2; } else { $mode = 0; } if ($mode == 1 and $flock_enabled) { # Write mode with flock. $truncate_please = 1; my $fhtemp = do { local *FHT }; $result = open( $fhtemp, "+>>$fn" ); if ($result) { $result = open($filehandle, ">&$fhtemp" ); close($fhtemp); } } elsif ($mode == 1) { # Write mode without flock. $result = open($fh, ">$fn"); } elsif ($mode == 2) { # Append mode. $result = open($fh, ">>$fn"); } elsif ($mode == 0) { $result = open($fh, $fn); } unless ($result) { # Open failed. fatal_error("Couldn't open $fn."); } flock($fh, 2) if $flock_enabled; if ($truncate_please) { truncate($fh, 0) or fatal_error("Could not truncate file $fn." +); } return $fh; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://174840]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-20 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found