diff options
Diffstat (limited to 'vendor/errno/src')
-rw-r--r-- | vendor/errno/src/hermit.rs | 10 | ||||
-rw-r--r-- | vendor/errno/src/lib.rs | 45 | ||||
-rw-r--r-- | vendor/errno/src/unix.rs | 82 | ||||
-rw-r--r-- | vendor/errno/src/wasi.rs | 57 | ||||
-rw-r--r-- | vendor/errno/src/windows.rs | 75 |
5 files changed, 143 insertions, 126 deletions
diff --git a/vendor/errno/src/hermit.rs b/vendor/errno/src/hermit.rs index d14a6c88f..99d4c3286 100644 --- a/vendor/errno/src/hermit.rs +++ b/vendor/errno/src/hermit.rs @@ -16,17 +16,17 @@ use Errno; -pub fn with_description<F, T>(_err: Errno, callback: F) -> T where - F: FnOnce(Result<&str, Errno>) -> T +pub fn with_description<F, T>(_err: Errno, callback: F) -> T +where + F: FnOnce(Result<&str, Errno>) -> T, { callback(Ok("unknown error")) } -pub const STRERROR_NAME: &'static str = "strerror_r"; +pub const STRERROR_NAME: &str = "strerror_r"; pub fn errno() -> Errno { Errno(0) } -pub fn set_errno(_: Errno) { -} +pub fn set_errno(_: Errno) {} diff --git a/vendor/errno/src/lib.rs b/vendor/errno/src/lib.rs index b3a1dcb1a..20875b5c3 100644 --- a/vendor/errno/src/lib.rs +++ b/vendor/errno/src/lib.rs @@ -17,27 +17,19 @@ //! println!("Error {}: {}", code, e); //! ``` -#![cfg_attr(target_os = "wasi", feature(thread_local))] #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(unix)] extern crate libc; -#[cfg(windows)] extern crate winapi; -#[cfg(target_os = "dragonfly")] extern crate errno_dragonfly; -#[cfg(target_os = "wasi")] extern crate libc; -#[cfg(target_os = "hermit")] extern crate libc; - #[cfg_attr(unix, path = "unix.rs")] #[cfg_attr(windows, path = "windows.rs")] #[cfg_attr(target_os = "wasi", path = "wasi.rs")] #[cfg_attr(target_os = "hermit", path = "hermit.rs")] mod sys; +use core::fmt; #[cfg(feature = "std")] -use std::fmt; +use std::error::Error; #[cfg(feature = "std")] use std::io; -#[cfg(feature = "std")] -use std::error::Error; /// Wraps a platform-specific error code. /// @@ -50,7 +42,6 @@ use std::error::Error; #[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd, Hash)] pub struct Errno(pub i32); -#[cfg(feature = "std")] impl fmt::Debug for Errno { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { sys::with_description(*self, |desc| { @@ -62,21 +53,24 @@ impl fmt::Debug for Errno { } } -#[cfg(feature = "std")] impl fmt::Display for Errno { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { sys::with_description(*self, |desc| match desc { - Ok(desc) => fmt.write_str(&desc), + Ok(desc) => fmt.write_str(desc), Err(fm_err) => write!( - fmt, "OS error {} ({} returned error {})", - self.0, sys::STRERROR_NAME, fm_err.0), + fmt, + "OS error {} ({} returned error {})", + self.0, + sys::STRERROR_NAME, + fm_err.0 + ), }) } } -impl Into<i32> for Errno { - fn into(self) -> i32 { - self.0 +impl From<Errno> for i32 { + fn from(e: Errno) -> Self { + e.0 } } @@ -128,16 +122,27 @@ fn check_description() { "Not owner" } else if cfg!(target_os = "wasi") { "Argument list too long" + } else if cfg!(target_os = "haiku") { + "Operation not allowed" } else { "Operation not permitted" }; - set_errno(Errno(1)); + let errno_code = if cfg!(target_os = "haiku") { + -2147483633 + } else { + 1 + }; + set_errno(Errno(errno_code)); assert_eq!(errno().to_string(), expect); assert_eq!( format!("{:?}", errno()), - format!("Errno {{ code: 1, description: Some({:?}) }}", expect)); + format!( + "Errno {{ code: {}, description: Some({:?}) }}", + errno_code, expect + ) + ); } #[cfg(feature = "std")] diff --git a/vendor/errno/src/unix.rs b/vendor/errno/src/unix.rs index 820d89748..c22c587c2 100644 --- a/vendor/errno/src/unix.rs +++ b/vendor/errno/src/unix.rs @@ -12,40 +12,42 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[cfg(feature = "std")] -use std::ffi::CStr; -use libc::c_int; -#[cfg(feature = "std")] -use libc::{self, c_char}; +use core::str; #[cfg(target_os = "dragonfly")] use errno_dragonfly::errno_location; +use libc::{self, c_char, c_int, size_t, strlen}; -use Errno; +use crate::Errno; -#[cfg(feature = "std")] -pub fn with_description<F, T>(err: Errno, callback: F) -> T where - F: FnOnce(Result<&str, Errno>) -> T +fn from_utf8_lossy(input: &[u8]) -> &str { + match str::from_utf8(input) { + Ok(valid) => valid, + Err(error) => unsafe { str::from_utf8_unchecked(&input[..error.valid_up_to()]) }, + } +} + +pub fn with_description<F, T>(err: Errno, callback: F) -> T +where + F: FnOnce(Result<&str, Errno>) -> T, { - let mut buf = [0 as c_char; 1024]; - unsafe { - if strerror_r(err.0, buf.as_mut_ptr(), buf.len() as libc::size_t) < 0 { + let mut buf = [0u8; 1024]; + let c_str = unsafe { + if strerror_r(err.0, buf.as_mut_ptr() as *mut _, buf.len() as size_t) < 0 { let fm_err = errno(); if fm_err != Errno(libc::ERANGE) { return callback(Err(fm_err)); } } - } - let c_str = unsafe { CStr::from_ptr(buf.as_ptr()) }; - callback(Ok(&String::from_utf8_lossy(c_str.to_bytes()))) + let c_str_len = strlen(buf.as_ptr() as *const _); + &buf[..c_str_len] + }; + callback(Ok(from_utf8_lossy(c_str))) } -#[cfg(feature = "std")] -pub const STRERROR_NAME: &'static str = "strerror_r"; +pub const STRERROR_NAME: &str = "strerror_r"; pub fn errno() -> Errno { - unsafe { - Errno(*errno_location()) - } + unsafe { Errno(*errno_location()) } } pub fn set_errno(Errno(errno): Errno) { @@ -54,26 +56,30 @@ pub fn set_errno(Errno(errno): Errno) { } } -extern { +extern "C" { #[cfg(not(target_os = "dragonfly"))] - #[cfg_attr(any(target_os = "macos", - target_os = "ios", - target_os = "freebsd"), - link_name = "__error")] - #[cfg_attr(any(target_os = "openbsd", - target_os = "netbsd", - target_os = "bitrig", - target_os = "android"), - link_name = "__errno")] - #[cfg_attr(any(target_os = "solaris", - target_os = "illumos"), - link_name = "___errno")] - #[cfg_attr(target_os = "linux", - link_name = "__errno_location")] + #[cfg_attr( + any(target_os = "macos", target_os = "ios", target_os = "freebsd"), + link_name = "__error" + )] + #[cfg_attr( + any( + target_os = "openbsd", + target_os = "netbsd", + target_os = "bitrig", + target_os = "android" + ), + link_name = "__errno" + )] + #[cfg_attr( + any(target_os = "solaris", target_os = "illumos"), + link_name = "___errno" + )] + #[cfg_attr(target_os = "haiku", link_name = "_errnop")] + #[cfg_attr(target_os = "linux", link_name = "__errno_location")] + #[cfg_attr(target_os = "aix", link_name = "_Errno")] fn errno_location() -> *mut c_int; - #[cfg(feature = "std")] #[cfg_attr(target_os = "linux", link_name = "__xpg_strerror_r")] - fn strerror_r(errnum: c_int, buf: *mut c_char, - buflen: libc::size_t) -> c_int; + fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t) -> c_int; } diff --git a/vendor/errno/src/wasi.rs b/vendor/errno/src/wasi.rs index 28e3d9f11..b18fa9b2c 100644 --- a/vendor/errno/src/wasi.rs +++ b/vendor/errno/src/wasi.rs @@ -12,54 +12,49 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[cfg(feature = "std")] -use std::ffi::CStr; -use libc::c_int; -#[cfg(feature = "std")] -use libc::{self, c_char}; +use core::str; +use libc::{self, c_char, c_int, size_t, strlen}; -use Errno; +use crate::Errno; -#[cfg(feature = "std")] -pub fn with_description<F, T>(err: Errno, callback: F) -> T where - F: FnOnce(Result<&str, Errno>) -> T +fn from_utf8_lossy(input: &[u8]) -> &str { + match str::from_utf8(input) { + Ok(valid) => valid, + Err(error) => unsafe { str::from_utf8_unchecked(&input[..error.valid_up_to()]) }, + } +} + +pub fn with_description<F, T>(err: Errno, callback: F) -> T +where + F: FnOnce(Result<&str, Errno>) -> T, { - let mut buf = [0 as c_char; 1024]; - unsafe { - if strerror_r(err.0, buf.as_mut_ptr(), buf.len() as libc::size_t) < 0 { + let mut buf = [0u8; 1024]; + let c_str = unsafe { + if strerror_r(err.0, buf.as_mut_ptr() as *mut _, buf.len() as size_t) < 0 { let fm_err = errno(); if fm_err != Errno(libc::ERANGE) { return callback(Err(fm_err)); } } - } - let c_str = unsafe { CStr::from_ptr(buf.as_ptr()) }; - callback(Ok(&String::from_utf8_lossy(c_str.to_bytes()))) + let c_str_len = strlen(buf.as_ptr() as *const _); + &buf[..c_str_len] + }; + callback(Ok(from_utf8_lossy(c_str))) } -#[cfg(feature = "std")] -pub const STRERROR_NAME: &'static str = "strerror_r"; +pub const STRERROR_NAME: &str = "strerror_r"; pub fn errno() -> Errno { - // libc_errno is thread-local, so simply read its value. - unsafe { - Errno(libc_errno) - } + unsafe { Errno(*__errno_location()) } } pub fn set_errno(Errno(new_errno): Errno) { - // libc_errno is thread-local, so simply assign to it. unsafe { - libc_errno = new_errno; + *__errno_location() = new_errno; } } -extern { - #[thread_local] - #[link_name = "errno"] - static mut libc_errno: c_int; - - #[cfg(feature = "std")] - fn strerror_r(errnum: c_int, buf: *mut c_char, - buflen: libc::size_t) -> c_int; +extern "C" { + fn __errno_location() -> *mut c_int; + fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t) -> c_int; } diff --git a/vendor/errno/src/windows.rs b/vendor/errno/src/windows.rs index 94e736210..9c7c0e400 100644 --- a/vendor/errno/src/windows.rs +++ b/vendor/errno/src/windows.rs @@ -12,59 +12,70 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[cfg(feature = "std")] -use std::ptr; -use winapi::shared::minwindef::DWORD; -#[cfg(feature = "std")] -use winapi::shared::ntdef::WCHAR; -#[cfg(feature = "std")] -use winapi::um::winbase::{FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS}; +use core::char::{self, REPLACEMENT_CHARACTER}; +use core::ptr; +use core::str; +use windows_sys::Win32::Foundation::{GetLastError, SetLastError, WIN32_ERROR}; +use windows_sys::Win32::System::Diagnostics::Debug::{ + FormatMessageW, FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS, +}; -use Errno; +use crate::Errno; -#[cfg(feature = "std")] -pub fn with_description<F, T>(err: Errno, callback: F) -> T where - F: FnOnce(Result<&str, Errno>) -> T +fn from_utf16_lossy<'a>(input: &[u16], output: &'a mut [u8]) -> &'a str { + let mut output_len = 0; + for c in char::decode_utf16(input.iter().copied().take_while(|&x| x != 0)) + .map(|x| x.unwrap_or(REPLACEMENT_CHARACTER)) + { + let c_len = c.len_utf8(); + if c_len > output.len() - output_len { + break; + } + c.encode_utf8(&mut output[output_len..]); + output_len += c_len; + } + unsafe { str::from_utf8_unchecked(&output[..output_len]) } +} + +pub fn with_description<F, T>(err: Errno, callback: F) -> T +where + F: FnOnce(Result<&str, Errno>) -> T, { // This value is calculated from the macro // MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT) - let lang_id = 0x0800 as DWORD; + let lang_id = 0x0800_u32; - let mut buf = [0 as WCHAR; 2048]; + let mut buf = [0u16; 2048]; unsafe { - let res = ::winapi::um::winbase::FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - ptr::null_mut(), - err.0 as DWORD, - lang_id, - buf.as_mut_ptr(), - buf.len() as DWORD, - ptr::null_mut()); + let res = FormatMessageW( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + ptr::null_mut(), + err.0 as u32, + lang_id, + buf.as_mut_ptr(), + buf.len() as u32, + ptr::null_mut(), + ); if res == 0 { // Sometimes FormatMessageW can fail e.g. system doesn't like lang_id let fm_err = errno(); return callback(Err(fm_err)); } - let msg = String::from_utf16_lossy(&buf[..res as usize]); + let mut msg = [0u8; 2048]; + let msg = from_utf16_lossy(&buf[..res as usize], &mut msg[..]); // Trim trailing CRLF inserted by FormatMessageW - #[allow(deprecated)] // TODO: remove when MSRV >= 1.30 - callback(Ok(msg.trim_right())) + callback(Ok(msg.trim_end())) } } -#[cfg(feature = "std")] -pub const STRERROR_NAME: &'static str = "FormatMessageW"; +pub const STRERROR_NAME: &str = "FormatMessageW"; pub fn errno() -> Errno { - unsafe { - Errno(::winapi::um::errhandlingapi::GetLastError() as i32) - } + unsafe { Errno(GetLastError() as i32) } } pub fn set_errno(Errno(errno): Errno) { - unsafe { - ::winapi::um::errhandlingapi::SetLastError(errno as DWORD) - } + unsafe { SetLastError(errno as WIN32_ERROR) } } |