-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Draft of Windows syscall organization
- Loading branch information
Showing
2 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |