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

Code before:
    ) async throws {
        try await self.threadPool.runIfActive { [sendableView] in
            try sendableView._withUnsafeDescriptor { descriptor in
                if lastAccessTime == nil, lastDataModificationTime == nil {
                    // If the timespec array is nil, as per the `futimens` docs,
                    // both the last accessed and last modification times
                    // will be set to now.
                    futimens(descriptor.rawValue, nil)
                } else {
                    let lastAccessTimespec: timespec
                    if let lastAccessTime = lastAccessTime {
                        lastAccessTimespec = timespec(
                            tv_sec: lastAccessTime.seconds,
                            tv_nsec: lastAccessTime.nanoseconds
                        )
                    } else {
                        // Don't modify the last access time.
                        // Note: tv_sec will be ignored.
                        lastAccessTimespec = timespec(
                            tv_sec: 0,
                            tv_nsec: Int(UTIME_OMIT)
                        )
                    }

                    let lastDataModificationTimespec: timespec
                    if let lastDataModificationTime = lastDataModificationTime {
                        lastDataModificationTimespec = timespec(
                            tv_sec: lastDataModificationTime.seconds,
                            tv_nsec: lastDataModificationTime.nanoseconds
                        )
                    } else {
                        // Don't modify the last modification time.
                        // Note: tv_sec will be ignored.
                        lastDataModificationTimespec = timespec(
                            tv_sec: 0,
                            tv_nsec: Int(UTIME_OMIT)
                        )
                    }

                    let result = futimens(descriptor.rawValue, [lastAccessTimespec, lastDataModificationTimespec])
                    guard result == 0 else {
                        throw FileSystemError.futimens(
                            errno: Errno(rawValue: result),
                            path: self.path,
                            lastAccessTime: lastAccessTime,
                            lastDataModificationTime: lastDataModificationTime,
                            location: .here()
                        )
                    }
                }
            } onUnavailable: {

Review comment (suggestion):
FWIW:  rather than all the if-elses we can switch over both values to create a `timespec`:

```swift
let ts: timespec

switch (lastAccessTime, lastModificationTime) {
case (.some(let lastAccess), .some(let lastModification)):
  ts = timspec(...)
case (.some(let lastAccess), .none):
  // ...
case (.none, .some(let lastModification)):
  // ...
case (.none, .none):
  // ...
}
```

Code after:
                                path: sendableView.path,
                                location: .here()
                            )
                        )
                    }
                }
                .get()
            } onUnavailable: {
                FileSystemError(
                    code: .closed,
                    message: "Couldn't read chunk, the file '\(sendableView.path)' is closed.",
                    cause: nil,
                    location: .here()
                )
            }
        }
    }

    public func readChunks(
        in range: Range<Int64>,
        chunkLength size: ByteCount
    ) -> FileChunks {
        return FileChunks(handle: self, chunkLength: size, range: range)
    }
}

// MARK: - Writable File Handle

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension SystemFileHandle: WritableFileHandleProtocol {
    @discardableResult
    public func write(
        contentsOf bytes: some (Sequence<UInt8> & Sendable),
        toAbsoluteOffset offset: Int64
    ) async throws -> Int64 {
        return try await self.threadPool.runIfActive { [sendableView] in
            return try sendableView._withUnsafeDescriptor { descriptor in
                try descriptor.write(contentsOf: bytes, toAbsoluteOffset: offset)
                    .flatMapError { error in
                        if let errno = error as? Errno, errno == .illegalSeek {
                            guard offset == 0 else {
                                return .failure(
                                    FileSystemError(
                                        code: .unsupported,
                                        message: "File is unseekable.",
                                        cause: nil,
                                        location: .here()
                                    )
                                )
                            }