From ff86df8e25e29a8df7fe49348183dece33c2d54c Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 17 Mar 2022 22:41:35 +0100 Subject: [PATCH] Use `makeNativePath()` instead of `u8path()` On non-Windows platforms the internal representation used for paths is the configured native (narrow) character set. Most of the time that's UTF-8 but only on Windows UTF-8 is *always* used for the internal representation. --- backuphelper.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/backuphelper.cpp b/backuphelper.cpp index 19984de..3198c9d 100644 --- a/backuphelper.cpp +++ b/backuphelper.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -60,8 +61,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::u8path(BasicFileInfo::pathForOpen(originalPath)); - const auto backupPathForOpen = std::filesystem::u8path(BasicFileInfo::pathForOpen(backupPath)); + const auto originalPathForOpen = std::filesystem::path(makeNativePath(BasicFileInfo::pathForOpen(originalPath))); + const auto backupPathForOpen = std::filesystem::path(makeNativePath(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 +112,7 @@ void createBackupFile(const std::string &backupDir, const std::string &originalP NativeFileStream &backupStream) { // determine dirs - const auto backupDirRelative = std::filesystem::u8path(backupDir).is_relative(); + const auto backupDirRelative = std::filesystem::path(makeNativePath(backupDir)).is_relative(); const auto originalDir = backupDirRelative ? BasicFileInfo::containingDirectory(originalPath) : string(); // determine the backup path @@ -142,7 +143,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(std::filesystem::u8path(BasicFileInfo::pathForOpen(backupPath)), ec)) { + if (!std::filesystem::exists(makeNativePath(BasicFileInfo::pathForOpen(backupPath)), ec)) { break; } } @@ -153,8 +154,8 @@ void createBackupFile(const std::string &backupDir, const std::string &originalP } // rename original file - const auto u8originalPath = std::filesystem::u8path(originalPath); - const auto backupPathForOpen = std::filesystem::u8path(BasicFileInfo::pathForOpen(backupPath)); + const auto u8originalPath = std::filesystem::path(makeNativePath(originalPath)); + const auto backupPathForOpen = std::filesystem::path(makeNativePath(BasicFileInfo::pathForOpen(backupPath))); std::filesystem::rename(u8originalPath, backupPathForOpen, ec); if (ec) { // try making a copy instead, maybe backup dir is on another partition @@ -199,7 +200,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(std::filesystem::u8path(BasicFileInfo::pathForOpen(originalPath)), ec); !ec) { + if (const auto canonicalPath = std::filesystem::canonical(makeNativePath(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());