summaryrefslogtreecommitdiffstats
path: root/vendor/errno/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/errno/src
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/errno/src')
-rw-r--r--vendor/errno/src/hermit.rs32
-rw-r--r--vendor/errno/src/lib.rs151
-rw-r--r--vendor/errno/src/unix.rs79
-rw-r--r--vendor/errno/src/wasi.rs65
-rw-r--r--vendor/errno/src/windows.rs70
5 files changed, 397 insertions, 0 deletions
diff --git a/vendor/errno/src/hermit.rs b/vendor/errno/src/hermit.rs
new file mode 100644
index 000000000..d14a6c88f
--- /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 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: &'static 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 000000000..b3a1dcb1a
--- /dev/null
+++ b/vendor/errno/src/lib.rs
@@ -0,0 +1,151 @@
+//! 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(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;
+
+#[cfg(feature = "std")]
+use std::fmt;
+#[cfg(feature = "std")]
+use std::io;
+#[cfg(feature = "std")]
+use std::error::Error;
+
+/// 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);
+
+#[cfg(feature = "std")]
+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()
+ })
+ }
+}
+
+#[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),
+ Err(fm_err) => write!(
+ fmt, "OS error {} ({} returned error {})",
+ self.0, sys::STRERROR_NAME, fm_err.0),
+ })
+ }
+}
+
+impl Into<i32> for Errno {
+ fn into(self) -> i32 {
+ self.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 {
+ "Operation not permitted"
+ };
+
+ set_errno(Errno(1));
+
+ assert_eq!(errno().to_string(), expect);
+ assert_eq!(
+ format!("{:?}", errno()),
+ format!("Errno {{ code: 1, description: Some({:?}) }}", 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 000000000..820d89748
--- /dev/null
+++ b/vendor/errno/src/unix.rs
@@ -0,0 +1,79 @@
+//! 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.
+
+#[cfg(feature = "std")]
+use std::ffi::CStr;
+use libc::c_int;
+#[cfg(feature = "std")]
+use libc::{self, c_char};
+#[cfg(target_os = "dragonfly")]
+use errno_dragonfly::errno_location;
+
+use Errno;
+
+#[cfg(feature = "std")]
+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 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())))
+}
+
+#[cfg(feature = "std")]
+pub const STRERROR_NAME: &'static str = "strerror_r";
+
+pub fn errno() -> Errno {
+ unsafe {
+ Errno(*errno_location())
+ }
+}
+
+pub fn set_errno(Errno(errno): Errno) {
+ unsafe {
+ *errno_location() = errno;
+ }
+}
+
+extern {
+ #[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")]
+ 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;
+}
diff --git a/vendor/errno/src/wasi.rs b/vendor/errno/src/wasi.rs
new file mode 100644
index 000000000..28e3d9f11
--- /dev/null
+++ b/vendor/errno/src/wasi.rs
@@ -0,0 +1,65 @@
+//! 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.
+
+#[cfg(feature = "std")]
+use std::ffi::CStr;
+use libc::c_int;
+#[cfg(feature = "std")]
+use libc::{self, c_char};
+
+use Errno;
+
+#[cfg(feature = "std")]
+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 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())))
+}
+
+#[cfg(feature = "std")]
+pub const STRERROR_NAME: &'static str = "strerror_r";
+
+pub fn errno() -> Errno {
+ // libc_errno is thread-local, so simply read its value.
+ unsafe {
+ Errno(libc_errno)
+ }
+}
+
+pub fn set_errno(Errno(new_errno): Errno) {
+ // libc_errno is thread-local, so simply assign to it.
+ unsafe {
+ libc_errno = 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;
+}
diff --git a/vendor/errno/src/windows.rs b/vendor/errno/src/windows.rs
new file mode 100644
index 000000000..94e736210
--- /dev/null
+++ b/vendor/errno/src/windows.rs
@@ -0,0 +1,70 @@
+//! 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.
+
+#[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 Errno;
+
+#[cfg(feature = "std")]
+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 mut buf = [0 as WCHAR; 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());
+ 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]);
+ // Trim trailing CRLF inserted by FormatMessageW
+ #[allow(deprecated)] // TODO: remove when MSRV >= 1.30
+ callback(Ok(msg.trim_right()))
+ }
+}
+
+#[cfg(feature = "std")]
+pub const STRERROR_NAME: &'static str = "FormatMessageW";
+
+pub fn errno() -> Errno {
+ unsafe {
+ Errno(::winapi::um::errhandlingapi::GetLastError() as i32)
+ }
+}
+
+pub fn set_errno(Errno(errno): Errno) {
+ unsafe {
+ ::winapi::um::errhandlingapi::SetLastError(errno as DWORD)
+ }
+}