Skip to content
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
46 changes: 10 additions & 36 deletions crates/host_env/src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ use std::io;
#[cfg(unix)]
use std::os::fd::BorrowedFd;

use crate::os::CheckLibcResult;

pub fn normalize_ioctl_request(request: i64) -> libc::c_ulong {
(request as u32) as libc::c_ulong
}

pub fn fcntl_int(fd: i32, cmd: i32, arg: i32) -> io::Result<i32> {
let ret = unsafe { libc::fcntl(fd, cmd, arg) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { libc::fcntl(fd, cmd, arg) }.check_libc_neg()
}

pub fn validate_fd(fd: i32) -> io::Result<()> {
Expand Down Expand Up @@ -56,12 +53,7 @@ pub fn set_blocking(fd: BorrowedFd<'_>, blocking: bool) -> io::Result<()> {
}

pub fn fcntl_with_bytes(fd: i32, cmd: i32, arg: &mut [u8]) -> io::Result<i32> {
let ret = unsafe { libc::fcntl(fd, cmd, arg.as_mut_ptr()) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { libc::fcntl(fd, cmd, arg.as_mut_ptr()) }.check_libc_neg()
}

/// # Safety
Expand All @@ -73,31 +65,16 @@ pub unsafe fn ioctl_ptr(
request: libc::c_ulong,
arg: *mut libc::c_void,
) -> io::Result<i32> {
let ret = unsafe { libc::ioctl(fd, request as _, arg) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { libc::ioctl(fd, request as _, arg) }.check_libc_neg()
}

pub fn ioctl_int(fd: i32, request: libc::c_ulong, arg: i32) -> io::Result<i32> {
let ret = unsafe { libc::ioctl(fd, request as _, arg) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { libc::ioctl(fd, request as _, arg) }.check_libc_neg()
}

#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
pub fn flock(fd: i32, operation: i32) -> io::Result<i32> {
let ret = unsafe { libc::flock(fd, operation) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { libc::flock(fd, operation) }.check_libc_neg()
}

#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
Expand Down Expand Up @@ -137,7 +114,7 @@ pub fn lockf(fd: i32, cmd: i32, len: i64, start: i64, whence: i32) -> Result<i32
..unsafe { core::mem::zeroed() }
};

let ret = unsafe {
unsafe {
libc::fcntl(
fd,
if (cmd & libc::LOCK_NB) != 0 {
Expand All @@ -147,10 +124,7 @@ pub fn lockf(fd: i32, cmd: i32, len: i64, start: i64, whence: i32) -> Result<i32
},
&lock,
)
};
if ret < 0 {
Err(LockfError::Io(io::Error::last_os_error()))
} else {
Ok(ret)
}
.check_libc_neg()
.map_err(LockfError::Io)
}
31 changes: 10 additions & 21 deletions crates/host_env/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use std::io;

#[cfg(windows)]
use crate::windows::{CheckWin32Bool, CheckWin32Handle};
#[cfg(unix)]
use crate::{crt_fd, fileutils, posix};
use memmap2::{Mmap, MmapMut, MmapOptions};
Expand Down Expand Up @@ -131,7 +133,7 @@ impl Drop for NamedMmap {
#[cfg(windows)]
pub fn duplicate_handle(handle: Handle) -> io::Result<Handle> {
let mut new_handle: Handle = INVALID_HANDLE;
let result = unsafe {
unsafe {
DuplicateHandle(
GetCurrentProcess(),
handle,
Expand All @@ -141,12 +143,9 @@ pub fn duplicate_handle(handle: Handle) -> io::Result<Handle> {
0,
DUPLICATE_SAME_ACCESS,
)
};
if result == 0 {
Err(io::Error::last_os_error())
} else {
Ok(new_handle)
}
.check_win32_bool()?;
Ok(new_handle)
}

#[cfg(windows)]
Expand Down Expand Up @@ -187,13 +186,9 @@ pub fn is_invalid_handle_value(handle: isize) -> bool {

#[cfg(windows)]
pub fn extend_file(handle: Handle, size: i64) -> io::Result<()> {
if unsafe { SetFilePointerEx(handle, size, core::ptr::null_mut(), FILE_BEGIN) } == 0 {
return Err(io::Error::last_os_error());
}
if unsafe { SetEndOfFile(handle) } == 0 {
return Err(io::Error::last_os_error());
}
Ok(())
unsafe { SetFilePointerEx(handle, size, core::ptr::null_mut(), FILE_BEGIN) }
.check_win32_bool()?;
unsafe { SetEndOfFile(handle) }.check_win32_bool()
}

#[cfg(unix)]
Expand All @@ -210,11 +205,7 @@ pub fn close_handle(handle: Handle) {

#[cfg(windows)]
pub fn flush_view(ptr: *const core::ffi::c_void, size: usize) -> io::Result<()> {
if unsafe { FlushViewOfFile(ptr, size) } == 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
unsafe { FlushViewOfFile(ptr, size) }.check_win32_bool()
}

#[cfg(windows)]
Expand Down Expand Up @@ -252,10 +243,8 @@ pub fn create_named_mapping(
size_lo,
tag_wide.as_ptr(),
)
};
if map_handle.is_null() {
return Err(io::Error::last_os_error());
}
.check_nonnull()?;

let off_hi = (offset as u64 >> 32) as u32;
let off_lo = offset as u32;
Expand Down
31 changes: 7 additions & 24 deletions crates/host_env/src/msvcrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use alloc::{string::String, vec::Vec};
use std::io;

use crate::crt_fd;
use crate::os::CheckLibcResult;
use windows_sys::Win32::System::Diagnostics::Debug;

pub type ErrorMode = u32;
Expand Down Expand Up @@ -89,39 +90,21 @@ pub fn kbhit() -> i32 {
}

pub fn locking(fd: i32, mode: i32, nbytes: i64) -> io::Result<()> {
let ret = unsafe { suppress_iph!(_locking(fd, mode, nbytes)) };
if ret == -1 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
unsafe { suppress_iph!(_locking(fd, mode, nbytes)) }.check_libc_neg()?;
Ok(())
}

pub fn heapmin() -> io::Result<()> {
let ret = unsafe { suppress_iph!(_heapmin()) };
if ret == -1 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
unsafe { suppress_iph!(_heapmin()) }.check_libc_neg()?;
Ok(())
}

pub fn setmode(fd: crt_fd::Borrowed<'_>, flags: i32) -> io::Result<i32> {
let ret = unsafe { suppress_iph!(_setmode(fd, flags)) };
if ret == -1 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { suppress_iph!(_setmode(fd, flags)) }.check_libc_neg()
}

pub fn open_osfhandle(handle: isize, flags: i32) -> io::Result<i32> {
let ret = unsafe { suppress_iph!(libc::open_osfhandle(handle, flags)) };
if ret == -1 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { suppress_iph!(libc::open_osfhandle(handle, flags)) }.check_libc_neg()
}

pub fn get_error_mode() -> u32 {
Expand Down
10 changes: 4 additions & 6 deletions crates/host_env/src/multiprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,11 @@ impl SemHandle {
#[cfg(windows)]
impl SemHandle {
pub fn create(value: i32, maxvalue: i32) -> io::Result<Self> {
use crate::windows::CheckWin32Handle;
let handle =
unsafe { CreateSemaphoreW(core::ptr::null(), value, maxvalue, core::ptr::null()) };
if handle == 0 as HANDLE {
Err(io::Error::last_os_error())
} else {
Ok(Self { raw: handle })
}
unsafe { CreateSemaphoreW(core::ptr::null(), value, maxvalue, core::ptr::null()) }
.check_nonnull()?;
Ok(Self { raw: handle })
}

#[inline]
Expand Down
Loading
Loading