forked from M-Labs/rust-fatfs
Fix failing in Dir::create_file/dir if existing destination entry has wrong type
This commit is contained in:
parent
4f08acf4ab
commit
674d1f8182
@ -116,7 +116,8 @@ impl <'a, T: ReadWriteSeek + 'a> Dir<'a, T> {
|
|||||||
if e.file_name().eq_ignore_ascii_case(name) || e.short_file_name().eq_ignore_ascii_case(name) {
|
if e.file_name().eq_ignore_ascii_case(name) || e.short_file_name().eq_ignore_ascii_case(name) {
|
||||||
// check if file or directory is expected
|
// check if file or directory is expected
|
||||||
if is_dir.is_some() && Some(e.is_dir()) != is_dir {
|
if is_dir.is_some() && Some(e.is_dir()) != is_dir {
|
||||||
return Err(io::Error::new(ErrorKind::NotFound, "unexpected file type in a path"))
|
let error_msg = if e.is_dir() { "Is a directory" } else { "Not a directory" };
|
||||||
|
return Err(io::Error::new(ErrorKind::Other, error_msg));
|
||||||
}
|
}
|
||||||
return Ok(e);
|
return Ok(e);
|
||||||
}
|
}
|
||||||
@ -193,7 +194,7 @@ impl <'a, T: ReadWriteSeek + 'a> Dir<'a, T> {
|
|||||||
}
|
}
|
||||||
// this is final filename in the path
|
// this is final filename in the path
|
||||||
let mut short_name_gen = ShortNameGenerator::new(name);
|
let mut short_name_gen = ShortNameGenerator::new(name);
|
||||||
let r = self.find_entry(name, Some(false), Some(&mut short_name_gen));
|
let r = self.find_entry(name, Some(true), Some(&mut short_name_gen));
|
||||||
match r {
|
match r {
|
||||||
// directory does not exist - create it
|
// directory does not exist - create it
|
||||||
Err(ref err) if err.kind() == ErrorKind::NotFound => {
|
Err(ref err) if err.kind() == ErrorKind::NotFound => {
|
||||||
|
@ -160,6 +160,15 @@ fn test_create_file(fs: FileSystem) {
|
|||||||
}
|
}
|
||||||
names = dir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
names = dir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
||||||
assert_eq!(names.len(), 4 + 512/32);
|
assert_eq!(names.len(), 4 + 512/32);
|
||||||
|
// check creating existing file opens it
|
||||||
|
{
|
||||||
|
let mut file = root_dir.create_file("very/long/path/new-file-with-long-name.txt").unwrap();
|
||||||
|
let mut content = String::new();
|
||||||
|
file.read_to_string(&mut content).unwrap();
|
||||||
|
assert_eq!(&content, &TEST_STR);
|
||||||
|
}
|
||||||
|
// check using create_file with existing directory fails
|
||||||
|
assert!(root_dir.create_file("very").is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -208,6 +217,14 @@ fn test_create_dir(fs: FileSystem) {
|
|||||||
names = subdir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
names = subdir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
||||||
assert_eq!(names, [".", "..", "test.txt", "new-dir-with-long-name"]);
|
assert_eq!(names, [".", "..", "test.txt", "new-dir-with-long-name"]);
|
||||||
}
|
}
|
||||||
|
// check if creating existing directory returns it
|
||||||
|
{
|
||||||
|
let subdir = root_dir.create_dir("very").unwrap();
|
||||||
|
names = subdir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
||||||
|
assert_eq!(names, [".", "..", "long"]);
|
||||||
|
}
|
||||||
|
// check using create_dir with existing file fails
|
||||||
|
assert!(root_dir.create_dir("very/long/path/test.txt").is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user