Inital setup

This commit is contained in:
jaby 2022-10-16 15:58:38 +02:00
parent af643d75fc
commit 685af072bb
4 changed files with 55 additions and 42 deletions

View File

@ -20,9 +20,9 @@ fn populate() -> Result<CDDesc, Error> {
folder
};
desc.root.add_dir(folder);
desc.root.add_file(file);
desc.root.add_file(file2);
desc.root.borrow_mut().add_dir(folder);
desc.root.borrow_mut().add_file(file);
desc.root.borrow_mut().add_file(file2);
desc.calculate_lbas();
Ok(desc)
@ -37,8 +37,8 @@ fn run_main() -> Result<(), Error> {
Layout::SystemArea(_) => println!("SystemArea:"),
Layout::PVD(_) => println!("PVD:"),
Layout::PathTables => println!("PathTables:"),
Layout::Directory{name, properties} => println!("Dir: {} @{}-{}", name, properties.lba, properties.overwrite_size_bytes.unwrap_or(0)),
Layout::File(file) => println!("File: {} @{}-{}", file, file.properties.lba, file.properties.overwrite_size_bytes.unwrap_or(0)),
Layout::Directory(dir) => println!("Dir: {} @{}-{}", dir.borrow().name, dir.borrow().properties.lba, dir.borrow().properties.overwrite_size_bytes.unwrap_or(0)),
Layout::File(file) => println!("File: {} @{}-{}", file.borrow(), file.borrow().properties.lba, file.borrow().properties.overwrite_size_bytes.unwrap_or(0)),
}
}
Ok(())

View File

