Skip to content

dd: truncate on open and check the output type before ftruncate - #16659

Open
kushaldave002 wants to merge 3 commits into
uutils:mainfrom
kushaldave002:fix/16645-dd-truncate-on-open
Open

kushaldave002 wants to merge 3 commits into
uutils:mainfrom
kushaldave002:fix/16645-dd-truncate-on-open

Conversation

@kushaldave002

Copy link
Copy Markdown

Fixes #16645

dd always called ftruncate() after opening the output, so it failed when ftruncate() failed, even when no seek was asked for. With seek=, it wrote to the file even after the truncate step failed.

Changes:

  • Without seek= (and without conv=notrunc or oflag=append), the output is opened with O_TRUNC, so no ftruncate() call is needed.
  • With seek=, dd now runs fstat on the output first. If that fails, it reports cannot fstat and exits before writing anything. It then truncates only regular files, as before.

Both repros from the issue now behave like GNU 9.12:

  • strace -e inject=ftruncate:error=EIO -P t.bin dd if=/dev/null of=t.bin exits 0 with an empty file.
  • strace -e inject=ftruncate:error=EPERM -e inject=fstat:error=EPERM -P big.bin dd ... seek=3 prints dd: cannot fstat 'big.bin': Operation not permitted, exits 1, and leaves the file untouched.

Tests: I added two tests in tests/by-util/test_dd.rs that run dd under strace with fault injection, the same way test_shred.rs does. They are skipped when strace is not available. Both fail without the change.

cargo test --no-default-features --features dd test_dd:: gives 170 passed, 2 ignored. cargo test -p uu_dd, cargo clippy --tests -D warnings, rustfmt, and cspell are clean.

AI disclosure: an AI coding agent wrote this change on my behalf, and I reviewed it before opening the PR. The fix was written from the issue description, not from GNU source.

Copilot AI balanced review requested due to automatic review settings November 17, 2026 22:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Comment thread src/uu/dd/src/dd.rs Outdated
)
.map_err_context(|| translate!("dd-error-failed-to-open", "path" => filename.quote()))?;

// Check the file type before truncating, and fail before writing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not check file type. Catch errno instead.

Comment thread src/uu/dd/src/dd.rs Outdated
// Without a seek, truncate when opening so that no `set_len()` is needed.
// `OpenOptions` does not allow truncating together with appending.
let truncate_on_open =
!settings.oconv.notrunc && settings.seek == 0 && !settings.oflags.append;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with oflag=append and no seek we still go through set_len(0), so a failing ftruncate still breaks it. could we pass O_TRUNC through custom_flags on unix?

@github-actions

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.49 MB (+108 KB, +0.07%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)

Copilot AI balanced review requested due to automatic review settings November 19, 2026 18:24
@kushaldave002

Copy link
Copy Markdown
Author

Thanks both. Updated: oflag=append without seek= now passes O_TRUNC via custom_flags on Unix, so no ftruncate is needed. With seek=, the file type is no longer checked up front; ftruncate runs first, and only on failure do we fstat (as GNU does) to decide whether to report it, which keeps test_failed_truncate_is_reported_and_copy_goes_on passing. Added a strace test for the append case.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Copilot AI balanced review requested due to automatic review settings November 20, 2026 03:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/tail/follow-name is no longer failing!
Congrats! The gnu test tests/tail/pid-pipe is no longer failing!

@codspeed

codspeed Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 55.51%

3 warnings

⚠️ 9 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 15 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 15 benchmarks with a significant change ran on a different major version of their CodSpeed integration

Their head results were measured with codspeed-rust 6.1.0, a different major version than their base results. A major version can change how benchmarks are measured, so the changes might come from this version difference rather than from the code. Comparisons between runs on the same major version are not affected.

See affected benchmarks

⚡ 1 improved benchmark
❌ 14 regressed benchmarks
✅ 3 untouched benchmarks
⏩ 466 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory dd_copy_default 28.7 KB 168.4 KB -82.96%
❌ Memory dd_copy_partial 32 KB 175.1 KB -81.7%
❌ Memory dd_copy_4k_blocks 32 KB 171.3 KB -81.3%
❌ Memory dd_copy_with_skip 32.3 KB 171.7 KB -81.17%
❌ Memory dd_copy_with_seek 32.3 KB 171.7 KB -81.17%
❌ Memory dd_copy_8k_blocks 36 KB 175.4 KB -79.46%
❌ Memory dd_copy_64k_blocks 92 KB 231.4 KB -60.23%
❌ CPU Simulation dd_copy_partial 743.8 µs 1,223.1 µs -39.19%
❌ CPU Simulation dd_copy_64k_blocks 909.2 µs 1,396.2 µs -34.88%
❌ CPU Simulation dd_copy_8k_blocks 1.9 ms 2.4 ms -20.17%
❌ CPU Simulation dd_copy_4k_blocks 2.7 ms 3.2 ms -15.08%
❌ Memory dd_copy_1m_blocks 1 MB 1.2 MB -11.72%
❌ CPU Simulation dd_copy_with_skip 4.9 ms 5.4 ms -8.95%
❌ CPU Simulation dd_copy_with_seek 5 ms 5.5 ms -8.89%
⚡ CPU Simulation dd_copy_1m_blocks 1,464.3 µs 919.3 µs +59.28%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing kushaldave002:fix/16645-dd-truncate-on-open (6ac0b59) with main (12953e7)2

