| 1 |
//! Windows system calls in the `io` module. |
| 2 |
|
| 3 |
use crate::backend::c; |
| 4 |
#[cfg(feature = "try_close")] |
| 5 |
use crate::backend::conv::ret; |
| 6 |
use crate::backend::conv::{borrowed_fd, ret_c_int, ret_send_recv, send_recv_len}; |
| 7 |
use crate::fd::{BorrowedFd, RawFd}; |
| 8 |
use crate::io; |
| 9 |
use crate::ioctl::{IoctlOutput, Opcode}; |
| 10 |
|
| 11 |
pub(crate) unsafe fn read(fd: BorrowedFd<'_>, buf: (*mut u8, usize)) -> io::Result<usize> { |
| 12 |
// `read` on a socket is equivalent to `recv` with no flags. |
| 13 |
ret_send_recv(c::recv( |
| 14 |
borrowed_fd(fd), |
| 15 |
buf.0.cast(), |
| 16 |
send_recv_len(buf.1), |
| 17 |
0, |
| 18 |
)) |
| 19 |
} |
| 20 |
|
| 21 |
pub(crate) fn write(fd: BorrowedFd<'_>, buf: &[u8]) -> io::Result<usize> { |
| 22 |
// `write` on a socket is equivalent to `send` with no flags. |
| 23 |
unsafe { |
| 24 |
ret_send_recv(c::send( |
| 25 |
borrowed_fd(fd), |
| 26 |
buf.as_ptr().cast(), |
| 27 |
send_recv_len(buf.len()), |
| 28 |
0, |
| 29 |
)) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
pub(crate) unsafe fn close(raw_fd: RawFd) { |
| 34 |
let _ = c::closesocket(raw_fd as c::SOCKET); |
| 35 |
} |
| 36 |
|
| 37 |
#[cfg(feature = "try_close")] |
| 38 |
pub(crate) unsafe fn try_close(raw_fd: RawFd) -> io::Result<()> { |
| 39 |
ret(c::closesocket(raw_fd as c::SOCKET)) |
| 40 |
} |
| 41 |
|
| 42 |
#[inline] |
| 43 |
pub(crate) unsafe fn ioctl( |
| 44 |
fd: BorrowedFd<'_>, |
| 45 |
request: Opcode, |
| 46 |
arg: *mut c::c_void, |
| 47 |
) -> io::Result<IoctlOutput> { |
| 48 |
ret_c_int(c::ioctl(borrowed_fd(fd), request, arg.cast())) |
| 49 |
} |
| 50 |
|
| 51 |
#[inline] |
| 52 |
pub(crate) unsafe fn ioctl_readonly( |
| 53 |
fd: BorrowedFd<'_>, |
| 54 |
request: Opcode, |
| 55 |
arg: *mut c::c_void, |
| 56 |
) -> io::Result<IoctlOutput> { |
| 57 |
ioctl(fd, request, arg) |
| 58 |
} |