Calculate size for DirectoryRecords

This commit is contained in:
jaby 2022-10-11 21:12:28 +02:00
parent 49093c0dbf
commit a2d628d190
3 changed files with 35 additions and 21 deletions

View File

@ -24,7 +24,7 @@ fn populate() -> Result<CDDesc, Error> {
desc.root.add_file(file);
desc.root.add_file(file2);
desc.update_dir_records();
desc.calculate_lbas();
Ok(desc)
}
@ -36,8 +36,8 @@ fn run_main() -> Result<(), Error> {
match element {
Layout::SystemArea(_) => println!("SystemArea:"),
Layout::PVD(_) => println!("PVD:"),
Layout::Directory{name, properties} => println!("Dir: {} @{}", name, properties.lba),
Layout::File(file) => println!("File: {} @{}", file, file.properties.lba),
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)),
}
}
Ok(())

View File

@ -17,6 +17,8 @@ impl DirectoryRecordMember {
pub fn collect_directory_record_member(files: &Vec<File>, dirs: &Vec<Directory>) -> Vec<DirectoryRecordMember> {
let mut collection = Vec::new();
collection.push(DirectoryRecordMember::Directory{name: "\x00".to_owned()});
collection.push(DirectoryRecordMember::Directory{name: "\x01".to_owned()});
for file in files {
if !file.properties.is_hidden {
if let Some(name) = file.name.as_string() {
@ -35,4 +37,4 @@ pub fn collect_directory_record_member(files: &Vec<File>, dirs: &Vec<Directory>)
collection.sort_by(|a, b| {a.get_name().cmp(b.get_name())});
collection
}
}

View File

@ -1,7 +1,7 @@
mod helper;
pub mod layout;
use cdtypes::types::cdstring::DString;
use cdtypes::types::{cdstring::DString, dir_record::DirectoryRecord};
use tool_helper::Error;
pub struct CDDesc {
@ -26,16 +26,17 @@ impl CDDesc {
layout::DefaultLayoutMut::new(self)
}
pub fn update_dir_records(&mut self) {
fn update_dir_record_of(dir: &mut Directory) {
dir.update_content();
for sub_dir in &mut dir.dirs {
update_dir_record_of(sub_dir);
}
}
pub fn calculate_lbas(&mut self) {
Self::for_each_dir_mut(&mut self.root, &|dir| {dir.update_content_size();});
update_dir_record_of(&mut self.root);
// Now layout iterate?
}
fn for_each_dir_mut<T: Fn(&mut Directory)>(dir: &mut Directory, function: &T) {
function(dir);
for sub_dir in &mut dir.dirs {
Self::for_each_dir_mut(sub_dir, function);
}
}
}
@ -77,7 +78,18 @@ impl Directory {
self.files.push(file);
}
fn update_content(&mut self) {
fn update_content_size(&mut self) {
let dir_member = helper::collect_directory_record_member(&self.files, &self.dirs);
let mut size_bytes = 0;
for member in dir_member {
size_bytes += DirectoryRecord::calculate_size_for(member.get_name().as_str(), true);
}
self.properties.overwrite_size_bytes = Some(size_bytes);
}
fn _update_content(&mut self) {
let dir_member = helper::collect_directory_record_member(&self.files, &self.dirs);
println!("{} updating content", self.name.as_string().unwrap_or("<No name>".to_owned()));
@ -96,12 +108,12 @@ impl std::fmt::Display for Directory {
pub struct File {
pub name: FileName,
pub properties: Properties,
content: Vec<u8>
_content: Vec<u8>
}
impl File {
pub fn new(file_name: &str) -> Result<File, Error> {
Ok(File{name: FileName::from_str(file_name)?, properties: Properties::default(), content: Vec::new()})
Ok(File{name: FileName::from_str(file_name)?, properties: Properties::default(), _content: Vec::new()})
}
}
@ -179,14 +191,14 @@ impl std::fmt::Display for FileName {
}
pub struct Properties {
pub lba: usize,
pub overwrite_size_sectors: Option<usize>,
pub is_hidden: bool
pub lba: usize,
pub overwrite_size_bytes: Option<usize>,
pub is_hidden: bool
}
impl Default for Properties {
fn default() -> Self {
Properties{lba: 0, overwrite_size_sectors: None, is_hidden: false}
Properties{lba: 0, overwrite_size_bytes: None, is_hidden: false}
}
}