Skip to content

FileManager: support querying file ids on Windows #3119

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
Dec 18, 2021
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
12 changes: 10 additions & 2 deletions Sources/Foundation/FileManager+Win32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,12 @@ extension FileManager {
}

internal func _lstatFile(atPath path: String, withFileSystemRepresentation fsRep: UnsafePointer<NativeFSRCharType>? = nil) throws -> stat {
let (stbuf, _) = try _statxFile(atPath: path, withFileSystemRepresentation: fsRep)
return stbuf
}

// FIXME(compnerd) the UInt64 should be UInt128 to uniquely identify the file across volumes
internal func _statxFile(atPath path: String, withFileSystemRepresentation fsRep: UnsafePointer<NativeFSRCharType>? = nil) throws -> (stat, UInt64) {
let _fsRep: UnsafePointer<NativeFSRCharType>
if fsRep == nil {
_fsRep = try __fileSystemRepresentation(withPath: path)
Expand Down Expand Up @@ -771,7 +777,8 @@ extension FileManager {
statInfo.st_atime = info.ftLastAccessTime.time_t
statInfo.st_ctime = info.ftCreationTime.time_t
statInfo.st_dev = info.dwVolumeSerialNumber
// inodes have meaning on FAT/HPFS/NTFS
// The inode, and therefore st_ino, has no meaning in the FAT, HPFS, or
// NTFS file systems. -- docs.microsoft.com
statInfo.st_ino = 0
statInfo.st_rdev = info.dwVolumeSerialNumber

Expand All @@ -795,7 +802,8 @@ extension FileManager {
statInfo.st_size = Int32(info.nFileSizeLow)
// Uid is always 0 on Windows systems
statInfo.st_uid = 0
return statInfo

return (statInfo, UInt64(info.nFileIndexHigh << 32) | UInt64(info.nFileIndexLow))
}

internal func _contentsEqual(atPath path1: String, andPath path2: String) -> Bool {
Expand Down
7 changes: 7 additions & 0 deletions Sources/Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ open class FileManager : NSObject {
#if os(Linux)
let (s, creationDate) = try _statxFile(atPath: path)
result[.creationDate] = creationDate
#elseif os(Windows)
let (s, ino) = try _statxFile(atPath: path)
result[.creationDate] = s.creationDate
#else
let s = try _lstatFile(atPath: path)
result[.creationDate] = s.creationDate
Expand All @@ -553,7 +556,11 @@ open class FileManager : NSObject {
result[.posixPermissions] = NSNumber(value: _filePermissionsMask(mode: UInt32(s.st_mode)))
result[.referenceCount] = NSNumber(value: UInt64(s.st_nlink))
result[.systemNumber] = NSNumber(value: UInt64(s.st_dev))
#if os(Windows)
result[.systemFileNumber] = NSNumber(value: UInt64(ino))
#else
result[.systemFileNumber] = NSNumber(value: UInt64(s.st_ino))
#endif

#if os(Windows)
result[.deviceIdentifier] = NSNumber(value: UInt64(s.st_rdev))
Expand Down