From 8995a43bc42258a13e497bfa47e163b612ea3948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Sun, 17 Jun 2018 22:26:07 +0200 Subject: [PATCH] Allow renaming to the same name --- src/dir.rs | 4 ++++ src/dir_entry.rs | 4 ++++ tests/write.rs | 2 ++ 3 files changed, 10 insertions(+) diff --git a/src/dir.rs b/src/dir.rs index f4a0520..001eaef 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -302,6 +302,10 @@ impl <'a, T: ReadWriteSeek + 'a> Dir<'a, T> { let mut short_name_gen = ShortNameGenerator::new(dst_name); let r = dst_dir.find_entry(dst_name, None, Some(&mut short_name_gen)); if r.is_ok() { + // check if source and destination entry is the same + if e.is_same_entry(&r.unwrap()) { + return Ok(()); + } return Err(io::Error::new(ErrorKind::AlreadyExists, "destination file already exists")) } // free long and short name entries diff --git a/src/dir_entry.rs b/src/dir_entry.rs index ed77ce3..171ddfa 100644 --- a/src/dir_entry.rs +++ b/src/dir_entry.rs @@ -666,6 +666,10 @@ impl <'a, T: ReadWriteSeek> DirEntry<'a, T> { DirEntryEditor::new(self.data.clone(), self.entry_pos) } + pub(crate) fn is_same_entry(&self, other: &DirEntry) -> bool { + self.entry_pos == other.entry_pos + } + /// Returns `File` struct for this entry. /// /// Panics if this is not a file. diff --git a/tests/write.rs b/tests/write.rs index 87397e4..91a8655 100644 --- a/tests/write.rs +++ b/tests/write.rs @@ -276,6 +276,8 @@ fn test_rename_file(fs: FileSystem) { let names = entries.iter().map(|r| r.file_name()).collect::>(); assert_eq!(names, ["long.txt", "short.txt", "very", "very-long-dir-name", "moved-file.txt"]); + assert!(root_dir.rename("moved-file.txt", &root_dir, "moved-file.txt").is_ok()); + let new_stats = fs.stats().unwrap(); assert_eq!(new_stats.free_clusters(), stats.free_clusters()); }