summaryrefslogtreecommitdiff
path: root/src/devices/file.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/devices/file.rs')
-rw-r--r--src/devices/file.rs52
1 files changed, 25 insertions, 27 deletions
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(());
}