Skip to content

Commit

Permalink
hil: added --overwrite-existing (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed Aug 29, 2024
1 parent f8c785e commit 17e515b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
22 changes: 17 additions & 5 deletions hil/src/commands/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use color_eyre::{
};
use tracing::info;

use crate::{current_dir, flash::FlashVariant};
use crate::{current_dir, download_s3::ExistingFileBehavior, flash::FlashVariant};

#[derive(Parser, Debug)]
pub struct Flash {
Expand All @@ -19,14 +19,22 @@ pub struct Flash {
/// Skips download by using an existing tarball on the filesystem.
#[arg(long)]
rts_path: Option<Utf8PathBuf>,
/// if this flag is given, uses flashcmd.txt instead of fastflashcmd.txt
/// If this flag is given, uses flashcmd.txt instead of fastflashcmd.txt
#[arg(long)]
slow: bool,
/// If this flag is given, overwites any existing files when downloading the rts.
#[arg(long)]
overwrite_existing: bool,
}

impl Flash {
pub async fn run(self) -> Result<()> {
let args = self;
let existing_file_behavior = if args.overwrite_existing {
ExistingFileBehavior::Overwrite
} else {
ExistingFileBehavior::Abort
};
ensure!(
crate::boot::is_recovery_mode_detected()?,
"orb must be in recovery mode to flash. Try running `orb-hil reboot -r`"
Expand All @@ -41,9 +49,13 @@ impl Flash {
.wrap_err("failed to parse filename")?,
);

crate::download_s3::download_url(s3_url, &download_path)
.await
.wrap_err("error while downloading from s3")?;
crate::download_s3::download_url(
s3_url,
&download_path,
existing_file_behavior,
)
.await
.wrap_err("error while downloading from s3")?;

download_path
} else if let Some(rts_path) = args.rts_path {
Expand Down
24 changes: 22 additions & 2 deletions hil/src/download_s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@ use indicatif::{ProgressState, ProgressStyle};
use tempfile::NamedTempFile;
use tracing::info;

#[derive(Debug, Eq, PartialEq)]
pub enum ExistingFileBehavior {
/// If a file exists, overwrite it
Overwrite,
/// If a file exists, abort
Abort,
}

/// `out_path` is the final path of the file after downloading.
pub async fn download_url(url: &str, out_path: &Utf8Path) -> Result<()> {
ensure!(!out_path.exists(), "{out_path:?} already exists!");
pub async fn download_url(
url: &str,
out_path: &Utf8Path,
existing_file_behavior: ExistingFileBehavior,
) -> Result<()> {
if existing_file_behavior == ExistingFileBehavior::Abort {
ensure!(!out_path.exists(), "{out_path:?} already exists!");
}
let parent_dir = out_path
.parent()
.expect("please provide the path to a file");
Expand Down Expand Up @@ -99,6 +113,12 @@ pub async fn download_url(url: &str, out_path: &Utf8Path) -> Result<()> {
let tmp_file = NamedTempFile::from_parts(tmp_file.into_std().await, tmp_file_path);
let out_path_clone = out_path.to_owned();
tokio::task::spawn_blocking(move || {
if existing_file_behavior == ExistingFileBehavior::Abort {
ensure!(
!out_path_clone.exists(),
"{out_path_clone:?} already exists!"
);
}
tmp_file
.persist(out_path_clone)
.wrap_err("failed to persist temporary file")
Expand Down

0 comments on commit 17e515b

Please sign in to comment.