From 012bae12b580cd514b879c127d2abf9b8fc23d29 Mon Sep 17 00:00:00 2001 From: Amanda Sternberg Date: Mon, 15 Sep 2025 12:33:17 +0200 Subject: [PATCH] Refactor safe_rename to use crossesDevices and added comments --- rslib/src/media/files.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rslib/src/media/files.rs b/rslib/src/media/files.rs index b0b06241d..f0c75c965 100644 --- a/rslib/src/media/files.rs +++ b/rslib/src/media/files.rs @@ -320,10 +320,12 @@ pub(crate) fn mtime_as_i64>(path: P) -> io::Result { .as_millis() as i64) } +/// On POSIX this is EXDEV (error 18) and on Windows it is ERROR_NOT_SAME_DEVICE +/// (error 17). pub(crate) fn safe_rename(src: &Path, dst: &Path) -> io::Result<()> { match fs::rename(src, dst) { Ok(_) => Ok(()), - Err(e) if e.raw_os_error() == Some(18) => { + Err(e) if e.kind() == io::ErrorKind::CrossesDevices => { fs::copy(src, dst)?; fs::remove_file(src)?; Ok(()) @@ -566,6 +568,8 @@ mod test { } #[test] fn safe_rename_moves_file() { + // This test only verifies that ´safe_rename´calls back to copy+remove logic + // when needed. It does not simulate a real cross-device move. let dir = tempdir().unwrap(); let src = dir.path().join("test_src.txt"); let dst = dir.path().join("test_dst.txt");