Report the system error when the G-code cannot be written

The G-code writer ignored the results of fwrite() and fflush(), and a
failed export always asked "Is the disk full?". Keep the first system
error and show it below that question, so a missing permission or a
removed drive says what went wrong.

Ported from bambulab/BambuStudio@302cadf9b9

Co-authored-by: BenJule <benjamin.luetker@gmail.com>
This commit is contained in:
Ian BassiandBenJule committed 2026-11-30 14:27:08 -03:00
1 parent 7ad819a2fa
commit 129481e149
2 files changed
+17 -3

No files matched your search

+14 -3
View File
@@ -61,6 +61,7 @@
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <cerrno>
#include <cfloat>
#include <climits>
#include <cmath>
@@ -2952,10 +2953,12 @@ void GCode::do_export(Print* print, const char* path, GCodeProcessorResult* resu
this->_do_export(*print, file, thumbnail_cb);
file.flush();
if (file.is_error()) {
const std::string os_error = file.last_error();
file.close();
boost::nowide::remove(path_tmp.c_str());
// TRN %1% is a file path.
throw Slic3r::RuntimeError(Slic3r::format(_(L("G-code export to %1% failed.\nIs the disk full?")), path) + "\n");
throw Slic3r::RuntimeError(Slic3r::format(_(L("G-code export to %1% failed.\nIs the disk full?")), path) + "\n" +
(os_error.empty() ? std::string() : os_error + "\n"));
}
} catch (std::exception &ex) {
// Rethrow on any exception. std::runtime_exception and CanceledException are expected to be thrown.
@@ -9174,9 +9177,15 @@ bool GCode::GCodeOutputStream::is_error() const
return ::ferror(this->f);
}
std::string GCode::GCodeOutputStream::last_error() const
{
return m_write_errno == 0 ? std::string() : std::string(::strerror(m_write_errno));
}
void GCode::GCodeOutputStream::flush()
{
::fflush(this->f);
if (::fflush(this->f) != 0 && m_write_errno == 0)
m_write_errno = errno;
}
void GCode::GCodeOutputStream::close()
@@ -9192,7 +9201,9 @@ void GCode::GCodeOutputStream::write(const char *what)
if (what != nullptr) {
const char* gcode = what;
// writes string to file
fwrite(gcode, 1, ::strlen(gcode), this->f);
const size_t len = ::strlen(gcode);
if (::fwrite(gcode, 1, len, this->f) != len && m_write_errno == 0)
m_write_errno = errno;
//FIXME don't allocate a string, maybe process a batch of lines?
m_processor.process_buffer(std::string(gcode));
}