From 6511d02414ec9dd4bcaa237336c5f74869e6c41a Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sun, 6 Jun 2021 10:43:02 -0600 Subject: Overhaul Nix's error types For many of Nix's consumers it be convenient to easily convert a Nix error into a std::io::Error. That's currently not possible because of the InvalidPath, InvalidUtf8, and UnsupportedOperation types that have no equivalent in std::io::Error. However, very few of Nix's public APIs actually return those unusual errors. So a more useful API would be for Nix's standard error type to implement Into. This commit makes Error a simple NewType around Errno. For most functions it's a drop-in replacement. There are only three exceptions: * clearenv now returns a bespoke error type. It was the only Nix function whose error couldn't be cleanly mapped onto an Errno. * sys::signal::signal now returns Error(Errno::ENOTSUP) instead of Error::UnsupportedOperation when the user passes an incompatible argument to `handler`. * When a NixPath exceeds PATH_MAX, it will now return Error(Errno::ENAMETOOLONG) instead of Error::InvalidPath. In the latter two cases there is now some abiguity about whether the error code was generated by Nix or by the OS. But I think the ambiguity is worth it for the sake of being able to implement Into. This commit also introduces Error::Sys() as a migration aid. Previously that as an enum variant. Now it's a function, but it will work in many of the same contexts as the original. Fixes #1155 --- test/sys/test_ioctl.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'test/sys/test_ioctl.rs') diff --git a/test/sys/test_ioctl.rs b/test/sys/test_ioctl.rs index ddb86968..d65e43ac 100644 --- a/test/sys/test_ioctl.rs +++ b/test/sys/test_ioctl.rs @@ -167,15 +167,15 @@ mod linux_ioctls { use tempfile::tempfile; use libc::{TCGETS, TCSBRK, TCSETS, TIOCNXCL, termios}; - use nix::Error::Sys; - use nix::errno::Errno::{ENOTTY, ENOSYS}; + use nix::Error; + use nix::errno::Errno; ioctl_none_bad!(tiocnxcl, TIOCNXCL); #[test] fn test_ioctl_none_bad() { let file = tempfile().unwrap(); let res = unsafe { tiocnxcl(file.as_raw_fd()) }; - assert_eq!(res, Err(Sys(ENOTTY))); + assert_eq!(res, Err(Error(Errno::ENOTTY))); } ioctl_read_bad!(tcgets, TCGETS, termios); @@ -184,7 +184,7 @@ mod linux_ioctls { let file = tempfile().unwrap(); let mut termios = unsafe { mem::zeroed() }; let res = unsafe { tcgets(file.as_raw_fd(), &mut termios) }; - assert_eq!(res, Err(Sys(ENOTTY))); + assert_eq!(res, Err(Error(Errno::ENOTTY))); } ioctl_write_int_bad!(tcsbrk, TCSBRK); @@ -192,7 +192,7 @@ mod linux_ioctls { fn test_ioctl_write_int_bad() { let file = tempfile().unwrap(); let res = unsafe { tcsbrk(file.as_raw_fd(), 0) }; - assert_eq!(res, Err(Sys(ENOTTY))); + assert_eq!(res, Err(Error(Errno::ENOTTY))); } ioctl_write_ptr_bad!(tcsets, TCSETS, termios); @@ -201,7 +201,7 @@ mod linux_ioctls { let file = tempfile().unwrap(); let termios: termios = unsafe { mem::zeroed() }; let res = unsafe { tcsets(file.as_raw_fd(), &termios) }; - assert_eq!(res, Err(Sys(ENOTTY))); + assert_eq!(res, Err(Error(Errno::ENOTTY))); } // FIXME: Find a suitable example for `ioctl_readwrite_bad` @@ -212,7 +212,7 @@ mod linux_ioctls { fn test_ioctl_none() { let file = tempfile().unwrap(); let res = unsafe { log_status(file.as_raw_fd()) }; - assert!(res == Err(Sys(ENOTTY)) || res == Err(Sys(ENOSYS))); + assert!(res == Err(Error(Errno::ENOTTY)) || res == Err(Error(Errno::ENOSYS))); } #[repr(C)] @@ -231,7 +231,7 @@ mod linux_ioctls { let file = tempfile().unwrap(); let data: v4l2_audio = unsafe { mem::zeroed() }; let res = unsafe { s_audio(file.as_raw_fd(), &data) }; - assert!(res == Err(Sys(ENOTTY)) || res == Err(Sys(ENOSYS))); + assert!(res == Err(Error(Errno::ENOTTY)) || res == Err(Error(Errno::ENOSYS))); } // From linux/net/bluetooth/hci_sock.h @@ -242,7 +242,7 @@ mod linux_ioctls { fn test_ioctl_write_int() { let file = tempfile().unwrap(); let res = unsafe { hcidevup(file.as_raw_fd(), 0) }; - assert!(res == Err(Sys(ENOTTY)) || res == Err(Sys(ENOSYS))); + assert!(res == Err(Error(Errno::ENOTTY)) || res == Err(Error(Errno::ENOSYS))); } // From linux/videodev2.h @@ -252,7 +252,7 @@ mod linux_ioctls { let file = tempfile().unwrap(); let mut data: v4l2_audio = unsafe { mem::zeroed() }; let res = unsafe { g_audio(file.as_raw_fd(), &mut data) }; - assert!(res == Err(Sys(ENOTTY)) || res == Err(Sys(ENOSYS))); + assert!(res == Err(Error(Errno::ENOTTY)) || res == Err(Error(Errno::ENOSYS))); } // From linux/videodev2.h @@ -262,7 +262,7 @@ mod linux_ioctls { let file = tempfile().unwrap(); let mut data: v4l2_audio = unsafe { mem::zeroed() }; let res = unsafe { enum_audio(file.as_raw_fd(), &mut data) }; - assert!(res == Err(Sys(ENOTTY)) || res == Err(Sys(ENOSYS))); + assert!(res == Err(Error(Errno::ENOTTY)) || res == Err(Error(Errno::ENOSYS))); } // FIXME: Find a suitable example for `ioctl_read_buf`. @@ -288,7 +288,7 @@ mod linux_ioctls { let file = tempfile().unwrap(); let data: [spi_ioc_transfer; 4] = unsafe { mem::zeroed() }; let res = unsafe { spi_ioc_message(file.as_raw_fd(), &data[..]) }; - assert!(res == Err(Sys(ENOTTY)) || res == Err(Sys(ENOSYS))); + assert!(res == Err(Error(Errno::ENOTTY)) || res == Err(Error(Errno::ENOSYS))); } // FIXME: Find a suitable example for `ioctl_readwrite_buf`. @@ -302,8 +302,8 @@ mod freebsd_ioctls { use tempfile::tempfile; use libc::termios; - use nix::Error::Sys; - use nix::errno::Errno::ENOTTY; + use nix::Error; + use nix::errno::Errno; // From sys/sys/ttycom.h const TTY_IOC_MAGIC: u8 = b't'; @@ -316,7 +316,7 @@ mod freebsd_ioctls { fn test_ioctl_none() { let file = tempfile().unwrap(); let res = unsafe { tiocnxcl(file.as_raw_fd()) }; - assert_eq!(res, Err(Sys(ENOTTY))); + assert_eq!(res, Err(Error(Errno::ENOTTY))); } ioctl_read!(tiocgeta, TTY_IOC_MAGIC, TTY_IOC_TYPE_GETA, termios); @@ -325,7 +325,7 @@ mod freebsd_ioctls { let file = tempfile().unwrap(); let mut termios = unsafe { mem::zeroed() }; let res = unsafe { tiocgeta(file.as_raw_fd(), &mut termios) }; - assert_eq!(res, Err(Sys(ENOTTY))); + assert_eq!(res, Err(Error(Errno::ENOTTY))); } ioctl_write_ptr!(tiocseta, TTY_IOC_MAGIC, TTY_IOC_TYPE_SETA, termios); @@ -334,6 +334,6 @@ mod freebsd_ioctls { let file = tempfile().unwrap(); let termios: termios = unsafe { mem::zeroed() }; let res = unsafe { tiocseta(file.as_raw_fd(), &termios) }; - assert_eq!(res, Err(Sys(ENOTTY))); + assert_eq!(res, Err(Error(Errno::ENOTTY))); } } -- cgit v1.2.3