Merge pull request #4030 from jfinkels/cp-reflink-never-sparse-always

cp: use sparse copy on --sparse=always
This commit is contained in:
Sylvestre Ledru 2022-11-11 08:43:15 +01:00 committed by GitHub
commit 76cc328f50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View file

@ -113,6 +113,7 @@ pub(crate) fn copy_on_write(
source_is_fifo: bool, source_is_fifo: bool,
) -> CopyResult<()> { ) -> CopyResult<()> {
let result = match (reflink_mode, sparse_mode) { let result = match (reflink_mode, sparse_mode) {
(ReflinkMode::Never, SparseMode::Always) => sparse_copy(source, dest),
(ReflinkMode::Never, _) => std::fs::copy(source, dest).map(|_| ()), (ReflinkMode::Never, _) => std::fs::copy(source, dest).map(|_| ()),
(ReflinkMode::Auto, SparseMode::Always) => sparse_copy(source, dest), (ReflinkMode::Auto, SparseMode::Always) => sparse_copy(source, dest),

View file

@ -2318,3 +2318,30 @@ fn test_copy_contents_fifo() {
assert!(output.stderr.is_empty()); assert!(output.stderr.is_empty());
assert_eq!(at.read("outfile"), "foo"); assert_eq!(at.read("outfile"), "foo");
} }
#[cfg(target_os = "linux")]
#[test]
fn test_reflink_never_sparse_always() {
let (at, mut ucmd) = at_and_ucmd!();
// Create a file and make it a large sparse file.
//
// On common Linux filesystems, setting the length to one megabyte
// should cause the file to become a sparse file, but it depends
// on the system.
std::fs::File::create(at.plus("src"))
.unwrap()
.set_len(1024 * 1024)
.unwrap();
ucmd.args(&["--reflink=never", "--sparse=always", "src", "dest"])
.succeeds()
.no_stdout()
.no_stderr();
at.file_exists("dest");
let src_metadata = std::fs::metadata(at.plus("src")).unwrap();
let dest_metadata = std::fs::metadata(at.plus("dest")).unwrap();
assert_eq!(src_metadata.blocks(), dest_metadata.blocks());
assert_eq!(dest_metadata.len(), 1024 * 1024);
}