From bbd63616b11cf87e5f0dab2b3c02cb462b4ad884 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 17 Dec 2016 21:56:57 +0100 Subject: [PATCH] Write to a temporary file before renaming with the final name. #1844 --- lib/Slic3r/Print.pm | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 84d9a4f70..7fc2bdd86 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -394,13 +394,16 @@ sub write_gcode { my $self = shift; my ($file) = @_; + my $tempfile; + # open output gcode file if we weren't supplied a file-handle my $fh; if (ref $file eq 'IO::Scalar') { $fh = $file; } else { - Slic3r::open(\$fh, ">", $file) - or die "Failed to open $file for writing\n"; + $tempfile = "$file.tmp"; + 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 binmode $fh, ':utf8'; @@ -414,6 +417,10 @@ sub write_gcode { # close our gcode file close $fh; + + if ($tempfile) { + rename $tempfile, $file; + } } # this method will return the supplied input file path after expanding its