From a61bdc24dab160aba1a72e976334bf99b8cfe151 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 30 Jan 2026 08:55:37 -0600 Subject: [PATCH] refactor: use std::filesystem for tr_sys_path_resolve() (#8282) --- libtransmission/file-posix.cc | 18 --------------- libtransmission/file-win32.cc | 42 ----------------------------------- libtransmission/file.cc | 21 ++++++++++++++++++ 3 files changed, 21 insertions(+), 60 deletions(-) diff --git a/libtransmission/file-posix.cc b/libtransmission/file-posix.cc index 6b99615ef..d2049c20b 100644 --- a/libtransmission/file-posix.cc +++ b/libtransmission/file-posix.cc @@ -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{}; - - 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: diff --git a/libtransmission/file-win32.cc b/libtransmission/file-win32.cc index 3a49e1405..76857e8ef 100644 --- a/libtransmission/file-win32.cc +++ b/libtransmission/file-win32.cc @@ -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)) diff --git a/libtransmission/file.cc b/libtransmission/file.cc index 4f2d6a867..d6dcbfd70 100644 --- a/libtransmission/file.cc +++ b/libtransmission/file.cc @@ -3,6 +3,8 @@ // or any future license endorsed by Mnemosyne LLC. // License text can be found in the licenses/ folder. +#include +#include #include #include #include @@ -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 tr_sys_dir_get_files( std::string_view folder, std::function const& test,