From a974a61cb824c3f01357cccd394965798bf511f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Sat, 7 Oct 2017 16:31:42 +0200 Subject: [PATCH] Fix dir entries pointing to root directory. Fixes 'ls dir/..'. --- src/dir.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index cde09b5..7c56fa9 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -191,18 +191,19 @@ impl <'a, 'b> DirEntry<'a, 'b> { } pub fn to_file(&self) -> File<'a, 'b> { - if self.is_dir() { - panic!("This is a directory"); - } + assert!(!self.is_dir(), "Not a file entry"); File::new(self.first_cluster(), Some(self.data.size), self.fs) } pub fn to_dir(&self) -> Dir<'a, 'b> { - if !self.is_dir() { - panic!("This is a file"); + assert!(self.is_dir(), "Not a directory entry"); + match self.first_cluster() { + Some(n) => { + let file = File::new(Some(n), None, self.fs); + Dir::new(DirRawStream::File(file), self.fs) + }, + None => self.fs.root_dir(), } - let file = File::new(self.first_cluster(), None, self.fs); - Dir::new(DirRawStream::File(file), self.fs) } pub fn len(&self) -> u64 { @@ -250,7 +251,7 @@ impl <'a, 'b> Dir<'a, 'b> { fn split_path<'c>(path: &'c str) -> (&'c str, Option<&'c str>) { let mut path_split = path.trim_matches('/').splitn(2, "/"); - let comp = path_split.next().unwrap(); + let comp = path_split.next().unwrap(); // safe unwrap - splitn always returns at least one element let rest_opt = path_split.next(); (comp, rest_opt) }