Skip to content

[Windows] Implement additional file attributes #730

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 5 commits into from
Jul 12, 2024
Merged
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
54 changes: 35 additions & 19 deletions Sources/FoundationEssentials/FileManager/FileManager+Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -553,48 +553,64 @@ extension _FileManagerImpl {
func attributesOfItem(atPath path: String) throws -> [FileAttributeKey : Any] {
#if os(Windows)
return try path.withNTPathRepresentation { pwszPath in
var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = .init()
guard GetFileAttributesExW(pwszPath, GetFileExInfoStandard, &faAttributes) else {
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) }

var info: BY_HANDLE_FILE_INFORMATION = BY_HANDLE_FILE_INFORMATION()
guard GetFileInformationByHandle(hFile, &info) else {
throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
}

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
info.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)
let systemNumber = UInt64(info.dwVolumeSerialNumber)
let systemFileNumber = UInt64(info.nFileIndexHigh << 32) | UInt64(info.nFileIndexLow)
let referenceCount = UInt64(info.nNumberOfLinks)

let isReadOnly = info.dwFileAttributes & FILE_ATTRIBUTE_READONLY != 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it is safer to ensure that the bit is set ...

Suggested change
let isReadOnly = info.dwFileAttributes & FILE_ATTRIBUTE_READONLY != 0
let isReadOnly = info.dwFileAttributes & FILE_ATTRIBUTE_READONLY == FILE_ATTRIBUTE_READONLY

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I prefer the != 0 version. If we're doing a bitwise AND, we know the answer is either 0 or FILE_ATTRIBUTE_READONLY. The only way we could trip up is if FILE_ATTRIBUTE_READONLY is not really a bit mask, but that would be wrong for either formulation (and we already know it's a bit mask anyway).

// Directories are always considered executable, but we check for other types
let isExecutable = fatType == .typeDirectory || SaferiIsExecutableFileType(pwszPath, 0)
var posixPermissions = UInt16(_S_IREAD)
if !isReadOnly {
posixPermissions |= UInt16(_S_IWRITE)
}
if isExecutable {
posixPermissions |= UInt16(_S_IEXEC)
}

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

// Uid is always 0 on Windows systems
.ownerAccountID: _writeFileAttributePrimitive(0, as: UInt.self),

// Group id is always 0 on Windows
.groupOwnerAccountID: _writeFileAttributePrimitive(0, as: UInt.self)

// TODO(compnerd) support these attributes, remapping the Windows semantics...
// .posixPermissions: ...,
// .referenceCount: ...,
// .systemNumber: ...,
// .systemFileNumber: ...,
// .ownerAccountID: ...,
// .groupownerAccountID: ...,
// .ownerAccountName: ...,
// .groupOwnerAccountName: ...,
// .deviceIdentifier: ...,
// TODO: Support .deviceIdentifier
]
}
#else
Expand Down