From 9112897efb4d6d224a58910e1d48e4571b5e0460 Mon Sep 17 00:00:00 2001 From: Martin Marmsoler Date: Tue, 28 May 2024 08:11:14 +0200 Subject: [PATCH] WIP --- src/fat/fat_table.rs | 21 ++++++++++----------- src/volume_mgr.rs | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/fat/fat_table.rs b/src/fat/fat_table.rs index b141d3f..ffbb394 100644 --- a/src/fat/fat_table.rs +++ b/src/fat/fat_table.rs @@ -1,25 +1,23 @@ /// FAT table definition /// /// -use crate::{ - blockdevice::BlockCount, - fat::FatType, -}; +use crate::fat::FatType; use byteorder::{ByteOrder, LittleEndian}; +/// Represents a single FAT table. It contains all information about which cluster is occupied +/// from a file pub struct FatTable<'a> { fat_type: FatType, - data: &'a mut [u8; 512], + data: &'a mut [u8], } impl<'a> FatTable<'a> { - - pub fn create_from_bytes(data: &mut [u8; 512], fat_type: FatType) -> Result { - let mut table = Self { + /// Attempt to parse a FAT table from a multiple sectors. + pub fn create_from_bytes(data: &'a mut [u8], fat_type: FatType) -> Result { + Ok(Self { data, fat_type, - }; - Ok(table) + }) } // FAT16 only @@ -30,6 +28,7 @@ impl<'a> FatTable<'a> { const FAT16_DIRTY_BIT: u16 = 15; const FAT32_DIRTY_BIT: u32 = 27; + pub (crate) fn dirty(&self) -> bool { match self.fat_type { FatType::Fat16 => (LittleEndian::read_u16(&self.data[2..2+2]) & (1 << Self::FAT16_DIRTY_BIT)) == 0, @@ -37,7 +36,7 @@ impl<'a> FatTable<'a> { } } - pub (crate) fn set_dirty(&self, dirty: bool) { + pub (crate) fn set_dirty(&mut self, dirty: bool) { match self.fat_type { FatType::Fat16 => { let mut v = LittleEndian::read_u16(&self.data[2..2+2]); diff --git a/src/volume_mgr.rs b/src/volume_mgr.rs index a1fedc6..e121de7 100644 --- a/src/volume_mgr.rs +++ b/src/volume_mgr.rs @@ -714,7 +714,7 @@ where let mut blocks = [Block::new()]; self.block_device.read(&mut blocks, f.lba_start + f.fat_start, "reading fat table"); let mut block = &mut blocks[0]; - let fat_table = fat::FatTable::create_from_bytes(&mut block.contents, f.get_fat_type()).map_err(Error::FormatError)?; + let mut fat_table = fat::FatTable::create_from_bytes(&mut block.contents, f.get_fat_type()).map_err(Error::FormatError)?; fat_table.set_dirty(true); if f.fat_nums == 1 || f.fat_nums == 2 { self.block_device.write(&blocks, f.lba_start + f.fat_start).await?;