Back to where build 64 was, but in much better shape now!

This commit is contained in:
Bas Wiel, van de 2024-05-03 15:03:05 +02:00
parent fa2d258298
commit 22c9e546cf
1 changed files with 81 additions and 7 deletions

View File

@ -186,7 +186,10 @@ impl DiskBuilder {
/// Writes the allocation tables to the disk for the specified partition.
pub fn write_allocation_tables(&mut self) -> Result<(), std::io::Error> {
let allocation_table_bytes = self.filesystem.allocation_table.as_bytes(self.filesystem.fat_type)?;
let allocation_table_bytes = self
.filesystem
.allocation_table
.as_bytes(self.filesystem.fat_type)?;
let mut sectors: Vec<[u8; 512]> = Vec::new();
let mut current_chunk: [u8; 512] = [0; 512];
@ -213,7 +216,11 @@ impl DiskBuilder {
let mut counter = 0;
while counter < self.filesystem.fat_copies {
for (index, sector) in sectors.iter().enumerate() {
self.partition.write_sector(&mut self.disk, sector_offset as u32 + index as u32, *sector)?;
self.partition.write_sector(
&mut self.disk,
sector_offset as u32 + index as u32,
*sector,
)?;
}
counter += 1;
sector_offset = sector_offset + self.filesystem.sectors_per_fat;
@ -222,6 +229,74 @@ impl DiskBuilder {
Ok(())
}
/// Writes directory entries from the root directory into the disk sectors.
///
/// This function serializes the directory entries from the root directory into bytes and writes
/// them into the disk sectors starting from a calculated sector offset. The entries are written
/// in 512-byte sectors. If the last set of bytes doesn't fill an entire sector, the remainder
/// is zero-padded and written as the final sector.
///
/// ## Parameters
/// - `&mut self`: Mutable reference to the current instance of the object that includes
/// filesystem and partition information. This is necessary for accessing the filesystem layout
/// and performing write operations.
///
/// ## Returns
/// - `Result<(), std::io::Error>`: The function returns `Ok(())` if all sectors are written
/// successfully to the disk. If there is an error during any of the write operations, an
/// `Err` containing the `std::io::Error` is returned.
///
/// ## Errors
/// - This function will return an `Err` if there is an issue with writing to the disk.
/// - 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();
// 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);
}
}
// Write the bytes off to disk
let mut current_chunk: [u8; 512] = [0; 512];
let mut current_index = 0;
for &byte in bytes.iter() {
current_chunk[current_index] = byte;
current_index += 1;
// Got a full sector? Push it onto the vec.
if current_index == 512 {
sectors.push(current_chunk);
current_chunk = [0; 512]; // Reset the chunk for new data
current_index = 0;
}
}
// Check if there is a leftover partial sector and push it if there is.
if current_index != 0 {
sectors.push(current_chunk);
}
// Write sectors to disk
for (index, sector) in sectors.iter().enumerate() {
self.partition.write_sector(
&mut self.disk,
sector_offset as u32 + index as u32,
*sector,
)?;
}
Ok(())
}
/// The builder is the struct that does all manner of validation before trying to
/// create a Disk so that what we create will make sense.
pub fn build(&mut self) -> Result<(), std::io::Error> {
@ -265,15 +340,14 @@ impl DiskBuilder {
}
}
// filesystem.mkdir(None, "DOS")?;
//filesystem.mkdir(None,"GAME")?;
// Last action: write the allocation table copies to the disk
self.write_allocation_tables()?;
// Write all files to storage
for file in self.filesystem.root_directory.children.clone() {
self.write_file_to_storage(&file)?;
}
// Last action: write the allocation table copies to the disk
self.write_allocation_tables()?;
self.write_dirs()?;
self.disk.commit_storage()?;
Ok(())
}