From 674d1f818235776e877623481643903e2a6ec0fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Sun, 17 Jun 2018 21:46:54 +0200 Subject: [PATCH] Fix failing in Dir::create_file/dir if existing destination entry has wrong type --- src/dir.rs | 5 +++-- tests/write.rs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index ad1e851..87eef2b 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -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) { // check if file or directory is expected 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); } @@ -193,7 +194,7 @@ impl <'a, T: ReadWriteSeek + 'a> Dir<'a, T> { } // this is final filename in the path 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 { // directory does not exist - create it Err(ref err) if err.kind() == ErrorKind::NotFound => { diff --git a/tests/write.rs b/tests/write.rs index f9a6ad4..05d18cd 100644 --- a/tests/write.rs +++ b/tests/write.rs @@ -160,6 +160,15 @@ fn test_create_file(fs: FileSystem) { } names = dir.iter().map(|r| r.unwrap().file_name()).collect::>(); 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] @@ -208,6 +217,14 @@ fn test_create_dir(fs: FileSystem) { names = subdir.iter().map(|r| r.unwrap().file_name()).collect::>(); 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::>(); + 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]