From 38d40a2c5d4b553f524d87755b8e2e0e47928b8a Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Wed, 7 Aug 2024 17:09:14 +1200 Subject: Refactor the file device This is the Windows side of the refactoring job. The windows crate has been added as a dependency in order to get a list of available drives by drive letter, and a virtual top-level root directory has been implemented in the Windows code to make it possible for programs to hierarchically navigate between available drives. --- src/devices/file.rs | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) (limited to 'src/devices/file.rs') diff --git a/src/devices/file.rs b/src/devices/file.rs index 26e14da..bf185d7 100644 --- a/src/devices/file.rs +++ b/src/devices/file.rs @@ -1,7 +1,6 @@ mod bedrock_file_path; mod buffered_file; mod circular_path_buffer; -mod directory_child; mod directory_listing; mod entry; mod operations; @@ -9,7 +8,6 @@ mod operations; pub use bedrock_file_path::*; pub use buffered_file::*; pub use circular_path_buffer::*; -pub use directory_child::*; pub use directory_listing::*; pub use entry::*; use operations::*; @@ -100,32 +98,32 @@ impl FileDevice { /// Opens the entry at the given path. pub fn open_entry(&mut self, path: BedrockFilePath) -> Result<(), ()> { - macro_rules! unres { - ($result:expr) => { match $result {Ok(v)=>v,Err(_)=>return Err(())} }; + match path.entry_type() { + Some(EntryType::File) => { + let open_result = std::fs::OpenOptions::new() + .read(self.enable_read) + .write(self.enable_write) + .open(path.as_path()); + // Keep the current entry open if we can't open the new path. + if let Ok(file) = open_result { + self.close_entry(); + self.name_buffer.populate(path.as_buffer()); + self.entry = Some((Entry::File(BufferedFile::new(file)), path)); + return Ok(()); + }; + } + Some(EntryType::Directory) => { + // Keep the current entry open if we can't open the new path. + if let Some(listing) = DirectoryListing::from_path(&path) { + self.close_entry(); + self.name_buffer.populate(path.as_buffer()); + self.entry = Some((Entry::Directory(listing), path)); + return Ok(()); + }; + } + // The entry either doesn't exist or is not a file or directory. + None => (), } - let absolute_path = path.as_path(); - let metadata = unres!(std::fs::metadata(&absolute_path)); - if metadata.is_file() { - let open_result = std::fs::OpenOptions::new() - .read(self.enable_read) - .write(self.enable_write) - .open(&absolute_path); - // Keep the current entry open if we can't open the new path. - if let Ok(file) = open_result { - self.close_entry(); - self.name_buffer.populate(path.as_buffer()); - self.entry = Some((Entry::File(BufferedFile::new(file)), path)); - return Ok(()); - }; - } else if metadata.is_dir() { - // Keep the current entry open if we can't open the new path. - if let Some(listing) = path.directory_listing() { - self.close_entry(); - self.name_buffer.populate(path.as_buffer()); - self.entry = Some((Entry::Directory(listing), path)); - return Ok(()); - }; - }; return Err(()); } -- cgit v1.2.3-70-g09d2