Add the overwrite flag

This commit is contained in:
Roey Darwish Dror 2019-08-01 13:19:31 +03:00
parent 2c11c9b251
commit 7007706b67
2 changed files with 15 additions and 7 deletions

View File

@ -59,6 +59,10 @@ pub struct CreateCommand {
requires = "path" requires = "path"
)] )]
pub image: Option<Byte>, pub image: Option<Byte>,
/// Overwrite existing image files. Use with caution
#[structopt(long = "overwrite")]
pub overwrite: bool,
} }
#[derive(StructOpt)] #[derive(StructOpt)]

View File

@ -73,13 +73,17 @@ fn fix_fstab(fstab: &str) -> String {
.join("\n") .join("\n")
} }
fn create_image(path: &Path, size: Byte) -> Result<LoopDevice, Error> { fn create_image(path: &Path, size: Byte, overwrite: bool) -> Result<LoopDevice, Error> {
{ {
let file = fs::OpenOptions::new() let mut options = fs::OpenOptions::new();
.write(true)
.create_new(true) options.write(true);
.open(path) if overwrite {
.context(ErrorKind::Image)?; options.create(true);
} else {
options.create_new(true);
}
let file = options.open(path).context(ErrorKind::Image)?;
file.set_len(size.get_bytes() as u64) file.set_len(size.get_bytes() as u64)
.context(ErrorKind::Image)?; .context(ErrorKind::Image)?;
@ -154,7 +158,7 @@ fn create(command: CreateCommand, running: Arc<AtomicBool>) -> Result<(), Error>
}; };
let image_loop = if let Some(size) = command.image { 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 { } else {
None None
}; };