summaryrefslogtreecommitdiff
path: root/src/devices/file/circular_path_buffer.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2024-08-06 19:11:46 +1200
committerBen Bridle <bridle.benjamin@gmail.com>2024-08-06 19:12:43 +1200
commit65b53003e8de9543ba25a3b3d3cace399b92dc1d (patch)
tree90ba625f4522553136e55b452c9f2f91f29d9753 /src/devices/file/circular_path_buffer.rs
parent406b4d3ea56a19c0389b4214a7d8d15bf3028eb8 (diff)
downloadbedrock-pc-65b53003e8de9543ba25a3b3d3cace399b92dc1d.zip
Refactor the file device
The major feature of this refactor is the creation of BedrockFilePath, a type which can be losslessly converted into an operating-system specific path or a Bedrock-style byte path. It also prevents file paths which can't be represented as Bedrock-style byte paths from being constructed, and allows the use of a base directory which acts as an inescapable sandbox. BedrockFilePath will support the creation of a virtual root directory on Windows which will allow navigation between drive letters via the standard hierarchical navigation ports. This commit has been tested on Linux, but not yet Windows. Further work is expected before the new code will work on Windows.
Diffstat (limited to 'src/devices/file/circular_path_buffer.rs')
-rw-r--r--src/devices/file/circular_path_buffer.rs45
1 files changed, 21 insertions, 24 deletions
diff --git a/src/devices/file/circular_path_buffer.rs b/src/devices/file/circular_path_buffer.rs
index e5d903b..9d1dea6 100644
--- a/src/devices/file/circular_path_buffer.rs
+++ b/src/devices/file/circular_path_buffer.rs
@@ -1,5 +1,3 @@
-use super::*;
-
pub struct CircularPathBuffer {
buffer: [u8; 256],
pointer: u8,
@@ -10,29 +8,29 @@ impl CircularPathBuffer {
Self { buffer: [0; 256] , pointer: 0 }
}
- pub fn clear(&mut self) {
- self.buffer.fill(0);
+ /// Clear the buffer, returning the previous buffer contents.
+ pub fn clear(&mut self) -> [u8; 256] {
self.pointer = 0;
+ std::mem::replace(&mut self.buffer, [0; 256])
}
- pub fn populate(&mut self, bytes: &[u8]) {
- self.clear();
- if bytes.len() > 255 {
- unreachable!(
- "Attempted to populate CircularPathBuffer with {} bytes: {:?}",
- bytes.len(), String::from_utf8_lossy(bytes)
- )
- }
- let self_slice = &mut self.buffer[..bytes.len()];
- self_slice.copy_from_slice(&bytes);
+ /// Reset the pointer and hot-swap the byte buffer.
+ pub fn populate(&mut self, buffer: [u8; 256]) {
+ self.pointer = 0;
+ self.buffer = buffer;
}
+ /// Move internal pointer to either the start of the path or the file name.
+ ///
+ /// If value is non-zero, the pointer will be moved to the byte
+ /// directly following the final forward-slash.
pub fn set_pointer(&mut self, value: u8) {
self.pointer = 0;
+ // Set the pointer to the start of the filename if value is truthy.
if value != 0x00 {
for (i, c) in self.buffer.iter().enumerate() {
match c {
- b'/' => self.pointer = (i + 1) as u8,
+ b'/' => self.pointer = (i as u8).saturating_add(1),
0x00 => break,
_ => continue,
}
@@ -40,23 +38,22 @@ impl CircularPathBuffer {
}
}
+ /// Read a single byte from the buffer.
pub fn read_byte(&mut self) -> u8 {
let pointer = self.pointer as usize;
self.pointer = self.pointer.wrapping_add(1);
self.buffer[pointer]
}
- // Returns an unsanitized relative path.
- pub fn push_byte(&mut self, value: u8) -> Option<PathBuf> {
- if value == 0x00 {
- let pointer = self.pointer as usize;
- let path = bytes_to_path(&self.buffer[..pointer]);
- self.clear();
- Some(path)
+ /// Write a single byte to the buffer.
+ ///
+ /// If a null-byte is written, the buffer will be cleared and returned.
+ pub fn push_byte(&mut self, byte: u8) -> Option<[u8; 256]> {
+ if byte == 0x00 {
+ Some(self.clear())
} else {
- let pointer = self.pointer as usize;
+ self.buffer[self.pointer as usize] = byte;
self.pointer = self.pointer.wrapping_add(1);
- self.buffer[pointer] = value;
None
}
}