Skip to content

Commit

Permalink
sanitizing also the version
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-eth committed Aug 5, 2024
1 parent c5d3b5e commit c0b27a4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
19 changes: 10 additions & 9 deletions src/dependency_downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ pub async fn download_dependency(
}

pub fn unzip_dependency(dependency: &HttpDependency) -> Result<()> {
let sanitized_name = sanitize_dependency_name(&dependency.name);
let file_name = format!("{}-{}", dependency.name, dependency.version);
let sanitized_name = sanitize_dependency_name(&file_name);
if sanitized_name.is_empty() {
return Err(DownloadError::FileNameError());
return Err(DownloadError::FileNameError);
}
let file_name = format!("{}-{}.zip", sanitized_name, dependency.version);
let target_name = format!("{}-{}/", sanitized_name, dependency.version);
let current_dir = DEPENDENCY_DIR.join(file_name);

let target_name = format!("{}/", sanitized_name);
let current_dir = DEPENDENCY_DIR.join(format!("{sanitized_name}.zip"));
let target = DEPENDENCY_DIR.join(target_name);
let archive = read_file(current_dir).unwrap();

Expand Down Expand Up @@ -232,15 +233,15 @@ async fn download_via_http(
dependency_directory: &Path,
) -> Result<()> {
println!("{}", format!("Started HTTP download of {dependency}").green());
let sanitized_name = sanitize_dependency_name(&dependency.name);
let zip_to_download = &format!("{}-{}.zip", dependency.name, dependency.version);
let sanitized_name = sanitize_dependency_name(&zip_to_download);
if sanitized_name.is_empty() {
return Err(DownloadError::FileNameError());
return Err(DownloadError::FileNameError);
}
let zip_to_download = &format!("{}-{}.zip", sanitized_name, dependency.version);
let resp = reqwest::get(url).await?;
let mut resp = resp.error_for_status()?;

let file_path = dependency_directory.join(zip_to_download);
let file_path = dependency_directory.join(sanitized_name);
let mut file = tokio_fs::File::create(&file_path)
.await
.map_err(|e| DownloadError::IOError { path: file_path.clone(), source: e })?;
Expand Down
10 changes: 2 additions & 8 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub enum DownloadError {
AsyncError(#[from] tokio::task::JoinError),

#[error("error during dependency sanitization operation: The dependency name contains illegal characters")]
FileNameError(),
FileNameError,
}

#[derive(Error, Debug)]
Expand All @@ -124,7 +124,7 @@ pub enum JanitorError {
LockError(LockError), // TODO: derive from LockError

#[error("error during dependency sanitization operation: The dependency name contains illegal characters")]
FileNameError(),
FileNameError,
}

#[derive(Error, Debug)]
Expand Down Expand Up @@ -180,9 +180,3 @@ pub enum PublishError {
#[error("unknown http error")]
UnknownError,
}

#[derive(Error, Debug)]
pub enum FileError {
#[error("error during dependency sanitization operation: The dependency name contains illegal characters")]
FileNameError(),
}
16 changes: 8 additions & 8 deletions src/janitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ pub fn cleanup_after(dependencies: &[Dependency]) -> Result<()> {
}

pub fn healthcheck_dependency(dependency: &Dependency) -> Result<()> {
let sanitized_name = sanitize_dependency_name(dependency.name());
let file_name: String = format!("{}-{}.zip", dependency.name(), dependency.version());
let sanitized_name = sanitize_dependency_name(&file_name);
if sanitized_name.is_empty() {
return Err(JanitorError::FileNameError());
return Err(JanitorError::FileNameError);
}
let file_name: String = format!("{}-{}", sanitized_name, dependency.version());
let new_path = DEPENDENCY_DIR.join(file_name);
let new_path = DEPENDENCY_DIR.join(sanitized_name);
match fs::metadata(new_path) {
Ok(_) => Ok(()),
Err(_) => Err(JanitorError::MissingDependency(dependency.to_string())),
}
}

pub fn cleanup_dependency(dependency: &Dependency, full: bool) -> Result<()> {
let sanitized_name = sanitize_dependency_name(dependency.name());
let file_name: String = format!("{}-{}.zip", dependency.name(), dependency.version());
let sanitized_name = sanitize_dependency_name(&file_name);
if sanitized_name.is_empty() {
return Err(JanitorError::FileNameError());
return Err(JanitorError::FileNameError);
}
let file_name: String = format!("{}-{}.zip", sanitized_name, dependency.version());
let new_path: std::path::PathBuf = DEPENDENCY_DIR.clone().join(file_name);
let new_path: std::path::PathBuf = DEPENDENCY_DIR.clone().join(sanitized_name);
if let Dependency::Http(_) = dependency {
fs::remove_file(&new_path)
.map_err(|e| JanitorError::IOError { path: new_path, source: e })?;
Expand Down
13 changes: 7 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,14 @@ pub fn sanitize_dependency_name(dependency_name: &str) -> String {
#[cfg(not(test))]
pub fn sha256_digest(dependency: &HttpDependency) -> String {
use crate::DEPENDENCY_DIR;
let dep_name = sanitize_dependency_name(&dependency.name);
if dep_name.is_empty() {
return dep_name;

let file_name = &format!("{}-{}.zip", dependency.name, dependency.version);
let sanitized_name = sanitize_dependency_name(&file_name);
if sanitized_name.is_empty() {
return sanitized_name;
}
let bytes =
std::fs::read(DEPENDENCY_DIR.join(format!("{}-{}.zip", dep_name, dependency.version)))
.unwrap(); // Vec<u8>

let bytes = std::fs::read(DEPENDENCY_DIR.join(sanitized_name)).unwrap(); // Vec<u8>
sha256::digest(bytes)
}

Expand Down

0 comments on commit c0b27a4

Please sign in to comment.