1414#include < algorithm>
1515#include < cerrno>
1616#include < cstring>
17+ #include < filesystem>
1718#include < functional>
1819#include < list>
1920#include < memory>
21+ #include < string_view>
22+
23+ #include " tonic/common/log.h"
24+ #include " tonic/filesystem/filesystem/windows_utils.h"
2025
2126namespace filesystem {
2227namespace {
@@ -181,9 +186,19 @@ std::string SimplifyPath(std::string path) {
181186}
182187
183188std::string AbsolutePath (const std::string& path) {
184- char absPath[MAX_PATH ];
185- _fullpath (absPath, path.c_str (), MAX_PATH );
186- return std::string (absPath);
189+ std::error_code ec;
190+ std::filesystem::path p (std::u8string_view (
191+ reinterpret_cast <const char8_t *>(path.data ()), path.size ()));
192+ std::filesystem::path abs_path = std::filesystem::absolute (p, ec);
193+ if (ec) {
194+ tonic::Log (" Failed to resolve absolute path for '%s': %s" , path.c_str (),
195+ ec.message ().c_str ());
196+ return std::string ();
197+ }
198+ abs_path = abs_path.lexically_normal ();
199+ std::u8string u8_str = abs_path.u8string ();
200+ return std::string (reinterpret_cast <const char *>(u8_str.data ()),
201+ u8_str.size ());
187202}
188203
189204std::string GetDirectoryName (const std::string& path) {
@@ -204,17 +219,30 @@ std::string GetBaseName(const std::string& path) {
204219}
205220
206221std::string GetAbsoluteFilePath (const std::string& path) {
222+ std::wstring wide_path = Utf8PathToWide (path);
207223 HANDLE file =
208- CreateFileA (path .c_str (), FILE_READ_ATTRIBUTES ,
224+ CreateFileW (wide_path .c_str (), FILE_READ_ATTRIBUTES ,
209225 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , NULL ,
210226 OPEN_EXISTING , FILE_FLAG_BACKUP_SEMANTICS , NULL );
211227 if (file == INVALID_HANDLE_VALUE ) {
212228 return std::string ();
213229 }
214- char buffer[MAX_PATH ];
230+ wchar_t buffer[MAX_PATH ];
215231 DWORD ret =
216- GetFinalPathNameByHandleA (file, buffer, MAX_PATH , FILE_NAME_NORMALIZED );
217- if (ret == 0 || ret > MAX_PATH ) {
232+ GetFinalPathNameByHandleW (file, buffer, MAX_PATH , FILE_NAME_NORMALIZED );
233+ std::wstring wide_result;
234+ if (ret > 0 && ret < MAX_PATH ) {
235+ wide_result.assign (buffer, ret);
236+ } else if (ret >= MAX_PATH ) {
237+ std::vector<wchar_t > dyn_buffer (ret);
238+ DWORD dyn_ret = GetFinalPathNameByHandleW (file, dyn_buffer.data (), ret,
239+ FILE_NAME_NORMALIZED );
240+ if (dyn_ret > 0 && dyn_ret < ret) {
241+ wide_result.assign (dyn_buffer.data (), dyn_ret);
242+ }
243+ }
244+
245+ if (wide_result.empty ()) {
218246 std::string result;
219247 if (GetLastError () == ERROR_ACCESS_DENIED ) {
220248 // In sandboxed apps, GetFinalPathNameByHandle requires the app to
@@ -231,10 +259,21 @@ std::string GetAbsoluteFilePath(const std::string& path) {
231259 CloseHandle (file);
232260 return result;
233261 }
234- std::string result (buffer);
235- result.erase (0 , strlen (" \\\\ ?\\ " ));
262+ // GetFinalPathNameByHandleW with FILE_NAME_NORMALIZED returns paths with
263+ // either a "\\?\UNC\server\share" prefix for network shares or a "\\?\"
264+ // prefix for local drive paths (e.g. "\\?\C:\..."). We normalize network
265+ // paths to standard UNC format ("\\server\share") and strip the "\\?\" prefix
266+ // for standard local paths.
267+ constexpr std::wstring_view kUncPrefix = L" \\\\ ?\\ UNC\\ " ;
268+ constexpr std::wstring_view kLongPathPrefix = L" \\\\ ?\\ " ;
269+ if (wide_result.starts_with (kUncPrefix )) {
270+ wide_result.erase (0 , kUncPrefix .size ());
271+ wide_result = L" \\\\ " + wide_result;
272+ } else if (wide_result.starts_with (kLongPathPrefix )) {
273+ wide_result.erase (0 , kLongPathPrefix .size ());
274+ }
236275 CloseHandle (file);
237- return result ;
276+ return WidePathToUtf8 (wide_result) ;
238277}
239278
240279} // namespace filesystem
0 commit comments