Open in CodSpeed

Footnotes

  1. 466 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (dded1b5) during the generation of this report, so 12953e7 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@sylvestre
sylvestre force-pushed the fix/16645-dd-truncate-on-open branch from b72f775 to ca8e944 Compare November 20, 2026 18:38
Copilot AI balanced review requested due to automatic review settings November 20, 2026 18:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre
sylvestre force-pushed the fix/16645-dd-truncate-on-open branch from ca8e944 to 553ac27 Compare November 21, 2026 14:48
Copilot AI balanced review requested due to automatic review settings November 21, 2026 14:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

kushaldave002 and others added 3 commits November 22, 2026 00:38
Without seek=, open the output with O_TRUNC instead of calling
ftruncate() afterwards, so a failing ftruncate() no longer makes dd
fail. With seek=, fstat the output first and stop before writing
anything if that fails, then only truncate regular files.

Fixes uutils#16645
With `seek=`, a failing `ftruncate` no longer triggers a file-type
check. The errno itself says whether the output can be truncated at
all: `EINVAL` means it cannot (e.g. `/dev/null`, a pipe), so it is
ignored as GNU does. Any other error still stats the output as GNU
does: if even that fails, report `cannot fstat` and stop before
writing; otherwise report the truncate failure, keep copying, and
exit 1 in the end.

This keeps `test_seek_fails_before_writing_when_fstat_fails` and
`test_failed_truncate_is_reported_and_copy_goes_on` passing, and the
GNU test `tests/dd/fail-ftruncate-fstat.sh` still sees the `fstat`
call it expects.
@sylvestre
sylvestre force-pushed the fix/16645-dd-truncate-on-open branch from 553ac27 to 6ac0b59 Compare November 21, 2026 23:38
Copilot AI balanced review requested due to automatic review settings November 21, 2026 23:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre

Copy link
Copy Markdown
Contributor

@codspeedbot fix this regression

@codspeed

codspeed Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

@codspeedbot fix this regression

This PR does not cause the regression, so I made no change. The report compares against the wrong base.

Why the report shows changes

  • The report used 12953e7 as its base, because no main run existed for dded1b5. 12953e7 is older than the main commit that upgraded codspeed-divan-compat from 5.0 to 6.1. Your branch is built with 6.1, so base and head were measured with different major versions of the CodSpeed integration. The report warns about this for all 15 changed benchmarks. Base and head also ran on different hardware.
  • The results do not look like a code change either. dd_copy_1m_blocks gets 59% faster in the same run where the other copies get slower. Every Memory benchmark grows by about the same +140 KB, whatever the block size.

What I measured

  • I compared CI run 6ac0b59 with a newer main run that already uses divan 6.1 (7913995). No dd benchmark changed.
  • I ran the dd benchmarks in CPU simulation on one machine, on the merge base dded1b5 and on your head 6ac0b59. All 9 dd benchmarks were unchanged, within 0.2%. For example, dd_copy_partial measured 552.3 µs on the base and 553.3 µs on the head (comparison).
  • I did not re-measure Memory, because that needs a CodSpeed macro runner and none is enabled for this organization. The change only adds O_TRUNC to the open flags and skips one ftruncate, so it does not allocate anything new.

Next step
You can acknowledge these results as not caused by the PR on CodSpeed. You can also rebase onto current main: the next report will then use a base on divan 6.1, and these changes should go away.

Summary
  • Warming up the performance wizard...
  • Inspect the CodSpeed report and PR diff — Report base 12953e7 predates the divan-compat 6.1 upgrade on main; against a newer main run (7913995) no dd benchmark changed.
  • Measure dd benchmarks on main and PR head locally — Simulation, merge-base dded1b5 vs PR head 6ac0b59
  • Handling comment — Comment handling finished successfully

Learnings updated. See what CodSpeed remembers about this repository.

Was this response helpful? React with 👍 or 👎 to help us improve CodSpeedBot.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dd: output truncation fails or writes anyway when ftruncate fails

4 participants