Skip to content

Commit

Permalink
Narrorws the scope of the unsafe blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaffin committed Aug 7, 2019
1 parent 79b92df commit 8c94765
Showing 1 changed file with 33 additions and 34 deletions.
67 changes: 33 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ impl LoopControl {
/// println!("{}", ld.path().unwrap().display());
/// ```
pub fn next_free(&self) -> io::Result<LoopDevice> {
let result;
unsafe {
result = ioctl(self.dev_file.as_raw_fd() as c_int, LOOP_CTL_GET_FREE.into());
}
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 {
Expand All @@ -99,8 +96,9 @@ impl LoopDevice {
/// Opens a loop device.
pub fn open<P: AsRef<Path>>(dev: P) -> io::Result<Self> {
// TODO create dev if it does not exist and begins with LOOP_PREFIX
let f = OpenOptions::new().read(true).write(true).open(dev)?;
Ok(Self { device: f })
Ok(Self {
device: OpenOptions::new().read(true).write(true).open(dev)?,
})
}

/// Attach the loop device to a file starting at offset into the file.
Expand Down Expand Up @@ -172,31 +170,33 @@ impl LoopDevice {
.open(backing_file)?;

// Attach the file
unsafe {
if ioctl(

if 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());
}
)
} < 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;
unsafe {
if ioctl(

if 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());
}
)
} < 0
{
self.detach()?;
return Err(io::Error::last_os_error());
}
Ok(())
}
Expand Down Expand Up @@ -225,31 +225,30 @@ impl LoopDevice {
/// ld.detach().unwrap();
/// ```
pub fn detach(&self) -> io::Result<()> {
unsafe {
if ioctl(self.device.as_raw_fd() as c_int, LOOP_CLR_FD.into(), 0) < 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
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(())
}
}

/// 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");
unsafe {
if ioctl(

if 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(())
}
)
} < 0
{
Err(io::Error::last_os_error())
} else {
println!("ok");
Ok(())
}
}
}
Expand Down

0 comments on commit 8c94765

Please sign in to comment.