copy file result enum

This commit is contained in:
David Kocik 2020-02-27 10:44:25 +01:00
parent c76526249a
commit 26c89300f3
3 changed files with 35 additions and 24 deletions

View file

@ -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. // 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); 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. // 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. // 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 // If with_check is true, then the content of the copied file is compared to the content
// of the source file before renaming. // 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. // Compares two files if identical.
extern int check_copy(const std::string& origin, const std::string& copy); 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. // Ignore system and hidden files, which may be created by the DropBox synchronisation process.
// https://github.com/prusa3d/PrusaSlicer/issues/1298 // https://github.com/prusa3d/PrusaSlicer/issues/1298

View file

@ -417,7 +417,7 @@ std::error_code rename_file(const std::string &from, const std::string &to)
#endif #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 source(from);
const boost::filesystem::path target(to); 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::permissions(target, perms, ec);
boost::filesystem::copy_file(source, target, boost::filesystem::copy_option::overwrite_if_exists, ec); boost::filesystem::copy_file(source, target, boost::filesystem::copy_option::overwrite_if_exists, ec);
if (ec) { if (ec) {
return -1; return FAIL_COPY_FILE;
} }
boost::filesystem::permissions(target, perms, ec); 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"; std::string to_temp = to + ".tmp";
int ret_val = copy_file_inner(from,to_temp); CopyFileResult ret_val = copy_file_inner(from,to_temp);
if(ret_val == 0) if(ret_val == SUCCESS)
{ {
if (with_check) if (with_check)
ret_val = check_copy(from, to_temp); ret_val = check_copy(from, to_temp);
if (ret_val == 0 && rename_file(to_temp, to)) if (ret_val == 0 && rename_file(to_temp, to))
ret_val = -3; ret_val = FAIL_RENAMING;
} }
return ret_val; return ret_val;
} }
int check_copy(const std::string &origin, const std::string &copy) CopyFileResult check_copy(const std::string &origin, const std::string &copy)
{ {
boost::nowide::ifstream f1(origin, std::ifstream::in | std::ifstream::binary | std::ifstream::ate); 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); boost::nowide::ifstream f2(copy, std::ifstream::in | std::ifstream::binary | std::ifstream::ate);
if (f1.fail()) if (f1.fail())
return -4; return FAIL_CHECK_ORIGIN_NOT_OPENED;
if (f2.fail()) if (f2.fail())
return -5; return FAIL_CHECK_TARGET_NOT_OPENED;
std::streampos fsize = f1.tellg(); std::streampos fsize = f1.tellg();
if (fsize != f2.tellg()) if (fsize != f2.tellg())
return -2; return FAIL_FILES_DIFFERENT;
f1.seekg(0, std::ifstream::beg); f1.seekg(0, std::ifstream::beg);
f2.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 &copy)
if (origin_cnt != copy_cnt || if (origin_cnt != copy_cnt ||
(origin_cnt > 0 && std::memcmp(buffer_origin.data(), buffer_copy.data(), origin_cnt) != 0)) (origin_cnt > 0 && std::memcmp(buffer_origin.data(), buffer_copy.data(), origin_cnt) != 0))
// Files are different. // Files are different.
return -2; return FAIL_FILES_DIFFERENT;
fsize -= origin_cnt; fsize -= origin_cnt;
} while (f1.good() && f2.good()); } while (f1.good() && f2.good());
// All data has been read and compared equal. // 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. // Ignore system and hidden files, which may be created by the DropBox synchronisation process.

View file

@ -104,21 +104,24 @@ void BackgroundSlicingProcess::process_fff()
bool with_check = GUI::RemovableDriveManager::get_instance().is_path_on_removable_drive(export_path); 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); int copy_ret_val = copy_file(m_temp_output_path, export_path, with_check);
switch (copy_ret_val){ switch (copy_ret_val){
case 0: break; // no error case SUCCESS: break; // no error
case -2: 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()); 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; 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()); 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; 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()); 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; 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()); 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; break;
default: 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; break;
} }
@ -473,7 +476,7 @@ void BackgroundSlicingProcess::prepare_upload()
if (m_print == m_fff_print) { if (m_print == m_fff_print) {
m_print->set_status(95, _utf8(L("Running post-processing scripts"))); 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"))); 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()); run_post_process_scripts(source_path.string(), m_fff_print->config());