From a4806b0e3b56a658710364176fda1bfaa0408942 Mon Sep 17 00:00:00 2001 From: Jamie Pine Date: Thu, 21 Apr 2022 23:35:35 -0700 Subject: [PATCH] fix windows build --- core/src/file/cas/checksum.rs | 29 +++++++++++++++++++++-------- core/src/file/indexer/scan.rs | 8 ++++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/core/src/file/cas/checksum.rs b/core/src/file/cas/checksum.rs index c97d01cec..95a40ff92 100644 --- a/core/src/file/cas/checksum.rs +++ b/core/src/file/cas/checksum.rs @@ -5,12 +5,29 @@ use ring::digest::{Context, SHA256}; use std::convert::TryInto; use std::fs::File; use std::io::{BufReader, Read}; + +#[cfg(target_os = "macos")] use std::os::unix::prelude::FileExt; +#[cfg(target_os = "windows")] +use std::os::windows::prelude::*; + static SAMPLE_COUNT: u64 = 4; static SAMPLE_SIZE: u64 = 10000; -pub fn partial_checksum(path: &str, size: u64) -> Result { +fn read_at(file: &File, offset: u64, size: u64) -> Result> { + let mut buf = vec![0u8; size as usize]; + + #[cfg(target_os = "macos")] + file.read_exact_at(&mut buf, offset)?; + + #[cfg(target_os = "windows")] + file.seek_read(&mut buffer[..], offset)?; + + Ok(buf) +} + +pub fn generate_cas_id(path: &str, size: u64) -> Result { // open file reference let file = File::open(path)?; @@ -21,20 +38,16 @@ pub fn partial_checksum(path: &str, size: u64) -> Result { // if size is small enough, just read the whole thing if SAMPLE_COUNT * SAMPLE_SIZE > size { - let mut buf = vec![0u8; size.try_into()?]; - file.read_exact_at(&mut buf, 0)?; + let buf = read_at(&file, 0, size.try_into()?)?; context.update(&buf); } else { // loop over samples for i in 0..SAMPLE_COUNT { - let start_point = (size / SAMPLE_COUNT) * i; - let mut buf = vec![0u8; SAMPLE_SIZE.try_into()?]; - file.read_exact_at(&mut buf, start_point)?; + let buf = read_at(&file, (size / SAMPLE_COUNT) * i, SAMPLE_SIZE.try_into()?)?; context.update(&buf); } // sample end of file - let mut buf = vec![0u8; SAMPLE_SIZE.try_into()?]; - file.read_exact_at(&mut buf, size - SAMPLE_SIZE)?; + let buf = read_at(&file, size - SAMPLE_SIZE, SAMPLE_SIZE.try_into()?)?; context.update(&buf); } diff --git a/core/src/file/indexer/scan.rs b/core/src/file/indexer/scan.rs index 7f075d117..bb6ece77c 100644 --- a/core/src/file/indexer/scan.rs +++ b/core/src/file/indexer/scan.rs @@ -1,4 +1,4 @@ -use crate::file::cas::checksum::partial_checksum; +use crate::file::cas::checksum::generate_cas_id; use crate::sys::locations::{create_location, LocationResource}; use crate::CoreContext; use anyhow::{anyhow, Result}; @@ -173,9 +173,9 @@ fn prepare_values( None => return Err(anyhow!("{}", file_path.to_str().unwrap_or_default())), }; - let partial_checksum = { + let cas_id = { if !metadata.is_dir() { - let mut x = partial_checksum(&file_path.to_str().unwrap(), metadata.len()).unwrap(); + let mut x = generate_cas_id(&file_path.to_str().unwrap(), metadata.len()).unwrap(); x.truncate(16); x } else { @@ -199,7 +199,7 @@ fn prepare_values( .map(|id| format!("\"{}\"", &id)) .unwrap_or("NULL".to_string()), parsed_date_created, - partial_checksum + cas_id ); println!("{}", values);