Repository: apple/swift-nio
File: Sources/_NIOFileSystem/Internal/System Calls/Syscall.swift (Swift)

Code before:
        _ pointer: CInterop.FTSPointer
    ) -> Result<Void, Errno> {
        nothingOrErrno {
            libc_fts_close(pointer)
        }
    }

    static func homeDirectoryForCurrentUser() -> Result<FilePath, Errno> {
        if let home = getenv("HOME"), home.pointee != 0 {
            return .success(FilePath(String(cString: home)))
        }

        #if os(Windows)
        if let profile = getenv("USERPROFILE"), profile.pointee != 0 {
            return .success(FilePath(String(cString: profile)))
        }
        return .failure(.noSuchFileOrDirectory)
        #elseif canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Android)
        return getpwuid_r(uid: Syscall.getuid())
        #else
        return .failure(.noSuchFileOrDirectory)
        #endif
    }

    #if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Android)
    static func getpwuid_r(uid: uid_t) -> Result<FilePath, Errno> {
        var pwd = passwd()
        var result: UnsafeMutablePointer<passwd>? = nil
        var buffer = [CChar](repeating: 0, count: 256)

        while true {
            let lookupResult = buffer.withUnsafeMutableBufferPointer { pointer in
                withUnsafeMutablePointer(to: &result) { resultPointer in
                    nothingOrErrno(retryOnInterrupt: true) {
                        libc_getpwuid_r(
                            uid,
                            &pwd,
                            pointer.baseAddress!,
                            pointer.count,
                            resultPointer
                        )
                    }
                }
            }

            switch lookupResult {
            case .success:
                guard result != nil, let directoryPointer = pwd.pw_dir else {
                    return .failure(.noSuchFileOrDirectory)
                }
                return .success(FilePath(String(cString: directoryPointer)))

Review comment (suggestion):
Let's rename this method: it's not a direct match to the `getpwuid_r` method because it discards the rest of the `passwd` result object. That's totally fine, it's hard to manage the lifecycle of that object and so it's a dangerous method to use. Let's call this something like `homeDirectoryFromPasswd` or similar. That also allows us to avoid this needing to take a `uid_t` parameter.

Code after:
        _ pointer: CInterop.FTSPointer
    ) -> Result<Void, Errno> {
        nothingOrErrno {
            libc_fts_close(pointer)
        }
    }

    @_spi(Testing)
    public static func homeDirectoryFromEnvironment() -> FilePath? {
        if let home = getenv("HOME"), home.pointee != 0 {
            return FilePath(String(cString: home))
        }
        #if os(Windows)
        if let profile = getenv("USERPROFILE"), profile.pointee != 0 {
            return FilePath(String(cString: profile))
        }
        #endif
        return nil
    }

    #if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
    @_spi(Testing)
    public static func homeDirectoryFromPasswd() -> Result<FilePath, Errno> {
        let uid = Syscall.getuid()
        var pwd = passwd()
        var result: UnsafeMutablePointer<passwd>? = nil

        return withUnsafeTemporaryAllocation(of: CChar.self, capacity: 1024) { buffer in
            let callResult = nothingOrErrno(retryOnInterrupt: true) {
                libc_getpwuid_r(
                    uid,
                    &pwd,
                    buffer.baseAddress!,
                    buffer.count,
                    &result
                )
            }
            switch callResult {
            case .success:
                guard result != nil, let directoryPointer = pwd.pw_dir else {
                    return .failure(.noSuchFileOrDirectory)
                }
                return .success(FilePath(String(cString: directoryPointer)))
            case .failure(let errno):
                return .failure(errno)
            }
        }
    }
    #endif
}