2023-06-25 15:29:52 +02:00
|
|
|
use std::path::Path;
|
2021-07-26 21:14:12 +02:00
|
|
|
|
2023-06-25 15:29:52 +02:00
|
|
|
use filebuffer::FileBuffer;
|
|
|
|
use tracing::{debug, trace};
|
2023-06-29 14:29:38 +02:00
|
|
|
use upend_base::hash::UpMultihash;
|
2020-09-15 19:26:47 +02:00
|
|
|
|
2020-09-14 21:18:53 +02:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct LoggerSink {
|
2020-09-15 19:26:47 +02:00
|
|
|
pub buffer: Vec<u8>,
|
2020-09-14 21:18:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::io::Write for LoggerSink {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
|
|
|
self.buffer.extend(buf.iter());
|
|
|
|
|
|
|
|
if self.buffer.ends_with(b"\n") {
|
|
|
|
self.flush()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> std::io::Result<()> {
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
"{}",
|
|
|
|
str::from_utf8(self.buffer.as_mut())
|
|
|
|
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?
|
|
|
|
.trim()
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2023-06-25 15:29:52 +02:00
|
|
|
|
2023-06-29 14:29:38 +02:00
|
|
|
pub fn hash_at_path<P: AsRef<Path>>(path: P) -> anyhow::Result<UpMultihash> {
|
2023-06-25 15:29:52 +02:00
|
|
|
let path = path.as_ref();
|
|
|
|
trace!("Hashing {:?}...", path);
|
|
|
|
let fbuffer = FileBuffer::open(path)?;
|
|
|
|
trace!("Finished hashing {:?}...", path);
|
2023-06-29 14:29:38 +02:00
|
|
|
Ok(upend_base::hash::sha256hash(&fbuffer)?)
|
2023-06-25 15:29:52 +02:00
|
|
|
}
|