diff options
author | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
---|---|---|
committer | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
commit | 1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch) | |
tree | 7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/errno/src | |
parent | 5ecd8cf2cba827454317368b68571df0d13d7842 (diff) | |
download | fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip |
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/errno/src')
-rw-r--r-- | vendor/errno/src/hermit.rs | 32 | ||||
-rw-r--r-- | vendor/errno/src/lib.rs | 156 | ||||
-rw-r--r-- | vendor/errno/src/unix.rs | 101 | ||||
-rw-r--r-- | vendor/errno/src/wasi.rs | 60 | ||||
-rw-r--r-- | vendor/errno/src/windows.rs | 81 |
5 files changed, 430 insertions, 0 deletions
diff --git a/vendor/errno/src/hermit.rs b/vendor/errno/src/hermit.rs new file mode 100644 index 0000000..331b6b1 --- /dev/null +++ b/vendor/errno/src/hermit.rs @@ -0,0 +1,32 @@ +//! Implementation of `errno` functionality for RustyHermit. +//! +//! Currently, the error handling in RustyHermit isn't clearly +//! defined. At the current stage of RustyHermit, only a placeholder +//! is provided to be compatible to the classical errno interface. + +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use crate::Errno; + +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: &str = "strerror_r"; + +pub fn errno() -> Errno { + Errno(0) +} + +pub fn set_errno(_: Errno) {} diff --git a/vendor/errno/src/lib.rs b/vendor/errno/src/lib.rs new file mode 100644 index 0000000..20875b5 --- /dev/null +++ b/vendor/errno/src/lib.rs @@ -0,0 +1,156 @@ +//! Cross-platform interface to the `errno` variable. +//! +//! # Examples +//! ``` +//! use errno::{Errno, errno, set_errno}; +//! +//! // Get the current value of errno +//! let e = errno(); +//! +//! // Set the current value of errno +//! set_errno(e); +//! +//! // Extract the error code as an i32 +//! let code = e.0; +//! +//! // Display a human-friendly error message +//! println!("Error {}: {}", code, e); +//! ``` + +#![cfg_attr(not(feature = "std"), no_std)] + +#[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::error::Error; +#[cfg(feature = "std")] +use std::io; + +/// Wraps a platform-specific error code. +/// +/// The `Display` instance maps the code to a human-readable string. It +/// calls [`strerror_r`][1] under POSIX, and [`FormatMessageW`][2] on +/// Windows. +/// +/// [1]: http://pubs.opengroup.org/onlinepubs/009695399/functions/strerror.html +/// [2]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms679351%28v=vs.85%29.aspx +#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd, Hash)] +pub struct Errno(pub i32); + +impl fmt::Debug for Errno { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + sys::with_description(*self, |desc| { + fmt.debug_struct("Errno") + .field("code", &self.0) + .field("description", &desc.ok()) + .finish() + }) + } +} + +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), + Err(fm_err) => write!( + fmt, + "OS error {} ({} returned error {})", + self.0, + sys::STRERROR_NAME, + fm_err.0 + ), + }) + } +} + +impl From<Errno> for i32 { + fn from(e: Errno) -> Self { + e.0 + } +} + +#[cfg(feature = "std")] +impl Error for Errno { + // TODO: Remove when MSRV >= 1.27 + #[allow(deprecated)] + fn description(&self) -> &str { + "system error" + } +} + +#[cfg(feature = "std")] +impl From<Errno> for io::Error { + fn from(errno: Errno) -> Self { + io::Error::from_raw_os_error(errno.0) + } +} + +/// Returns the platform-specific value of `errno`. +pub fn errno() -> Errno { + sys::errno() +} + +/// Sets the platform-specific value of `errno`. +pub fn set_errno(err: Errno) { + sys::set_errno(err) +} + +#[test] +fn it_works() { + let x = errno(); + set_errno(x); +} + +#[cfg(feature = "std")] +#[test] +fn it_works_with_to_string() { + let x = errno(); + let _ = x.to_string(); +} + +#[cfg(feature = "std")] +#[test] +fn check_description() { + let expect = if cfg!(windows) { + "Incorrect function." + } else if cfg!(target_os = "illumos") { + "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" + }; + + 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: {}, description: Some({:?}) }}", + errno_code, expect + ) + ); +} + +#[cfg(feature = "std")] +#[test] +fn check_error_into_errno() { + const ERROR_CODE: i32 = 1; + + let error = io::Error::from_raw_os_error(ERROR_CODE); + let new_error: io::Error = Errno(ERROR_CODE).into(); + assert_eq!(error.kind(), new_error.kind()); +} diff --git a/vendor/errno/src/unix.rs b/vendor/errno/src/unix.rs new file mode 100644 index 0000000..18951e4 --- /dev/null +++ b/vendor/errno/src/unix.rs @@ -0,0 +1,101 @@ +//! Implementation of `errno` functionality for Unix systems. +//! +//! Adapted from `src/libstd/sys/unix/os.rs` in the Rust distribution. + +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use core::str; +use libc::{self, c_int, size_t, strerror_r, strlen}; + +use crate::Errno; + +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 = [0u8; 1024]; + let c_str = unsafe { + let rc = strerror_r(err.0, buf.as_mut_ptr() as *mut _, buf.len() as size_t); + if rc != 0 { + // Handle negative return codes for compatibility with glibc < 2.13 + let fm_err = match rc < 0 { + true => errno(), + false => Errno(rc), + }; + if fm_err != Errno(libc::ERANGE) { + return callback(Err(fm_err)); + } + } + let c_str_len = strlen(buf.as_ptr() as *const _); + &buf[..c_str_len] + }; + callback(Ok(from_utf8_lossy(c_str))) +} + +pub const STRERROR_NAME: &str = "strerror_r"; + +pub fn errno() -> Errno { + unsafe { Errno(*errno_location()) } +} + +pub fn set_errno(Errno(errno): Errno) { + unsafe { + *errno_location() = errno; + } +} + +extern "C" { + #[cfg_attr( + any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "freebsd" + ), + link_name = "__error" + )] + #[cfg_attr( + any( + target_os = "openbsd", + target_os = "netbsd", + target_os = "bitrig", + target_os = "android", + target_os = "espidf", + target_env = "newlib" + ), + 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( + any( + target_os = "linux", + target_os = "hurd", + target_os = "redox", + target_os = "dragonfly" + ), + link_name = "__errno_location" + )] + #[cfg_attr(target_os = "aix", link_name = "_Errno")] + #[cfg_attr(target_os = "nto", link_name = "__get_errno_ptr")] + fn errno_location() -> *mut c_int; +} diff --git a/vendor/errno/src/wasi.rs b/vendor/errno/src/wasi.rs new file mode 100644 index 0000000..404d7d5 --- /dev/null +++ b/vendor/errno/src/wasi.rs @@ -0,0 +1,60 @@ +//! Implementation of `errno` functionality for WASI. +//! +//! Adapted from `unix.rs`. + +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use core::str; +use libc::{self, c_int, size_t, strerror_r, strlen}; + +use crate::Errno; + +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 = [0u8; 1024]; + let c_str = unsafe { + let rc = strerror_r(err.0, buf.as_mut_ptr() as *mut _, buf.len() as size_t); + if rc != 0 { + let fm_err = Errno(rc); + if fm_err != Errno(libc::ERANGE) { + return callback(Err(fm_err)); + } + } + let c_str_len = strlen(buf.as_ptr() as *const _); + &buf[..c_str_len] + }; + callback(Ok(from_utf8_lossy(c_str))) +} + +pub const STRERROR_NAME: &str = "strerror_r"; + +pub fn errno() -> Errno { + unsafe { Errno(*__errno_location()) } +} + +pub fn set_errno(Errno(new_errno): Errno) { + unsafe { + *__errno_location() = new_errno; + } +} + +extern "C" { + fn __errno_location() -> *mut c_int; +} diff --git a/vendor/errno/src/windows.rs b/vendor/errno/src/windows.rs new file mode 100644 index 0000000..9c7c0e4 --- /dev/null +++ b/vendor/errno/src/windows.rs @@ -0,0 +1,81 @@ +//! Implementation of `errno` functionality for Windows. +//! +//! Adapted from `src/libstd/sys/windows/os.rs` in the Rust distribution. + +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +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 crate::Errno; + +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_u32; + + let mut buf = [0u16; 2048]; + + unsafe { + 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 mut msg = [0u8; 2048]; + let msg = from_utf16_lossy(&buf[..res as usize], &mut msg[..]); + // Trim trailing CRLF inserted by FormatMessageW + callback(Ok(msg.trim_end())) + } +} + +pub const STRERROR_NAME: &str = "FormatMessageW"; + +pub fn errno() -> Errno { + unsafe { Errno(GetLastError() as i32) } +} + +pub fn set_errno(Errno(errno): Errno) { + unsafe { SetLastError(errno as WIN32_ERROR) } +} |