From 2c11dd768a70c264a6e498f639793b5ca2044e0d 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 b274836e3..0c94fdf5a 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -393,13 +393,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'; @@ -413,6 +416,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