refactor(storage-server): add SectorId, FileName, PackedPath newtypes, eliminate raw primitives and DRY violations
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::block_device::BlockDevice;
|
||||
use crate::block_device::{BlockDevice, SectorId};
|
||||
|
||||
const SECTOR_SIZE: usize = 512;
|
||||
const MAX_SECTORS: usize = 64;
|
||||
@@ -26,8 +26,8 @@ impl BlockDevice for MockBlockDevice {
|
||||
self.sectors
|
||||
}
|
||||
|
||||
fn read_sector(&mut self, sector: u32, buf: &mut [u8]) -> bool {
|
||||
let idx = sector as usize;
|
||||
fn read_sector(&mut self, sector: SectorId, buf: &mut [u8]) -> bool {
|
||||
let idx = sector.raw() as usize;
|
||||
if idx >= self.sectors || buf.len() < SECTOR_SIZE {
|
||||
return false;
|
||||
}
|
||||
@@ -35,8 +35,8 @@ impl BlockDevice for MockBlockDevice {
|
||||
true
|
||||
}
|
||||
|
||||
fn write_sector(&mut self, sector: u32, buf: &[u8]) -> bool {
|
||||
let idx = sector as usize;
|
||||
fn write_sector(&mut self, sector: SectorId, buf: &[u8]) -> bool {
|
||||
let idx = sector.raw() as usize;
|
||||
if idx >= self.sectors || buf.len() < SECTOR_SIZE {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,24 +1,22 @@
|
||||
use crate::filesystem::{DirEntry, FileHandle, FileSystem};
|
||||
use crate::filesystem::{DirEntry, FileHandle, FileName, FileSystem};
|
||||
|
||||
const MAX_FILES: usize = 16;
|
||||
const MAX_FILE_SIZE: usize = 256;
|
||||
|
||||
struct FileEntry {
|
||||
name: [u8; 32],
|
||||
name_len: u8,
|
||||
name: FileName,
|
||||
data: [u8; MAX_FILE_SIZE],
|
||||
size: usize,
|
||||
open: bool,
|
||||
}
|
||||
|
||||
impl FileEntry {
|
||||
fn empty() -> Self {
|
||||
fn new(name: FileName) -> Self {
|
||||
Self {
|
||||
name: [0; 32],
|
||||
name_len: 0,
|
||||
name,
|
||||
data: [0; MAX_FILE_SIZE],
|
||||
size: 0,
|
||||
open: false,
|
||||
open: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,9 +35,9 @@ impl MockFileSystem {
|
||||
}
|
||||
|
||||
fn find_by_name(&self, path: &[u8]) -> Option<usize> {
|
||||
for i in 0..self.count {
|
||||
for i in 0..MAX_FILES {
|
||||
if let Some(entry) = &self.files[i] {
|
||||
if &entry.name[..entry.name_len as usize] == path {
|
||||
if entry.name == *path {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
@@ -82,16 +80,14 @@ impl FileSystem for MockFileSystem {
|
||||
|
||||
fn list_dir(&mut self, _path: &[u8], entries: &mut [DirEntry]) -> Option<usize> {
|
||||
let mut count = 0;
|
||||
for i in 0..self.count {
|
||||
for i in 0..MAX_FILES {
|
||||
if count >= entries.len() { break; }
|
||||
if let Some(file) = &self.files[i] {
|
||||
let mut entry = DirEntry::empty();
|
||||
let len = file.name_len as usize;
|
||||
entry.name[..len].copy_from_slice(&file.name[..len]);
|
||||
entry.name_len = file.name_len;
|
||||
entry.size = file.size as u32;
|
||||
entry.is_dir = false;
|
||||
entries[count] = entry;
|
||||
entries[count] = DirEntry {
|
||||
name: file.name,
|
||||
size: file.size,
|
||||
is_dir: false,
|
||||
};
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
@@ -99,15 +95,11 @@ impl FileSystem for MockFileSystem {
|
||||
}
|
||||
|
||||
fn create(&mut self, path: &[u8]) -> Option<FileHandle> {
|
||||
if self.count >= MAX_FILES || path.len() > 32 {
|
||||
if self.count >= MAX_FILES {
|
||||
return None;
|
||||
}
|
||||
let idx = self.count;
|
||||
let mut entry = FileEntry::empty();
|
||||
entry.name[..path.len()].copy_from_slice(path);
|
||||
entry.name_len = path.len() as u8;
|
||||
entry.open = true;
|
||||
self.files[idx] = Some(entry);
|
||||
self.files[idx] = Some(FileEntry::new(FileName::from_bytes(path)));
|
||||
self.count += 1;
|
||||
Some(FileHandle::new(idx as u16))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user