Skip to content

Commit 0086133

Browse files
authored
Enable reading attributes of directory on Windows (#770)
1 parent 3d8294a commit 0086133

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
@@ -555,7 +555,7 @@ extension _FileManagerImpl {
555555
func attributesOfItem(atPath path: String) throws -> [FileAttributeKey : Any] {
556556
#if os(Windows)
557557
return try path.withNTPathRepresentation { pwszPath in
558-
let hFile = CreateFileW(pwszPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nil, OPEN_EXISTING, 0, nil)
558+
let hFile = CreateFileW(pwszPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nil)
559559
if hFile == INVALID_HANDLE_VALUE {
560560
throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
561561
}

Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift

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

0 commit comments

Comments
 (0)