mirror of
				https://git.proxmox.com/git/proxmox-backup
				synced 2025-11-02 15:18:42 +00:00 
			
		
		
		
	move compute_file_csum to src/tools.rs
This commit is contained in:
		
							parent
							
								
									dda72456d7
								
							
						
					
					
						commit
						1bc1d81a00
					
				@ -1,5 +1,5 @@
 | 
			
		||||
use anyhow::{format_err, Error};
 | 
			
		||||
use std::io::{Read, Write, Seek, SeekFrom};
 | 
			
		||||
use std::io::{Write, Seek, SeekFrom};
 | 
			
		||||
use std::fs::File;
 | 
			
		||||
use std::sync::Arc;
 | 
			
		||||
use std::os::unix::fs::OpenOptionsExt;
 | 
			
		||||
@ -9,7 +9,10 @@ use serde_json::{json, Value};
 | 
			
		||||
 | 
			
		||||
use proxmox::tools::digest_to_hex;
 | 
			
		||||
 | 
			
		||||
use crate::backup::*;
 | 
			
		||||
use crate::{
 | 
			
		||||
    tools::compute_file_csum,
 | 
			
		||||
    backup::*,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
use super::{HttpClient, H2Client};
 | 
			
		||||
 | 
			
		||||
@ -219,29 +222,3 @@ impl BackupReader {
 | 
			
		||||
        Ok(index)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn compute_file_csum(file: &mut File) -> Result<([u8; 32], u64), Error> {
 | 
			
		||||
 | 
			
		||||
    file.seek(SeekFrom::Start(0))?;
 | 
			
		||||
 | 
			
		||||
    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(count) => count,
 | 
			
		||||
            Err(ref err) if err.kind() == std::io::ErrorKind::Interrupted => { continue; }
 | 
			
		||||
            Err(err) => return Err(err.into()),
 | 
			
		||||
        };
 | 
			
		||||
        if count == 0 {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        size += count as u64;
 | 
			
		||||
        hasher.update(&buffer[..count]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let csum = hasher.finish();
 | 
			
		||||
 | 
			
		||||
    Ok((csum, size))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -8,10 +8,13 @@ use std::collections::HashMap;
 | 
			
		||||
use std::io::{Seek, SeekFrom};
 | 
			
		||||
 | 
			
		||||
use proxmox::api::error::{StatusCode, HttpError};
 | 
			
		||||
use crate::server::{WorkerTask};
 | 
			
		||||
use crate::backup::*;
 | 
			
		||||
use crate::api2::types::*;
 | 
			
		||||
use super::*;
 | 
			
		||||
use crate::{
 | 
			
		||||
    tools::compute_file_csum,
 | 
			
		||||
    server::WorkerTask,
 | 
			
		||||
    backup::*,
 | 
			
		||||
    api2::types::*,
 | 
			
		||||
    client::*,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// fixme: implement filters
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										31
									
								
								src/tools.rs
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								src/tools.rs
									
									
									
									
									
								
							@ -5,7 +5,7 @@ use std::any::Any;
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use std::hash::BuildHasher;
 | 
			
		||||
use std::fs::File;
 | 
			
		||||
use std::io::{self, BufRead, ErrorKind, Read};
 | 
			
		||||
use std::io::{self, BufRead, ErrorKind, Read, Seek, SeekFrom};
 | 
			
		||||
use std::os::unix::io::RawFd;
 | 
			
		||||
use std::path::Path;
 | 
			
		||||
 | 
			
		||||
@ -563,3 +563,32 @@ pub fn strip_ascii_whitespace(line: &[u8]) -> &[u8] {
 | 
			
		||||
        None => &[],
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Seeks to start of file and computes the SHA256 hash
 | 
			
		||||
pub fn compute_file_csum(file: &mut File) -> Result<([u8; 32], u64), Error> {
 | 
			
		||||
 | 
			
		||||
    file.seek(SeekFrom::Start(0))?;
 | 
			
		||||
 | 
			
		||||
    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(count) => count,
 | 
			
		||||
            Err(ref err) if err.kind() == std::io::ErrorKind::Interrupted => {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            Err(err) => return Err(err.into()),
 | 
			
		||||
        };
 | 
			
		||||
        if count == 0 {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        size += count as u64;
 | 
			
		||||
        hasher.update(&buffer[..count]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let csum = hasher.finish();
 | 
			
		||||
 | 
			
		||||
    Ok((csum, size))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user