Add enough Windows functionality to run WASI tutorial (#22)
* Add partial impl of determine_type_rights fn * Add draft of fd_fdstat_get hostcall * Add writev wrapper for writing IoVec in RawHandle * Move IoVec and writev to separate helper crate * Add Win error handling Clean up closing and duplicating RawHandle * Wrap Win file type result * Add draft impl of fd_close and fd_read * Refactor getting file access rights * Remove winapi from the main Cargo.toml * Add very rough draft of open_path (very incomplete) * Clean up WinError with macro * Ignore dir handle in openat if path absolute * Decode oflags and advance open_path hostcall * Clean up AccessRight and FlagsAndAttributes flags * Implement path_get (without symlink expansion yet!) * Add ShareMode and fix path_get for nested paths * Add some error mappings between Win and WASI * Clean up fdflags conversions * Fix sharing violation when calling openat at '.' * Apply Alex's fix of using ManuallyDrop instead forget * Clean up * Explicitly specify workspace to avoid comp errors at tests
This commit is contained in:
117
winx/src/winerror.rs
Normal file
117
winx/src/winerror.rs
Normal file
@@ -0,0 +1,117 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
use winapi::shared::winerror;
|
||||
use winapi::um::errhandlingapi::GetLastError;
|
||||
|
||||
macro_rules! win_error_expand {
|
||||
{
|
||||
$(
|
||||
#[doc=$doc:literal]
|
||||
$error:ident,
|
||||
)*
|
||||
} => {
|
||||
/// Wraps WINAPI error code as enum.
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
||||
#[repr(u32)]
|
||||
pub enum WinError {
|
||||
/// Unknown error occurred.
|
||||
UnknownError = std::u32::MAX,
|
||||
$(
|
||||
#[doc=$doc]
|
||||
$error = winerror::$error,
|
||||
)*
|
||||
}
|
||||
|
||||
fn desc(err: WinError) -> &'static str {
|
||||
use WinError::*;
|
||||
match err {
|
||||
UnknownError => r" Unknown error occurred.",
|
||||
$($error => $doc,)*
|
||||
}
|
||||
}
|
||||
|
||||
fn from_u32(err: u32) -> WinError {
|
||||
use WinError::*;
|
||||
match err {
|
||||
$(winerror::$error => $error,)*
|
||||
_ => UnknownError,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
win_error_expand! {
|
||||
/// The operation completed successfully.
|
||||
ERROR_SUCCESS,
|
||||
/// The system cannot find the file specified.
|
||||
ERROR_FILE_NOT_FOUND,
|
||||
/// The system cannot find the path specified.
|
||||
ERROR_PATH_NOT_FOUND,
|
||||
/// The system cannot open the file.
|
||||
ERROR_TOO_MANY_OPEN_FILES,
|
||||
/// Access is denied.
|
||||
ERROR_ACCESS_DENIED,
|
||||
/// The handle is invalid.
|
||||
ERROR_INVALID_HANDLE,
|
||||
/// Not enough storage is available to process this command.
|
||||
ERROR_NOT_ENOUGH_MEMORY,
|
||||
/// The environment is incorrect.
|
||||
ERROR_BAD_ENVIRONMENT,
|
||||
/// Not enough storage is available to complete this operation.
|
||||
ERROR_OUTOFMEMORY,
|
||||
/// The device is not ready.
|
||||
ERROR_NOT_READY,
|
||||
/// The request is not supported.
|
||||
ERROR_NOT_SUPPORTED,
|
||||
/// The file exists.
|
||||
ERROR_FILE_EXISTS,
|
||||
/// The pipe has been ended.
|
||||
ERROR_BROKEN_PIPE,
|
||||
/// The file name is too long.
|
||||
ERROR_BUFFER_OVERFLOW,
|
||||
/// The directory is not empty.
|
||||
ERROR_DIR_NOT_EMPTY,
|
||||
/// The volume label you entered exceeds the label character limit of the destination file system.
|
||||
ERROR_LABEL_TOO_LONG,
|
||||
/// The requested resource is in use.
|
||||
ERROR_BUSY,
|
||||
/// The file name, directory name, or volume label syntax is incorrect.
|
||||
ERROR_INVALID_NAME,
|
||||
/// The process cannot access the file because it is being used by another process.
|
||||
ERROR_SHARING_VIOLATION,
|
||||
}
|
||||
|
||||
impl WinError {
|
||||
/// Returns the last error as WinError.
|
||||
pub fn last() -> Self {
|
||||
Self::from_u32(unsafe { GetLastError() })
|
||||
}
|
||||
|
||||
/// Constructs WinError from error code.
|
||||
pub fn from_u32(err: u32) -> Self {
|
||||
from_u32(err)
|
||||
}
|
||||
|
||||
/// Returns error's description string. This description matches
|
||||
/// the docs for the error.
|
||||
pub fn desc(self) -> &'static str {
|
||||
desc(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for WinError {
|
||||
fn description(&self) -> &str {
|
||||
self.desc()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for WinError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "{:?}: {}", self, self.desc())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<WinError> for std::io::Error {
|
||||
fn from(err: WinError) -> Self {
|
||||
Self::from_raw_os_error(err as i32)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user