Skip to content

Commit 483f3ff

Browse files
authored
Merge pull request #3119 from compnerd/inodes
FileManager: support querying file ids on Windows
2 parents 73d1943 + dec612e commit 483f3ff

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Sources/Foundation/FileManager+Win32.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,12 @@ extension FileManager {
740740
}
741741

742742
internal func _lstatFile(atPath path: String, withFileSystemRepresentation fsRep: UnsafePointer<NativeFSRCharType>? = nil) throws -> stat {
743+
let (stbuf, _) = try _statxFile(atPath: path, withFileSystemRepresentation: fsRep)
744+
return stbuf
745+
}
746+
747+
// FIXME(compnerd) the UInt64 should be UInt128 to uniquely identify the file across volumes
748+
internal func _statxFile(atPath path: String, withFileSystemRepresentation fsRep: UnsafePointer<NativeFSRCharType>? = nil) throws -> (stat, UInt64) {
743749
let _fsRep: UnsafePointer<NativeFSRCharType>
744750
if fsRep == nil {
745751
_fsRep = try __fileSystemRepresentation(withPath: path)
@@ -771,7 +777,8 @@ extension FileManager {
771777
statInfo.st_atime = info.ftLastAccessTime.time_t
772778
statInfo.st_ctime = info.ftCreationTime.time_t
773779
statInfo.st_dev = info.dwVolumeSerialNumber
774-
// inodes have meaning on FAT/HPFS/NTFS
780+
// The inode, and therefore st_ino, has no meaning in the FAT, HPFS, or
781+
// NTFS file systems. -- docs.microsoft.com
775782
statInfo.st_ino = 0
776783
statInfo.st_rdev = info.dwVolumeSerialNumber
777784

@@ -795,7 +802,8 @@ extension FileManager {
795802
statInfo.st_size = Int32(info.nFileSizeLow)
796803
// Uid is always 0 on Windows systems
797804
statInfo.st_uid = 0
798-
return statInfo
805+
806+
return (statInfo, UInt64(info.nFileIndexHigh << 32) | UInt64(info.nFileIndexLow))
799807
}
800808

801809
internal func _contentsEqual(atPath path1: String, andPath path2: String) -> Bool {

Sources/Foundation/FileManager.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,9 @@ open class FileManager : NSObject {
538538
#if os(Linux)
539539
let (s, creationDate) = try _statxFile(atPath: path)
540540
result[.creationDate] = creationDate
541+
#elseif os(Windows)
542+
let (s, ino) = try _statxFile(atPath: path)
543+
result[.creationDate] = s.creationDate
541544
#else
542545
let s = try _lstatFile(atPath: path)
543546
result[.creationDate] = s.creationDate
@@ -553,7 +556,11 @@ open class FileManager : NSObject {
553556
result[.posixPermissions] = NSNumber(value: _filePermissionsMask(mode: UInt32(s.st_mode)))
554557
result[.referenceCount] = NSNumber(value: UInt64(s.st_nlink))
555558
result[.systemNumber] = NSNumber(value: UInt64(s.st_dev))
559+
#if os(Windows)
560+
result[.systemFileNumber] = NSNumber(value: UInt64(ino))
561+
#else
556562
result[.systemFileNumber] = NSNumber(value: UInt64(s.st_ino))
563+
#endif
557564

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

0 commit comments

Comments
 (0)