[wasi-common]: winx now returns io::Error directly (#1243)

* Winx now returns io::Error

This commit is a spiritual follower of #1242 in the sense that it
adjusts `winx` to also return `io::Error` directly rather than
tossing a custom error type here and there.

* Adapt wasi-common to changes in winx

* Run cargo fmt

* Swap overly big map_err with explicit match
This commit is contained in:
Jakub Konka
2020-03-09 10:32:01 +01:00
committed by GitHub
parent fbe29da5cc
commit e5b9f1b786
10 changed files with 281 additions and 450 deletions

View File

@@ -4,16 +4,15 @@ use crate::ntdll::{
NtQueryInformationFile, RtlNtStatusToDosError, FILE_ACCESS_INFORMATION, FILE_INFORMATION_CLASS,
FILE_MODE_INFORMATION, IO_STATUS_BLOCK,
};
use crate::{winerror, Result};
use bitflags::bitflags;
use cvt::cvt;
use std::ffi::{c_void, OsString};
use std::fs::File;
use std::io;
use std::io::{Error, Result};
use std::os::windows::prelude::{AsRawHandle, OsStringExt, RawHandle};
use winapi::shared::{
minwindef::{self, DWORD},
ntstatus,
ntstatus, winerror,
};
use winapi::um::{fileapi, fileapi::GetFileType, minwinbase, winbase, winnt};
@@ -57,8 +56,8 @@ impl FileType {
pub unsafe fn get_file_type(handle: RawHandle) -> Result<FileType> {
let file_type = FileType(GetFileType(handle));
let err = winerror::WinError::last();
if file_type.is_unknown() && err != winerror::WinError::ERROR_SUCCESS {
let err = Error::last_os_error();
if file_type.is_unknown() && err.raw_os_error().unwrap() as u32 != winerror::ERROR_SUCCESS {
Err(err)
} else {
Ok(file_type)
@@ -341,23 +340,20 @@ pub fn get_file_path(file: &File) -> Result<OsString> {
let handle = file.as_raw_handle();
let read_len =
unsafe { GetFinalPathNameByHandleW(handle, raw_path.as_mut_ptr(), WIDE_MAX_PATH, 0) };
if read_len == 0 {
// failed to read
return Err(winerror::WinError::last());
}
cvt(unsafe { GetFinalPathNameByHandleW(handle, raw_path.as_mut_ptr(), WIDE_MAX_PATH, 0) })?;
// obtain a slice containing the written bytes, and check for it being too long
// (practically probably impossible)
let written_bytes = raw_path
.get(..read_len as usize)
.ok_or(winerror::WinError::ERROR_BUFFER_OVERFLOW)?;
.ok_or(Error::from_raw_os_error(
winerror::ERROR_BUFFER_OVERFLOW as i32,
))?;
Ok(OsString::from_wide(written_bytes))
}
pub fn get_fileinfo(file: &File) -> io::Result<fileapi::BY_HANDLE_FILE_INFORMATION> {
pub fn get_fileinfo(file: &File) -> Result<fileapi::BY_HANDLE_FILE_INFORMATION> {
use fileapi::{GetFileInformationByHandle, BY_HANDLE_FILE_INFORMATION};
use std::mem;
@@ -371,7 +367,7 @@ pub fn get_fileinfo(file: &File) -> io::Result<fileapi::BY_HANDLE_FILE_INFORMATI
Ok(info)
}
pub fn change_time(file: &File) -> io::Result<i64> {
pub fn change_time(file: &File) -> Result<i64> {
use fileapi::FILE_BASIC_INFO;
use minwinbase::FileBasicInfo;
use std::mem;
@@ -407,7 +403,9 @@ pub fn query_access_information(handle: RawHandle) -> Result<AccessMode> {
);
if status != ntstatus::STATUS_SUCCESS {
return Err(winerror::WinError::from_u32(RtlNtStatusToDosError(status)));
return Err(Error::from_raw_os_error(
RtlNtStatusToDosError(status) as i32
));
}
}
@@ -428,7 +426,9 @@ pub fn query_mode_information(handle: RawHandle) -> Result<FileModeInformation>
);
if status != ntstatus::STATUS_SUCCESS {
return Err(winerror::WinError::from_u32(RtlNtStatusToDosError(status)));
return Err(Error::from_raw_os_error(
RtlNtStatusToDosError(status) as i32
));
}
}
@@ -448,7 +448,7 @@ pub fn reopen_file(handle: RawHandle, access_mode: AccessMode, flags: Flags) ->
};
if new_handle == winapi::um::handleapi::INVALID_HANDLE_VALUE {
return Err(winerror::WinError::last());
return Err(Error::last_os_error());
}
Ok(new_handle)

View File

@@ -24,8 +24,3 @@
pub mod file;
mod ntdll;
pub mod time;
pub mod winerror;
use winerror::WinError;
pub type Result<T> = std::result::Result<T, WinError>;

View File

@@ -1,7 +1,8 @@
use cvt::cvt;
use std::io::Result;
use winapi::um::{profileapi::QueryPerformanceFrequency, winnt::LARGE_INTEGER};
pub fn perf_counter_frequency() -> std::io::Result<u64> {
pub fn perf_counter_frequency() -> Result<u64> {
unsafe {
let mut frequency: LARGE_INTEGER = std::mem::zeroed();
cvt(QueryPerformanceFrequency(&mut frequency))?;

View File

@@ -1,127 +0,0 @@
#![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,
/// A required privilege is not held by the client.
ERROR_PRIVILEGE_NOT_HELD,
/// The file or directory is not a reparse point.
ERROR_NOT_A_REPARSE_POINT,
/// An attempt was made to move the file pointer before the beginning of the file.
ERROR_NEGATIVE_SEEK,
/// The directory name is invalid.
ERROR_DIRECTORY,
/// Cannot create a file when that file already exists.
ERROR_ALREADY_EXISTS,
}
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)
}
}