mirror of
https://github.com/philmmanjaro/alma.git
synced 2025-07-25 22:49:28 +02:00
Add the overwrite flag
This commit is contained in:
parent
2c11c9b251
commit
7007706b67
@ -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)]
|
||||||
|
18
src/main.rs
18
src/main.rs
@ -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
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user