Files
swift-system/Sources/System/Internals/Syscalls.swift
T
Michael Ilseman d7611ca8ad 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.
2021-02-25 21:14:46 -07:00

118 lines
2.8 KiB
Swift

/*
This source file is part of the Swift System open source project
Copyright (c) 2020 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
*/
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
@_implementationOnly import Darwin
#elseif os(Linux) || os(FreeBSD) || os(Android)
@_implementationOnly import Glibc
#elseif os(Windows)
@_implementationOnly import ucrt
#else
#error("Unsupported Platform")
#endif
// Interacting with the mocking system, tracing, etc., is a potentially significant
// amount of code size, so we hand outline that code for every syscall
// open
internal func system_open(
_ path: UnsafePointer<CInterop.PlatformChar>, _ oflag: Int32
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return _mock(String(_errorCorrectingPlatformString: path), oflag)
}
#endif
return open(path, oflag)
}
internal func system_open(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ oflag: Int32, _ mode: CInterop.Mode
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return _mock(String(_errorCorrectingPlatformString: path), oflag, mode)
}
#endif
return open(path, oflag, mode)
}
// close
internal func system_close(_ fd: Int32) -> Int32 {
#if ENABLE_MOCKING
if mockingEnabled { return _mock(fd) }
#endif
return close(fd)
}
// read
internal func system_read(
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled { return _mockInt(fd, buf, nbyte) }
#endif
return read(fd, buf, nbyte)
}
// pread
internal func system_pread(
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled { return _mockInt(fd, buf, nbyte, offset) }
#endif
return pread(fd, buf, nbyte, offset)
}
// lseek
internal func system_lseek(
_ fd: Int32, _ off: off_t, _ whence: Int32
) -> off_t {
#if ENABLE_MOCKING
if mockingEnabled { return _mockOffT(fd, off, whence) }
#endif
return lseek(fd, off, whence)
}
// write
internal func system_write(
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled { return _mockInt(fd, buf, nbyte) }
#endif
return write(fd, buf, nbyte)
}
// pwrite
internal func system_pwrite(
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int, _ offset: off_t
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled { return _mockInt(fd, buf, nbyte, offset) }
#endif
return pwrite(fd, buf, nbyte, offset)
}
public func system_dup(_ fd: Int32) -> Int32 {
#if ENABLE_MOCKING
if mockingEnabled { return _mock(fd) }
#endif
return dup(fd)
}
public func system_dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
#if ENABLE_MOCKING
if mockingEnabled { return _mock(fd, fd2) }
#endif
return dup2(fd, fd2)
}