async_fetcher/
checksum_system.rs1use crate::checksum::{Checksum, ChecksumError};
5use futures::prelude::*;
6use poolshark::{global::Pool, Poolable};
7use std::{path::Path, sync::Arc};
8
9pub fn checksum_stream<I: Stream<Item = (Arc<Path>, Checksum)> + Send + Unpin + 'static>(
20 inputs: I,
21) -> impl Stream<Item = impl Future<Output = (Arc<Path>, Result<(), ChecksumError>)>> {
22 let buffer_pool = Arc::new(Pool::<Buf>::new(64, 1));
23
24 inputs.map(move |(dest, checksum)| {
25 let pool = buffer_pool.clone();
26
27 async {
28 tokio::task::spawn_blocking(move || {
29 let mut pooled_buf = pool.take();
30 let result = validate_checksum(&mut (&mut *pooled_buf).0[..], &dest, &checksum);
31 (dest, result)
32 })
33 .await
34 .unwrap()
35 }
36 })
37}
38
39pub fn validate_checksum(
41 buf: &mut [u8],
42 dest: &Path,
43 checksum: &Checksum,
44) -> Result<(), ChecksumError> {
45 let error = match std::fs::File::open(&*dest) {
46 Ok(file) => match checksum.validate(file, buf) {
47 Ok(()) => return Ok(()),
48 Err(why) => why,
49 },
50 Err(why) => ChecksumError::from(why),
51 };
52
53 let _ = std::fs::remove_file(&*dest);
54 Err(error)
55}
56
57struct Buf(Box<[u8; 8 * 1024]>);
59
60impl Poolable for Buf {
61 fn empty() -> Self {
62 Self(Box::new([0u8; 8 * 1024]))
63 }
64
65 fn reset(&mut self) {
66 self.0.fill(0);
67 }
68
69 fn capacity(&self) -> usize {
70 1
71 }
72}