Skip to content

Commit 80b21e6

Browse files
jmschonfeldcompnerd
authored andcommitted
Enable reading attributes of directory on Windows (swiftlang#770)
1 parent c0a485e commit 80b21e6

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Sources/FoundationEssentials/FileManager/FileManager+Files.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ extension _FileManagerImpl {
553553
func attributesOfItem(atPath path: String) throws -> [FileAttributeKey : Any] {
554554
#if os(Windows)
555555
return try path.withNTPathRepresentation { pwszPath in
556-
let hFile = CreateFileW(pwszPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nil, OPEN_EXISTING, 0, nil)
556+
let hFile = CreateFileW(pwszPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nil)
557557
if hFile == INVALID_HANDLE_VALUE {
558558
throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
559559
}

Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,4 +869,39 @@ final class FileManagerTests : XCTestCase {
869869
try $0.setAttributes(attrs, ofItemAtPath: "foo")
870870
}
871871
}
872+
873+
func testAttributesOfItemAtPath() throws {
874+
try FileManagerPlayground {
875+
"file"
876+
File("fileWithContents", contents: randomData())
877+
Directory("directory") {
878+
"file"
879+
}
880+
}.test {
881+
do {
882+
let attrs = try $0.attributesOfItem(atPath: "file")
883+
XCTAssertEqual(attrs[.size] as? UInt, 0)
884+
XCTAssertEqual(attrs[.type] as? FileAttributeType, FileAttributeType.typeRegular)
885+
}
886+
887+
do {
888+
let attrs = try $0.attributesOfItem(atPath: "fileWithContents")
889+
XCTAssertGreaterThan(try XCTUnwrap(attrs[.size] as? UInt), 0)
890+
XCTAssertEqual(attrs[.type] as? FileAttributeType, FileAttributeType.typeRegular)
891+
}
892+
893+
do {
894+
let attrs = try $0.attributesOfItem(atPath: "directory")
895+
XCTAssertEqual(attrs[.type] as? FileAttributeType, FileAttributeType.typeDirectory)
896+
}
897+
898+
#if !os(Windows)
899+
do {
900+
try $0.createSymbolicLink(atPath: "symlink", withDestinationPath: "file")
901+
let attrs = try $0.attributesOfItem(atPath: "symlink")
902+
XCTAssertEqual(attrs[.type] as? FileAttributeType, FileAttributeType.typeSymbolicLink)
903+
}
904+
#endif
905+
}
906+
}
872907
}

0 commit comments

Comments
 (0)