Revert inplace edit on HUP or full disk

J

jgraber

Are there any gotcha's on this demo script to
revert an inplace edit when catching a HUP signal,
or on a full disk error?

Is there a variable that holds the backup filename,
that would also handle format -i'dir/*.bak' ?

demo appears to filter correctly without HUP,
revert on catching kill -HUP,
revert on catching fake full disk.

On kill -KILL, demo leaves a backup file,
but not a filtered file.

run on linux like this:
% cp demo junk
% perl -i~ demo junk &
[1] 27622
% kill -HUP 27622
demo caught ' fake full disk', reverting by rename 'junk~' to 'junk' status (1=ok): 1, and quit now


#!/usr/local/bin/perl
use warnings;
use strict;
$SIG{HUP} = \&sig_hup_handler;
sub sig_hup_handler{
local $" = ' '; # $" is list sep
die "$0 caught '@_', reverting by rename '$ARGV$^I' to '$ARGV' status (1=ok): ",
rename( $ARGV . $^I, $ARGV),
", and quit now\n"; }
my $i = 0;
while(<>){
s/$/"# $i"/e; # add line number
print;
sleep 1 if ++$i < 10;
if (eof) {print "# last line\n";
$i = 0; # then send full disk error to sig_hup
sig_hup_handler($!) if not close ;
# sig_hup_handler(' fake full disk') if not 0;

}
}
 
B

Brian McCauley

Are there any gotcha's on this demo script to
revert an inplace edit when catching a HUP signal,
or on a full disk error?

demo appears to filter correctly without HUP,
revert on catching kill -HUP,
revert on catching fake full disk.

On kill -KILL, demo leaves a backup file,
but not a filtered file.

The solution is not to use the inbuilt inplace edit mechanism that
renames at the start but rather to create the new file with a suffix
and then rename that over the original. On properly enginerred OSs the
process of renaming over an exisitng file is atomic so there is never
an instant when there's no file under the original name.

See also IO::AtomicFile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top