Skip to content

Commit

Permalink
Allow setting sizelimit when attaching file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Brodersen committed Dec 18, 2017
1 parent a742e44 commit 13a6716
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion losetup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn attach(image: &str, loopdev: Option<&str>, offset: u64) {
match loopdev {
None => LoopControl::open().and_then(|lc| lc.next_free()),
Some(dev) => LoopDevice::open(&dev),
}.and_then(|ld| ld.attach(&image, offset))
}.and_then(|ld| ld.attach(&image, offset, None))
)
}

Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//!
//! println!("{}", ld.get_path().unwrap().display());
//!
//! ld.attach("test.img", 0).unwrap();
//! ld.attach("test.img", 0, None).unwrap();
//! // ...
//! ld.detach().unwrap();
//! ```
Expand Down Expand Up @@ -97,10 +97,10 @@ impl LoopDevice {
/// ```rust
/// use loopdev::LoopDevice;
/// let ld = LoopDevice::open("/dev/loop4").unwrap();
/// ld.attach("test.img", 0).unwrap();
/// ld.attach("test.img", 0, None).unwrap();
/// # ld.detach().unwrap();
/// ```
pub fn attach<P: AsRef<Path>>(&self, backing_file: P, offset: u64) -> io::Result<()> {
pub fn attach<P: AsRef<Path>>(&self, backing_file: P, offset: u64, size: Option<u64>) -> io::Result<()> {
let bf = try!(OpenOptions::new().read(true).write(true).open(backing_file));

// Attach the file
Expand All @@ -118,6 +118,9 @@ impl LoopDevice {
// Set offset for backing_file
let mut info: loop_info64 = Default::default();
info.lo_offset = offset;
if let Some(size) = size {
info.lo_sizelimit = size;
}
unsafe {
if ioctl(
self.device.as_raw_fd() as c_int,
Expand Down Expand Up @@ -146,7 +149,7 @@ impl LoopDevice {
/// ```rust
/// use loopdev::LoopDevice;
/// let ld = LoopDevice::open("/dev/loop6").unwrap();
/// # ld.attach("test.img", 0).unwrap();
/// # ld.attach("test.img", 0, None).unwrap();
/// ld.detach().unwrap();
/// ```
pub fn detach(&self) -> io::Result<()> {
Expand Down

0 comments on commit 13a6716

Please sign in to comment.