Skip to content

Correctly set .fileTypeSymlink on windows #773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Sources/FoundationEssentials/FileManager/FileManager+Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ extension _FileManagerImpl {
func attributesOfItem(atPath path: String) throws -> [FileAttributeKey : Any] {
#if os(Windows)
return try path.withNTPathRepresentation { pwszPath in
let hFile = CreateFileW(pwszPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nil)
let hFile = CreateFileW(pwszPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nil)
if hFile == INVALID_HANDLE_VALUE {
throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
}
Expand All @@ -567,7 +567,7 @@ extension _FileManagerImpl {
}

let dwFileType = GetFileType(hFile)
let fatType: FileAttributeType = switch (dwFileType) {
var fatType: FileAttributeType = switch (dwFileType) {
case FILE_TYPE_CHAR: FileAttributeType.typeCharacterSpecial
case FILE_TYPE_DISK:
info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY
Expand All @@ -578,6 +578,16 @@ extension _FileManagerImpl {
default: FileAttributeType.typeUnknown
}

if info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT == FILE_ATTRIBUTE_REPARSE_POINT {
// This could by a symlink, check if that's the case and update fatType if necessary
var tagInfo = FILE_ATTRIBUTE_TAG_INFO()
if GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, &tagInfo, DWORD(MemoryLayout<FILE_ATTRIBUTE_TAG_INFO>.size)) {
if tagInfo.ReparseTag == IO_REPARSE_TAG_SYMLINK {
fatType = .typeSymbolicLink
}
}
}

let systemNumber = UInt64(info.dwVolumeSerialNumber)
let systemFileNumber = UInt64(info.nFileIndexHigh << 32) | UInt64(info.nFileIndexLow)
let referenceCount = UInt64(info.nNumberOfLinks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -890,13 +890,11 @@ final class FileManagerTests : XCTestCase {
XCTAssertEqual(attrs[.type] as? FileAttributeType, FileAttributeType.typeDirectory)
}

#if !os(Windows)
do {
try $0.createSymbolicLink(atPath: "symlink", withDestinationPath: "file")
let attrs = try $0.attributesOfItem(atPath: "symlink")
XCTAssertEqual(attrs[.type] as? FileAttributeType, FileAttributeType.typeSymbolicLink)
}
#endif
}
}
}