Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More ErrorKinds for common errnos #79965

Merged
merged 17 commits into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ErrorKind: Provide many more ErrorKinds, motivated by Unix errnos
Rationale for the mappings etc. is extensively discussed in the MR
  #79965

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
  • Loading branch information
ijackson committed Jun 18, 2021
commit 1ec9454403e9bb0361d0725524c4bd27030474cc
116 changes: 116 additions & 0 deletions library/std/src/io/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// The network containing the remote host is not reachable.
#[unstable(feature = "io_error_more", issue = "86442")]
NetworkUnreachable,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// The connection was aborted (terminated) by the remote server.
#[stable(feature = "rust1", since = "1.0.0")]
ConnectionAborted,
Expand All @@ -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,
Expand All @@ -129,6 +138,37 @@ 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,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// 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,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// A non-empty directory was specified where an empty directory was expected.
#[unstable(feature = "io_error_more", issue = "86442")]
DirectoryNotEmpty,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// The filesystem or storage medium is read-only, but a write operation was attempted.
#[unstable(feature = "io_error_more", issue = "86442")]
ReadOnlyFilesystem,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// Loop in the filesystem; often, too many levels of symbolic links.
///
/// There was a loop (or excessively long chain) resolving a filesystem 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,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// A parameter was incorrect.
#[stable(feature = "rust1", since = "1.0.0")]
InvalidInput,
Expand Down Expand Up @@ -158,6 +198,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::new`.
#[unstable(feature = "io_error_more", issue = "86442")]
NotSeekable,
/// Filesystem quota was exceeded.
#[unstable(feature = "io_error_more", issue = "86442")]
FilesystemQuotaExceeded,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// 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,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// Resource is busy.
#[unstable(feature = "io_error_more", issue = "86442")]
ResourceBusy,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// 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,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// 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,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// 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,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// 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,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std uses file_name in functions, rather than filename. It'd be odd to break correspondence between snake_case and CamelCase, so I think this should be FileNameTooLong to match. Same goes for InvalidFileName.

/// 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,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// This operation was interrupted.
///
/// Interrupted operations can typically be retried.
Expand Down Expand Up @@ -213,19 +309,39 @@ impl ErrorKind {
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 (e.g. symbolic link loop)",
NotSeekable => "seek on unseekable file",
TimedOut => "timed out",
TooManyLinks => "too many links",
Uncategorized => "uncategorized error",
UnexpectedEof => "unexpected end of file",
Unsupported => "unsupported",
Expand Down
20 changes: 20 additions & 0 deletions library/std/src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,40 @@ pub use libc::signal;
pub fn decode_error_kind(errno: i32) -> ErrorKind {
use ErrorKind::*;
match errno as libc::c_int {
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,

Expand Down
21 changes: 21 additions & 0 deletions library/std/src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
| c::ERROR_RUNLEVEL_SWITCH_TIMEOUT
| c::ERROR_RUNLEVEL_SWITCH_AGENT_TIMEOUT => return TimedOut,
c::ERROR_CALL_NOT_IMPLEMENTED => return 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,
_ => {}
}

Expand All @@ -104,6 +122,9 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
c::WSAENOTCONN => NotConnected,
c::WSAEWOULDBLOCK => WouldBlock,
c::WSAETIMEDOUT => TimedOut,
c::WSAEHOSTUNREACH => HostUnreachable,
c::WSAENETDOWN => NetworkDown,
c::WSAENETUNREACH => NetworkUnreachable,

_ => Uncategorized,
}
Expand Down