Fix dealing with non-ASCII characters in backup helper on Windows

See https://github.com/Martchus/tageditor/issues/81
This commit is contained in:
Martchus 2022-03-16 19:13:09 +01:00
parent 526352e016
commit 4f221798e9
1 changed files with 10 additions and 9 deletions

View File

@ -60,8 +60,8 @@ void restoreOriginalFileFromBackupFile(
backupStream.exceptions(ios_base::badbit | ios_base::failbit);
// check whether backup file actually exists and close the backup stream afterwards
const auto originalPathForOpen = std::filesystem::path(BasicFileInfo::pathForOpen(originalPath));
const auto backupPathForOpen = std::filesystem::path(BasicFileInfo::pathForOpen(backupPath));
const auto originalPathForOpen = std::filesystem::u8path(BasicFileInfo::pathForOpen(originalPath));
const auto backupPathForOpen = std::filesystem::u8path(BasicFileInfo::pathForOpen(backupPath));
auto ec = std::error_code();
if (!std::filesystem::exists(backupPathForOpen, ec) && !ec) {
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)
{
// 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();
// 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
if (!std::filesystem::exists(BasicFileInfo::pathForOpen(backupPath), ec)) {
if (!std::filesystem::exists(std::filesystem::u8path(BasicFileInfo::pathForOpen(backupPath)), ec)) {
break;
}
}
@ -153,15 +153,16 @@ void createBackupFile(const std::string &backupDir, const std::string &originalP
}
// rename original file
const auto backupPathForOpen = BasicFileInfo::pathForOpen(backupPath);
std::filesystem::rename(originalPath, backupPathForOpen, ec);
const auto u8originalPath = std::filesystem::u8path(originalPath);
const auto backupPathForOpen = std::filesystem::u8path(BasicFileInfo::pathForOpen(backupPath));
std::filesystem::rename(u8originalPath, backupPathForOpen, ec);
if (ec) {
// 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) {
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
@ -198,7 +199,7 @@ void createBackupFileCanonical(const std::string &backupDir, std::string &origin
CppUtilities::NativeFileStream &originalStream, CppUtilities::NativeFileStream &backupStream)
{
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();
} else {
throw std::ios_base::failure("Unable to canonicalize path of original file before rewriting it: " + ec.message());