1use std::fs::{self, File};
2use std::io::{self, Read, Seek, SeekFrom};
3use std::path::Path;
4
5pub fn clone_or_copy_file<P: AsRef<Path>, Q: AsRef<Path>>(
9 src: P,
10 dest: Q,
11) -> io::Result<Option<u64>> {
12 if let Ok(_) = crate::clone_file(&src, &dest) {
13 Ok(None)
14 } else {
15 fs::copy(&src, &dest).and_then(|x| Ok(Some(x)))
16 }
17}
18
19pub fn clone_or_copy_file_range<P: AsRef<Path>, Q: AsRef<Path>>(
23 src: P,
24 src_offset: u64,
25 src_length: u64,
26 dest: Q,
27 dest_offset: u64,
28) -> io::Result<Option<u64>> {
29 if let Ok(_) = crate::clone_file_range(&src, src_offset, src_length, &dest, dest_offset) {
30 Ok(None)
31 } else {
32 todo!()
33 }
34}
35
36pub fn copy_file_range<P: AsRef<Path>, Q: AsRef<Path>>(
42 src: P,
43 src_offset: u64,
44 src_length: u64,
45 dest: Q,
46 dest_offset: u64,
47) -> io::Result<u64> {
48 let mut src_file = File::open(src)?;
49 let mut dest_file = File::options().write(true).create(true).open(dest)?;
50
51 src_file.seek(SeekFrom::Start(src_offset))?;
52 let mut src_file = src_file.take(src_length);
53
54 dest_file.seek(SeekFrom::Start(dest_offset))?;
55
56 io::copy(&mut src_file, &mut dest_file)
57}