Skip to content

Commit

Permalink
Bug: Fix local paths incorrectly parsed as urls on windows in bita
Browse files Browse the repository at this point in the history
Previously in bita, when a user provided an input path an attempt was
made to parse it as a URL. If this failed it would fallback to being
used as a local path. However, windows paths are succesfully parsed as a
url (e.g. `C:\temp\my_archive.cba`) which caused a bug on windows where
inputs were always treated as a remote path.

This fix inverts the behavior and first checks if the path is a valid
local path before checking if it's a valid remote path.
  • Loading branch information
rminderhoud authored and oll3 committed Jan 23, 2022
1 parent 2dbeb23 commit 8791dbc
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,25 @@ fn parse_size(size_str: &str) -> Result<usize> {

fn parse_input_config(matches: &clap::ArgMatches<'_>) -> Result<clone_cmd::InputArchive> {
let input = matches.value_of("INPUT").unwrap().to_string();
Ok(match input.parse::<Url>() {
Ok(url) => {
// Use as URL
clone_cmd::InputArchive::Remote(Box::new(clone_cmd::RemoteInput {

let input_path = Path::new(&input);
if input_path.exists() {
return Ok(clone_cmd::InputArchive::Local(input.into()));
}

// Absolute windows paths, e.g. C:\temp.cba can be parsed as a URL. This
// check prevents invalid paths from being used as a remote path.
if Url::from_file_path(&input).is_ok() {
return Err(anyhow!(format!(
"Input is not a valid path: {}",
input_path.display()
)));
}

if let Ok(url) = input.parse::<Url>() {
// Use as URL
return Ok(clone_cmd::InputArchive::Remote(Box::new(
clone_cmd::RemoteInput {
url,
retries: matches
.value_of("http-retry-count")
Expand Down Expand Up @@ -165,13 +180,14 @@ fn parse_input_config(matches: &clap::ArgMatches<'_>) -> Result<clone_cmd::Input
}
None => HeaderMap::new(),
},
}))
}
Err(_) => {
// Use as path
clone_cmd::InputArchive::Local(input.into())
}
})
},
)));
};

Err(anyhow!(
"Input is not a valid local path or remote url: {}",
input
))
}

fn init_log(level: log::LevelFilter) -> Result<()> {
Expand Down

0 comments on commit 8791dbc

Please sign in to comment.