Repository: apple/swift-nio
File: Sources/NIOFileSystem/Internal/SystemFileHandle.swift (Swift)

Code before:
                    times: [timespec(lastAccess), timespec(lastDataModification)]
                )
            }

            try syscallResult.mapError { errno in
                FileSystemError.futimens(
                    errno: errno,
                    path: self.path,
                    lastAccessTime: lastAccess,
                    lastDataModificationTime: lastDataModification,
                    location: .here()
                )
            }.get()
        } onUnavailable: {
            FileSystemError(
                code: .closed,
                message: "Couldn't modify file dates, the file '\(self.path)' is closed.",
                cause: nil,
                location: .here()
            )
        }
    }
}

fileprivate extension timespec {
    init(_ fileinfoTimespec: FileInfo.Timespec) {
        // Clamp seconds to be positive
        let seconds = max(0, fileinfoTimespec.seconds)

        // If nanoseconds are not UTIME_NOW or UTIME_OMIT, clamp to be between
        // 0 and 1,000 million.
        let nanoseconds: Int
        switch fileinfoTimespec {
        case .now, .omit:
            nanoseconds = fileinfoTimespec.nanoseconds
        default:
            nanoseconds = min(1_000_000_000, max(0, fileinfoTimespec.nanoseconds))
        }

        self.init(
            tv_sec: seconds,
            tv_nsec: nanoseconds
        )
    }
}

// MARK: - Readable File Handle

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension SystemFileHandle: ReadableFileHandleProtocol {
    // Notes which apply to the following block of functions:

Review comment (bug):
nit: don't put access modifiers on `extension`s, it's too easy to accidentally give a method the wrong access (this is something we apply to all of our projects)

```suggestion
extension timespec {
    fileprivate init(_ fileinfoTimespec: FileInfo.Timespec) {
```

Code after:
                    times: [timespec(lastAccess), timespec(lastDataModification)]
                )
            }

            try syscallResult.mapError { errno in
                FileSystemError.futimens(
                    errno: errno,
                    path: self.path,
                    lastAccessTime: lastAccess,
                    lastDataModificationTime: lastDataModification,
                    location: .here()
                )
            }.get()
        } onUnavailable: {
            FileSystemError(
                code: .closed,
                message: "Couldn't modify file dates, the file '\(self.path)' is closed.",
                cause: nil,
                location: .here()
            )
        }
    }
}

extension timespec {
    fileprivate init(_ fileinfoTimespec: FileInfo.Timespec) {
        // Clamp seconds to be positive
        let seconds = max(0, fileinfoTimespec.seconds)

        // If nanoseconds are not UTIME_NOW or UTIME_OMIT, clamp to be between
        // 0 and 1,000 million.
        let nanoseconds: Int
        switch fileinfoTimespec {
        case .now, .omit:
            nanoseconds = fileinfoTimespec.nanoseconds
        default:
            nanoseconds = min(1_000_000_000, max(0, fileinfoTimespec.nanoseconds))
        }

        self.init(
            tv_sec: seconds,
            tv_nsec: nanoseconds
        )
    }
}

// MARK: - Readable File Handle

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension SystemFileHandle: ReadableFileHandleProtocol {
    // Notes which apply to the following block of functions: