mirror of
https://github.com/philmmanjaro/alma.git
synced 2025-07-25 14:39:29 +02:00
Add the overwrite flag
This commit is contained in:
parent
2c11c9b251
commit
7007706b67
@ -59,6 +59,10 @@ pub struct CreateCommand {
|
||||
requires = "path"
|
||||
)]
|
||||
pub image: Option<Byte>,
|
||||
|
||||
/// Overwrite existing image files. Use with caution
|
||||
#[structopt(long = "overwrite")]
|
||||
pub overwrite: bool,
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
|
18
src/main.rs
18
src/main.rs
@ -73,13 +73,17 @@ fn fix_fstab(fstab: &str) -> String {
|
||||
.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()
|
||||
.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<AtomicBool>) -> 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
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user