diff --git a/.gitignore b/.gitignore index a9d37c5..3b37adf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -target -Cargo.lock +target/ +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml index 0288c52..c2dd4a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "loopdev" description = "Setup and control loop devices" -version = "0.1.1" +version = "0.1.2" authors = ["Michael Daffin "] license = "MIT" documentation = "https://disconnected.systems/loopdev/loopdev/" @@ -13,5 +13,3 @@ keywords = ["loop", "losetup"] [dependencies] errno = "0.1" libc = "0.2" -docopt = "0.6" -rustc-serialize = "0.3" diff --git a/losetup/Cargo.lock b/losetup/Cargo.lock new file mode 100644 index 0000000..12fde0d --- /dev/null +++ b/losetup/Cargo.lock @@ -0,0 +1,234 @@ +[root] +name = "losetup" +version = "0.1.2" +dependencies = [ + "clap 2.23.3 (registry+https://github.com/rust-lang/crates.io-index)", + "docopt 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "loopdev 0.1.2", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aho-corasick" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ansi_term" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "atty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bitflags" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "clap" +version = "2.23.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "docopt" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "errno" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "loopdev" +version = "0.1.2" +dependencies = [ + "errno 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "memchr" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rustc-serialize" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "strsim" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "term_size" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread-id" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-segmentation" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-width" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unreachable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "utf8-ranges" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "vec_map" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699" +"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6" +"checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159" +"checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4" +"checksum clap 2.23.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f57e9b63057a545ad2ecd773ea61e49422ed1b1d63d74d5da5ecaee55b3396cd" +"checksum docopt 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ab32ea6e284d87987066f21a9e809a73c14720571ef34516f0890b3d355ccfd8" +"checksum errno 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1e2b2decb0484e15560df3210cf0d78654bb0864b2c138977c07e377a1bae0e2" +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3b37545ab726dd833ec6420aaba8231c5b320814b9029ad585555d2a03e94fbf" +"checksum libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)" = "babb8281da88cba992fa1f4ddec7d63ed96280a1a53ec9b919fd37b53d71e502" +"checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4" +"checksum regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4278c17d0f6d62dfef0ab00028feb45bd7d2102843f80763474eeb1be8a10c01" +"checksum regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9191b1f57603095f105d317e375d19b1c9c5c3185ea9633a99a6dcbed04457" +"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" +"checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694" +"checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209" +"checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a" +"checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7" +"checksum unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18127285758f0e2c6cf325bb3f3d138a12fee27de4f23e146cd6a179f26c2cf3" +"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f" +"checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91" +"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" +"checksum vec_map 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8cdc8b93bd0198ed872357fb2e667f7125646b1762f16d60b2c96350d361897" +"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/losetup/Cargo.toml b/losetup/Cargo.toml new file mode 100644 index 0000000..50bf5b0 --- /dev/null +++ b/losetup/Cargo.toml @@ -0,0 +1,13 @@ +[package] +authors = ["Michael Daffin "] +name = "losetup" +version = "0.1.2" + +[dependencies] +clap = "2.23.3" +docopt = "0.7.0" +rustc-serialize = "0.3.24" + +[dependencies.loopdev] +optional = false +path = ".." diff --git a/losetup/src/main.rs b/losetup/src/main.rs new file mode 100644 index 0000000..c6e6c06 --- /dev/null +++ b/losetup/src/main.rs @@ -0,0 +1,83 @@ +#[macro_use] +extern crate clap; +extern crate loopdev; + +use std::io::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() { + match LoopControl::open().and_then(|lc| lc.next_free()) { + Ok(ld) => println!("{}", ld.get_path().unwrap().display()), + Err(err) => { + writeln!(&mut std::io::stderr(), "{}", err).unwrap(); + exit(1) + } + } +} + +fn attach(image: &str, loopdev: Option<&str>, offset: 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(&image, offset))) +} + +fn detach(dev: &str) { + exit_on_error!(LoopDevice::open(dev).and_then(|ld| ld.detach())) +} + +fn list(_free: bool, _used: bool) { + unimplemented!(); +} + +fn main() { + let matches = clap_app!(losetup => + (version: "0.1.2") + (author: "Michael Daffin ") + (about: "Setup and control loop devices") + (@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 offset: -o --offset +takes_value "the offset within the file to start at") + ) + (@subcommand detach => + (about: "detach the loop device from the backing file") + (@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(); + + 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, matches.value_of("offset").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")); + } +} diff --git a/src/bin/losetup.rs b/src/bin/losetup.rs deleted file mode 100644 index af48a57..0000000 --- a/src/bin/losetup.rs +++ /dev/null @@ -1,96 +0,0 @@ -extern crate rustc_serialize; -extern crate docopt; -extern crate loopdev; - -use docopt::Docopt; -use std::io::Write; -use std::process::exit; -use loopdev::{LoopControl, LoopDevice}; - -const USAGE: &'static str = " -Usage: - losetup attach [--offset=] [] - losetup detach - losetup find - losetup [list] [--free|--used] - losetup (--help|--version) - -Set up and control loop devices. - -Options: - -f, --free find unused devices - -u, --used find used devices - -o, --offset start at at into file - -h, --help display this help and exit - -V, --version output version information and exit -"; - -macro_rules! exit_on_error { - ($e:expr) => ({match $e { - Ok(d) => d, - Err(err) => { - writeln!(&mut std::io::stderr(), "{}", err).unwrap(); - exit(1) - }, - } - }) -} - -#[derive(Debug, RustcDecodable)] -struct Args { - cmd_attach: bool, - cmd_detach: bool, - cmd_find: bool, - cmd_list: bool, - arg_image: String, - arg_loopdev: Option, - arg_file: String, - flag_free: bool, - flag_used: bool, -} - -fn find() { - match LoopControl::open().and_then(|lc| lc.next_free()) { - Ok(ld) => println!("{}", ld.get_path().unwrap().display()), - Err(err) => { - writeln!(&mut std::io::stderr(), "{}", err).unwrap(); - exit(1) - } - } -} - -fn attach(image: String, loopdev: Option) { - exit_on_error!(match loopdev { - None => LoopControl::open().and_then(|lc| lc.next_free()), - Some(dev) => LoopDevice::open(&dev), - } - .and_then(|ld| ld.attach(&image, 0))) -} - -fn detach(dev: String) { - exit_on_error!(LoopDevice::open(&dev).and_then(|ld| ld.detach())) -} - -fn list(_free: bool, _used: bool) { - exit_on_error!(Err(String::from("TODO: list loop devices"))) -} - -fn main() { - let args: Args = Docopt::new(USAGE) - .and_then(|d| d.decode()) - .unwrap_or_else(|e| e.exit()); - if args.cmd_find { - find(); - } else if args.cmd_attach { - attach(args.arg_image, args.arg_loopdev); - } else if args.cmd_detach { - detach(args.arg_file); - } else { - // No flags given default to find all - if !args.flag_free && !args.flag_used { - list(true, true) - } else { - list(args.flag_free, args.flag_used); - } - } -}