mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Motivation: In #3363 we converted `_NIOFileSystem` to `NIOFileSystem` and removed the (unreleased) overloads for FilePath/NIOFilePath. This change adds back `_NIOFileSystem` such that it matches the API it had at 2.86.0. Modifications: - Add back `_NIOFileSystem` and `_NIOFileSystemFoundationCompat` such that their API is at 2.86.0 Result: - `NIOFileSystem` uses `NIOFilePath` - `_NIOFileSystem` uses `FilePath`
149 lines
4.1 KiB
Swift
149 lines
4.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import SystemPackage
|
|
|
|
#if canImport(Darwin)
|
|
import Darwin
|
|
#elseif canImport(Glibc)
|
|
@preconcurrency import Glibc
|
|
#elseif canImport(Musl)
|
|
@preconcurrency import Musl
|
|
#elseif canImport(Android)
|
|
@preconcurrency import Android
|
|
#endif
|
|
|
|
extension Errno {
|
|
@_spi(Testing)
|
|
public static var _current: Errno {
|
|
get {
|
|
#if canImport(Darwin)
|
|
return Errno(rawValue: Darwin.errno)
|
|
#elseif canImport(Glibc)
|
|
return Errno(rawValue: Glibc.errno)
|
|
#elseif canImport(Musl)
|
|
return Errno(rawValue: Musl.errno)
|
|
#elseif canImport(Android)
|
|
return Errno(rawValue: Android.errno)
|
|
#endif
|
|
}
|
|
set {
|
|
#if canImport(Darwin)
|
|
Darwin.errno = newValue.rawValue
|
|
#elseif canImport(Glibc)
|
|
Glibc.errno = newValue.rawValue
|
|
#elseif canImport(Musl)
|
|
Musl.errno = newValue.rawValue
|
|
#elseif canImport(Android)
|
|
Android.errno = newValue.rawValue
|
|
#endif
|
|
}
|
|
}
|
|
|
|
fileprivate static func clear() {
|
|
#if canImport(Darwin)
|
|
Darwin.errno = 0
|
|
#elseif canImport(Glibc)
|
|
Glibc.errno = 0
|
|
#elseif canImport(Musl)
|
|
Musl.errno = 0
|
|
#elseif canImport(Android)
|
|
Android.errno = 0
|
|
#endif
|
|
}
|
|
}
|
|
|
|
/// Returns a `Result` representing the value returned from the given closure
|
|
/// or an `Errno` if that value was -1.
|
|
///
|
|
/// If desired this function can call the closure in a loop until it does not
|
|
/// result in `Errno` being `.interrupted`.
|
|
@_spi(Testing)
|
|
public func valueOrErrno<I: FixedWidthInteger>(
|
|
retryOnInterrupt: Bool = true,
|
|
_ fn: () -> I
|
|
) -> Result<I, Errno> {
|
|
while true {
|
|
Errno.clear()
|
|
let result = fn()
|
|
if result == -1 {
|
|
let errno = Errno._current
|
|
if errno == .interrupted, retryOnInterrupt {
|
|
continue
|
|
} else {
|
|
return .failure(errno)
|
|
}
|
|
} else {
|
|
return .success(result)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// As `valueOrErrno` but discards the success value.
|
|
@_spi(Testing)
|
|
public func nothingOrErrno<I: FixedWidthInteger>(
|
|
retryOnInterrupt: Bool = true,
|
|
_ fn: () -> I
|
|
) -> Result<Void, Errno> {
|
|
valueOrErrno(retryOnInterrupt: retryOnInterrupt, fn).map { _ in }
|
|
}
|
|
|
|
/// Returns a `Result` representing the value returned from the given closure
|
|
/// or an `Errno` if that value was `nil`.
|
|
///
|
|
/// If desired this function can call the closure in a loop until it does not
|
|
/// result in `Errno` being `.interrupted`. `Errno` is only checked if the
|
|
/// closure returns `nil`.
|
|
@_spi(Testing)
|
|
public func optionalValueOrErrno<R>(
|
|
retryOnInterrupt: Bool = true,
|
|
_ fn: () -> R?
|
|
) -> Result<R?, Errno> {
|
|
while true {
|
|
Errno.clear()
|
|
if let result = fn() {
|
|
return .success(result)
|
|
} else {
|
|
let errno = Errno._current
|
|
if errno == .interrupted, retryOnInterrupt {
|
|
continue
|
|
} else if errno.rawValue == 0 {
|
|
return .success(nil)
|
|
} else {
|
|
return .failure(errno)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// As `valueOrErrno` but unconditionally checks the current `Errno`.
|
|
@_spi(Testing)
|
|
public func valueOrErrno<R>(
|
|
retryOnInterrupt: Bool = true,
|
|
_ fn: () -> R
|
|
) -> Result<R, Errno> {
|
|
while true {
|
|
Errno.clear()
|
|
let value = fn()
|
|
let errno = Errno._current
|
|
if errno.rawValue == 0 {
|
|
return .success(value)
|
|
} else if errno == .interrupted, retryOnInterrupt {
|
|
continue
|
|
} else {
|
|
return .failure(errno)
|
|
}
|
|
}
|
|
}
|