Fix g-code export to permissionless filesystems

Fix #2521
Fix #3102
This commit is contained in:
Vojtech Kral 2019-11-01 10:51:26 +01:00
parent 85b6c6d2dd
commit d729315de3

View file

@ -424,14 +424,19 @@ int copy_file(const std::string &from, const std::string &to)
static const auto perms = boost::filesystem::owner_read | boost::filesystem::owner_write | boost::filesystem::group_read | boost::filesystem::others_read; // aka 644 static const auto perms = boost::filesystem::owner_read | boost::filesystem::owner_write | boost::filesystem::group_read | boost::filesystem::others_read; // aka 644
// Make sure the file has correct permission both before and after we copy over it. // Make sure the file has correct permission both before and after we copy over it.
try { // NOTE: error_code variants are used here to supress expception throwing.
if (boost::filesystem::exists(target)) // Error code of permission() calls is ignored on purpose - if they fail,
boost::filesystem::permissions(target, perms); // the copy_file() function will fail appropriately and we don't want the permission()
boost::filesystem::copy_file(source, target, boost::filesystem::copy_option::overwrite_if_exists); // calls to cause needless failures on permissionless filesystems (ie. FATs on SD cards etc.)
boost::filesystem::permissions(target, perms); // or when the target file doesn't exist.
} catch (std::exception & /* ex */) { boost::system::error_code ec;
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 -1;
} }
boost::filesystem::permissions(target, perms, ec);
return 0; return 0;
} }