From f7726c0ad9152f6e992dcdab95085d0f05ca2d0e Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sun, 28 Aug 2016 17:41:12 -0700 Subject: [PATCH] Root of VFS can now be browsed --- src/collection.rs | 51 +++++++++++++++++++++++------------------------ src/vfs.rs | 4 ++++ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/collection.rs b/src/collection.rs index 8a34605..01ad620 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -15,13 +15,8 @@ pub struct Song { } impl Song { - pub fn read(collection: &Collection, file: &fs::DirEntry) -> Result { - let file_meta = try!(file.metadata()); - assert!(file_meta.is_file()); - - let file_path = file.path(); - let file_path = file_path.as_path(); - let virtual_path = try!(collection.vfs.real_to_virtual(file_path)); + pub fn read(collection: &Collection, path: &Path) -> Result { + let virtual_path = try!(collection.vfs.real_to_virtual(path)); let path_string = try!(virtual_path.to_str().ok_or(PError::PathDecoding)); let display_name = virtual_path.file_stem().unwrap(); @@ -43,14 +38,9 @@ pub struct Directory { impl Directory { pub fn read(collection: &Collection, - file: &fs::DirEntry) + path: &Path) -> Result { - let file_meta = try!(file.metadata()); - assert!(file_meta.is_dir()); - - let file_path = file.path(); - let file_path = file_path.as_path(); - let virtual_path = try!(collection.vfs.real_to_virtual(file_path)); + let virtual_path = try!(collection.vfs.real_to_virtual(path)); let path_string = try!(virtual_path.to_str().ok_or(PError::PathDecoding)); let display_name = virtual_path.iter().last().unwrap(); @@ -148,27 +138,36 @@ impl Collection { Ok(()) } - pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), PError> { + fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), PError> { self.vfs.mount(name, real_path) } pub fn browse(&self, path: &Path) -> Result, PError> { - let full_path = try!(self.vfs.virtual_to_real(path)); - let mut out = vec![]; - for file in try!(fs::read_dir(full_path)) { - let file = try!(file); - let file_meta = try!(file.metadata()); - if file_meta.is_file() { - let song = try!(Song::read(self, &file)); - out.push(CollectionFile::Song(song)); - } else if file_meta.is_dir() { - let directory = try!(Directory::read(self, &file)); + + if path.components().count() == 0 { + let mount_points = self.vfs.get_mount_points(); + for (_, target) in mount_points { + let directory = try!(Directory::read(self, target.as_path())); out.push(CollectionFile::Directory(directory)); } + } else { + let full_path = try!(self.vfs.virtual_to_real(path)); + for file in try!(fs::read_dir(full_path)) { + let file = try!(file); + let file_meta = try!(file.metadata()); + if file_meta.is_file() { + let song = try!(Song::read(self, file.path().as_path())); + out.push(CollectionFile::Song(song)); + } else if file_meta.is_dir() { + let directory = try!(Directory::read(self, file.path().as_path())); + out.push(CollectionFile::Directory(directory)); + } + } } + Ok(out) } @@ -179,7 +178,7 @@ impl Collection { let file: fs::DirEntry = try!(file); let file_meta = try!(file.metadata()); if file_meta.is_file() { - let song = try!(Song::read(self, &file)); + let song = try!(Song::read(self, file.path().as_path())); acc.push(song); } else { let explore_path = file.path(); diff --git a/src/vfs.rs b/src/vfs.rs index 13b0855..e3d69b1 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -46,6 +46,10 @@ impl Vfs { } Err(PError::PathNotInVfs) } + + pub fn get_mount_points(&self) -> &HashMap { + return &self.mount_points; + } } #[test]