Skip to content

Commit 88b4538

Browse files
authored
Merge pull request #2344 from gmittert/2StepForward1StepFallBack
Fallback to GetFullPathNameW for Windows _canonicalizedPath
2 parents 387127f + 9827a0e commit 88b4538

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Foundation/FileManager+Win32.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,16 @@ extension FileManager {
311311
nil, DWORD(OPEN_EXISTING), DWORD(FILE_FLAG_BACKUP_SEMANTICS),
312312
nil)
313313
}
314-
if hFile == INVALID_HANDLE_VALUE {
315-
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
314+
guard hFile != INVALID_HANDLE_VALUE else {
315+
return try path.withCString(encodedAs: UTF16.self) {
316+
var dwLength = GetFullPathNameW($0, 0, nil, nil)
317+
var szPath = Array<WCHAR>(repeating: 0, count: Int(dwLength + 1))
318+
dwLength = GetFullPathNameW($0, DWORD(szPath.count), &szPath, nil)
319+
guard dwLength > 0 && dwLength <= szPath.count else {
320+
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
321+
}
322+
return String(decodingCString: szPath, as: UTF16.self)
323+
}
316324
}
317325
defer { CloseHandle(hFile) }
318326

0 commit comments

Comments
 (0)