diff --git a/losetup/Cargo.toml b/losetup/Cargo.toml index a5844f8..cf1a676 100644 --- a/losetup/Cargo.toml +++ b/losetup/Cargo.toml @@ -2,6 +2,7 @@ authors = ["Michael Daffin "] name = "losetup" version = "0.2.0" +description = "Setup and control loopback devices" [dependencies] clap = "2.23.3" diff --git a/losetup/src/main.rs b/losetup/src/main.rs index 3d10aa2..8ab280a 100644 --- a/losetup/src/main.rs +++ b/losetup/src/main.rs @@ -2,87 +2,81 @@ extern crate clap; extern crate loopdev; -use std::io::Write; +use std::io::{self, Write}; use std::process::exit; use loopdev::{LoopControl, LoopDevice}; -macro_rules! exit_on_error { - ($e:expr) => ({match $e { - Ok(d) => d, - Err(err) => { - writeln!(&mut std::io::stderr(), "{}", err).unwrap(); - exit(1) - }, - } - }) +fn find() -> io::Result<()> { + let loopdev = LoopControl::open()?.next_free()?; + println!("{}", loopdev.path().unwrap().display()); + Ok(()) } -fn find() { - match LoopControl::open().and_then(|lc| lc.next_free()) { - Ok(ld) => println!("{}", ld.path().unwrap().display()), - Err(err) => { - writeln!(&mut std::io::stderr(), "{}", err).unwrap(); - exit(1) - } +fn attach(matches: &clap::ArgMatches) -> io::Result<()> { + let quite = matches.is_present("quite"); + let image = matches.value_of("image").unwrap(); + let offset = value_t!(matches.value_of("offset"), u64).unwrap_or(0); + let sizelimit = value_t!(matches.value_of("sizelimit"), u64).unwrap_or(0); + let loopdev = match matches.value_of("loopdev") { + Some(loopdev) => LoopDevice::open(&loopdev)?, + None => LoopControl::open().and_then(|lc| lc.next_free())?, + }; + loopdev.attach_with_sizelimit(&image, offset, sizelimit)?; + if !quite { + println!("{}", loopdev.path().unwrap().display()); } + Ok(()) } -fn attach(image: &str, loopdev: Option<&str>, offset: u64, sizelimit: u64) { - exit_on_error!(match loopdev { - None => LoopControl::open().and_then(|lc| lc.next_free()), - Some(dev) => LoopDevice::open(&dev), - } - .and_then(|ld| ld.attach_with_sizelimit(&image, offset, sizelimit))) +fn detach(matches: &clap::ArgMatches) -> io::Result<()> { + let loopdev = matches.value_of("file").unwrap(); + LoopDevice::open(loopdev)?.detach() } -fn detach(dev: &str) { - exit_on_error!(LoopDevice::open(dev).and_then(|ld| ld.detach())) -} - -fn list(_free: bool, _used: bool) { +fn list(matches: Option<&clap::ArgMatches>) -> io::Result<()> { + let (_free, _used) = match matches { + Some(matches) => (matches.is_present("free"), matches.is_present("used")), + None => (false, false), + }; unimplemented!(); } fn main() { let matches = clap_app!(losetup => - (version: "0.1.2") - (author: "Michael Daffin ") - (about: "Setup and control loop devices") + (version: crate_version!()) + (author: crate_authors!()) + (about: crate_description!()) (@subcommand find => (about: "find the next free loop device") - ) + ) (@subcommand attach => (about: "attach the loop device to a backing file") - (@arg image: +required "the backing file to attach") - (@arg loopdev: "the loop device to attach") + (@arg image: +required "the backing file to attach") + (@arg loopdev: "the loop device to attach") (@arg offset: -o --offset +takes_value "the offset within the file to start at") (@arg sizelimit: -s --sizelimit +takes_value "the file is limited to this size") - ) + (@arg quite: -q --quite "don't print the device name") + ) (@subcommand detach => (about: "detach the loop device from the backing file") - (@arg file: +required "The file to detach") - ) + (@arg file: +required "The file to detach") + ) (@subcommand list => (about: "list the available loop devices") (@arg free: -f --free "find free devices") (@arg used: -u --used "find used devices") - ) - ) - .get_matches(); + ) + ).get_matches(); - if let Some(_) = matches.subcommand_matches("find") { - find(); - } else if let Some(matches) = matches.subcommand_matches("attach") { - let image = matches.value_of("image").unwrap(); - let loopdev = matches.value_of("loopdev"); - attach(image, - loopdev, - value_t!(matches.value_of("offset"), u64).unwrap_or(0), - value_t!(matches.value_of("sizelimit"), u64).unwrap_or(0)); - } else if let Some(matches) = matches.subcommand_matches("detach") { - let file = matches.value_of("file").unwrap(); - detach(file); - } else { - list(matches.is_present("free"), matches.is_present("used")); + let result = match matches.subcommand() { + ("find", _) => find(), + ("attach", Some(matches)) => attach(matches), + ("detach", Some(matches)) => detach(matches), + (_, matches) => list(matches), + }; + + if let Err(err) = result { + writeln!(&mut std::io::stderr(), "{}", err).unwrap(); + exit(1); } }