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

Code before:
                    code: .closed,
                    message: "Couldn't write bytes, the file '\(sendableView.path)' is closed.",
                    cause: nil,
                    location: .here()
                )
            }
        }
    }

    public func resize(to size: ByteCount) async throws {
        try await self.threadPool.runIfActive { [sendableView] in
            try sendableView._resize(to: size).get()
        }
    }

    public func setTimes(
        lastAccessTime: FileInfo.Timespec?,
        lastDataModificationTime: FileInfo.Timespec?
    ) 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.

Review comment (refactor):
Can you move this to `Syscalls.swift`? The pattern there is to wrap the syscall so we can more easily deal with platform differences and allows us to mock the sys calls for testing.

The function should then be wrapped up in a static function on `Syscall` returning a `Result<Void, Errno>`.

Code after:
                                    location: .here()
                                )
                            )
                        }

                        return descriptor.readChunk(length: length.bytes).mapError { error in
                            FileSystemError.read(
                                usingSyscall: .read,
                                error: error,
                                path: sendableView.path,
                                location: .here()
                            )
                        }
                    } else {
                        return .failure(
                            FileSystemError.read(
                                usingSyscall: .pread,
                                error: error,
                                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),