use crate::io::{IoSlice, IoSliceMut, SeekFrom};
use crate::sys::syscall;
use crate::{BorrowedHandle, OwnedHandle, Result};
const O_CLOEXEC: usize = 0o2000000;
const IOV_MAX: usize = 1024;
const F_GETFD: usize = 1;
const F_GETFL: usize = 3;
const F_SETFL: usize = 4;
const O_NONBLOCK: usize = 0o4000;
const F_SETFD: usize = 2;
const F_DUPFD_CLOEXEC: usize = 1030;
const FD_CLOEXEC: usize = 1;
const TIOCGWINSZ: usize = 0x5413;
const SEEK_SET: usize = 0;
const SEEK_CUR: usize = 1;
const SEEK_END: usize = 2;
#[derive(Clone, Copy)]
#[repr(C)]
pub(crate) struct RawIoSlice {
base: *mut u8,
len: usize,
}
impl RawIoSlice {
#[inline]
pub(crate) fn new(base: *mut u8, len: usize) -> Self {
Self { base, len }
}
#[inline]
pub(crate) fn ptr(&self) -> *mut u8 {
self.base
}
#[inline]
pub(crate) fn len(&self) -> usize {
self.len
}
}
pub(crate) unsafe fn fcntl(fd: i32, cmd: usize, arg: usize) -> Result<usize> {
#[cfg(target_pointer_width = "32")]
return unsafe { syscall!(FCNTL64, fd, cmd, arg) };
#[cfg(target_pointer_width = "64")]
return unsafe { syscall!(FCNTL, fd, cmd, arg) };
}
#[inline]
fn halves(offset: u64) -> (usize, usize) {
#[cfg(target_pointer_width = "32")]
return (offset as usize, (offset >> 32) as usize);
#[cfg(target_pointer_width = "64")]
return (offset as usize, 0);
}
pub(crate) fn read(handle: BorrowedHandle<'_>, buf: &mut [u8]) -> Result<usize> {
unsafe { syscall!(READ, handle.as_raw(), buf.as_mut_ptr(), buf.len()) }
}
pub(crate) fn write(handle: BorrowedHandle<'_>, buf: &[u8]) -> Result<usize> {
unsafe { syscall!(WRITE, handle.as_raw(), buf.as_ptr(), buf.len()) }
}
pub(crate) fn read_at(handle: BorrowedHandle<'_>, buf: &mut [u8], offset: u64) -> Result<usize> {
let iov = RawIoSlice::new(buf.as_mut_ptr(), buf.len());
let (low, high) = halves(offset);
unsafe { syscall!(PREADV, handle.as_raw(), &raw const iov, 1, low, high) }
}
pub(crate) fn write_at(handle: BorrowedHandle<'_>, buf: &[u8], offset: u64) -> Result<usize> {
let iov = RawIoSlice::new(buf.as_ptr().cast_mut(), buf.len());
let (low, high) = halves(offset);
unsafe { syscall!(PWRITEV, handle.as_raw(), &raw const iov, 1, low, high) }
}
pub(crate) fn read_vectored(
handle: BorrowedHandle<'_>,
bufs: &mut [IoSliceMut<'_>],
) -> Result<usize> {
let count = bufs.len().min(IOV_MAX);
unsafe { syscall!(READV, handle.as_raw(), bufs.as_mut_ptr(), count) }
}
pub(crate) fn write_vectored(handle: BorrowedHandle<'_>, bufs: &[IoSlice<'_>]) -> Result<usize> {
let count = bufs.len().min(IOV_MAX);
unsafe { syscall!(WRITEV, handle.as_raw(), bufs.as_ptr(), count) }
}
pub(crate) fn seek(handle: BorrowedHandle<'_>, pos: SeekFrom) -> Result<u64> {
let (offset, whence) = match pos {
SeekFrom::Start(offset) => (offset as i64, SEEK_SET),
SeekFrom::Current(offset) => (offset, SEEK_CUR),
SeekFrom::End(offset) => (offset, SEEK_END),
};
#[cfg(target_pointer_width = "64")]
return unsafe { syscall!(LSEEK, handle.as_raw(), offset, whence) }.map(|pos| pos as u64);
#[cfg(target_pointer_width = "32")]
{
let mut result = 0u64;
let high = (offset >> 32) as usize;
let low = offset as usize;
#[cfg(not(target_arch = "riscv32"))]
unsafe { syscall!(_LLSEEK, handle.as_raw(), high, low, &raw mut result, whence) }?;
#[cfg(target_arch = "riscv32")]
unsafe { syscall!(LLSEEK, handle.as_raw(), high, low, &raw mut result, whence) }?;
Ok(result)
}
}
pub(crate) fn pipe() -> Result<(OwnedHandle, OwnedHandle)> {
let mut fds = [0i32; 2];
unsafe { syscall!(PIPE2, fds.as_mut_ptr(), O_CLOEXEC) }?;
Ok(unsafe { (OwnedHandle::from_raw(fds[0]), OwnedHandle::from_raw(fds[1])) })
}
pub(crate) fn duplicate(handle: BorrowedHandle<'_>) -> Result<OwnedHandle> {
let fd = unsafe { fcntl(handle.as_raw(), F_DUPFD_CLOEXEC, 0) }?;
Ok(unsafe { OwnedHandle::from_raw(fd as i32) })
}
fn std_handle(fd: i32) -> Option<BorrowedHandle<'static>> {
Some(unsafe { BorrowedHandle::borrow_raw(fd) })
}
pub(crate) fn stdin() -> Option<BorrowedHandle<'static>> {
std_handle(0)
}
pub(crate) fn stdout() -> Option<BorrowedHandle<'static>> {
std_handle(1)
}
pub(crate) fn stderr() -> Option<BorrowedHandle<'static>> {
std_handle(2)
}
pub(crate) fn is_terminal(handle: BorrowedHandle<'_>) -> bool {
let mut size = [0u16; 4];
unsafe { syscall!(IOCTL, handle.as_raw(), TIOCGWINSZ, size.as_mut_ptr()) }.is_ok()
}
pub(crate) fn set_inheritable(handle: BorrowedHandle<'_>, inheritable: bool) -> Result<()> {
let flags = if inheritable { 0 } else { FD_CLOEXEC };
unsafe { fcntl(handle.as_raw(), F_SETFD, flags) }?;
Ok(())
}
pub(crate) fn is_inheritable(handle: BorrowedHandle<'_>) -> Result<bool> {
let flags = unsafe { fcntl(handle.as_raw(), F_GETFD, 0) }?;
Ok(flags & FD_CLOEXEC == 0)
}
pub(crate) fn set_nonblocking(handle: BorrowedHandle<'_>, nonblocking: bool) -> Result<()> {
let flags = unsafe { fcntl(handle.as_raw(), F_GETFL, 0) }?;
let new = if nonblocking {
flags | O_NONBLOCK
} else {
flags & !O_NONBLOCK
};
if new != flags {
unsafe { fcntl(handle.as_raw(), F_SETFL, new) }?;
}
Ok(())
}