resolve junctions and symlinks in canonicalize_path on Windows (#2670)

* resolve junctions and symlinks in canonicalize_path on Windows

* Shorten the canonicalize_path comment

---------

Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
This commit is contained in:
metsw24-maxandyhirose authored and GitHub committed 2026-11-16 21:19:39 -05:00
1 parent 546e821c65
commit 654fd3546c
2 files changed
+72 -3

No files matched your search

+29 -3
View File
@@ -6111,9 +6111,35 @@ inline bool is_valid_path(const std::string &path) {
inline bool canonicalize_path(const char *path, std::string &resolved) {
#if defined(_WIN32)
char buf[_MAX_PATH];
if (_fullpath(buf, path, _MAX_PATH) == nullptr) { return false; }
resolved = buf;
// Resolve junctions and symlinks, as realpath() does
auto wpath = u8string_to_wstring(path);
if (wpath.empty()) { return false; }
CREATEFILE2_EXTENDED_PARAMETERS params{};
params.dwSize = sizeof(params);
params.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS; // Needed to open a directory
auto h = ::CreateFile2(wpath.c_str(), 0,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
OPEN_EXISTING, &params);
if (h == INVALID_HANDLE_VALUE) { return false; }
auto guard = scope_exit([&]() { ::CloseHandle(h); });
std::wstring wbuf(MAX_PATH, L'\0');
auto len = ::GetFinalPathNameByHandleW(
h, &wbuf[0], static_cast<DWORD>(wbuf.size()), FILE_NAME_NORMALIZED);
if (len >= wbuf.size()) {
wbuf.resize(len);
len = ::GetFinalPathNameByHandleW(
h, &wbuf[0], static_cast<DWORD>(wbuf.size()), FILE_NAME_NORMALIZED);
}
if (len == 0 || len >= wbuf.size()) { return false; }
auto n = ::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast<int>(len),
nullptr, 0, nullptr, nullptr);
if (n <= 0) { return false; }
resolved.assign(static_cast<size_t>(n), '\0');
::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast<int>(len),
&resolved[0], n, nullptr, nullptr);
#elif defined(PATH_MAX)
char buf[PATH_MAX];
if (realpath(path, buf) == nullptr) { return false; }