diff --git a/src/libslic3r/Utils.hpp b/src/libslic3r/Utils.hpp index 06c435809..5cdf75037 100644 --- a/src/libslic3r/Utils.hpp +++ b/src/libslic3r/Utils.hpp @@ -64,15 +64,23 @@ extern std::string normalize_utf8_nfc(const char *src); // for a short while, so the file may not be movable. Retry while we see recoverable errors. extern std::error_code rename_file(const std::string &from, const std::string &to); +enum CopyFileResult { + SUCCESS = 0, + FAIL_COPY_FILE, + FAIL_FILES_DIFFERENT, + FAIL_RENAMING, + FAIL_CHECK_ORIGIN_NOT_OPENED, + FAIL_CHECK_TARGET_NOT_OPENED +}; // Copy a file, adjust the access attributes, so that the target is writable. -int copy_file_inner(const std::string &from, const std::string &to); +CopyFileResult copy_file_inner(const std::string &from, const std::string &to); // Copy file to a temp file first, then rename it to the final file name. // If with_check is true, then the content of the copied file is compared to the content // of the source file before renaming. -extern int copy_file(const std::string &from, const std::string &to, const bool with_check = false); +extern CopyFileResult copy_file(const std::string &from, const std::string &to, const bool with_check = false); -// Compares two files, returns 0 if identical, -1 if different. -extern int check_copy(const std::string& origin, const std::string& copy); +// Compares two files if identical. +extern CopyFileResult check_copy(const std::string& origin, const std::string& copy); // Ignore system and hidden files, which may be created by the DropBox synchronisation process. // https://github.com/prusa3d/PrusaSlicer/issues/1298 diff --git a/src/libslic3r/utils.cpp b/src/libslic3r/utils.cpp index 104d0c5ff..ad91e5239 100644 --- a/src/libslic3r/utils.cpp +++ b/src/libslic3r/utils.cpp @@ -417,7 +417,7 @@ std::error_code rename_file(const std::string &from, const std::string &to) #endif } -int copy_file_inner(const std::string& from, const std::string& to) +CopyFileResult copy_file_inner(const std::string& from, const std::string& to) { const boost::filesystem::path source(from); const boost::filesystem::path target(to); @@ -433,40 +433,40 @@ int copy_file_inner(const std::string& from, const std::string& to) boost::filesystem::permissions(target, perms, ec); boost::filesystem::copy_file(source, target, boost::filesystem::copy_option::overwrite_if_exists, ec); if (ec) { - return -1; + return FAIL_COPY_FILE; } boost::filesystem::permissions(target, perms, ec); - return 0; + return SUCCESS; } -int copy_file(const std::string &from, const std::string &to, const bool with_check) +CopyFileResult copy_file(const std::string &from, const std::string &to, const bool with_check) { std::string to_temp = to + ".tmp"; - int ret_val = copy_file_inner(from,to_temp); - if(ret_val == 0) + CopyFileResult ret_val = copy_file_inner(from,to_temp); + if(ret_val == SUCCESS) { if (with_check) ret_val = check_copy(from, to_temp); if (ret_val == 0 && rename_file(to_temp, to)) - ret_val = -3; + ret_val = FAIL_RENAMING; } return ret_val; } -int check_copy(const std::string &origin, const std::string ©) +CopyFileResult check_copy(const std::string &origin, const std::string ©) { boost::nowide::ifstream f1(origin, std::ifstream::in | std::ifstream::binary | std::ifstream::ate); boost::nowide::ifstream f2(copy, std::ifstream::in | std::ifstream::binary | std::ifstream::ate); if (f1.fail()) - return -4; + return FAIL_CHECK_ORIGIN_NOT_OPENED; if (f2.fail()) - return -5; + return FAIL_CHECK_TARGET_NOT_OPENED; std::streampos fsize = f1.tellg(); if (fsize != f2.tellg()) - return -2; + return FAIL_FILES_DIFFERENT; f1.seekg(0, std::ifstream::beg); f2.seekg(0, std::ifstream::beg); @@ -483,12 +483,12 @@ int check_copy(const std::string &origin, const std::string ©) if (origin_cnt != copy_cnt || (origin_cnt > 0 && std::memcmp(buffer_origin.data(), buffer_copy.data(), origin_cnt) != 0)) // Files are different. - return -2; + return FAIL_FILES_DIFFERENT; fsize -= origin_cnt; } while (f1.good() && f2.good()); // All data has been read and compared equal. - return (f1.eof() && f2.eof() && fsize == 0) ? 0 : -2; + return (f1.eof() && f2.eof() && fsize == 0) ? SUCCESS : FAIL_FILES_DIFFERENT; } // Ignore system and hidden files, which may be created by the DropBox synchronisation process. diff --git a/src/slic3r/GUI/BackgroundSlicingProcess.cpp b/src/slic3r/GUI/BackgroundSlicingProcess.cpp index c4e6272ba..c3cbba395 100644 --- a/src/slic3r/GUI/BackgroundSlicingProcess.cpp +++ b/src/slic3r/GUI/BackgroundSlicingProcess.cpp @@ -104,21 +104,24 @@ void BackgroundSlicingProcess::process_fff() bool with_check = GUI::RemovableDriveManager::get_instance().is_path_on_removable_drive(export_path); int copy_ret_val = copy_file(m_temp_output_path, export_path, with_check); switch (copy_ret_val){ - case 0: break; // no error - case -2: + case SUCCESS: break; // no error + case FAIL_COPY_FILE: + throw std::runtime_error(_utf8(L("Copying of the temporary G-code to the output G-code failed. Maybe the SD card is write locked?"))); + break; + case FAIL_FILES_DIFFERENT: throw std::runtime_error((boost::format(_utf8(L("Copying of the temporary G-code to the output G-code failed. There might be problem with target device, please try exporting again or using different device. The corrupted output G-code is at %1%.tmp."))) % export_path).str()); break; - case -3: + case FAIL_RENAMING: throw std::runtime_error((boost::format(_utf8(L("Renaming of the G-code after copying to the selected destination folder has failed. Current path is %1%.tmp. Please try exporting again."))) % export_path).str()); break; - case -4: + case FAIL_CHECK_ORIGIN_NOT_OPENED: throw std::runtime_error((boost::format(_utf8(L("Copying of the temporary G-code has finished but the original code at %1% couldn't be opened during copy check. The output G-code is at %2%.tmp."))) % m_temp_output_path % export_path).str()); break; - case -5: + case FAIL_CHECK_TARGET_NOT_OPENED: throw std::runtime_error((boost::format(_utf8(L("Copying of the temporary G-code has finished but the exported code couldn't be opened during copy check. The output G-code is at %1%.tmp."))) % export_path).str()); break; default: - throw std::runtime_error(_utf8(L("Copying of the temporary G-code to the output G-code failed. Maybe the SD card is write locked?"))); + BOOST_LOG_TRIVIAL(warning) << "Unexpected fail code(" << (int)copy_ret_val << ") durring copy_file() to " << export_path << "."; break; } @@ -473,7 +476,7 @@ void BackgroundSlicingProcess::prepare_upload() if (m_print == m_fff_print) { m_print->set_status(95, _utf8(L("Running post-processing scripts"))); - if (copy_file(m_temp_output_path, source_path.string()) != 0) { + if (copy_file(m_temp_output_path, source_path.string()) != SUCCESS) { throw std::runtime_error(_utf8(L("Copying of the temporary G-code to the output G-code failed"))); } run_post_process_scripts(source_path.string(), m_fff_print->config());