fix: pass UTF-8 strings into std::filesystem::path (#8269)

* feat: temporary replacement for `std::filesystem::u8path()`

* fix: pass UTF-8 strings into `std::filesystem::path`
This commit is contained in:
Yat Ho
2026-01-29 22:43:50 +08:00
committed by GitHub
parent dd008ae1d0
commit 19c52a3e61
3 changed files with 27 additions and 3 deletions

View File

@@ -54,7 +54,7 @@ bool FreeSpaceLabel::Impl::on_freespace_timer()
}
auto ec = std::error_code{};
auto const capacity = std::filesystem::space(dir_, ec);
auto const capacity = std::filesystem::space(tr_u8path(dir_), ec);
auto const text = !ec ?
fmt::format(fmt::runtime(_("{disk_space} free")), fmt::arg("disk_space", tr_strlsize(capacity.available))) :
_("Error");

View File

@@ -7,8 +7,10 @@
#include <cstdint> // uint64_t
#include <ctime> // time_t
#include <filesystem>
#include <functional>
#include <optional>
#include <ranges>
#include <string>
#include <string_view>
#include <vector>
@@ -508,3 +510,25 @@ bool tr_sys_dir_close(tr_sys_dir_t handle, tr_error* error = nullptr);
/** @} */
/** @} */
/**
* Temporary replacement of deprecated `std::filesystem::u8path()` while we migrate
* from char to char8_t strings.
*
* https://stackoverflow.com/a/57635139/11390656
* @{
*/
template<typename InputIt>
[[nodiscard]] std::filesystem::path tr_u8path(InputIt begin, InputIt end)
{
auto const view = std::ranges::subrange(begin, end) | std::views::transform([](char const c) -> char8_t { return c; });
return { view.begin(), view.end() };
}
[[nodiscard]] inline std::filesystem::path tr_u8path(std::string_view path)
{
return tr_u8path(path.begin(), path.end());
}
/// @}

View File

@@ -2232,7 +2232,7 @@ using SessionAccessors = std::pair<SessionGetter, SessionSetter>;
[](tr_session const& src) -> tr_variant
{
auto ec = std::error_code{};
auto const space = std::filesystem::space(src.downloadDir(), ec);
auto const space = std::filesystem::space(tr_u8path(src.downloadDir()), ec);
return !ec ? space.available : tr_variant{ -1 };
},
nullptr);
@@ -2737,7 +2737,7 @@ using SessionAccessors = std::pair<SessionGetter, SessionSetter>;
// get the free space
auto ec = std::error_code{};
auto const capacity = std::filesystem::space(*path, ec);
auto const capacity = std::filesystem::space(tr_u8path(*path), ec);
// response
args_out.try_emplace(TR_KEY_path, *path);