diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2024-06-09 10:33:32 +1200 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2024-06-09 10:33:32 +1200 |
commit | e80aa38c10f9e08d70ef2e4bf130a779c1c6a84e (patch) | |
tree | 243f173de6b4027542f3aedc4ea0df4f196dc4c7 | |
parent | 059bf509b8ea8f6d1d61943530a2d967f4c9752c (diff) | |
download | bedrock-pc-e80aa38c10f9e08d70ef2e4bf130a779c1c6a84e.zip |
Update file device for release
Removed the sandbox directory of the file device, so the file device is
now sandboxed to the root directory. The sandboxing code has been left
intact for future use.
Permission flags have been added to the file device to allow each
category of file operation to be enabled or disabled in the emulator.
The default directory is set to be the current working directory from
when the emulator boots. This is the directory which is used when the
program attempts to ascend to the parent directory when no entry is
currently open.
-rw-r--r-- | src/devices/file.rs | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/src/devices/file.rs b/src/devices/file.rs index 236f9d1..234da7c 100644 --- a/src/devices/file.rs +++ b/src/devices/file.rs @@ -24,6 +24,7 @@ pub struct FileDevice { /// The path to which the file device is confined. Files and directories /// outside of this directory cannot be accessed. pub base_path: PathBuf, + pub default_path: PathBuf, pub open_buffer: CircularPathBuffer, pub move_buffer: CircularPathBuffer, @@ -34,13 +35,22 @@ pub struct FileDevice { pub move_success: bool, pub new_pointer: u32, pub new_length: u32, + + pub enable_create: bool, + pub enable_move: bool, + pub enable_delete: bool, + pub enable_read: bool, + pub enable_write: bool, } impl FileDevice { pub fn new() -> Self { Self { - base_path: PathBuf::from("/home/ben/Sandbox"), - + base_path: PathBuf::from("/"), + default_path: match std::env::current_dir() { + Ok(dir) => PathBuf::from(dir), + Err(_) => PathBuf::from("/"), + }, open_buffer: CircularPathBuffer::new(), move_buffer: CircularPathBuffer::new(), name_buffer: CircularPathBuffer::new(), @@ -50,6 +60,12 @@ impl FileDevice { move_success: false, new_pointer: 0, new_length: 0, + + enable_create: true, + enable_move: true, + enable_delete: false, + enable_read: true, + enable_write: true, } } @@ -74,7 +90,6 @@ impl FileDevice { self.close_entry(); if !is_blank_path(&relative_path) { if let Ok(path) = self.attach_base(&relative_path) { - println!("Attempting to open entry at: {path:?}"); let _ = self.open_entry(&path); }; } @@ -93,7 +108,11 @@ impl FileDevice { if !path.starts_with(&self.base_path) { return Err(()); } let metadata = raise_on_err!(metadata(&path)); if metadata.is_file() { - if let Ok(file) = OpenOptions::new().read(true).write(true).open(&path) { + let open_result = OpenOptions::new() + .read(self.enable_read) + .write(self.enable_write) + .open(&path); + if let Ok(file) = open_result { self.close_entry(); let file_entry = Entry::File(BufferedFile::new(file)); self.entry = Some((file_entry, path.to_owned())); @@ -117,18 +136,24 @@ impl FileDevice { if let Some(dest) = self.move_buffer.push_byte(byte) { if let Some((_, source)) = &self.entry { if is_blank_path(&dest) { - eprintln!("Attempting to delete entry at: {source:?}"); - self.move_success = delete_entry(&source); + match self.enable_delete { + true => self.move_success = delete_entry(&source), + false => self.move_success = false, + } } else if let Ok(destination) = self.attach_base(&dest) { - eprintln!("Attempting to move entry to: {destination:?}"); - self.move_success = move_entry(&source, &destination); + match self.enable_move { + true => self.move_success = move_entry(&source, &destination), + false => self.move_success = false, + } } } else { if is_blank_path(&dest) { self.move_success = false; } else if let Ok(destination) = self.attach_base(&dest) { - eprintln!("Attempting to create entry at: {destination:?}"); - self.move_success = create_file(&destination); + match self.enable_create { + true => self.move_success = create_file(&destination), + false => self.move_success = false, + } } } self.close_entry(); @@ -142,6 +167,9 @@ impl FileDevice { let path = parent_path.to_owned(); let _ = self.open_entry(&path); } + } else { + let default_path = self.default_path.to_owned(); + let _ = self.open_entry(&default_path); } } |