Allow renaming to the same name

This commit is contained in:
Rafał Harabień 2018-06-17 22:26:07 +02:00
parent 8c1dae9f0c
commit 8995a43bc4
3 changed files with 10 additions and 0 deletions

View File

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

View File

@ -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<T>) -> bool {
self.entry_pos == other.entry_pos
}
/// Returns `File` struct for this entry.
///
/// Panics if this is not a file.

View File

@ -276,6 +276,8 @@ fn test_rename_file(fs: FileSystem) {
let names = entries.iter().map(|r| r.file_name()).collect::<Vec<_>>();
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());
}