#[cfg(not(any(
target_arch = "x86",
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64",
)))]
compile_error!("oscall does not support this architecture on Windows");
pub(crate) mod event;
pub(crate) mod fs;
pub(crate) mod io;
pub(crate) mod mem;
pub(crate) mod net;
pub(crate) mod process;
pub(crate) mod rand;
pub(crate) mod sync;
pub(crate) mod thread;
pub(crate) mod time;
use core::ffi::c_void;
use crate::{Error, ErrorKind, Result};
pub(crate) type RawHandle = *mut c_void;
macro_rules! dll {
($dll:literal { $($item:tt)* }) => {
#[cfg_attr(
target_arch = "x86",
link(
name = $dll,
kind = "raw-dylib",
modifiers = "+verbatim",
import_name_type = "undecorated"
)
)]
#[cfg_attr(
not(target_arch = "x86"),
link(name = $dll, kind = "raw-dylib", modifiers = "+verbatim")
)]
unsafe extern "system" {
$($item)*
}
};
}
#[allow(unused_imports)]
pub(crate) use dll;
dll!("kernel32.dll" {
fn CloseHandle(handle: *mut c_void) -> i32;
fn GetLastError() -> u32;
});
#[inline]
pub(crate) fn last_error() -> Error {
Error::from_raw_os_error(unsafe { GetLastError() } as i32)
}
#[inline]
#[allow(dead_code)]
pub(crate) fn cvt(ret: i32) -> Result<()> {
if ret == 0 { Err(last_error()) } else { Ok(()) }
}
dll!("ntdll.dll" {
fn RtlNtStatusToDosError(status: i32) -> u32;
});
#[inline]
#[allow(dead_code)]
pub(crate) fn nt_error(status: i32) -> Error {
Error::from_raw_os_error(unsafe { RtlNtStatusToDosError(status) } as i32)
}
pub(crate) unsafe fn close(handle: RawHandle) {
unsafe { CloseHandle(handle) };
}
pub(crate) const OTHER_ERROR: i32 = 31;
pub(crate) const ERRORS: &[(i32, ErrorKind)] = {
use ErrorKind::*;
&[
(2, NotFound), (3, NotFound), (15, NotFound), (53, NotFound), (67, NotFound), (5, PermissionDenied), (1314, PermissionDenied), (10013, PermissionDenied), (183, AlreadyExists), (80, AlreadyExists), (10035, WouldBlock), (10036, InProgress), (10037, InProgress), (997, InProgress), (10004, Interrupted), (87, InvalidInput), (131, InvalidInput), (10022, InvalidInput), (10014, InvalidInput), (1460, TimedOut), (121, TimedOut), (258, TimedOut), (10060, TimedOut), (50, Unsupported), (120, Unsupported), (1, Unsupported), (10045, Unsupported), (10047, Unsupported), (10043, Unsupported), (10044, Unsupported), (14, OutOfMemory), (8, OutOfMemory), (1455, OutOfMemory), (6, BadHandle), (10009, BadHandle), (10038, BadHandle), (109, BrokenPipe), (232, BrokenPipe), (10058, BrokenPipe), (267, NotADirectory), (336, IsADirectory), (145, DirectoryNotEmpty), (123, InvalidFilename), (206, InvalidFilename), (161, InvalidFilename), (122, BufferTooSmall), (1921, FilesystemLoop), (112, StorageFull), (39, StorageFull), (1295, QuotaExceeded), (223, FileTooLarge), (19, ReadOnlyFilesystem), (17, CrossesDevices), (1142, TooManyLinks), (4, TooManyOpenFiles), (10024, TooManyOpenFiles), (170, ResourceBusy), (32, ResourceBusy), (33, ResourceBusy), (1131, Deadlock), (132, NotSeekable), (10061, ConnectionRefused), (1225, ConnectionRefused), (10054, ConnectionReset), (64, ConnectionReset), (10053, ConnectionAborted), (1236, ConnectionAborted), (10057, NotConnected), (233, NotConnected), (10056, AlreadyConnected), (10048, AddrInUse), (10049, AddrNotAvailable), (10050, NetworkDown), (10051, NetworkUnreachable), (1231, NetworkUnreachable), (10065, HostUnreachable), (1232, HostUnreachable), (10040, MessageTooLong), (234, MessageTooLong), ]
};
#[cfg(test)]
mod tests {
use super::*;
dll!("kernel32.dll" {
fn GetCurrentProcessId() -> u32;
fn GetFileType(handle: *mut c_void) -> u32;
});
#[test]
fn calls() {
let pid = unsafe { GetCurrentProcessId() };
assert!(pid > 0);
#[cfg(feature = "std")]
assert_eq!(pid, std::process::id());
let kind = unsafe { GetFileType(usize::MAX as *mut c_void) };
assert_eq!(kind, 0);
let err = last_error();
assert_eq!(err.raw_os_error(), 6);
assert_eq!(err.kind(), ErrorKind::BadHandle);
}
}