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.
This commit is contained in:
parent
b441860cc6
commit
ff86df8e25
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <c++utilities/conversion/stringbuilder.h>
|
#include <c++utilities/conversion/stringbuilder.h>
|
||||||
#include <c++utilities/conversion/stringconversion.h>
|
#include <c++utilities/conversion/stringconversion.h>
|
||||||
|
#include <c++utilities/io/path.h>
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
@ -60,8 +61,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::u8path(BasicFileInfo::pathForOpen(originalPath));
|
const auto originalPathForOpen = std::filesystem::path(makeNativePath(BasicFileInfo::pathForOpen(originalPath)));
|
||||||
const auto backupPathForOpen = std::filesystem::u8path(BasicFileInfo::pathForOpen(backupPath));
|
const auto backupPathForOpen = std::filesystem::path(makeNativePath(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 +112,7 @@ void createBackupFile(const std::string &backupDir, const std::string &originalP
|
||||||
NativeFileStream &backupStream)
|
NativeFileStream &backupStream)
|
||||||
{
|
{
|
||||||
// determine dirs
|
// 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();
|
const auto originalDir = backupDirRelative ? BasicFileInfo::containingDirectory(originalPath) : string();
|
||||||
|
|
||||||
// determine the backup path
|
// 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
|
// 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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,8 +154,8 @@ void createBackupFile(const std::string &backupDir, const std::string &originalP
|
||||||
}
|
}
|
||||||
|
|
||||||
// rename original file
|
// rename original file
|
||||||
const auto u8originalPath = std::filesystem::u8path(originalPath);
|
const auto u8originalPath = std::filesystem::path(makeNativePath(originalPath));
|
||||||
const auto backupPathForOpen = std::filesystem::u8path(BasicFileInfo::pathForOpen(backupPath));
|
const auto backupPathForOpen = std::filesystem::path(makeNativePath(BasicFileInfo::pathForOpen(backupPath)));
|
||||||
std::filesystem::rename(u8originalPath, backupPathForOpen, ec);
|
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
|
||||||
|
@ -199,7 +200,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(std::filesystem::u8path(BasicFileInfo::pathForOpen(originalPath)), ec); !ec) {
|
if (const auto canonicalPath = std::filesystem::canonical(makeNativePath(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