1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

dd: correctly set file size when conv=sparse

Before this commit, if `sparsefile` were a regular file of non-zero
size whose contents are all null bytes, then

    dd if=sparsefile of=outfile conv=notrunc

would have resulted in `outfile` having zero size as reported by
`stat`. After this commit, `outfile` will have the same size as
`sparsefile` (even if the contents are represented sparsely by the
filesystem).
This commit is contained in:
Jeffrey Finkelstein 2022-11-27 21:01:25 -05:00 committed by Sylvestre Ledru
parent e307f624e8
commit 9632db4f86
2 changed files with 47 additions and 1 deletions

View file

@ -1394,3 +1394,27 @@ fn test_sync_delayed_reader() {
assert_eq!(&output.stdout, &expected);
assert_eq!(&output.stderr, b"0+8 records in\n4+0 records out\n");
}
/// Test for making a sparse copy of the input file.
#[test]
fn test_sparse() {
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("infile"))
.unwrap()
.set_len(1024 * 1024)
.unwrap();
// Perform a sparse copy.
ucmd.args(&["bs=32K", "if=infile", "of=outfile", "conv=sparse"])
.succeeds();
// The number of bytes in the file should be accurate though the
// number of blocks stored on disk may be zero.
assert_eq!(at.metadata("infile").len(), at.metadata("outfile").len());
}