mirror of
https://github.com/apple/swift-system
synced 2026-11-26 04:57:54 +00:00
Internalize SystemInternals
Scrap the separate module SystemInternals and instead have it be a subdirectory. Adjust all access control accordingly. One day, when Swift gets proper submodules or supermodules (i.e. resilience domains), we can reinstate it. For now, a separate module makes binary distribution much more complicated.
This commit is contained in:
1 parent
b63554656d
commit
d7611ca8ad
14 files changed
+93
-130
No files matched your search
+1
-5
@@ -15,18 +15,14 @@ import PackageDescription
|
||||
let targets: [PackageDescription.Target] = [
|
||||
.target(
|
||||
name: "SystemPackage",
|
||||
dependencies: ["SystemInternals"],
|
||||
path: "Sources/System"),
|
||||
.target(
|
||||
name: "SystemInternals",
|
||||
dependencies: ["CSystem"],
|
||||
path: "Sources/System",
|
||||
swiftSettings: [
|
||||
.define("ENABLE_MOCKING", .when(configuration: .debug))
|
||||
]),
|
||||
.target(
|
||||
name: "CSystem",
|
||||
dependencies: []),
|
||||
|
||||
.testTarget(
|
||||
name: "SystemTests",
|
||||
dependencies: ["SystemPackage"],
|
||||
|
||||
@@ -1472,7 +1472,6 @@ extension Errno {
|
||||
#endif
|
||||
}
|
||||
|
||||
@_implementationOnly import SystemInternals
|
||||
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
|
||||
extension Errno {
|
||||
// TODO: We want to provide safe access to `errno`, but we need a
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
@_implementationOnly import SystemInternals
|
||||
|
||||
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
|
||||
extension FileDescriptor {
|
||||
/// Opens or creates a file for reading or writing.
|
||||
@@ -122,7 +120,7 @@ extension FileDescriptor {
|
||||
internal func _seek(
|
||||
offset: Int64, from whence: FileDescriptor.SeekOrigin
|
||||
) -> Result<Int64, Errno> {
|
||||
let newOffset = system_lseek(self.rawValue, COffT(offset), whence.rawValue)
|
||||
let newOffset = system_lseek(self.rawValue, _COffT(offset), whence.rawValue)
|
||||
return valueOrErrno(Int64(newOffset))
|
||||
}
|
||||
|
||||
@@ -210,7 +208,7 @@ extension FileDescriptor {
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Int, Errno> {
|
||||
valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_pread(self.rawValue, buffer.baseAddress, buffer.count, COffT(offset))
|
||||
system_pread(self.rawValue, buffer.baseAddress, buffer.count, _COffT(offset))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -292,7 +290,7 @@ extension FileDescriptor {
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Int, Errno> {
|
||||
valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_pwrite(self.rawValue, buffer.baseAddress, buffer.count, COffT(offset))
|
||||
system_pwrite(self.rawValue, buffer.baseAddress, buffer.count, _COffT(offset))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -322,7 +322,6 @@ extension FilePath {
|
||||
}
|
||||
|
||||
// Whether we are providing Windows paths
|
||||
@_implementationOnly import var SystemInternals.forceWindowsPaths
|
||||
@inline(__always)
|
||||
internal var _windowsPaths: Bool {
|
||||
#if os(Windows)
|
||||
|
||||
@@ -15,32 +15,26 @@ import CSystem
|
||||
// TODO: Should CSystem just include all the header files we need?
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
@_implementationOnly import Darwin
|
||||
#elseif os(Linux) || os(FreeBSD) || os(Android)
|
||||
import Glibc
|
||||
@_implementationOnly import Glibc
|
||||
#elseif os(Windows)
|
||||
import ucrt
|
||||
@_implementationOnly import ucrt
|
||||
#else
|
||||
#error("Unsupported Platform")
|
||||
#endif
|
||||
|
||||
public typealias COffT = off_t
|
||||
|
||||
#if os(Windows)
|
||||
public typealias CModeT = CInt
|
||||
#else
|
||||
public typealias CModeT = mode_t
|
||||
#endif
|
||||
internal typealias _COffT = off_t
|
||||
|
||||
// MARK: syscalls and variables
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
public var system_errno: CInt {
|
||||
internal var system_errno: CInt {
|
||||
get { Darwin.errno }
|
||||
set { Darwin.errno = newValue }
|
||||
}
|
||||
#elseif os(Windows)
|
||||
public var system_errno: CInt {
|
||||
internal var system_errno: CInt {
|
||||
get {
|
||||
var value: CInt = 0
|
||||
// TODO(compnerd) handle the error?
|
||||
@@ -52,7 +46,7 @@ public var system_errno: CInt {
|
||||
}
|
||||
}
|
||||
#else
|
||||
public var system_errno: CInt {
|
||||
internal var system_errno: CInt {
|
||||
get { Glibc.errno }
|
||||
set { Glibc.errno = newValue }
|
||||
}
|
||||
@@ -62,33 +56,21 @@ public var system_errno: CInt {
|
||||
|
||||
// Convention: `system_foo` is system's wrapper for `foo`.
|
||||
|
||||
public func system_strerror(_ __errnum: Int32) -> UnsafeMutablePointer<Int8>! {
|
||||
internal func system_strerror(_ __errnum: Int32) -> UnsafeMutablePointer<Int8>! {
|
||||
strerror(__errnum)
|
||||
}
|
||||
|
||||
public func system_strlen(_ s: UnsafePointer<Int8>) -> Int {
|
||||
internal func system_strlen(_ s: UnsafePointer<Int8>) -> Int {
|
||||
strlen(s)
|
||||
}
|
||||
|
||||
#if os(Windows)
|
||||
public typealias _PlatformChar = UInt16
|
||||
#else
|
||||
public typealias _PlatformChar = CChar
|
||||
#endif
|
||||
#if os(Windows)
|
||||
public typealias _PlatformUnicodeEncoding = UTF16
|
||||
#else
|
||||
public typealias _PlatformUnicodeEncoding = UTF8
|
||||
#endif
|
||||
|
||||
|
||||
// Convention: `system_platform_foo` is a
|
||||
// platform-representation-abstracted wrapper around `foo`-like functionality.
|
||||
// Type and layout differences such as the `char` vs `wchar` are abstracted.
|
||||
//
|
||||
|
||||
// strlen for the platform string
|
||||
public func system_platform_strlen(_ s: UnsafePointer<_PlatformChar>) -> Int {
|
||||
internal func system_platform_strlen(_ s: UnsafePointer<CInterop.PlatformChar>) -> Int {
|
||||
#if os(Windows)
|
||||
return wcslen(s)
|
||||
#else
|
||||
@@ -98,8 +80,8 @@ public func system_platform_strlen(_ s: UnsafePointer<_PlatformChar>) -> Int {
|
||||
|
||||
// Interop between String and platfrom string
|
||||
extension String {
|
||||
public func _withPlatformString<Result>(
|
||||
_ body: (UnsafePointer<_PlatformChar>) throws -> Result
|
||||
internal func _withPlatformString<Result>(
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
||||
) rethrows -> Result {
|
||||
// Need to #if because CChar may be signed
|
||||
#if os(Windows)
|
||||
@@ -109,7 +91,7 @@ extension String {
|
||||
#endif
|
||||
}
|
||||
|
||||
public init?(_platformString platformString: UnsafePointer<_PlatformChar>) {
|
||||
internal init?(_platformString platformString: UnsafePointer<CInterop.PlatformChar>) {
|
||||
// Need to #if because CChar may be signed
|
||||
#if os(Windows)
|
||||
guard let strRes = String.decodeCString(
|
||||
@@ -126,8 +108,8 @@ extension String {
|
||||
#endif
|
||||
}
|
||||
|
||||
public init(
|
||||
_errorCorrectingPlatformString platformString: UnsafePointer<_PlatformChar>
|
||||
internal init(
|
||||
_errorCorrectingPlatformString platformString: UnsafePointer<CInterop.PlatformChar>
|
||||
) {
|
||||
// Need to #if because CChar may be signed
|
||||
#if os(Windows)
|
||||
@@ -53,7 +53,7 @@ internal enum ForceErrno: Equatable {
|
||||
}
|
||||
|
||||
// Provide access to the driver, context, and trace stack of mocking
|
||||
public class MockingDriver {
|
||||
internal class MockingDriver {
|
||||
// Record syscalls and their arguments
|
||||
internal var trace = Trace()
|
||||
|
||||
@@ -109,7 +109,7 @@ private var contextualMockingEnabled: Bool {
|
||||
extension MockingDriver {
|
||||
internal static var enabled: Bool { mockingEnabled }
|
||||
|
||||
public static var forceWindowsPaths: Bool {
|
||||
internal static var forceWindowsPaths: Bool {
|
||||
currentMockingDriver?.forceWindowsSyntaxForPaths ?? false
|
||||
}
|
||||
}
|
||||
@@ -126,8 +126,8 @@ internal var mockingEnabled: Bool {
|
||||
#endif
|
||||
}
|
||||
|
||||
@inline(__always) @inlinable
|
||||
public var forceWindowsPaths: Bool {
|
||||
@inline(__always)
|
||||
internal var forceWindowsPaths: Bool {
|
||||
#if !ENABLE_MOCKING
|
||||
return false
|
||||
#else
|
||||
@@ -183,8 +183,8 @@ internal func _mockInt(
|
||||
|
||||
internal func _mockOffT(
|
||||
name: String = #function, _ args: AnyHashable...
|
||||
) -> COffT {
|
||||
COffT(mockImpl(name: name, args))
|
||||
) -> _COffT {
|
||||
_COffT(mockImpl(name: name, args))
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
*/
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
@_implementationOnly import Darwin
|
||||
#elseif os(Linux) || os(FreeBSD) || os(Android)
|
||||
import Glibc
|
||||
@_implementationOnly import Glibc
|
||||
#elseif os(Windows)
|
||||
import ucrt
|
||||
@_implementationOnly import ucrt
|
||||
#else
|
||||
#error("Unsupported Platform")
|
||||
#endif
|
||||
@@ -21,8 +21,8 @@ import ucrt
|
||||
// amount of code size, so we hand outline that code for every syscall
|
||||
|
||||
// open
|
||||
public func system_open(
|
||||
_ path: UnsafePointer<_PlatformChar>, _ oflag: Int32
|
||||
internal func system_open(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>, _ oflag: Int32
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled {
|
||||
@@ -32,8 +32,9 @@ public func system_open(
|
||||
return open(path, oflag)
|
||||
}
|
||||
|
||||
public func system_open(
|
||||
_ path: UnsafePointer<_PlatformChar>, _ oflag: Int32, _ mode: CModeT
|
||||
internal func system_open(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ oflag: Int32, _ mode: CInterop.Mode
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled {
|
||||
@@ -44,7 +45,7 @@ public func system_open(
|
||||
}
|
||||
|
||||
// close
|
||||
public func system_close(_ fd: Int32) -> Int32 {
|
||||
internal func system_close(_ fd: Int32) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(fd) }
|
||||
#endif
|
||||
@@ -52,7 +53,7 @@ public func system_close(_ fd: Int32) -> Int32 {
|
||||
}
|
||||
|
||||
// read
|
||||
public func system_read(
|
||||
internal func system_read(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
@@ -62,7 +63,7 @@ public func system_read(
|
||||
}
|
||||
|
||||
// pread
|
||||
public func system_pread(
|
||||
internal func system_pread(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
@@ -72,7 +73,7 @@ public func system_pread(
|
||||
}
|
||||
|
||||
// lseek
|
||||
public func system_lseek(
|
||||
internal func system_lseek(
|
||||
_ fd: Int32, _ off: off_t, _ whence: Int32
|
||||
) -> off_t {
|
||||
#if ENABLE_MOCKING
|
||||
@@ -82,7 +83,7 @@ public func system_lseek(
|
||||
}
|
||||
|
||||
// write
|
||||
public func system_write(
|
||||
internal func system_write(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
@@ -92,7 +93,7 @@ public func system_write(
|
||||
}
|
||||
|
||||
// pwrite
|
||||
public func system_pwrite(
|
||||
internal func system_pwrite(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
+4
-4
@@ -9,12 +9,12 @@
|
||||
|
||||
#if os(Windows)
|
||||
|
||||
import ucrt
|
||||
import WinSDK
|
||||
@_implementationOnly import ucrt
|
||||
@_implementationOnly import WinSDK
|
||||
|
||||
@inline(__always)
|
||||
internal func open(
|
||||
_ path: UnsafePointer<_PlatformChar>, _ oflag: Int32
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>, _ oflag: Int32
|
||||
) -> CInt {
|
||||
var fh: CInt = -1
|
||||
_ = _wsopen_s(&fh, path, oflag, _SH_DENYNO, _S_IREAD | _S_IWRITE)
|
||||
@@ -23,7 +23,7 @@ internal func open(
|
||||
|
||||
@inline(__always)
|
||||
internal func open(
|
||||
_ path: UnsafePointer<_PlatformChar>, _ oflag: Int32, _ mode: CModeT
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>, _ oflag: Int32, _ mode: CModeT
|
||||
) -> CInt {
|
||||
// TODO(compnerd): Apply read/write permissions
|
||||
var fh: CInt = -1
|
||||
@@ -7,9 +7,7 @@
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
@_implementationOnly import SystemInternals
|
||||
|
||||
// Public typealiases that can't be reexported from SystemInternals
|
||||
// MARK: - Public typealiases
|
||||
|
||||
/// The C `mode_t` type.
|
||||
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
|
||||
@@ -32,4 +30,30 @@ public enum CInterop {
|
||||
|
||||
/// The C `char` type
|
||||
public typealias Char = CChar
|
||||
|
||||
#if os(Windows)
|
||||
/// The platform's preferred character type. On Unix, this is an 8-bit C
|
||||
/// `char` (which may be signed or unsigned, depending on platform). On
|
||||
/// Windows, this is `UInt16` (a "wide" character).
|
||||
public typealias PlatformChar = UInt16
|
||||
#else
|
||||
/// The platform's preferred character type. On Unix, this is an 8-bit C
|
||||
/// `char` (which may be signed or unsigned, depending on platform). On
|
||||
/// Windows, this is `UInt16` (a "wide" character).
|
||||
public typealias PlatformChar = CInterop.Char
|
||||
#endif
|
||||
|
||||
#if os(Windows)
|
||||
/// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on
|
||||
/// Windows it is UTF-16. Native strings may contain invalid Unicode,
|
||||
/// which will be handled by either error-correction or failing, depending
|
||||
/// on API.
|
||||
public typealias PlatformUnicodeEncoding = UTF16
|
||||
#else
|
||||
/// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on
|
||||
/// Windows it is UTF-16. Native strings may contain invalid Unicode,
|
||||
/// which will be handled by either error-correction or failing, depending
|
||||
/// on API.
|
||||
public typealias PlatformUnicodeEncoding = UTF8
|
||||
#endif
|
||||
}
|
||||
@@ -7,54 +7,6 @@
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension CInterop {
|
||||
#if os(Windows)
|
||||
/// The platform's preferred character type. On Unix, this is an 8-bit C
|
||||
/// `char` (which may be signed or unsigned, depending on platform). On
|
||||
/// Windows, this is `UInt16` (a "wide" character).
|
||||
public typealias PlatformChar = UInt16
|
||||
#else
|
||||
/// The platform's preferred character type. On Unix, this is an 8-bit C
|
||||
/// `char` (which may be signed or unsigned, depending on platform). On
|
||||
/// Windows, this is `UInt16` (a "wide" character).
|
||||
public typealias PlatformChar = CInterop.Char
|
||||
#endif
|
||||
|
||||
#if os(Windows)
|
||||
/// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on
|
||||
/// Windows it is UTF-16. Native strings may contain invalid Unicode,
|
||||
/// which will be handled by either error-correction or failing, depending
|
||||
/// on API.
|
||||
public typealias PlatformUnicodeEncoding = UTF16
|
||||
#else
|
||||
/// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on
|
||||
/// Windows it is UTF-16. Native strings may contain invalid Unicode,
|
||||
/// which will be handled by either error-correction or failing, depending
|
||||
/// on API.
|
||||
public typealias PlatformUnicodeEncoding = UTF8
|
||||
#endif
|
||||
}
|
||||
|
||||
extension CInterop.PlatformChar {
|
||||
internal var _platformCodeUnit: CInterop.PlatformUnicodeEncoding.CodeUnit {
|
||||
#if os(Windows)
|
||||
return self
|
||||
#else
|
||||
return CInterop.PlatformUnicodeEncoding.CodeUnit(bitPattern: self)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
extension CInterop.PlatformUnicodeEncoding.CodeUnit {
|
||||
internal var _platformChar: CInterop.PlatformChar {
|
||||
#if os(Windows)
|
||||
return self
|
||||
#else
|
||||
return CInterop.PlatformChar(bitPattern: self)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension String {
|
||||
/// Creates a string by interpreting the null-terminated platform string as
|
||||
@@ -106,6 +58,25 @@ extension String {
|
||||
|
||||
}
|
||||
|
||||
extension CInterop.PlatformChar {
|
||||
internal var _platformCodeUnit: CInterop.PlatformUnicodeEncoding.CodeUnit {
|
||||
#if os(Windows)
|
||||
return self
|
||||
#else
|
||||
return CInterop.PlatformUnicodeEncoding.CodeUnit(bitPattern: self)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
extension CInterop.PlatformUnicodeEncoding.CodeUnit {
|
||||
internal var _platformChar: CInterop.PlatformChar {
|
||||
#if os(Windows)
|
||||
return self
|
||||
#else
|
||||
return CInterop.PlatformChar(bitPattern: self)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
internal protocol _PlatformStringable {
|
||||
func _withPlatformString<Result>(
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
||||
|
||||
@@ -239,7 +239,6 @@ extension SystemString: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
internal var debugDescription: String { description.debugDescription }
|
||||
}
|
||||
|
||||
@_implementationOnly import func SystemInternals.system_platform_strlen
|
||||
extension SystemString {
|
||||
/// Creates a system string by copying bytes from a null-terminated platform string.
|
||||
///
|
||||
|
||||
@@ -10,13 +10,11 @@
|
||||
import XCTest
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
import SystemPackage
|
||||
@testable import SystemPackage
|
||||
#else
|
||||
import System
|
||||
@testable import System
|
||||
#endif
|
||||
|
||||
@testable import SystemInternals
|
||||
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
final class MockingTest: XCTestCase {
|
||||
func testMocking() {
|
||||
|
||||
@@ -42,8 +42,6 @@ import XCTest
|
||||
@testable import System
|
||||
#endif
|
||||
|
||||
import SystemInternals
|
||||
|
||||
private func makeRaw(
|
||||
_ str: String
|
||||
) -> [CInterop.PlatformChar] {
|
||||
|
||||
@@ -9,12 +9,10 @@
|
||||
|
||||
import XCTest
|
||||
|
||||
@testable import SystemInternals
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
import SystemPackage
|
||||
@testable import SystemPackage
|
||||
#else
|
||||
import System
|
||||
@testable import System
|
||||
#endif
|
||||
|
||||
// To aid debugging, force failures to fatal error
|
||||
|
||||
Reference in new issue
Block a user