}
$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);
+
+ if (exists($pos_calculating->{'history'}) &&
+ defined($remoteglotconf::json_history_dir)) {
+ my $halfmove_num = scalar @{$pos_calculating->{'history'}};
+ (my $fen = $pos_calculating->fen()) =~ tr,/ ,-_,;
+ my $filename = $remoteglotconf::json_history_dir . "/move$halfmove_num-$fen.json";
+
+ # TODO: Avoid overwriting earlier analysis if it's better.
+ atomic_set_contents($filename, $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 {