refactor: use std::filesystem for tr_sys_path_resolve() (#8282)

This commit is contained in:
Charles Kerr
2026-01-30 08:55:37 -06:00
committed by GitHub
parent c223e3bb64
commit a61bdc24da
3 changed files with 21 additions and 60 deletions

View File

@@ -226,24 +226,6 @@ bool tr_sys_path_is_same(std::string_view const path1, std::string_view const pa
return ret;
}
std::string tr_sys_path_resolve(std::string_view path, tr_error* error)
{
auto const szpath = tr_pathbuf{ path };
auto buf = std::array<char, PATH_MAX>{};
if (auto const* const ret = realpath(szpath, std::data(buf)); ret != nullptr)
{
return ret;
}
if (error != nullptr)
{
error->set_from_errno(errno);
}
return {};
}
std::string_view tr_sys_path_basename(std::string_view path, tr_error* /*error*/)
{
// As per the basename() manpage:

View File

@@ -488,48 +488,6 @@ bool tr_sys_path_is_same(std::string_view const path1, std::string_view const pa
fi1->nFileIndexLow == fi2->nFileIndexLow;
}
std::string tr_sys_path_resolve(std::string_view path, tr_error* error)
{
auto ret = std::string{};
if (auto const wide_path = path_to_native_path(path); !std::empty(wide_path))
{
if (auto const handle = CreateFileW(
wide_path.c_str(),
FILE_READ_EA,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
nullptr);
handle != INVALID_HANDLE_VALUE)
{
if (auto const wide_ret_size = GetFinalPathNameByHandleW(handle, nullptr, 0, 0); wide_ret_size != 0)
{
auto wide_ret = std::wstring{};
wide_ret.resize(wide_ret_size);
if (GetFinalPathNameByHandleW(handle, std::data(wide_ret), wide_ret_size, 0) == wide_ret_size - 1)
{
// `wide_ret_size` includes the terminating '\0'; remove it from `wide_ret`
wide_ret.resize(std::size(wide_ret) - 1);
TR_ASSERT(tr_strv_starts_with(wide_ret, NativeLocalPathPrefix));
ret = native_path_to_path(wide_ret);
}
}
CloseHandle(handle);
}
}
if (!std::empty(ret))
{
return ret;
}
set_system_error(error, GetLastError());
return {};
}
std::string_view tr_sys_path_basename(std::string_view path, tr_error* error)
{
if (std::empty(path))

View File

@@ -3,6 +3,8 @@
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <filesystem>
#include <system_error>
#include <string>
#include <string_view>
#include <vector>
@@ -11,6 +13,25 @@
#include "libtransmission/file.h"
#include "libtransmission/tr-assert.h"
std::string tr_sys_path_resolve(std::string_view path, tr_error* error)
{
auto ec = std::error_code{};
auto const canonical_path = std::filesystem::canonical(tr_u8path(path), ec);
if (ec)
{
if (error != nullptr)
{
error->set(ec.value(), ec.message());
}
return {};
}
auto const u8_path = canonical_path.u8string();
return { std::begin(u8_path), std::end(u8_path) };
}
std::vector<std::string> tr_sys_dir_get_files(
std::string_view folder,
std::function<bool(std::string_view)> const& test,