Skip to content

Commit 750314f

Browse files
committed
Foundation: adjust _fileExists for NT style paths
Adjust the `_fileExists` implementation in `FileManager` for Windows to use NT style paths to exceed path limits of `PATH_MAX` and address the full paths lengths supported by Windows NT.
1 parent 9b1d17f commit 750314f

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

Sources/Foundation/FileManager+Win32.swift

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -742,27 +742,31 @@ extension FileManager {
742742
}
743743

744744
internal func _fileExists(atPath path: String, isDirectory: UnsafeMutablePointer<ObjCBool>?) -> Bool {
745-
var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = WIN32_FILE_ATTRIBUTE_DATA()
746-
do { faAttributes = try windowsFileAttributes(atPath: path) } catch { return false }
747-
if faAttributes.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT == FILE_ATTRIBUTE_REPARSE_POINT {
748-
let handle: HANDLE = (try? FileManager.default._fileSystemRepresentation(withPath: path) {
749-
CreateFileW($0, 0, FILE_SHARE_READ, nil, OPEN_EXISTING,
750-
FILE_FLAG_BACKUP_SEMANTICS, nil)
751-
}) ?? INVALID_HANDLE_VALUE
752-
if handle == INVALID_HANDLE_VALUE { return false }
753-
defer { CloseHandle(handle) }
754-
755-
if let isDirectory = isDirectory {
756-
var info: BY_HANDLE_FILE_INFORMATION = BY_HANDLE_FILE_INFORMATION()
757-
GetFileInformationByHandle(handle, &info)
758-
isDirectory.pointee = ObjCBool(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY)
759-
}
760-
} else {
761-
if let isDirectory = isDirectory {
762-
isDirectory.pointee = ObjCBool(faAttributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY)
763-
}
764-
}
765-
return true
745+
return (try? withNTPathRepresentation(of: path) {
746+
var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = .init()
747+
guard GetFileAttributesExW($0, GetFileExInfoStandard, &faAttributes) else {
748+
return false
749+
}
750+
751+
if let isDirectory {
752+
var dwFileAttributes = faAttributes.dwFileAttributes
753+
754+
if dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT == FILE_ATTRIBUTE_REPARSE_POINT {
755+
let hFile: HANDLE = CreateFileW($0, 0, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nil)
756+
if hFile == INVALID_HANDLE_VALUE { return false }
757+
defer { CloseHandle(hFile) }
758+
759+
var info: BY_HANDLE_FILE_INFORMATION = .init()
760+
GetFileInformationByHandle(hFile, &info)
761+
762+
dwFileAttributes = info.dwFileAttributes
763+
}
764+
765+
isDirectory.pointee = ObjCBool(dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY)
766+
}
767+
768+
return true
769+
}) ?? false
766770
}
767771

768772

0 commit comments

Comments
 (0)