Fix reading FAT32 root dir.

This commit is contained in:
Rafał Harabień 2017-09-23 21:53:32 +02:00
parent c65d26bd2b
commit e44d7fc6c2
4 changed files with 7 additions and 7 deletions

View File

@ -7,7 +7,7 @@ use std::str;
use rustfat::FatFileSystem;
fn main() {
let file = File::open("resources/fat12.img").unwrap();
let file = File::open("resources/fat32.img").unwrap();
let buf_rdr = BufReader::new(file);
let mut fs = FatFileSystem::new(Box::new(buf_rdr)).unwrap();
let mut root_dir = fs.root_dir();

View File

@ -64,7 +64,7 @@ impl FatDirEntry {
}
pub fn get_file(&self) -> FatFile {
FatFile::new(self.get_cluster(), self.size, self.state.clone())
FatFile::new(self.get_cluster(), Some(self.size), self.state.clone())
}
pub fn get_size(&self) -> u32 {

View File

@ -8,14 +8,14 @@ use fs::FatSharedStateRef;
#[allow(dead_code)]
pub struct FatFile {
first_cluster: u32,
size: u32,
size: Option<u32>,
offset: u32,
current_cluster: Option<u32>,
state: FatSharedStateRef,
}
impl FatFile {
pub(crate) fn new(first_cluster: u32, size: u32, state: FatSharedStateRef) -> FatFile {
pub(crate) fn new(first_cluster: u32, size: Option<u32>, state: FatSharedStateRef) -> FatFile {
FatFile {
first_cluster, size, state,
current_cluster: Some(first_cluster),
@ -32,9 +32,9 @@ impl Read for FatFile {
loop {
let offset_in_cluster = self.offset % cluster_size;
let bytes_left_in_cluster = (cluster_size - offset_in_cluster) as usize;
let bytes_left_in_file = (self.size - self.offset) as usize;
let bytes_left_in_file = self.size.map(|size| (size - self.offset) as usize).unwrap_or(bytes_left_in_cluster);
let bytes_left_in_buf = buf.len() - buf_offset;
let read_size = cmp::min(cmp::min(bytes_left_in_file, bytes_left_in_cluster), bytes_left_in_buf);
let read_size = cmp::min(cmp::min(bytes_left_in_buf, bytes_left_in_cluster), bytes_left_in_file);
if read_size == 0 {
break;
}

View File

@ -244,7 +244,7 @@ impl FatFileSystem {
let root_rdr: Box<Read> = match state.fat_type {
FatType::Fat12 | FatType::Fat16 => Box::new(FatSlice::from_sectors(
state.first_data_sector - state.root_dir_sectors, state.root_dir_sectors, self.state.clone())),
_ => Box::new(FatFile::new(state.boot.bpb.root_cluster, state.root_dir_sectors, self.state.clone()))
_ => Box::new(FatFile::new(state.boot.bpb.root_cluster, None, self.state.clone())) // FIXME
};
FatDir::new(root_rdr, self.state.clone())
}