Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sketch] sysconf, pathconf, fpathconf #33

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Sources/System/FilePath/FilePathString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ extension FilePath {
/// The pointer passed as an argument to `body` is valid
/// only during the execution of this method.
/// Don't try to store the pointer for later use.
@_alwaysEmitIntoClient
public func withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
) rethrows -> Result {
#if !os(Windows)
try withCString(body)
#else
try _withPlatformString(body)
#endif
}
}

Expand Down Expand Up @@ -412,7 +417,7 @@ extension FilePath {
#if os(Windows)
fatalError("FilePath.withCString() unsupported on Windows ")
#else
return try withPlatformString(body)
return try _withPlatformString(body)
#endif
}
}
22 changes: 14 additions & 8 deletions Sources/System/Internals/Mocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,20 @@ private func originalSyscallName(_ function: String) -> String {

private func mockImpl(
name: String,
path: UnsafePointer<CChar>?,
_ args: [AnyHashable]
) -> CInt {
precondition(mockingEnabled)
let origName = originalSyscallName(name)
guard let driver = currentMockingDriver else {
fatalError("Mocking requested from non-mocking context")
}
driver.trace.add(Trace.Entry(name: origName, args))
var mockArgs: Array<AnyHashable> = []
if let p = path {
mockArgs.append(String(_errorCorrectingPlatformString: p))
}
mockArgs.append(contentsOf: args)
driver.trace.add(Trace.Entry(name: origName, mockArgs))

switch driver.forceErrno {
case .none: break
Expand All @@ -170,21 +177,20 @@ private func mockImpl(
}

internal func _mock(
name: String = #function, _ args: AnyHashable...
name: String = #function, path: UnsafePointer<CChar>? = nil, _ args: AnyHashable...
) -> CInt {
precondition(mockingEnabled)
return mockImpl(name: name, args)
return mockImpl(name: name, path: path, args)
}
internal func _mockInt(
name: String = #function, _ args: AnyHashable...
name: String = #function, path: UnsafePointer<CChar>? = nil, _ args: AnyHashable...
) -> Int {
Int(mockImpl(name: name, args))
Int(mockImpl(name: name, path: path, args))
}

internal func _mockOffT(
name: String = #function, _ args: AnyHashable...
name: String = #function, path: UnsafePointer<CChar>? = nil, _ args: AnyHashable...
) -> _COffT {
_COffT(mockImpl(name: name, args))
_COffT(mockImpl(name: name, path: path, args))
}
#endif // ENABLE_MOCKING

Expand Down
31 changes: 29 additions & 2 deletions Sources/System/Internals/Syscalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal func system_open(
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return _mock(String(_errorCorrectingPlatformString: path), oflag)
return _mock(path: path, oflag)
}
#endif
return open(path, oflag)
Expand All @@ -38,7 +38,7 @@ internal func system_open(
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return _mock(String(_errorCorrectingPlatformString: path), oflag, mode)
return _mock(path: path, oflag, mode)
}
#endif
return open(path, oflag, mode)
Expand Down Expand Up @@ -115,3 +115,30 @@ internal func system_dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
#endif
return dup2(fd, fd2)
}

internal func system_sysconf(_ name: CInt) -> Int {
#if ENABLE_MOCKING
if mockingEnabled { return _mockInt(name) }
#endif
return sysconf(name)
}

internal func system_pathconf(
_ path: UnsafePointer<CChar>, _ name: CInt
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled {
return _mockInt(path: path, name)
}
#endif
return pathconf(path, name)
}

internal func system_fpathconf(
_ fd: CInt, _ name: CInt
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled { return _mockInt(fd, name) }
#endif
return fpathconf(fd, name)
}
Loading