Merge branch 'development'

This commit is contained in:
Bas Wiel, van de 2024-05-04 18:24:59 +02:00
commit 6c59c83c95
5 changed files with 65 additions and 15 deletions

View File

@ -114,13 +114,20 @@ impl DiskBuilder {
/// assumes that each address in `file.allocated_clusters` can hold a cluster of data
/// of size defined by `filesystem.get_sectors_per_cluster() * 512`.
pub fn write_file_to_storage(&mut self, file: &DirEntry) -> Result<(), std::io::Error> {
if file.entry_type != DirEntryType::File && file.entry_type != DirEntryType::SysFile {
if file.entry_type != DirEntryType::File
&& file.entry_type != DirEntryType::SysFile
&& file.entry_type != DirEntryType::Directory
{
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"This method only works on files, not on other entry types.",
));
}
let bytes = file.get_data(); // Assuming this returns a &[u8]
if bytes.len() == 0 {
// Don't touch anything if we don't have any bytes.
return Ok(());
}
let cluster_size = self.filesystem.get_sectors_per_cluster() * 512;
let chunks = bytes.chunks(cluster_size);
let addresses = &file.allocated_clusters; // Use a reference
@ -251,18 +258,42 @@ impl DiskBuilder {
/// - Errors can include but are not limited to I/O errors like disk write failures.
///
pub fn write_dirs(&mut self) -> Result<(), std::io::Error> {
let sector_offset: u32 = self.filesystem.reserved_sector_count as u32
+ (self.filesystem.fat_copies as u32 * self.filesystem.sectors_per_fat as u32);
let mut bytes = Vec::new();
let mut sectors: Vec<[u8; 512]> = Vec::new();
self.write_root_dir()?;
// Run through all the root dir's children and get their byte-wise representation.
for entry in self.filesystem.root_directory.get_children() {
// Push all those raw bytes into the bytes vector for further processing
for byte in entry.as_bytes()? {
bytes.push(byte);
for entry in &self.filesystem.root_directory.children.clone() {
if entry.is_directory() {
self.write_directory_recursive(entry)?;
} else {
self.write_file_to_storage(entry)?;
}
}
Ok(())
}
// General method to write any directory recursively
fn write_directory_recursive(&mut self, dir: &DirEntry) -> Result<(), std::io::Error> {
self.write_file_to_storage(dir)?;
for entry in &dir.children {
if entry.is_directory() {
if entry.directory_name() != "." && entry.directory_name() != ".." {
self.write_file_to_storage(entry)?;
}
self.write_directory_recursive(entry)?;
} else {
if entry.directory_name() != "." && entry.directory_name() != ".." {
self.write_file_to_storage(entry)?;
}
}
}
Ok(())
}
fn write_root_dir(&mut self) -> Result<(), std::io::Error> {
let sector_offset: u32 = self.filesystem.reserved_sector_count as u32
+ (self.filesystem.fat_copies as u32 * self.filesystem.sectors_per_fat as u32);
let bytes = self.filesystem.root_directory.children_bytes()?;
let mut sectors: Vec<[u8; 512]> = Vec::new();
// Write the bytes off to disk
let mut current_chunk: [u8; 512] = [0; 512];
@ -345,9 +376,11 @@ impl DiskBuilder {
self.write_file_to_storage(&file)?;
}
// REMOVE THIS
self.filesystem.mkdir(None, "DOS")?;
// Last action: write the allocation table copies to the disk
self.write_allocation_tables()?;
self.write_dirs()?;
self.write_allocation_tables()?;
self.disk.commit_storage()?;
Ok(())
}

View File

@ -392,6 +392,7 @@ impl Fat {
let allocated_clusters: Vec<u32> = vec![new_start_cluster];
let parent_entry = parent.unwrap_or(&mut self.root_directory);
let parent_clusters = vec![parent_entry.get_start_cluster()];
// Instantiate the new entry.
let mut new_entry = DirEntry::new(
@ -418,7 +419,7 @@ impl Fat {
"..",
DirEntryType::Directory,
None,
&allocated_clusters,
&parent_clusters,
0,
None,
)?);

View File

@ -347,6 +347,7 @@ impl DirEntry {
pub fn add_child(&mut self, child: DirEntry) {
self.children.push(child);
self.data = self.children_bytes().unwrap();
}
pub fn count_children(&self) -> usize {
@ -364,6 +365,23 @@ impl DirEntry {
pub fn get_data(&self) -> &[u8] {
&self.data.as_slice()
}
pub fn is_directory(&self) -> bool {
match self.get_type() {
DirEntryType::Directory => true,
DirEntryType::RootDirectory => true,
_ => false,
}
}
pub fn children_bytes(&self) -> Result<Vec<u8>, std::io::Error> {
let mut bytes = Vec::new();
for child in &self.children {
bytes.extend_from_slice(&child.as_bytes()?);
}
Ok(bytes)
}
}
#[cfg(test)]

View File

@ -7,7 +7,7 @@ pub struct GameMetadata {
title: String,
publisher: String,
year: u32,
comment: String,
comment: Option<String>,
}
impl fmt::Display for GameMetadata {

View File

@ -3,8 +3,6 @@ metadata:
title: Alley Cat
publisher: IBM
year: 1984
comment: Testing DOSContainer, not functional yet.
filename: alleycat.zip
diskname: alleycat.vhd
disktype: pcxt
disksize: 10