Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Murmele committed May 28, 2024
1 parent 7bda477 commit 9112897
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
21 changes: 10 additions & 11 deletions src/fat/fat_table.rs
Original file line number Diff line number Diff line change
@@ -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<Self, &'static str> {
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<Self, &'static str> {
Ok(Self {
data,
fat_type,
};
Ok(table)
})
}

// FAT16 only
Expand All @@ -30,14 +28,15 @@ 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,
FatType::Fat32 => (LittleEndian::read_u32(&self.data[4..4+4]) & (1 << Self::FAT32_DIRTY_BIT)) == 0
}
}

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]);
Expand Down
2 changes: 1 addition & 1 deletion src/volume_mgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down

0 comments on commit 9112897

Please sign in to comment.