Skip to content

Commit

Permalink
FileManager: support querying file ids on Windows
Browse files Browse the repository at this point in the history
The moral equivalent to the inode is the FileID on Windows.  Populate
this properly into the FileInfo response from FileManager.  This was
identified by a failure in the swift-driver test suite failing to
recursively resolve response files.
  • Loading branch information
compnerd committed Dec 17, 2021
1 parent 73d1943 commit dec612e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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

0 comments on commit dec612e

Please sign in to comment.