From: Steinar H. Gunderson Date: Wed, 19 Nov 2014 22:24:35 +0000 (+0100) Subject: Factor out atomic file overwriting into its own function. X-Git-Url: https://git.sesse.net/?p=remoteglot;a=commitdiff_plain;h=a8b50dfb8117c5495784ae330ccefa8db2355e83;ds=sidebyside Factor out atomic file overwriting into its own function. --- diff --git a/remoteglot.pl b/remoteglot.pl index d82c20b..fb74e6e 100755 --- a/remoteglot.pl +++ b/remoteglot.pl @@ -742,11 +742,18 @@ sub output_json { } $json->{'refutation_lines'} = \%refutation_lines; - open my $fh, ">", $remoteglotconf::json_output . ".tmp" + my $encoded = JSON::XS::encode_json($json); + atomic_set_contents($remoteglotconf::json_output, $encoded); +} + +sub atomic_set_contents { + my ($filename, $contents) = @_; + + open my $fh, ">", $filename . ".tmp" or return; - print $fh JSON::XS::encode_json($json); + print $fh $contents; close $fh; - rename($remoteglotconf::json_output . ".tmp", $remoteglotconf::json_output); + rename($filename . ".tmp", $filename); } sub uciprint {