MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / copy_sparse_file

Function copy_sparse_file

crates/openshell-driver-vm/src/rootfs.rs:244–279  ·  view source on GitHub ↗
(source: &Path, dest: &Path)

Source from the content-addressed store, hash-verified

242}
243
244fn copy_sparse_file(source: &Path, dest: &Path) -> Result<(), String> {
245 const BUFFER_SIZE: usize = 1024 * 1024;
246
247 let mut source_file =
248 File::open(source).map_err(|e| format!("open {}: {e}", source.display()))?;
249 let mut dest_file =
250 File::create(dest).map_err(|e| format!("create {}: {e}", dest.display()))?;
251 let mut buffer = vec![0_u8; BUFFER_SIZE];
252 let mut size = 0_u64;
253
254 loop {
255 let read = source_file
256 .read(&mut buffer)
257 .map_err(|e| format!("read {}: {e}", source.display()))?;
258 if read == 0 {
259 break;
260 }
261
262 if buffer[..read].iter().all(|byte| *byte == 0) {
263 let skip =
264 i64::try_from(read).map_err(|_| format!("sparse copy chunk too large: {read}"))?;
265 dest_file
266 .seek(SeekFrom::Current(skip))
267 .map_err(|e| format!("seek {}: {e}", dest.display()))?;
268 } else {
269 dest_file
270 .write_all(&buffer[..read])
271 .map_err(|e| format!("write {}: {e}", dest.display()))?;
272 }
273 size += read as u64;
274 }
275
276 dest_file
277 .set_len(size)
278 .map_err(|e| format!("size {}: {e}", dest.display()))
279}
280
281#[cfg(test)]
282fn append_rootfs_tree_to_archive(

Callers 1

Calls 1

readMethod · 0.45

Tested by

no test coverage detected