From b37c392fa75637541c05a0a5dfcf35e96ffd4a28 Mon Sep 17 00:00:00 2001 From: Michael Daffin Date: Sat, 10 Aug 2019 13:49:12 +0100 Subject: [PATCH] Refactors the error handling to remove some duplication --- src/lib.rs | 56 +++++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 296fa64..f71e185 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,12 +71,10 @@ impl LoopControl { /// println!("{}", ld.path().unwrap().display()); /// ``` pub fn next_free(&self) -> io::Result { - let result = unsafe { ioctl(self.dev_file.as_raw_fd() as c_int, LOOP_CTL_GET_FREE.into()) }; - if result < 0 { - Err(io::Error::last_os_error()) - } else { - LoopDevice::open(&format!("{}{}", LOOP_PREFIX, result)) - } + let dev_num = ioctl_to_error(unsafe { + ioctl(self.dev_file.as_raw_fd() as c_int, LOOP_CTL_GET_FREE.into()) + })?; + LoopDevice::open(&format!("{}{}", LOOP_PREFIX, dev_num)) } } @@ -170,33 +168,29 @@ impl LoopDevice { .open(backing_file)?; // Attach the file - - if unsafe { + ioctl_to_error(unsafe { ioctl( self.device.as_raw_fd() as c_int, LOOP_SET_FD.into(), bf.as_raw_fd() as c_int, ) - } < 0 - { - return Err(io::Error::last_os_error()); - } + })?; // Set offset for backing_file let mut info = loop_info64::default(); info.lo_offset = offset; info.lo_sizelimit = sizelimit; - if unsafe { + if let Err(err) = ioctl_to_error(unsafe { ioctl( self.device.as_raw_fd() as c_int, LOOP_SET_STATUS64.into(), &mut info, ) - } < 0 - { - self.detach()?; - return Err(io::Error::last_os_error()); + }) { + // Ignore the error to preserve the original error + let _ = self.detach(); + return Err(err); } Ok(()) } @@ -225,31 +219,21 @@ impl LoopDevice { /// ld.detach().unwrap(); /// ``` pub fn detach(&self) -> io::Result<()> { - if unsafe { ioctl(self.device.as_raw_fd() as c_int, LOOP_CLR_FD.into(), 0) } < 0 { - Err(io::Error::last_os_error()) - } else { - Ok(()) - } + ioctl_to_error(unsafe { ioctl(self.device.as_raw_fd() as c_int, LOOP_CLR_FD.into(), 0) })?; + Ok(()) } /// Resize a live loop device. If the size of the backing file changes this can be called to /// inform the loop driver about the new size. pub fn set_capacity(&self) -> io::Result<()> { - println!("running set_capacity"); - - if unsafe { + ioctl_to_error(unsafe { ioctl( self.device.as_raw_fd() as c_int, LOOP_SET_CAPACITY.into(), 0, ) - } < 0 - { - Err(io::Error::last_os_error()) - } else { - println!("ok"); - Ok(()) - } + })?; + Ok(()) } } @@ -289,3 +273,11 @@ impl Default for loop_info64 { } } } + +fn ioctl_to_error(ret: i32) -> io::Result { + if ret < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(ret) + } +}