From a8b50dfb8117c5495784ae330ccefa8db2355e83 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 19 Nov 2014 23:24:35 +0100 Subject: [PATCH] Factor out atomic file overwriting into its own function. --- remoteglot.pl | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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 { -- 2.39.2