Skip to content

cp: put the backup back when the copy fails after making it - #16320

Open
draarivpatel-ui wants to merge 3 commits into
uutils:mainfrom
draarivpatel-ui:fix-cp-backup-restore
Open

draarivpatel-ui wants to merge 3 commits into
uutils:mainfrom
draarivpatel-ui:fix-cp-backup-restore

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #16240

Summary

With --backup, cp backs up the destination and then copies. If the copy fails after the backup was made, GNU 9.12 puts the backup back in place of dest, so the destination is left as it was before the attempt. uutils kept the backup at dest~ and left dest missing or truncated:

$ echo old > dest; echo secret > secret; chmod 000 secret
$ uutils cp --backup=simple --force secret dest
cp: 'secret' -> 'dest': Permission denied
$ ls
dest~        # dest is gone
$ gnu cp --backup=simple --force secret dest
cp: 'secret' -> 'dest': Permission denied
$ ls
dest dest~   # dest holds 'old'

Fix

copy_file now checks whether a backup file exists after handle_existing_dest ran, and when handle_copy_mode fails, it restores the backup onto dest before returning the error: fs::rename when the backup was a renamed symlink destination, fs::copy for a regular-file backup. Restoring is best-effort; the original copy error is still what gets reported.

The same failure modes the issue describes are covered: any failure inside handle_copy_mode (source open, destination create, stream copy) after the backup.

Testing

  • test_cp_backup_restored_when_copy_fails: a --force copy from an unreadable source fails after the backup; dest and dest~ must both hold the original content.
  • test_cp_backup_restored_when_source_cannot_be_statted: the source disappears between the backup and the stat; dest is restored from dest~.
  • Both fail on main (dest missing) and pass with the fix.
  • cargo test --features cp: 5261 passed, 25 ignored, 0 failed.

Copilot AI balanced review requested due to automatic review settings November 10, 2026 05:10

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.

@draarivpatel-ui

Copy link
Copy Markdown
Author

The CICD workflow runs are gated behind maintainer approval for first-time contributors (action_required), so they are waiting for approval to start. Happy to provide anything needed.

Comment thread tests/by-util/test_cp.rs Outdated

#[test]
#[cfg(unix)]
#[cfg_attr(wasi_runner, ignore = "WASI: no chmod syscall")]

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.

I don't see a chmod here.

Comment thread src/uu/cp/src/cp.rs Outdated
fs::copy(backup_path, dest).map(|_| ())
};

if let Err(err) = restore {

@anastygnome anastygnome Nov 10, 2026 •

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.

/// Restore the backup after a failed copy, matching GNU cp semantics.
fn restore_backup(dest: &Path, backup_path: &Path, was_dest_symlink: bool) {
    // Best-effort restoration; the original copy error is reported.
    if was_dest_symlink {
        let _ = fs::rename(backup_path, dest);
    } else {
        let _ = fs::copy(backup_path, dest);
    }
}

Copilot AI balanced review requested due to automatic review settings November 11, 2026 08:13

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 11, 2026 09:47

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

thanks for this PR!
The "was a backup made" check is wrong both ways: numbered backups are never restored, and a stale dest~ is copied into a dest that never existed (details inline). Having handle_existing_dest return the backup path it made would fix both, and restoring with a rename would match the issue.

also, can you make it smaller ? it is a too large diff

@draarivpatel-ui

Copy link
Copy Markdown
Author

Thanks for the review. Both inline comments and the pre-push findings are addressed in the updated branch.

On tests/by-util/test_cp.rs: the unreadable source is set with at.set_mode("secret", 0). That helper calls fs::set_permissions with PermissionsExt::set_mode on Unix, which is chmod(2). The mode is restored after the command runs. The regression test checks that the failed copy leaves the original destination content in dest and removes the created backup for simple, numbered, and existing backup modes.

On cp.rs: the earlier restore_backup sketch used a best-effort rename for symlink destinations and copy for regular files. The updated code keeps that best-effort rename behavior in restore_backup, but the decision to restore is now gated on both a backup that was actually created and an explicit destination-changed flag. This avoids restoring a stale dest~ when no destination existed (created_backup == None) and covers numbered backups. For symlinks, delete_path already marks destination_changed when replacing a pre-existing entry, while copying a symlink to a non-existent destination leaves created_backup as None.

On Windows: the progress callback declares data: *mut c_void to match the native Win32 LPVOID contract, with a small adapter for the locked windows-sys binding signature. CopyFileExW now passes COPY_FILE_ALLOW_DECRYPTED_DESTINATION as dwcopyflags, matching std::fs::copy. The path helper uses named constants instead of raw ASCII codes, and the callback doc comments are clean rustdoc.

Verification: 6 package tests and 361 cp integration tests pass with zero failures. Clippy with warnings denied and formatting checks pass, including Windows target checks on x86_64 and aarch64. The patch applies cleanly to current upstream main.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 01:08

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

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Binary size comparison:

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

Total size of compared binaries: 155.48 MB (+100 KB, +0.06%)

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

@sylvestre
sylvestre force-pushed the fix-cp-backup-restore branch from 8b3c9e9 to 3d0e841 Compare November 18, 2026 20:28
Copilot AI balanced review requested due to automatic review settings November 18, 2026 20:28

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-cp-backup-restore branch from 3d0e841 to 295c0a7 Compare November 19, 2026 16:37
Copilot AI balanced review requested due to automatic review settings November 19, 2026 16:37

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.

Use relative symlinks in test_cp_backup_restore_preserves_destination_symlink so the destination link target resolves inside the WASI preopened directory instead of appearing dangling.

Place #[uutests::wasi_ignore(NoPermissionBits)] before #[test] on the permission-dependent backup restore tests so the proc macro attaches the ignore attribute before the test harness expands #[test].

Signed-off-by: draarivpatel-ui <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 02: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.

@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!

Copilot AI balanced review requested due to automatic review settings November 20, 2026 17:50

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.

@codspeed

codspeed Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will regress 3 benchmarks

3 warnings, 1 note

⚠️ 123 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

⚠️ 7 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

⚠️ 7 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

⚡ 4 improved benchmarks
❌ 3 regressed benchmarks
✅ 406 untouched benchmarks
⏩ 70 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ CPU Simulation true_consecutive_calls 294.7 ns 350.2 ns -15.86%
❌ CPU Simulation dd_copy_with_skip 4.8 ms 4.9 ms -3.03%
❌ CPU Simulation dd_copy_4k_blocks 2.6 ms 2.7 ms -3.01%
⚡ Memory socket_addrs_direct[100000] 68 KB 8.6 KB ×7.9
⚡ Memory hostname_ip_lookup[100000] 66.4 KB 9.1 KB ×7.3
⚡ CPU Simulation cksum_crc 34.2 ms 33.1 ms +3.36%
⚡ CPU Simulation cksum_multiple_files 51.1 ms 49.5 ms +3.27%

Tip

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


Comparing draarivpatel-ui:fix-cp-backup-restore (af3c1d2) with main (9311390)2

Open in CodSpeed

Footnotes

  1. 70 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 (276ffe9) during the generation of this report, so 9311390 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

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.

cp: put the backup back when the copy fails after making it

4 participants