Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1405,12 +1405,13 @@ fn finalize<T>(
// not yet written.
let wstat_update = output.flush()?;

// Sync the output, if configured to do so.
output.sync()?;

// With `conv=sparse`, trailing zero blocks were skipped, not written.
// Set the final length first so that `fsync`/`fdatasync` covers it.
output.extend_sparse()?;

// Sync the output, if configured to do so.
output.sync()?;

// Print the final read/write statistics.
let wstat = wstat + wstat_update;
let prog_update = ProgUpdate::new(rstat, wstat, start.elapsed(), ProgUpdateType::Final);
Expand Down
35 changes: 35 additions & 0 deletions tests/by-util/test_dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,41 @@ fn test_sparse_notrunc_extends_file() {
assert_eq!(at.metadata("outfile").len(), 1024);
}

/// With `conv=sparse,fsync` (or `fdatasync`), the trailing zero block extends
/// the file before the sync, so the sync covers the final length.
#[rstest]
#[case::fsync("fsync")]
#[case::fdatasync("fdatasync")]
#[cfg(target_os = "linux")]
#[cfg(not(wasi_runner))]
fn test_sparse_extends_before_sync(#[case] sync: &str) {
let (at, _ucmd) = at_and_ucmd!();
at.write_bytes("infile", &[0; 4096]);
let trace = at.plus("trace");
let Ok(out) = Command::new("strace")
.arg("-o")
.arg(&trace)
.args(["-e", "trace=fsync,fdatasync,ftruncate"])
.arg(get_tests_binary())
.arg("dd")
.args(["if=infile", "of=outfile", "bs=4096", "status=none"])
.arg(format!("conv=sparse,{sync}"))
.current_dir(at.as_string())
.output()
else {
return; // missing strace
};
if String::from_utf8_lossy(&out.stderr).contains("ptrace") {
return; // tracing not permitted here
}
assert!(out.status.success());
assert_eq!(at.metadata("outfile").len(), 4096);
let trace = at.read("trace");
let extend = trace.find(", 4096)").expect("final ftruncate");
let synced = trace.find(&format!("{sync}(")).expect("sync call");
assert!(extend < synced, "{trace}");
}

/// `conv=notrunc,sparse` must not shrink an output file longer than the copy.
#[test]
fn test_sparse_notrunc_does_not_shrink() {
Expand Down
Loading