Write to a temporary file before renaming with the final name. #1844

This commit is contained in:
Alessandro Ranellucci 2016-12-17 21:56:57 +01:00 committed by bubnikv
parent 6b1a72aac9
commit bbd63616b1

View file

@ -394,13 +394,16 @@ sub write_gcode {
my $self = shift; my $self = shift;
my ($file) = @_; my ($file) = @_;
my $tempfile;
# open output gcode file if we weren't supplied a file-handle # open output gcode file if we weren't supplied a file-handle
my $fh; my $fh;
if (ref $file eq 'IO::Scalar') { if (ref $file eq 'IO::Scalar') {
$fh = $file; $fh = $file;
} else { } else {
Slic3r::open(\$fh, ">", $file) $tempfile = "$file.tmp";
or die "Failed to open $file for writing\n"; Slic3r::open(\$fh, ">", $tempfile)
or die "Failed to open $tempfile for writing\n";
# enable UTF-8 output since user might have entered Unicode characters in fields like notes # enable UTF-8 output since user might have entered Unicode characters in fields like notes
binmode $fh, ':utf8'; binmode $fh, ':utf8';
@ -414,6 +417,10 @@ sub write_gcode {
# close our gcode file # close our gcode file
close $fh; close $fh;
if ($tempfile) {
rename $tempfile, $file;
}
} }
# this method will return the supplied input file path after expanding its # this method will return the supplied input file path after expanding its