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

Code before:
                                    error: error,
                                    path: sendableView.path,
                                    location: .here()
                                )
                            )
                        }
                    }
                    .get()
            } onUnavailable: {
                FileSystemError(
                    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)
                        )
                    }

Review comment (suggestion):
It's usually helpful to break this into two functions: the sync version and then a wrapper which calls it from the thread pool. This makes it easier to later implement more complicated function without continually hopping into and out of the thread pool.

Code after:
                    fromAbsoluteOffset: offset,
                    length: length.bytes
                ).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()
                                )
                            )
                        }

                        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)