| 1 | extern crate libmount; |
| 2 | extern crate argparse; |
| 3 | extern crate env_logger; |
| 4 | #[macro_use] extern crate log; |
| 5 | |
| 6 | use std::path::PathBuf; |
| 7 | use std::process::exit; |
| 8 | |
| 9 | use argparse::{ArgumentParser, Parse, StoreOption}; |
| 10 | |
| 11 | |
| 12 | fn main() { |
| 13 | env_logger::init(); |
| 14 | let mut target = PathBuf::new(); |
| 15 | let mut size = None::<usize>; |
| 16 | let mut mode = None::<String>; |
| 17 | let mut uid = None::<u32>; |
| 18 | let mut gid = None::<u32>; |
| 19 | { |
| 20 | let mut ap = ArgumentParser::new(); |
| 21 | ap.set_description("Tmpfs mount utility. Similar to `mount --tmpfs`"); |
| 22 | ap.refer(&mut target).add_argument("target", Parse, |
| 23 | "Target directory to mount tmpfs to").required(); |
| 24 | ap.refer(&mut size).add_option(&["--size"], StoreOption, |
| 25 | "Set size of the filesystem"); |
| 26 | ap.refer(&mut mode).add_option(&["--mode"], StoreOption, |
| 27 | "Set mode of the root directory"); |
| 28 | ap.refer(&mut uid).add_option(&["--uid"], StoreOption, |
| 29 | "Set uid of the directory"); |
| 30 | ap.refer(&mut gid).add_option(&["--gid"], StoreOption, |
| 31 | "Set gid of the directory"); |
| 32 | ap.parse_args_or_exit(); |
| 33 | } |
| 34 | let mut mnt = libmount::Tmpfs::new(target); |
| 35 | if let Some(x) = size { mnt = mnt.size_bytes(x); }; |
| 36 | if let Some(ref x) = mode { |
| 37 | mnt = mnt.mode(u32::from_str_radix(x, 8).expect("valid octal mode")); |
| 38 | } |
| 39 | if let Some(x) = uid { mnt = mnt.uid(x); } |
| 40 | if let Some(x) = gid { mnt = mnt.gid(x); } |
| 41 | match mnt.mount() { |
| 42 | Ok(()) => {} |
| 43 | Err(e) => { |
| 44 | error!("{}", e); |
| 45 | exit(1); |
| 46 | } |
| 47 | } |
| 48 | } |