Skip to content

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

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

kushaldave002 wants to merge 2 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.

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
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.

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