move some tools used by the client

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller
2021-07-09 14:10:15 +02:00
parent 75f83c6a81
commit ba0ccc5991
6 changed files with 40 additions and 34 deletions

View File

@ -11,6 +11,7 @@ anyhow = "1.0"
libc = "0.2"
nix = "0.19.1"
nom = "5.1"
openssl = "0.10"
regex = "1.2"
serde = "1.0"
serde_json = "1.0"

View File

@ -5,6 +5,7 @@ pub mod json;
pub mod nom;
pub mod process_locker;
pub mod str;
pub mod sha;
mod command;
pub use command::{command_output, command_output_as_string, run_command};

29
pbs-tools/src/sha.rs Normal file
View File

@ -0,0 +1,29 @@
//! SHA helpers.
use std::io::Read;
use anyhow::Error;
/// Calculate the sha256sum from a readable object.
pub fn sha256(file: &mut dyn Read) -> Result<([u8; 32], u64), Error> {
let mut hasher = openssl::sha::Sha256::new();
let mut buffer = proxmox::tools::vec::undefined(256 * 1024);
let mut size: u64 = 0;
loop {
let count = match file.read(&mut buffer) {
Ok(0) => break,
Ok(count) => count,
Err(ref err) if err.kind() == std::io::ErrorKind::Interrupted => {
continue;
}
Err(err) => return Err(err.into()),
};
size += count as u64;
hasher.update(&buffer[..count]);
}
let csum = hasher.finish();
Ok((csum, size))
}