Auto merge of #79965 - ijackson:moreerrnos, r=joshtriplett

More ErrorKinds for common errnos

From the commit message of the main commit here (as revised):

```
There are a number of IO error situations which it would be very
useful for Rust code to be able to recognise without having to resort
to OS-specific code.  Taking some Unix examples, `ENOTEMPTY` and
`EXDEV` have obvious recovery strategies.  Recently I was surprised to
discover that `ENOSPC` came out as `ErrorKind::Other`.

Since I am familiar with Unix I reviwed the list of errno values in
  https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html

Here, I add those that most clearly seem to be needed.

`@CraftSpider` provided information about Windows, and references, which
I have tried to take into account.

This has to be insta-stable because we can't sensibly have a different
set of ErrorKinds depending on a std feature flag.

I have *not* added these to the mapping tables for any operating
systems other than Unix and Windows.  I hope that it is OK to add them
now for Unix and Windows now, and maybe add them to other OS's mapping
tables as and when someone on that OS is able to consider the
situation.

I adopted the general principle that it was usually a bad idea to map
two distinct error values to the same Rust error code.  I notice that
this principle is already violated in the case of `EACCES` and
`EPERM`, which both map to `PermissionDenied`.  I think this was
probably a mistake but it would be quite hard to change now, so I
don't propose to do anything about that.

However, for Windows, there are sometimes different error codes for
identical situations.  Eg there are WSA* versions of some error
codes as well as ERROR_* ones.  Also Windows seems to have a great
many more erorr codes.  I don't know precisely what best practice
would be for Windows.
```

<strike>

