Trying to be better about file reading.
@@ -21,7 +21,7 @@ use serde_cbor::{de, to_vec, Value};
use std::fs;
use std::fs::File;
use std::fs::Permissions;
-use std::io::{Read, Write};
+use std::io::{BufRead, BufReader, Read, Write};
use std::os::unix::fs::MetadataExt;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
@@ -196,24 +196,28 @@ pub fn local_import(source_path: &str) -> Result<(String, u32, u32), String> {
let mut hasher = Blake2s::new(HASH_SIZE);
{
let mut input = File::open(&source_path).unwrap();
+ let mut reader = BufReader::with_capacity(CHUNK_SIZE * 2, input);
let mut output = File::create(&temp_path).unwrap();
// Need to bring in blake2fs here to create hash
loop {
- let mut chunk = vec![0u8; CHUNK_SIZE];
- match input.read(&mut chunk) {
- Ok(n) => {
- if n == 0 {
+ let length = {
+ let chunk = match reader.fill_buf() {
+ Ok(c) => c,
+ Err(e) => return Err(format!("Failed to read chunk from source {:?}", e)),
+ };
+ if chunk.len() == 0 {
output.sync_all().unwrap();
break;
}
- hasher.update(&chunk[0..n]);
- if let Err(e) = output.write(&chunk[0..n]) {
+ hasher.update(&chunk);
+ if let Err(e) = output.write(&chunk) {
return Err(format!("failed to write chunk {:?}", e));
}
- }
- Err(e) => return Err(format!("failed to read chunk from source {:?}", e)),
- }
+ chunk.len()
+ };
+ reader.consume(length);
+ thread::sleep(Duration::from_millis(2));
}
}
let hash_result = hasher.finalize();
