Skip to content
Closed
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
1 change: 1 addition & 0 deletions .vscode/cspell.dictionaries/workspace.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ vmsplice

# * vars/libc
COMFOLLOW
EXDEV
FILENO
FTSENT
HOSTSIZE
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/uu/mv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ uucore = { workspace = true, features = [
] }
thiserror = { workspace = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true, features = ["Win32_Foundation"] }

[target.'cfg(unix)'.dependencies]
libc = { workspace = true }

[[bin]]
name = "mv"
path = "src/main.rs"
18 changes: 17 additions & 1 deletion src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,14 +650,30 @@ fn rename(
Ok(())
}

#[cfg(unix)]
fn is_err_not_same_device(err: &std::io::Error) -> bool {
matches!(err.raw_os_error(), Some(libc::EXDEV))
}

#[cfg(windows)]
fn is_err_not_same_device(err: &std::io::Error) -> bool {
let errno = windows_sys::Win32::Foundation::ERROR_NOT_SAME_DEVICE as i32;
matches!(err.raw_os_error(), Some(e) if e == errno)
}

/// A wrapper around `fs::rename`, so that if it fails, we try falling back on
/// copying and removing.
fn rename_with_fallback(
from: &Path,
to: &Path,
multi_progress: Option<&MultiProgress>,
) -> io::Result<()> {
if fs::rename(from, to).is_err() {
if let Err(err) = fs::rename(from, to) {
// We will only copy if they're not on the same device, otherwise we'll report an error.
if !is_err_not_same_device(&err) {
return Err(err);
}

// Get metadata without following symlinks
let metadata = from.symlink_metadata()?;
let file_type = metadata.file_type();
Expand Down
46 changes: 46 additions & 0 deletions tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,52 @@ fn test_acl() {
assert!(compare_xattrs(&file, &file_target));
}

#[ignore = "broken on windows"]
#[test]
#[cfg(windows)]
fn test_move_should_not_fallback_to_copy() {
use std::os::windows::fs::OpenOptionsExt;

let (at, mut ucmd) = at_and_ucmd!();

let locked_file = "a_file_is_locked";
let locked_file_path = at.plus(locked_file);
let file = std::fs::OpenOptions::new()
.create(true)
.write(true)
.share_mode(
uucore::windows_sys::Win32::Storage::FileSystem::FILE_SHARE_READ
| uucore::windows_sys::Win32::Storage::FileSystem::FILE_SHARE_WRITE,
)
.open(locked_file_path);

let target_file = "target_file";
ucmd.arg(locked_file).arg(target_file).fails();

assert!(at.file_exists(locked_file));
assert!(!at.file_exists(target_file));

drop(file);
}

#[test]
#[cfg(unix)]
fn test_move_should_not_fallback_to_copy() {
let (at, mut ucmd) = at_and_ucmd!();

let readonly_dir = "readonly_dir";
let locked_file = "readonly_dir/a_file_is_locked";
at.mkdir(readonly_dir);
at.touch(locked_file);
at.set_mode(readonly_dir, 0o555);

let target_file = "target_file";
ucmd.arg(locked_file).arg(target_file).fails();

assert!(at.file_exists(locked_file));
assert!(!at.file_exists(target_file));
}

// Todo:

// $ at.touch a b
Expand Down
Loading