eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:
I want to edit a file "in place", preserving the file's permissions, yet be safely re-runnable should an interruption occur at any time. Though the script does not need to be multi-user safe, I am vaguely interested in how that might be achieved (I guess with file locking). I know about Perl's -i switch, but this switch first renames the file, so if the user CTRL-C's (or power is lost) after the rename but before the new file is written, the script is not re-runnable because the original file has been renamed (or may not be complete). Is that right? Anyway, here is my attempt at solving this problem.
# Slurp file $fname into array @lines. open(my $fh, $fname) or die "open '$fname': $!"; my @lines = <$fh>; close($fh); # Mangle @lines as appropriate ... if ($file_contents_changed) { my $bak = $fname . $$ . '.tmp'; open(my $fh, '>'.$bak) or die "create $bak: $!"; print $fh @lines or die "writing $bak: $!"; close($fh) or die "close $bak: $!"; defined(my $mode = (stat($fname))[2]) or die "stat $fname: $!"; -w _ or (chmod($mode|0200, $fname) or die "chmod $fname: $!"); rename($bak, $fname) or die "rename $bak $fname: $!"; chmod($mode, $fname) or die "chmod $fname: $!"; }
This has at least one flaw. In the case of a read-only file, if you are interrupted after chmod($mode|0200) but before chmod($mode), the file $fname will have the wrong permissions on re-run. Improvements welcome.
References Added Later
- CPAN module File::Replace by haukex and the node replied to: How does rename() work on read-only files? (2018)
- From Path::Tiny's spew method: "The file is written to a temporary file in the same directory, then renamed over the original". Though not core, note that Path::Tiny is included with Strawberry Perl.
- Perl Best Practices book: is this one a best practice or a dodgy practice? (2005 battle with TheDamian)
- Implementation detail on rename (from Perl's win32.c) related to tye's Re^4: Perl Best Practices book: is this one a best practice or a dodgy practice? (atomic rename) can be found at Re^7: Read in hostfile, modify, output
- Re^2: Perl Best Practices book: is this one a best practice or a dodgy practice? warns that the ANSI C rename function similarly has different semantics on Unix and Windows (and these differences seem to be poorly documented)
- Re^2: Private Utilities (mentions Aristotle's rename utility, an improvement on Larry's original rename script)
- perlrun (perl -i switch)
- perlvar ($^I variable)
- File lock demo by LanX (2021)
- Re^3: file modifications using file::find (2021)
- Re: Yet another config file editing programme : Tell me how to make it better ! by dazz (2021)
- Using the file handle created by File::Temp to set specific file permissions by mldvx4 (2021)
- Race condition (wikipedia)
- Heisenbug (wikipedia)
|
---|