Skip to content

FoundationEssentials: add type to the file attributes on Windows #634

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
May 28, 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
20 changes: 19 additions & 1 deletion Sources/FoundationEssentials/FileManager/FileManager+Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -531,20 +531,38 @@ extension _FileManagerImpl {
throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
}

let hFile = CreateFileW(pwszPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nil, OPEN_EXISTING, 0, nil)
if hFile == INVALID_HANDLE_VALUE {
throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
}
defer { CloseHandle(hFile) }

let dwFileType = GetFileType(hFile)
let fatType: FileAttributeType = switch (dwFileType) {
case FILE_TYPE_CHAR: FileAttributeType.typeCharacterSpecial
case FILE_TYPE_DISK:
faAttributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY
? FileAttributeType.typeDirectory
: FileAttributeType.typeRegular
case FILE_TYPE_PIPE: FileAttributeType.typeSocket
case FILE_TYPE_UNKNOWN: FileAttributeType.typeUnknown
default: FileAttributeType.typeUnknown
}

let size: UInt64 = (UInt64(faAttributes.nFileSizeHigh) << 32) | UInt64(faAttributes.nFileSizeLow)
let creation: Date = Date(timeIntervalSince1970: faAttributes.ftCreationTime.timeIntervalSince1970)
let modification: Date = Date(timeIntervalSince1970: faAttributes.ftLastWriteTime.timeIntervalSince1970)
return [
.size: _writeFileAttributePrimitive(size, as: UInt.self),
.modificationDate: modification,
.creationDate: creation,
.type: fatType,

// TODO(compnerd) support these attributes, remapping the Windows semantics...
// .posixPermissions: ...,
// .referenceCount: ...,
// .systemNumber: ...,
// .systemFileNumber: ...,
// .type: ...,
// .ownerAccountID: ...,
// .groupownerAccountID: ...,
// .ownerAccountName: ...,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ final class FileManagerTests : XCTestCase {
File("foo", attributes: [.posixPermissions : UInt16(0o644)])
}.test {
let attributes = try $0.attributesOfItem(atPath: "foo")
#if !os(Windows)
// Ensure the unconventional UInt16 was accepted as input
XCTAssertEqual(attributes[.posixPermissions] as? UInt, 0o644)
#if FOUNDATION_FRAMEWORK
Expand All @@ -643,6 +644,7 @@ final class FileManagerTests : XCTestCase {
// Ensure that the file type can be converted to a String when it is an ObjC enum
XCTAssertEqual(attributes[.type] as? String, FileAttributeType.typeRegular.rawValue)
#endif
#endif
// Ensure that the file type can be converted to a FileAttributeType when it is an ObjC enum and in swift-foundation
XCTAssertEqual(attributes[.type] as? FileAttributeType, .typeRegular)

Expand Down