Skip to content

Commit

Permalink
fix buffer copy error
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Jul 17, 2024
1 parent 47639c0 commit 6119939
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,23 @@ fn copy_chunk_int(file_path: &str, src: (u64, u64), dst: (u64, u64)) -> anyhow::
.truncate(false)
.open(file_path)?;

file.seek(SeekFrom::Start(src.0))?;
//seek to source start

//read buffer
let mut bytes_left = src.1 - src.0;
let mut buffer = vec![0u8; std::cmp::min(1000 * 1000, bytes_left as usize)];
let mut bytes_written = 0;
while bytes_left > 0 {
file.seek(SeekFrom::Start(src.0 + bytes_written))?;
//seek to source start
let bytes_read = std::cmp::min(buffer.len() as u64, bytes_left);
buffer.resize(bytes_read as usize, 0);
file.read_exact(buffer.as_mut_slice())?;
bytes_left -= bytes_read;

//seek to destination start
file.seek(SeekFrom::Start(dst.0))?;
file.seek(SeekFrom::Start(dst.0 + bytes_written))?;
file.write_all(&buffer)?;
bytes_written += bytes_read;
}

Ok(())
Expand Down

0 comments on commit 6119939

Please sign in to comment.