1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#![cfg(feature = "md5sum")]

use std::{
    fs::File,
    io::{self, Read},
    path::Path,
};

use crate::common::FileMeta;

use super::io::IOSum;

/// Get the md5sum of a file
pub fn md5sum(path: &Path) -> io::Result<FileMeta> {
    let file = File::open(path)?;
    let mut md5sum = IOSum::new(file);

    let mut buf: Box<[u8]> = Box::from([0u8; 1204 * 16]);

    let mut c = 1;
    while c > 0 {
        c = md5sum.read(buf.as_mut())?;
    }

    let size = md5sum.byte_count() as u32;
    let (_, hash) = md5sum.into_inner();
    Ok(FileMeta { size, hash })
}