@ -1,4 +1,5 @@
use super::*;
use std::{cell::RefCell, rc::Rc};
const CURRENT_DIR_NAME:&'static str = "/x00";
const PARENT_DIR_NAME:&'static str = "/x01";
@ -22,12 +23,12 @@ impl DirectoryRecordMember {
}
}
pub fn collect_path_table_member(root: &Directory) -> Vec<PathTableMember> {
fn collect_path_table_for(collection: &mut Vec<PathTableMember>, dirs: &Vec<Directory>, parent_id: usize) {
pub fn collect_path_table_member(root: Rc<RefCell<Directory>>) -> Vec<PathTableMember> {
fn collect_path_table_for(collection: &mut Vec<PathTableMember>, dirs: &Vec<Rc<RefCell<Directory>>>, parent_id: usize) {
let mut cur_dirs = Vec::new();
for dir in dirs {
cur_dirs.push(PathTableMember{name: dir.name.as_string().unwrap_or("".to_owned()), parent_table_id: parent_id});
cur_dirs.push(PathTableMember{name: dir.borrow().name.as_string().unwrap_or("".to_owned()), parent_table_id: parent_id});
}
cur_dirs.sort_by(|a, b| {
@ -37,22 +38,24 @@ pub fn collect_path_table_member(root: &Directory) -> Vec<PathTableMember> {
collection.append(&mut cur_dirs);
for dir in dirs {
collect_path_table_for(collection, &dir.dirs, parent_id + 1);
collect_path_table_for(collection, &dir.borrow().dirs, parent_id + 1);
}
}
let mut collection = Vec::new();
let root = root.borrow();
collection.push(PathTableMember{name: CURRENT_DIR_NAME.to_owned(), parent_table_id: 1});
collect_path_table_for(&mut collection, &root.dirs, 1);
collection
}
pub fn collect_directory_record_member(files: &Vec<File>, dirs: &Vec<Directory>) -> Vec<DirectoryRecordMember> {
pub fn collect_directory_record_member(files: &Vec<Rc<RefCell<File>>>, dirs: &Vec<Rc<RefCell<Directory>>>) -> Vec<DirectoryRecordMember> {
let mut collection = Vec::new();
collection.push(DirectoryRecordMember::Directory{name: CURRENT_DIR_NAME.to_owned()});
collection.push(DirectoryRecordMember::Directory{name: PARENT_DIR_NAME.to_owned()});
for file in files {
let file = file.borrow();
if !file.properties.is_hidden {
if let Some(name) = file.name.as_string() {
collection.push(DirectoryRecordMember::File{name});
@ -61,8 +64,8 @@ pub fn collect_directory_record_member(files: &Vec<File>, dirs: &Vec<Directory>)
}
for dir in dirs {
if !dir.properties.is_hidden {
if let Some(name) = dir.name.as_string() {
if !dir.borrow().properties.is_hidden {
if let Some(name) = dir.borrow().name.as_string() {
collection.push(DirectoryRecordMember::Directory{name});
}
}

View File

@ -1,7 +1,8 @@
use super::*;
use std::cell::RefCell;
pub type MemoryLayout<'a> = Vec<Layout<'a>>;
pub type MemoryLayoutMut<'a> = Vec<LayoutMut<'a>>;
pub type MemoryLayout = Vec<Layout>;
pub type MemoryLayoutMut = Vec<LayoutMut>;
macro_rules! declare_memory_layout {
($($val:ident),*) => {
@ -18,31 +19,33 @@ macro_rules! declare_memory_layout {
pub fn new(parent: &$($val),* CDDesc) -> Vec<[< Layout$($val:camel),* >]> {
let mut layout = Vec::new();
layout.push([< Layout$($val:camel),* >]::SystemArea(&$($val),* parent.system_area));
layout.push([< Layout$($val:camel),* >]::PVD(&$($val),* parent.pvd));
layout.push([< Layout$($val:camel),* >]::SystemArea(parent.system_area.clone()));
layout.push([< Layout$($val:camel),* >]::PVD(parent.pvd.clone()));
layout.push(([< Layout$($val:camel),* >]::PathTables));
[< add_dir_and_subdir $(_$val),* >](&mut layout, &$($val),* parent.root);
[< add_dir_and_subdir $(_$val),* >](&mut layout, parent.root.clone());
layout
}
}
pub enum [< Layout$($val:camel),* >]<'a> {
SystemArea(&'a $($val),* SystemArea),
PVD(&'a $($val),* PrimaryVolumeDescriptor),
pub enum [< Layout$($val:camel),* >] {
SystemArea(Rc<RefCell<SystemArea>>),
PVD(Rc<RefCell<PrimaryVolumeDescriptor>>),
PathTables,
Directory{name: &'a $($val),* DirectoryName, properties: &'a $($val),* Properties},
File(&'a $($val),* File)
Directory(Rc<RefCell<Directory>>),
File(Rc<RefCell<File>>)
}
fn [< add_dir_and_subdir $(_$val),* >]<'a>(layout: &mut Vec<[< Layout$($val:camel),* >]::<'a>>, dir: &'a $($val),* Directory) {
layout.push([< Layout$($val:camel),* >]::Directory{name: &$($val),* dir.name, properties: &$($val),* dir.properties});
fn [< add_dir_and_subdir $(_$val),* >]<'a>(layout: &mut Vec<[< Layout$($val:camel),* >]>, dir: Rc<RefCell<Directory>>) {
layout.push([< Layout$($val:camel),* >]::Directory(dir.clone()));
let $($val),* dir = dir.[< borrow $(_$val),* >]();
for file in dir.files.[< iter$(_$val),* >]() {
layout.push([< Layout$($val:camel),* >]::File(file));
layout.push([< Layout$($val:camel),* >]::File(file.clone()));
}
for dir in dir.dirs.[< iter$(_$val),* >]() {
[< add_dir_and_subdir $(_$val),* >](layout, dir);
[< add_dir_and_subdir $(_$val),* >](layout, dir.clone());
}
}
}

View File

@ -4,11 +4,12 @@ pub mod layout;
use cdtypes::types::{cdstring::DString, dir_record::DirectoryRecord, helper::{round_bytes_mode2_form1, sector_count_mode2_form1}, sector::*, path_table::PathTableL};
use layout::LayoutMut;
use tool_helper::Error;
use std::{cell::RefCell, rc::Rc};
pub struct CDDesc {
system_area: SystemArea,
pvd: PrimaryVolumeDescriptor,
pub root: Directory
system_area: Rc<RefCell<SystemArea>>,
pvd: Rc<RefCell<PrimaryVolumeDescriptor>>,
pub root: Rc<RefCell<Directory>>
}
impl CDDesc {
@ -16,7 +17,7 @@ impl CDDesc {
pub fn new() -> CDDesc {
match Directory::new("root") {
Ok(root) => CDDesc{system_area: SystemArea::new(), pvd: PrimaryVolumeDescriptor::new(), root},
Ok(root) => CDDesc{system_area: Rc::new(RefCell::new(SystemArea::new())), pvd: Rc::new(RefCell::new(PrimaryVolumeDescriptor::new())), root: Rc::new(RefCell::new(root))},
Err(error) => panic!("Creating root directory failed with: {}", error)
}
}
@ -33,7 +34,7 @@ impl CDDesc {
let mut path_table_properties = {
let mut size_bytes = 0;
helper::collect_path_table_member(&self.root).into_iter().for_each(|element| {
helper::collect_path_table_member(self.root.clone()).into_iter().for_each(|element| {
println!("PT: {} ^{}", element.name, element.parent_table_id);
size_bytes += PathTableL::calculate_size_for(element.name.as_ref());
});
@ -41,9 +42,9 @@ impl CDDesc {
size_bytes = round_bytes_mode2_form1(size_bytes)*4;
Properties{lba: 0, overwrite_size_bytes: Some(size_bytes), is_hidden: false}
};
let mut cur_lba = 0;
let mut cur_lba = 0;
Self::for_each_dir_mut(&mut self.root, &|dir| {dir.update_content_size();});
Self::for_each_dir_mut(self.root.clone(), &|dir| {dir.update_content_size();});
// Now layout iterate?
for element in self.get_memory_layout_mut() {
@ -54,18 +55,22 @@ impl CDDesc {
match element {
LayoutMut::SystemArea(system_area) => {
let mut system_area = system_area.borrow_mut();
cur_lba = update_lba(&mut system_area.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW);
},
LayoutMut::PVD(pvd) => {
let mut pvd = pvd.borrow_mut();
cur_lba = update_lba(&mut pvd.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW);
},
LayoutMut::PathTables => {
cur_lba = update_lba(&mut path_table_properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW);
},
LayoutMut::Directory{name: _, properties} => {
cur_lba = update_lba(properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW)
LayoutMut::Directory(dir) => {
let mut dir = dir.borrow_mut();
cur_lba = update_lba(&mut dir.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW)
},
LayoutMut::File(file) => {
let mut file = file.borrow_mut();
cur_lba = update_lba(&mut file.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW);
}
}
@ -74,10 +79,12 @@ impl CDDesc {
}
}
fn for_each_dir_mut<T: Fn(&mut Directory)>(dir: &mut Directory, function: &T) {
function(dir);
fn for_each_dir_mut<T: Fn(&mut Directory)>(dir: Rc<RefCell<Directory>>, function: &T) {
let mut dir = dir.borrow_mut();
function(&mut dir);
for sub_dir in &mut dir.dirs {
Self::for_each_dir_mut(sub_dir, function);
Self::for_each_dir_mut(sub_dir.clone(), function);
}
}
}
@ -109,8 +116,8 @@ impl PrimaryVolumeDescriptor {
pub struct Directory {
pub name: DirectoryName,
pub properties: Properties,
files: Vec<File>,
dirs: Vec<Directory>
files: Vec<Rc<RefCell<File>>>,
dirs: Vec<Rc<RefCell<Directory>>>
}
impl Directory {
@ -119,11 +126,11 @@ impl Directory {
}
pub fn add_dir(&mut self, dir: Directory) {
self.dirs.push(dir);
self.dirs.push(Rc::new(RefCell::new(dir)));
}
pub fn add_file(&mut self, file: File) {
self.files.push(file);
self.files.push(Rc::new(RefCell::new(file)));
}
fn update_content_size(&mut self) {