Repository: apple/swift-nio
File: Sources/NIOPosix/System.swift (Swift)

Code before:
    public static func unlink(pathname: String) throws {
        _ = try syscall(blocking: false) {
            sysUnlink(pathname)
        }
    }

    @inline(never)
    public static func mkdir(pathname: String, mode: mode_t) throws {
        _ = try syscall(blocking: true) {
            sysMkdir(pathname, mode)
        }
    }

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
    @inline(never)
    public static func mkpath(pathname: String, mode: mode_t) throws {
        _ = try syscall(blocking: true) {
            sysMkpath(pathname, mode)
        }
    }
#endif

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
    @inline(never)
    public static func opendir(pathname: String) throws -> UnsafeMutablePointer<DIR> {
        guard let result = sysOpendir(pathname) else {
            let err = errno
            throw IOError(errnoCode: err, reason: #function)
        }
        return result
    }

    @inline(never)
    public static func readdir(dir: UnsafeMutablePointer<DIR>) throws -> UnsafeMutablePointer<dirent>? {
        guard let result = sysReaddir(dir) else {
            let err = errno
            if err == 0 {
                return nil
            }
            throw IOError(errnoCode: err, reason: #function)
        }
        return result
    }

    @inline(never)
    public static func closedir(dir: UnsafeMutablePointer<DIR>) throws {
        _ = try syscall(blocking: true) {
            sysClosedir(dir)
        }
    }
#elseif os(Linux) || os(FreeBSD) || os(Android)

Review comment (refactor):
This is missing the retry on `EINTR` logic. Can I propose adding a syscall helper for functions that return pointers?

Code after:
    @inline(never)
    internal static func getpeername(socket: CInt, address: UnsafeMutablePointer<sockaddr>, addressLength: UnsafeMutablePointer<socklen_t>) throws {
        _ = try syscall(blocking: false) {
            return sysGetpeername(socket, address, addressLength)
        }
    }

    @inline(never)
    internal static func getsockname(socket: CInt, address: UnsafeMutablePointer<sockaddr>, addressLength: UnsafeMutablePointer<socklen_t>) throws {
        _ = try syscall(blocking: false) {
            return sysGetsockname(socket, address, addressLength)
        }
    }
#endif

    @inline(never)
    internal static func if_nametoindex(_ name: UnsafePointer<CChar>?) throws -> CUnsignedInt {
        return try syscall(blocking: false) {
            sysIfNameToIndex(name)
        }.result
    }

#if !os(Windows)
    @inline(never)
    internal static func poll(fds: UnsafeMutablePointer<pollfd>, nfds: nfds_t, timeout: CInt) throws -> CInt {
        return try syscall(blocking: false) {
            sysPoll(fds, nfds, timeout)
        }.result
    }

    @inline(never)
    internal static func fstat(descriptor: CInt, outStat: UnsafeMutablePointer<stat>) throws {
        _ = try syscall(blocking: false) {
            sysFstat(descriptor, outStat)
        }
    }

    @inline(never)
    internal static func stat(pathname: String, outStat: UnsafeMutablePointer<stat>) throws {
        _ = try syscall(blocking: false) {
            sysStat(pathname, outStat)
        }
    }

    @inline(never)
    internal static func lstat(pathname: String, outStat: UnsafeMutablePointer<stat>) throws {
        _ = try syscall(blocking: false) {
            sysLstat(pathname, outStat)
        }
    }