diff --git a/src/args.rs b/src/args.rs index 778d0e8..a88af22 100644 --- a/src/args.rs +++ b/src/args.rs @@ -59,6 +59,10 @@ pub struct CreateCommand { requires = "path" )] pub image: Option, + + /// Overwrite existing image files. Use with caution + #[structopt(long = "overwrite")] + pub overwrite: bool, } #[derive(StructOpt)] diff --git a/src/main.rs b/src/main.rs index 9e016af..79130f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,13 +73,17 @@ fn fix_fstab(fstab: &str) -> String { .join("\n") } -fn create_image(path: &Path, size: Byte) -> Result { +fn create_image(path: &Path, size: Byte, overwrite: bool) -> Result { { - let file = fs::OpenOptions::new() - .write(true) - .create_new(true) - .open(path) - .context(ErrorKind::Image)?; + let mut options = fs::OpenOptions::new(); + + options.write(true); + if overwrite { + options.create(true); + } else { + options.create_new(true); + } + let file = options.open(path).context(ErrorKind::Image)?; file.set_len(size.get_bytes() as u64) .context(ErrorKind::Image)?; @@ -154,7 +158,7 @@ fn create(command: CreateCommand, running: Arc) -> Result<(), Error> }; let image_loop = if let Some(size) = command.image { - Some(create_image(&storage_device_path, size)?) + Some(create_image(&storage_device_path, size, command.overwrite)?) } else { None };