Skip to content

Commit

Permalink
cp: document sparse_copy_without_hole more
Browse files Browse the repository at this point in the history
  • Loading branch information
neyo8826 committed Sep 13, 2024
1 parent ad18516 commit 0e1d535
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/uu/cp/src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ where
}
let src_fd = src_file.as_raw_fd();
let mut current_offset: isize = 0;
let step = std::cmp::min(size, 16 * 1024 * 1024) as usize; // 16 MiB
// Maximize the data read at once to 16 MiB to avoid memory hogging with large files
// 16 MiB chunks should saturate an SSD
let step = std::cmp::min(size, 16 * 1024 * 1024) as usize;
let mut buf: Vec<u8> = vec![0x0; step];
loop {
let result = unsafe { libc::lseek(src_fd, current_offset.try_into().unwrap(), SEEK_DATA) }
Expand All @@ -160,7 +162,9 @@ where
return Err(std::io::Error::last_os_error());
}
let len: isize = hole - current_offset;
// Read and write data in chunks of `step` while reusing the same buffer
for i in (0..len).step_by(step) {
// Ensure we don't read past the end of the file or the start of the next hole
let read_len = std::cmp::min((len - i) as usize, step);
let buf = &mut buf[..read_len];
src_file.read_exact_at(buf, (current_offset + i) as u64)?;
Expand Down

0 comments on commit 0e1d535

Please sign in to comment.