Skip to content

Commit

Permalink
WIP: Draft of Windows syscall organization
Browse files Browse the repository at this point in the history
  • Loading branch information
milseman committed Oct 23, 2020
1 parent 173c684 commit 78dd525
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/SystemInternals/Syscalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ private func mockImpl(
switch driver.forceErrno {
case .none: break
case .always(let e):
errno = e
system_errno = e
return -1
case .counted(let e, let count):
assert(count >= 1)
errno = e
system_errno = e
driver.forceErrno = count > 1 ? .counted(errno: e, count: count-1) : .none
return -1
}
Expand Down
88 changes: 88 additions & 0 deletions Sources/SystemInternals/WindowsSyscallAdapters.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
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(Windows)

internal func open(_ path: UnsafePointer<CChar>, _ oflag: Int32) {
var fh: CInt = -1
_ = _sopen_s(&fh, path, oflag, _SH_DENYNO, _S_IREAD | _S_IWRITE)
return fh
}

internal func open(
_ path: UnsafePointer<CChar>, _ oflag: Int32, _ mode: mode_t
) -> CInt {
// TODO: Is it ok to ignore permissions? Or should Windows not
// have the permissions API version?
var fh: CInt = -1
_ = _sopen_s(&fh, path, oflag, _SH_DENYNO, _S_IREAD | _S_IWRITE)
return fh
}

internal let close = _close
internal let lseek = _lseek

internal func read(
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int
) -> Int {
Int(_read(fd, buf, numericCast(nbyte)))
}

internal func write(
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int
) -> Int {
Int(_write(fd, buf, numericCast(nbyte)))
}

internal func pread(
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t
) -> Int {
let handle: intptr_t = _get_osfhandle(fd)
if handle == /* INVALID_HANDLE_VALUE */ -1 { return Int(EBADF) }

// NOTE: this is a non-owning handle, do *not* call CloseHandle on it
let hFile: HANDLE = HANDLE(bitPattern: handle)!

var ovlOverlapped: OVERLAPPED = OVERLAPPED()
ovlOverlapped.OffsetHigh = DWORD(UInt32(offset >> 32) & 0xffffffff)
ovlOverlapped.Offset = DWORD(UInt32(offset >> 0) & 0xffffffff)

var nNumberOfBytesRead: DWORD = 0
if !ReadFile(hFile, buf, DWORD(nbyte), &nNumberOfBytesRead, &ovlOverlapped) {
let _ = GetLastError()
// TODO(compnerd) map windows error to errno
return Int(-1)
}
return Int(nNumberOfBytesRead)
}

internal func pwrite(
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int, _ offset: off_t
) -> Int {
let handle: intptr_t = _get_osfhandle(fd)
if handle == /* INVALID_HANDLE_VALUE */ -1 { return Int(EBADF) }

// NOTE: this is a non-owning handle, do *not* call CloseHandle on it
let hFile: HANDLE = HANDLE(bitPattern: handle)!

var ovlOverlapped: OVERLAPPED = OVERLAPPED()
ovlOverlapped.OffsetHigh = DWORD(UInt32(offset >> 32) & 0xffffffff)
ovlOverlapped.Offset = DWORD(UInt32(offset >> 0) & 0xffffffff)

var nNumberOfBytesWritten: DWORD = 0
if !WriteFile(hFile, buf, DWORD(nbyte), &nNumberOfBytesWritten,
&ovlOverlapped) {
let _ = GetLastError()
// TODO(compnerd) map windows error to errno
return Int(-1)
}
return Int(nNumberOfBytesWritten)
}

#endif

0 comments on commit 78dd525

Please sign in to comment.