]> git.sesse.net Git - remoteglot/commitdiff
Factor out atomic file overwriting into its own function.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 19 Nov 2014 22:24:35 +0000 (23:24 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 19 Nov 2014 22:24:35 +0000 (23:24 +0100)
remoteglot.pl

index d82c20bed25034896c341b979aa9a5b36bb0fac9..fb74e6eab5c0d04c454b532ae047ed509fe2141c 100755 (executable)
@@ -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 {