diff options
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | src/sys/aio.rs | 21 | ||||
-rw-r--r-- | test/sys/test_aio.rs | 57 |
3 files changed, 75 insertions, 6 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 534dd20a..d5d4528b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). # Fixed - Fix compilation and tests for OpenBSD targets ([#688](https://github.com/nix-rust/nix/pull/688)) +- Fixed error handling in `AioCb::fsync`, `AioCb::read`, and `AioCb::write`. + It is no longer an error to drop an `AioCb` that failed to enqueue in the OS. + ([#715](https://github.com/nix-rust/nix/pull/715)) # Removed - The syscall module has been removed. This only exposed enough functionality for diff --git a/src/sys/aio.rs b/src/sys/aio.rs index 5d56f1cd..22bd3959 100644 --- a/src/sys/aio.rs +++ b/src/sys/aio.rs @@ -232,16 +232,22 @@ impl<'a> AioCb<'a> { /// An asynchronous version of `fsync`. pub fn fsync(&mut self, mode: AioFsyncMode) -> Result<()> { let p: *mut libc::aiocb = &mut self.aiocb; - self.in_progress = true; - Errno::result(unsafe { libc::aio_fsync(mode as libc::c_int, p) }).map(drop) + Errno::result(unsafe { + libc::aio_fsync(mode as libc::c_int, p) + }).map(|_| { + self.in_progress = true; + }) } /// Asynchronously reads from a file descriptor into a buffer pub fn read(&mut self) -> Result<()> { assert!(self.mutable, "Can't read into an immutable buffer"); let p: *mut libc::aiocb = &mut self.aiocb; - self.in_progress = true; - Errno::result(unsafe { libc::aio_read(p) }).map(drop) + Errno::result(unsafe { + libc::aio_read(p) + }).map(|_| { + self.in_progress = true; + }) } /// Retrieve return status of an asynchronous operation. Should only be @@ -257,8 +263,11 @@ impl<'a> AioCb<'a> { /// Asynchronously writes from a buffer to a file descriptor pub fn write(&mut self) -> Result<()> { let p: *mut libc::aiocb = &mut self.aiocb; - self.in_progress = true; - Errno::result(unsafe { libc::aio_write(p) }).map(drop) + Errno::result(unsafe { + libc::aio_write(p) + }).map(|_| { + self.in_progress = true; + }) } } diff --git a/test/sys/test_aio.rs b/test/sys/test_aio.rs index 23cab56e..67fd0850 100644 --- a/test/sys/test_aio.rs +++ b/test/sys/test_aio.rs @@ -88,6 +88,26 @@ fn test_fsync() { aiocb.aio_return().unwrap(); } +/// `AioCb::fsync` should not modify the `AioCb` object if libc::aio_fsync returns +/// an error +// Skip on Linux, because Linux's AIO implementation can't detect errors +// synchronously +#[test] +#[cfg(any(target_os = "freebsd", target_os = "macos"))] +fn test_fsync_error() { + use std::mem; + + const INITIAL: &'static [u8] = b"abcdef123456"; + // Create an invalid AioFsyncMode + let mode = unsafe { mem::transmute(666) }; + let mut f = tempfile().unwrap(); + f.write(INITIAL).unwrap(); + let mut aiocb = AioCb::from_fd( f.as_raw_fd(), + 0, //priority + SigevNotify::SigevNone); + let err = aiocb.fsync(mode); + assert!(err.is_err()); +} #[test] #[cfg_attr(all(target_env = "musl", target_arch = "x86_64"), ignore)] @@ -156,6 +176,26 @@ fn test_read() { assert!(EXPECT == rbuf.deref().deref()); } +/// `AioCb::read` should not modify the `AioCb` object if libc::aio_read returns +/// an error +// Skip on Linux, because Linux's AIO implementation can't detect errors +// synchronously +#[test] +#[cfg(any(target_os = "freebsd", target_os = "macos"))] +fn test_read_error() { + const INITIAL: &'static [u8] = b"abcdef123456"; + let rbuf = Rc::new(vec![0; 4].into_boxed_slice()); + let mut f = tempfile().unwrap(); + f.write(INITIAL).unwrap(); + let mut aiocb = AioCb::from_boxed_slice( f.as_raw_fd(), + -1, //an invalid offset + rbuf.clone(), + 0, //priority + SigevNotify::SigevNone, + LioOpcode::LIO_NOP); + assert!(aiocb.read().is_err()); +} + // Tests from_mut_slice #[test] #[cfg_attr(all(target_env = "musl", target_arch = "x86_64"), ignore)] @@ -230,6 +270,23 @@ fn test_write() { assert!(rbuf == EXPECT); } +/// `AioCb::write` should not modify the `AioCb` object if libc::aio_write returns +/// an error +// Skip on Linux, because Linux's AIO implementation can't detect errors +// synchronously +#[test] +#[cfg(any(target_os = "freebsd", target_os = "macos"))] +fn test_write_error() { + let wbuf = "CDEF".to_string().into_bytes(); + let mut aiocb = AioCb::from_slice( 666, // An invalid file descriptor + 0, //offset + &wbuf, + 0, //priority + SigevNotify::SigevNone, + LioOpcode::LIO_NOP); + assert!(aiocb.write().is_err()); +} + lazy_static! { pub static ref SIGNALED: AtomicBool = AtomicBool::new(false); } |