From 0b22cfa0b361eabdb6bc39a3c22910f00dc5a587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Wed, 30 May 2018 17:54:03 +0200 Subject: [PATCH] Avoid building short name when traversing path --- src/dir.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index 4855a96..97d28cc 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -138,11 +138,13 @@ impl <'a, 'b> Dir<'a, 'b> { /// Creates new file or opens existing without truncating. pub fn create_file(&mut self, path: &str) -> io::Result> { let (name, rest_opt) = split_path(path); - let mut short_name_gen = ShortNameGenerator::new(name); - let r = self.find_entry(name, Some(&mut short_name_gen)); match rest_opt { - Some(rest) => r?.to_dir().create_file(rest), + // path contains more than 1 component + Some(rest) => self.find_entry(name, None)?.to_dir().create_file(rest), None => { + // this is final filename in the path + let mut short_name_gen = ShortNameGenerator::new(name); + let r = self.find_entry(name, Some(&mut short_name_gen)); match r { Err(ref err) if err.kind() == ErrorKind::NotFound => { let short_name = short_name_gen.generate()?; @@ -158,11 +160,13 @@ impl <'a, 'b> Dir<'a, 'b> { /// Creates new directory or opens existing. pub fn create_dir(&mut self, path: &str) -> io::Result> { let (name, rest_opt) = split_path(path); - let mut short_name_gen = ShortNameGenerator::new(name); - let r = self.find_entry(name, Some(&mut short_name_gen)); match rest_opt { - Some(rest) => r?.to_dir().create_dir(rest), + // path contains more than 1 component + Some(rest) => self.find_entry(name, None)?.to_dir().create_dir(rest), None => { + // this is final filename in the path + let mut short_name_gen = ShortNameGenerator::new(name); + let r = self.find_entry(name, Some(&mut short_name_gen)); match r { Err(ref err) if err.kind() == ErrorKind::NotFound => { // alloc cluster for directory data