Skip to content

Commit

Permalink
refactor step impl
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Jun 22, 2024
1 parent 8a85e81 commit 225ef10
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 216 deletions.
216 changes: 0 additions & 216 deletions src/api/app/step.rs

This file was deleted.

133 changes: 133 additions & 0 deletions src/api/app/step/cache_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use std::path::Path;

use anyhow::Result;
use futures::{StreamExt, TryStreamExt};
use tokio::{fs::File, io::BufWriter};
use tokio_util::io::ReaderStream;

use crate::api::{app::App, step::{FileMeta, StepResult}};

impl App {
// cache | output | to do
// x | x | StepResult::Skip
// x | | copy from cache
// | x | StepResult::Continue
// | | StepResult::Continue
pub(super) async fn execute_step_cache_check(&self, dir: &Path, metadata: &FileMeta) -> Result<StepResult> {
let output_path = dir.join(&metadata.filename);

let Some(cached_path) = self.cache.loc(metadata.cache.as_ref()) else {
return Ok(StepResult::Continue);
};

if !cached_path.try_exists()? {
return Ok(StepResult::Continue);
}

let cached_meta = cached_path.metadata()?;
let cached_size = cached_meta.len();

let output_size = if output_path.try_exists()? {
let meta = output_path.metadata()?;
Some(meta.len())
} else {
None
};

if let Some(output_size) = output_size {
// if output file exists...

if output_size != cached_size || metadata.size.is_some_and(|x| x != output_size) {
// size mismatch
// TODO: print warning
println!("WARNING size mismatch: {}", metadata.filename);
tokio::fs::remove_file(&output_path).await?;
//return Ok(StepResult::Continue);
} else {
let hasher = metadata.get_hasher();
if let Some((format, mut hasher, content)) = hasher {

Check warning on line 48 in src/api/app/step/cache_check.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `format`

warning: unused variable: `format` --> src/api/app/step/cache_check.rs:48:30 | 48 | if let Some((format, mut hasher, content)) = hasher { | ^^^^^^ help: if this is intentional, prefix it with an underscore: `_format`
let output_file = File::open(&output_path).await?;
let mut stream = ReaderStream::new(output_file);

while let Some(item) = stream.try_next().await? {
hasher.update(&item);
}

let hash = hex::encode(&hasher.finalize());

if content == hash {
// size and hash match, skip rest of the steps
// TODO: print info
println!("Skipping (output hash matches) {}", metadata.filename);
return Ok(StepResult::Skip);
} else {
// hash mismatch
// TODO: print warning
println!("WARNING Hash mismatch: {}", metadata.filename);
tokio::fs::remove_file(&output_path).await?;
}

Check warning on line 68 in src/api/app/step/cache_check.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant else block

warning: redundant else block --> src/api/app/step/cache_check.rs:63:28 | 63 | } else { | ____________________________^ 64 | | // hash mismatch 65 | | // TODO: print warning 66 | | println!("WARNING Hash mismatch: {}", metadata.filename); 67 | | tokio::fs::remove_file(&output_path).await?; 68 | | } | |_____________________^ | = help: remove the `else` block and move the contents out = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_else = note: `-W clippy::redundant-else` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::redundant_else)]`
} else {
// FileInfo doesn't have any hashes
// so we must check from cache
// return skip if equal, do nothing otherwise to fallback copyfromcache
let target_file = File::open(&output_path).await?;
let cached_file = File::open(&cached_path).await?;

let mut target_stream = ReaderStream::new(target_file);
let mut cached_stream = ReaderStream::new(cached_file);

let is_equal = loop {
match (target_stream.next().await, cached_stream.next().await) {
(Some(Ok(a)), Some(Ok(b))) => {
if a != b {
break false;
}
},
(None, None) => break true,
_ => break false,
}
};

if is_equal {
// TODO: print info
println!("Skipping (eq cached) {}", metadata.filename);
return Ok(StepResult::Skip);
}
}
}
}

// == Copying from cache ==

let mut hasher = metadata.get_hasher();

let target_file = File::create(&output_path).await?;
let mut target_writer = BufWriter::new(target_file);

let cached_file = File::open(&cached_path).await?;
let mut stream = ReaderStream::new(cached_file);

while let Some(item) = stream.try_next().await? {
if let Some((_, ref mut digest, _)) = hasher {
digest.update(&item);
}

tokio::io::copy(&mut item.as_ref(), &mut target_writer).await?;
}

if let Some((_, hasher, content)) = hasher {
let hash = hex::encode(&hasher.finalize());

if hash != content {
// TODO: print warning
println!("WARNING Hash Mismatch on CacheCopy: {}", metadata.filename);
tokio::fs::remove_file(&output_path).await?;
tokio::fs::remove_file(&cached_path).await?;
return Ok(StepResult::Continue);
}
}

println!("Copied: {}", metadata.filename);
Ok(StepResult::Skip)
}
}
Loading

0 comments on commit 225ef10

Please sign in to comment.