Fix dealing with non-ASCII characters in backup helper on Windows
See https://github.com/Martchus/tageditor/issues/81
This commit is contained in:
parent
526352e016
commit
4f221798e9
|
@ -60,8 +60,8 @@ void restoreOriginalFileFromBackupFile(
|
||||||
backupStream.exceptions(ios_base::badbit | ios_base::failbit);
|
backupStream.exceptions(ios_base::badbit | ios_base::failbit);
|
||||||
|
|
||||||
// check whether backup file actually exists and close the backup stream afterwards
|
// check whether backup file actually exists and close the backup stream afterwards
|
||||||
const auto originalPathForOpen = std::filesystem::path(BasicFileInfo::pathForOpen(originalPath));
|
const auto originalPathForOpen = std::filesystem::u8path(BasicFileInfo::pathForOpen(originalPath));
|
||||||
const auto backupPathForOpen = std::filesystem::path(BasicFileInfo::pathForOpen(backupPath));
|
const auto backupPathForOpen = std::filesystem::u8path(BasicFileInfo::pathForOpen(backupPath));
|
||||||
auto ec = std::error_code();
|
auto ec = std::error_code();
|
||||||
if (!std::filesystem::exists(backupPathForOpen, ec) && !ec) {
|
if (!std::filesystem::exists(backupPathForOpen, ec) && !ec) {
|
||||||
throw std::ios_base::failure("Backup/temporary file has not been created.");
|
throw std::ios_base::failure("Backup/temporary file has not been created.");
|
||||||
|
@ -111,7 +111,7 @@ void createBackupFile(const std::string &backupDir, const std::string &originalP
|
||||||
NativeFileStream &backupStream)
|
NativeFileStream &backupStream)
|
||||||
{
|
{
|
||||||
// determine dirs
|
// determine dirs
|
||||||
const auto backupDirRelative = std::filesystem::path(backupDir).is_relative();
|
const auto backupDirRelative = std::filesystem::u8path(backupDir).is_relative();
|
||||||
const auto originalDir = backupDirRelative ? BasicFileInfo::containingDirectory(originalPath) : string();
|
const auto originalDir = backupDirRelative ? BasicFileInfo::containingDirectory(originalPath) : string();
|
||||||
|
|
||||||
// determine the backup path
|
// determine the backup path
|
||||||
|
@ -142,7 +142,7 @@ void createBackupFile(const std::string &backupDir, const std::string &originalP
|
||||||
}
|
}
|
||||||
|
|
||||||
// test whether the backup path is still unused; otherwise continue loop
|
// test whether the backup path is still unused; otherwise continue loop
|
||||||
if (!std::filesystem::exists(BasicFileInfo::pathForOpen(backupPath), ec)) {
|
if (!std::filesystem::exists(std::filesystem::u8path(BasicFileInfo::pathForOpen(backupPath)), ec)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,15 +153,16 @@ void createBackupFile(const std::string &backupDir, const std::string &originalP
|
||||||
}
|
}
|
||||||
|
|
||||||
// rename original file
|
// rename original file
|
||||||
const auto backupPathForOpen = BasicFileInfo::pathForOpen(backupPath);
|
const auto u8originalPath = std::filesystem::u8path(originalPath);
|
||||||
std::filesystem::rename(originalPath, backupPathForOpen, ec);
|
const auto backupPathForOpen = std::filesystem::u8path(BasicFileInfo::pathForOpen(backupPath));
|
||||||
|
std::filesystem::rename(u8originalPath, backupPathForOpen, ec);
|
||||||
if (ec) {
|
if (ec) {
|
||||||
// try making a copy instead, maybe backup dir is on another partition
|
// try making a copy instead, maybe backup dir is on another partition
|
||||||
std::filesystem::copy_file(originalPath, backupPathForOpen, ec);
|
std::filesystem::copy_file(u8originalPath, backupPathForOpen, ec);
|
||||||
}
|
}
|
||||||
if (ec) {
|
if (ec) {
|
||||||
throw std::ios_base::failure(
|
throw std::ios_base::failure(
|
||||||
argsToString("Unable to create backup file \"", backupPathForOpen, "\" of \"", originalPath, "\" before rewriting it: " + ec.message()));
|
argsToString("Unable to create backup file \"", BasicFileInfo::pathForOpen(backupPath), "\" of \"", originalPath, "\" before rewriting it: " + ec.message()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// manage streams
|
// manage streams
|
||||||
|
@ -198,7 +199,7 @@ void createBackupFileCanonical(const std::string &backupDir, std::string &origin
|
||||||
CppUtilities::NativeFileStream &originalStream, CppUtilities::NativeFileStream &backupStream)
|
CppUtilities::NativeFileStream &originalStream, CppUtilities::NativeFileStream &backupStream)
|
||||||
{
|
{
|
||||||
auto ec = std::error_code();
|
auto ec = std::error_code();
|
||||||
if (const auto canonicalPath = std::filesystem::canonical(BasicFileInfo::pathForOpen(originalPath), ec); !ec) {
|
if (const auto canonicalPath = std::filesystem::canonical(std::filesystem::u8path(BasicFileInfo::pathForOpen(originalPath)), ec); !ec) {
|
||||||
originalPath = canonicalPath.string();
|
originalPath = canonicalPath.string();
|
||||||
} else {
|
} else {
|
||||||
throw std::ios_base::failure("Unable to canonicalize path of original file before rewriting it: " + ec.message());
|
throw std::ios_base::failure("Unable to canonicalize path of original file before rewriting it: " + ec.message());
|
||||||
|
|
Loading…
Reference in New Issue