```
Errno values I wasn't sure about so *haven't* included:

EMFILE ENFILE ENOBUFS ENOLCK:

  These are all fairly Unix-specific resource exhaustion situations.
  In practice it seemed not very likely to me that anyone would want
  to handle these differently to `Other`.

ENOMEM ERANGE EDOM EOVERFLOW

  Normally these don't get exposed to the Rust callers I hope.  They
  don't tend to come out of filesystem APIs.

EILSEQ

  Hopefully Rust libraries open files in binary mode and do the
  converstion in Rust.  So Rust code ought not to be exposed to
  EILSEQ.

EIO

  The range of things that could cause this is troublesome.  I found
  it difficult to describe.  I do think it would be useful to add this
  at some point, because EIO on a filesystem operation is much more
  serious than most other errors.

ENETDOWN

  I wasn't sure if this was useful or, indeed, if any modern systems
  use it.

ENOEXEC

  It is not clear to me how a Rust program could respond to this.  It
  seems rather niche.

EPROTO ENETRESET ENODATA ENOMSG ENOPROTOOPT ENOSR ENOSTR ETIME
ENOTRECOVERABLE EOWNERDEAD EBADMSG EPROTONOSUPPORT EPROTOTYPE EIDRM

  These are network or STREAMS related errors which I have never in
  my own Unix programming found the need to do anything with.  I think
  someone who understands these better should be the one to try to
  find good Rust names and descriptions for them.

ENOTTY ENXIO ENODEV EOPNOTSUPP ESRCH EALREADY ECANCELED ECHILD
EINPROGRESS

  These are very hard to get unless you're already doing something
  very Unix-specific, in which case the raw_os_error interface is
  probably more suitable than relying on the Rust ErrorKind mapping.

EFAULT EBADF

  These would seem to be the result of application UB.
```
</strike>
<i>(omitted errnos are discussed below, especially in https://github.com/rust-lang/rust/pull/79965#issuecomment-810468334)
This commit is contained in:
bors 2021-07-03 04:12:36 +00:00
commit fdd9a07147
5 changed files with 2107 additions and 109 deletions

View file

@ -105,6 +105,12 @@ pub enum ErrorKind {
/// The connection was reset by the remote server.
#[stable(feature = "rust1", since = "1.0.0")]
ConnectionReset,
/// The remote host is not reachable.
#[unstable(feature = "io_error_more", issue = "86442")]
HostUnreachable,
/// The network containing the remote host is not reachable.
#[unstable(feature = "io_error_more", issue = "86442")]
NetworkUnreachable,
/// The connection was aborted (terminated) by the remote server.
#[stable(feature = "rust1", since = "1.0.0")]
ConnectionAborted,
@ -119,6 +125,9 @@ pub enum ErrorKind {
/// local.
#[stable(feature = "rust1", since = "1.0.0")]
AddrNotAvailable,
/// The system's networking is down.
#[unstable(feature = "io_error_more", issue = "86442")]
NetworkDown,
/// The operation failed because a pipe was closed.
#[stable(feature = "rust1", since = "1.0.0")]
BrokenPipe,
@ -129,6 +138,38 @@ pub enum ErrorKind {
/// requested to not occur.
#[stable(feature = "rust1", since = "1.0.0")]
WouldBlock,
/// A filesystem object is, unexpectedly, not a directory.
///
/// For example, a filesystem path was specified where one of the intermediate directory
/// components was, in fact, a plain file.
#[unstable(feature = "io_error_more", issue = "86442")]
NotADirectory,
/// The filesystem object is, unexpectedly, a directory.
///
/// A directory was specified when a non-directory was expected.
#[unstable(feature = "io_error_more", issue = "86442")]
IsADirectory,
/// A non-empty directory was specified where an empty directory was expected.
#[unstable(feature = "io_error_more", issue = "86442")]
DirectoryNotEmpty,
/// The filesystem or storage medium is read-only, but a write operation was attempted.
#[unstable(feature = "io_error_more", issue = "86442")]
ReadOnlyFilesystem,
/// Loop in the filesystem or IO subsystem; often, too many levels of symbolic links.
///
/// There was a loop (or excessively long chain) resolving a filesystem object
/// or file IO object.
///
/// On Unix this is usually the result of a symbolic link loop; or, of exceeding the
/// system-specific limit on the depth of symlink traversal.
#[unstable(feature = "io_error_more", issue = "86442")]
FilesystemLoop,
/// Stale network file handle.
///
/// With some network filesystems, notably NFS, an open file (or directory) can be invalidated
/// by problems with the network or server.
#[unstable(feature = "io_error_more", issue = "86442")]
StaleNetworkFileHandle,
/// A parameter was incorrect.
#[stable(feature = "rust1", since = "1.0.0")]
InvalidInput,
@ -158,6 +199,62 @@ pub enum ErrorKind {
/// [`Ok(0)`]: Ok
#[stable(feature = "rust1", since = "1.0.0")]
WriteZero,
/// The underlying storage (typically, a filesystem) is full.
///
/// This does not include out of quota errors.
#[unstable(feature = "io_error_more", issue = "86442")]
StorageFull,
/// Seek on unseekable file.
///
/// Seeking was attempted on an open file handle which is not suitable for seeking - for
/// example, on Unix, a named pipe opened with `File::open`.
#[unstable(feature = "io_error_more", issue = "86442")]
NotSeekable,
/// Filesystem quota was exceeded.
#[unstable(feature = "io_error_more", issue = "86442")]
FilesystemQuotaExceeded,
/// File larger than allowed or supported.
///
/// This might arise from a hard limit of the underlying filesystem or file access API, or from
/// an administratively imposed resource limitation. Simple disk full, and out of quota, have
/// their own errors.
#[unstable(feature = "io_error_more", issue = "86442")]
FileTooLarge,
/// Resource is busy.
#[unstable(feature = "io_error_more", issue = "86442")]
ResourceBusy,
/// Executable file is busy.
///
/// An attempt was made to write to a file which is also in use as a running program. (Not all
/// operating systems detect this situation.)
#[unstable(feature = "io_error_more", issue = "86442")]
ExecutableFileBusy,
/// Deadlock (avoided).
///
/// A file locking operation would result in deadlock. This situation is typically detected, if
/// at all, on a best-effort basis.
#[unstable(feature = "io_error_more", issue = "86442")]
Deadlock,
/// Cross-device or cross-filesystem (hard) link or rename.
#[unstable(feature = "io_error_more", issue = "86442")]
CrossesDevices,
/// Too many (hard) links to the same filesystem object.
///
/// The filesystem does not support making so many hardlinks to the same file.
#[unstable(feature = "io_error_more", issue = "86442")]
TooManyLinks,
/// Filename too long.
///
/// The limit might be from the underlying filesystem or API, or an administratively imposed
/// resource limit.
#[unstable(feature = "io_error_more", issue = "86442")]
FilenameTooLong,
/// Program argument list too long.
///
/// When trying to run an external program, a system or process limit on the size of the
/// arguments would have been exceeded.
#[unstable(feature = "io_error_more", issue = "86442")]
ArgumentListTooLong,
/// This operation was interrupted.
///
/// Interrupted operations can typically be retried.
@ -209,28 +306,49 @@ pub enum ErrorKind {
impl ErrorKind {
pub(crate) fn as_str(&self) -> &'static str {
use ErrorKind::*;
match *self {
ErrorKind::NotFound => "entity not found",
ErrorKind::PermissionDenied => "permission denied",
ErrorKind::ConnectionRefused => "connection refused",
ErrorKind::ConnectionReset => "connection reset",
ErrorKind::ConnectionAborted => "connection aborted",
ErrorKind::NotConnected => "not connected",
ErrorKind::AddrInUse => "address in use",
ErrorKind::AddrNotAvailable => "address not available",
ErrorKind::BrokenPipe => "broken pipe",
ErrorKind::AlreadyExists => "entity already exists",
ErrorKind::WouldBlock => "operation would block",
ErrorKind::InvalidInput => "invalid input parameter",
ErrorKind::InvalidData => "invalid data",
ErrorKind::TimedOut => "timed out",
ErrorKind::WriteZero => "write zero",
ErrorKind::Interrupted => "operation interrupted",
ErrorKind::UnexpectedEof => "unexpected end of file",
ErrorKind::Unsupported => "unsupported",
ErrorKind::OutOfMemory => "out of memory",
ErrorKind::Other => "other error",
ErrorKind::Uncategorized => "uncategorized error",
AddrInUse => "address in use",
AddrNotAvailable => "address not available",
AlreadyExists => "entity already exists",
ArgumentListTooLong => "argument list too long",
BrokenPipe => "broken pipe",
ResourceBusy => "resource busy",
ConnectionAborted => "connection aborted",
ConnectionRefused => "connection refused",
ConnectionReset => "connection reset",
CrossesDevices => "cross-device link or rename",
Deadlock => "deadlock",
DirectoryNotEmpty => "directory not empty",
ExecutableFileBusy => "executable file busy",
FilenameTooLong => "filename too long",
FilesystemQuotaExceeded => "filesystem quota exceeded",
FileTooLarge => "file too large",
HostUnreachable => "host unreachable",
Interrupted => "operation interrupted",
InvalidData => "invalid data",
InvalidInput => "invalid input parameter",
IsADirectory => "is a directory",
NetworkDown => "network down",
NetworkUnreachable => "network unreachable",
NotADirectory => "not a directory",
StorageFull => "no storage space",
NotConnected => "not connected",
NotFound => "entity not found",
Other => "other error",
OutOfMemory => "out of memory",
PermissionDenied => "permission denied",
ReadOnlyFilesystem => "read-only filesystem or storage medium",
StaleNetworkFileHandle => "stale network file handle",
FilesystemLoop => "filesystem loop or indirection limit (e.g. symlink loop)",
NotSeekable => "seek on unseekable file",
TimedOut => "timed out",
TooManyLinks => "too many links",
Uncategorized => "uncategorized error",
UnexpectedEof => "unexpected end of file",
Unsupported => "unsupported",
WouldBlock => "operation would block",
WriteZero => "write zero",
}
}
}

View file

@ -133,29 +133,51 @@ pub use crate::sys::android::signal;
pub use libc::signal;
pub fn decode_error_kind(errno: i32) -> ErrorKind {
use ErrorKind::*;
match errno as libc::c_int {
libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
libc::ECONNRESET => ErrorKind::ConnectionReset,
libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
libc::EPIPE => ErrorKind::BrokenPipe,
libc::ENOTCONN => ErrorKind::NotConnected,
libc::ECONNABORTED => ErrorKind::ConnectionAborted,
libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
libc::EADDRINUSE => ErrorKind::AddrInUse,
libc::ENOENT => ErrorKind::NotFound,
libc::EINTR => ErrorKind::Interrupted,
libc::EINVAL => ErrorKind::InvalidInput,
libc::ETIMEDOUT => ErrorKind::TimedOut,
libc::EEXIST => ErrorKind::AlreadyExists,
libc::ENOSYS => ErrorKind::Unsupported,
libc::ENOMEM => ErrorKind::OutOfMemory,
libc::E2BIG => ArgumentListTooLong,
libc::EADDRINUSE => AddrInUse,
libc::EADDRNOTAVAIL => AddrNotAvailable,
libc::EBUSY => ResourceBusy,
libc::ECONNABORTED => ConnectionAborted,
libc::ECONNREFUSED => ConnectionRefused,
libc::ECONNRESET => ConnectionReset,
libc::EDEADLK => Deadlock,
libc::EDQUOT => FilesystemQuotaExceeded,
libc::EEXIST => AlreadyExists,
libc::EFBIG => FileTooLarge,
libc::EHOSTUNREACH => HostUnreachable,
libc::EINTR => Interrupted,
libc::EINVAL => InvalidInput,
libc::EISDIR => IsADirectory,
libc::ELOOP => FilesystemLoop,
libc::ENOENT => NotFound,
libc::ENOMEM => OutOfMemory,
libc::ENOSPC => StorageFull,
libc::ENOSYS => Unsupported,
libc::EMLINK => TooManyLinks,
libc::ENAMETOOLONG => FilenameTooLong,
libc::ENETDOWN => NetworkDown,
libc::ENETUNREACH => NetworkUnreachable,
libc::ENOTCONN => NotConnected,
libc::ENOTDIR => NotADirectory,
libc::ENOTEMPTY => DirectoryNotEmpty,
libc::EPIPE => BrokenPipe,
libc::EROFS => ReadOnlyFilesystem,
libc::ESPIPE => NotSeekable,
libc::ESTALE => StaleNetworkFileHandle,
libc::ETIMEDOUT => TimedOut,
libc::ETXTBSY => ExecutableFileBusy,
libc::EXDEV => CrossesDevices,
libc::EACCES | libc::EPERM => PermissionDenied,
// These two constants can have the same value on some systems,
// but different values on others, so we can't use a match
// clause
x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => ErrorKind::WouldBlock,
x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => WouldBlock,
_ => ErrorKind::Uncategorized,
_ => Uncategorized,
}
}

View file

@ -10,6 +10,10 @@ use crate::ptr;
use libc::{c_void, size_t, wchar_t};
#[path = "c/errors.rs"] // c.rs is included from two places so we need to specify this
mod errors;
pub use errors::*;
pub use self::EXCEPTION_DISPOSITION::*;
pub use self::FILE_INFO_BY_HANDLE_CLASS::*;
@ -134,19 +138,6 @@ pub const WSASYS_STATUS_LEN: usize = 128;
pub const WSAPROTOCOL_LEN: DWORD = 255;
pub const INVALID_SOCKET: SOCKET = !0;
pub const WSAEACCES: c_int = 10013;
pub const WSAEINVAL: c_int = 10022;
pub const WSAEWOULDBLOCK: c_int = 10035;
pub const WSAEPROTOTYPE: c_int = 10041;
pub const WSAEADDRINUSE: c_int = 10048;
pub const WSAEADDRNOTAVAIL: c_int = 10049;
pub const WSAECONNABORTED: c_int = 10053;
pub const WSAECONNRESET: c_int = 10054;
pub const WSAENOTCONN: c_int = 10057;
pub const WSAESHUTDOWN: c_int = 10058;
pub const WSAETIMEDOUT: c_int = 10060;
pub const WSAECONNREFUSED: c_int = 10061;
pub const MAX_PROTOCOL_CHAIN: DWORD = 7;
pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;
@ -166,42 +157,6 @@ pub const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
pub const PROGRESS_CONTINUE: DWORD = 0;
// List of Windows system error codes with descriptions:
// https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes#system-error-codes
pub const ERROR_FILE_NOT_FOUND: DWORD = 2;
pub const ERROR_PATH_NOT_FOUND: DWORD = 3;
pub const ERROR_ACCESS_DENIED: DWORD = 5;
pub const ERROR_INVALID_HANDLE: DWORD = 6;
pub const ERROR_NOT_ENOUGH_MEMORY: DWORD = 8;
pub const ERROR_OUTOFMEMORY: DWORD = 14;
pub const ERROR_NO_MORE_FILES: DWORD = 18;
pub const ERROR_SHARING_VIOLATION: u32 = 32;
pub const ERROR_HANDLE_EOF: DWORD = 38;
pub const ERROR_FILE_EXISTS: DWORD = 80;
pub const ERROR_INVALID_PARAMETER: DWORD = 87;
pub const ERROR_BROKEN_PIPE: DWORD = 109;
pub const ERROR_CALL_NOT_IMPLEMENTED: DWORD = 120;
pub const ERROR_SEM_TIMEOUT: DWORD = 121;
pub const ERROR_INSUFFICIENT_BUFFER: DWORD = 122;
pub const ERROR_ALREADY_EXISTS: DWORD = 183;
pub const ERROR_ENVVAR_NOT_FOUND: DWORD = 203;
pub const ERROR_NO_DATA: DWORD = 232;
pub const ERROR_DRIVER_CANCEL_TIMEOUT: DWORD = 594;
pub const ERROR_OPERATION_ABORTED: DWORD = 995;
pub const ERROR_IO_PENDING: DWORD = 997;
pub const ERROR_SERVICE_REQUEST_TIMEOUT: DWORD = 1053;
pub const ERROR_COUNTER_TIMEOUT: DWORD = 1121;
pub const ERROR_TIMEOUT: DWORD = 1460;
pub const ERROR_RESOURCE_CALL_TIMED_OUT: DWORD = 5910;
pub const ERROR_CTX_MODEM_RESPONSE_TIMEOUT: DWORD = 7012;
pub const ERROR_CTX_CLIENT_QUERY_TIMEOUT: DWORD = 7040;
pub const FRS_ERR_SYSVOL_POPULATE_TIMEOUT: DWORD = 8014;
pub const ERROR_DS_TIMELIMIT_EXCEEDED: DWORD = 8226;
pub const DNS_ERROR_RECORD_TIMED_OUT: DWORD = 9705;
pub const ERROR_IPSEC_IKE_TIMED_OUT: DWORD = 13805;
pub const ERROR_RUNLEVEL_SWITCH_TIMEOUT: DWORD = 15402;
pub const ERROR_RUNLEVEL_SWITCH_AGENT_TIMEOUT: DWORD = 15403;
pub const E_NOTIMPL: HRESULT = 0x80004001u32 as HRESULT;
pub const INVALID_HANDLE_VALUE: HANDLE = !0 as HANDLE;

File diff suppressed because it is too large Load diff

View file

@ -61,16 +61,18 @@ pub unsafe fn cleanup() {
}
pub fn decode_error_kind(errno: i32) -> ErrorKind {
use ErrorKind::*;
match errno as c::DWORD {
c::ERROR_ACCESS_DENIED => return ErrorKind::PermissionDenied,
c::ERROR_ALREADY_EXISTS => return ErrorKind::AlreadyExists,
c::ERROR_FILE_EXISTS => return ErrorKind::AlreadyExists,
c::ERROR_BROKEN_PIPE => return ErrorKind::BrokenPipe,
c::ERROR_FILE_NOT_FOUND => return ErrorKind::NotFound,
c::ERROR_PATH_NOT_FOUND => return ErrorKind::NotFound,
c::ERROR_NO_DATA => return ErrorKind::BrokenPipe,
c::ERROR_INVALID_PARAMETER => return ErrorKind::InvalidInput,
c::ERROR_NOT_ENOUGH_MEMORY | c::ERROR_OUTOFMEMORY => return ErrorKind::OutOfMemory,
c::ERROR_ACCESS_DENIED => return PermissionDenied,
c::ERROR_ALREADY_EXISTS => return AlreadyExists,
c::ERROR_FILE_EXISTS => return AlreadyExists,
c::ERROR_BROKEN_PIPE => return BrokenPipe,
c::ERROR_FILE_NOT_FOUND => return NotFound,
c::ERROR_PATH_NOT_FOUND => return NotFound,
c::ERROR_NO_DATA => return BrokenPipe,
c::ERROR_INVALID_PARAMETER => return InvalidInput,
c::ERROR_NOT_ENOUGH_MEMORY | c::ERROR_OUTOFMEMORY => return OutOfMemory,
c::ERROR_SEM_TIMEOUT
| c::WAIT_TIMEOUT
| c::ERROR_DRIVER_CANCEL_TIMEOUT
@ -86,24 +88,42 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
| c::DNS_ERROR_RECORD_TIMED_OUT
| c::ERROR_IPSEC_IKE_TIMED_OUT
| c::ERROR_RUNLEVEL_SWITCH_TIMEOUT
| c::ERROR_RUNLEVEL_SWITCH_AGENT_TIMEOUT => return ErrorKind::TimedOut,
c::ERROR_CALL_NOT_IMPLEMENTED => return ErrorKind::Unsupported,
| c::ERROR_RUNLEVEL_SWITCH_AGENT_TIMEOUT => return TimedOut,
c::ERROR_CALL_NOT_IMPLEMENTED => return Unsupported,
c::ERROR_HOST_UNREACHABLE => return HostUnreachable,
c::ERROR_NETWORK_UNREACHABLE => return NetworkUnreachable,
c::ERROR_DIRECTORY => return NotADirectory,
c::ERROR_DIRECTORY_NOT_SUPPORTED => return IsADirectory,
c::ERROR_DIR_NOT_EMPTY => return DirectoryNotEmpty,
c::ERROR_WRITE_PROTECT => return ReadOnlyFilesystem,
c::ERROR_DISK_FULL | c::ERROR_HANDLE_DISK_FULL => return StorageFull,
c::ERROR_SEEK_ON_DEVICE => return NotSeekable,
c::ERROR_DISK_QUOTA_EXCEEDED => return FilesystemQuotaExceeded,
c::ERROR_FILE_TOO_LARGE => return FileTooLarge,
c::ERROR_BUSY => return ResourceBusy,
c::ERROR_POSSIBLE_DEADLOCK => return Deadlock,
c::ERROR_NOT_SAME_DEVICE => return CrossesDevices,
c::ERROR_TOO_MANY_LINKS => return TooManyLinks,
c::ERROR_FILENAME_EXCED_RANGE => return FilenameTooLong,
_ => {}
}
match errno {
c::WSAEACCES => ErrorKind::PermissionDenied,
c::WSAEADDRINUSE => ErrorKind::AddrInUse,
c::WSAEADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
c::WSAECONNABORTED => ErrorKind::ConnectionAborted,
c::WSAECONNREFUSED => ErrorKind::ConnectionRefused,
c::WSAECONNRESET => ErrorKind::ConnectionReset,
c::WSAEINVAL => ErrorKind::InvalidInput,
c::WSAENOTCONN => ErrorKind::NotConnected,
c::WSAEWOULDBLOCK => ErrorKind::WouldBlock,
c::WSAETIMEDOUT => ErrorKind::TimedOut,
c::WSAEACCES => PermissionDenied,
c::WSAEADDRINUSE => AddrInUse,
c::WSAEADDRNOTAVAIL => AddrNotAvailable,
c::WSAECONNABORTED => ConnectionAborted,
c::WSAECONNREFUSED => ConnectionRefused,
c::WSAECONNRESET => ConnectionReset,
c::WSAEINVAL => InvalidInput,
c::WSAENOTCONN => NotConnected,
c::WSAEWOULDBLOCK => WouldBlock,
c::WSAETIMEDOUT => TimedOut,
c::WSAEHOSTUNREACH => HostUnreachable,
c::WSAENETDOWN => NetworkDown,
c::WSAENETUNREACH => NetworkUnreachable,
_ => ErrorKind::Uncategorized,
_ => Uncategorized,
}
}