If you have custom extensions on FilePath or FileDescriptor that use unqualified stat() or stat(_:_:) calls, you may encounter build errors when the new System.Stat API is introduced. To maintain compatibility with older deployment targets (macOS 12.0, iOS 15.0+), replace the unqualified calls with CInterop.Stat (the type) and CInterop.stat(_:_:) (the function).
When to use this:
Only use this migration if you meet all these criteria:
- You have a custom extension on
FilePath or FileDescriptor. - You use unqualified
stat() or stat(_:_:) calls inside that extension. - You must support deployment targets older than the new
Stat API availability.
If you already use qualified calls (e.g., Darwin.stat()), no migration is needed.
// Before (Unqualified calls causing conflicts)
extension FilePath {
func isRegularFile() throws -> Bool {
var s = stat()
guard stat(self.string, &s) == 0 else {
throw Errno.current
}
return s.st_mode & S_IFMT == S_IFREG
}
}
// After (Migrated for compatibility with older targets)
extension FilePath {
func isRegularFile() throws -> Bool {
var s = CInterop.Stat() // Use CInterop.Stat type
guard CInterop.stat(self.string, &s) == 0 else { // Use CInterop.stat function
throw Errno.current
}
return s.st_mode & S_IFMT == S_IFREG
}
}
// Recommended: Migrate to the new System Stat API for newer targets
extension FilePath {
func isRegularFile() throws -> Bool {
if #available(macOS X, iOS Y, *) {
return try stat().type == .regular // Uses the new type-safe API
}
// Fallback for older targets
var s = CInterop.Stat()
guard CInterop.stat(self.string, &s) == 0 else {
throw Errno.current
}
return s.st_mode & S_IFMT == S_IFREG
}
}