Skip to content

mv: add --exchange to atomically swap two paths#13215

Open
sylvestre wants to merge 4 commits into
uutils:mainfrom
sylvestre:mv-exchange
Open

mv: add --exchange to atomically swap two paths#13215
sylvestre wants to merge 4 commits into
uutils:mainfrom
sylvestre:mv-exchange

Conversation

@sylvestre

Copy link
Copy Markdown
Contributor

Should make test tests/mv/mv-exchange.sh pass

@github-actions

github-actions Bot commented Jun 29, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/misc/io-errors (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/symlink (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/mv/mv-exchange is no longer failing!
Note: The gnu test tests/expand/bounded-memory is now being skipped but was previously passing.
Note: The gnu test tests/unexpand/bounded-memory is now being skipped but was previously passing.
Congrats! The gnu test tests/tail/tail-n0f is now passing!

@codspeed-hq

codspeed-hq Bot commented Jun 29, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
❌ 2 regressed benchmarks
✅ 328 untouched benchmarks
⏩ 46 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation mv_force_overwrite 62.8 ms 66 ms -4.83%
Simulation mv_single_file 62.6 ms 65.4 ms -4.26%
Simulation du_max_depth_balanced_tree[(6, 4, 10)] 26 ms 25.2 ms +3.37%

Tip

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


Comparing sylvestre:mv-exchange (2c878bd) with main (c9ecfa0)

Open in CodSpeed

Footnotes

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

Comment thread tests/by-util/test_mv.rs
Comment thread tests/by-util/test_mv.rs

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 would also test the scenario where source and target are the same file.

Comment thread tests/by-util/test_mv.rs
Should make test tests/mv/mv-exchange.sh pass
sylvestre added 3 commits July 5, 2026 14:15
Matches GNU mv: 'mv --exchange a b c dir/' exchanges each source with
the file of the same name inside dir/. Also add a same-file check
(mirroring regular mv's behavior) and strengthen existing exchange
tests with actual error-message assertions, per review feedback.
The test asserts a 'cannot move' stderr message that only occurs when
--exchange actually attempts the renameat2 syscall; on other platforms
it short-circuits to a 'not supported' error instead, matching the cfg
gating already used by the other --exchange tests.
Comment thread src/uu/mv/src/mv.rs
Comment on lines +603 to +607
let target_dir = paths.last().unwrap();
if !target_dir.is_dir() {
return Err(MvError::TargetNotADirectory(target_dir.quote().to_string()).into());
}
for source in &paths[..paths.len() - 1] {

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 would use split_last as it allows a cleaner for loop:

Suggested change
let target_dir = paths.last().unwrap();
if !target_dir.is_dir() {
return Err(MvError::TargetNotADirectory(target_dir.quote().to_string()).into());
}
for source in &paths[..paths.len() - 1] {
let (target_dir, sources) = paths.split_last().unwrap();
if !target_dir.is_dir() {
return Err(MvError::TargetNotADirectory(target_dir.quote().to_string()).into());
}
for source in sources {

Comment thread src/uu/mv/src/mv.rs
/// each of the other operands is exchanged with the file of the same name
/// inside that directory (matching GNU `mv --exchange a b c dir/`).
fn exchange_paths(paths: &[PathBuf], opts: &Options) -> UResult<()> {
match paths.len() {

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.

Here it might be an option to match directly on paths.

Comment thread src/uu/mv/src/mv.rs
Comment on lines +647 to +654
#[cfg(not(any(target_os = "linux", target_os = "android")))]
{
let _ = (from, to, opts);
Err(USimpleError::new(
1,
translate!("mv-error-exchange-not-supported"),
))
}

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.

This snippet feels out of place: the user gets some of the other "--exchange"-related errors, but if he provides the correct params, he suddenly gets a "not supported" error. Maybe this error should be returned in uumain?

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.

2 participants