Implement reading files beyond cluster (untested).

This commit is contained in:
Rafał Harabień 2017-09-23 20:16:32 +02:00
parent 88fd442dac
commit b535b460f9
3 changed files with 55 additions and 61 deletions

View File

@ -1,6 +1,5 @@
use std::io::prelude::*; use std::io::prelude::*;
use std::io; use std::io;
use std::io::Cursor;
use std::str; use std::str;
use byteorder::{LittleEndian, ReadBytesExt}; use byteorder::{LittleEndian, ReadBytesExt};
use chrono::{DateTime, Date, TimeZone, Local}; use chrono::{DateTime, Date, TimeZone, Local};
@ -96,46 +95,37 @@ impl FatDir {
pub fn list(&mut self) -> io::Result<Vec<FatDirEntry>> { pub fn list(&mut self) -> io::Result<Vec<FatDirEntry>> {
let mut entries = Vec::new(); let mut entries = Vec::new();
let cluster_size = self.state.borrow().get_cluster_size() as usize;
let mut buf = vec![0; cluster_size];
loop { loop {
let size = self.rdr.read(&mut buf)?; let entry = self.read_dir_entry()?;
if size == 0 { if entry.name[0] == 0 {
break; break; // end of dir
} }
if entry.name[0] == 0xE5 {
let mut cur = Cursor::new(&buf[..size]); continue; // deleted
loop {
let entry = self.read_dir_entry(&mut cur)?;
if entry.name[0] == 0 {
break; // end of dir
}
if entry.name[0] == 0xE5 {
continue; // deleted
}
entries.push(entry);
} }
entries.push(entry);
} }
Ok(entries) Ok(entries)
} }
fn read_dir_entry(&self, rdr: &mut Read) -> io::Result<FatDirEntry> { fn read_dir_entry(&mut self) -> io::Result<FatDirEntry> {
let mut name = [0; 11]; let mut name = [0; 11];
rdr.read(&mut name)?; self.rdr.read(&mut name)?;
let attrs = FatFileAttributes::from_bits(self.rdr.read_u8()?).expect("invalid attributes");
Ok(FatDirEntry { Ok(FatDirEntry {
name: name, name,
attrs: FatFileAttributes::from_bits(rdr.read_u8()?).unwrap(), attrs,
reserved_0: rdr.read_u8()?, reserved_0: self.rdr.read_u8()?,
create_time_0: rdr.read_u8()?, create_time_0: self.rdr.read_u8()?,
create_time_1: rdr.read_u16::<LittleEndian>()?, create_time_1: self.rdr.read_u16::<LittleEndian>()?,
create_date: rdr.read_u16::<LittleEndian>()?, create_date: self.rdr.read_u16::<LittleEndian>()?,
access_date: rdr.read_u16::<LittleEndian>()?, access_date: self.rdr.read_u16::<LittleEndian>()?,
first_cluster_hi: rdr.read_u16::<LittleEndian>()?, first_cluster_hi: self.rdr.read_u16::<LittleEndian>()?,
modify_time: rdr.read_u16::<LittleEndian>()?, modify_time: self.rdr.read_u16::<LittleEndian>()?,
modify_date: rdr.read_u16::<LittleEndian>()?, modify_date: self.rdr.read_u16::<LittleEndian>()?,
first_cluster_lo: rdr.read_u16::<LittleEndian>()?, first_cluster_lo: self.rdr.read_u16::<LittleEndian>()?,
size: rdr.read_u32::<LittleEndian>()?, size: self.rdr.read_u32::<LittleEndian>()?,
state: self.state.clone(), state: self.state.clone(),
}) })
} }

View File

@ -7,35 +7,50 @@ use fs::FatSharedStateRef;
#[allow(dead_code)] #[allow(dead_code)]
pub struct FatFile { pub struct FatFile {
first_sector: u32, first_cluster: u32,
size: u32, size: u32,
offset: u32, offset: u32,
current_cluster: Option<u32>,
state: FatSharedStateRef, state: FatSharedStateRef,
} }
impl FatFile { impl FatFile {
pub(crate) fn new(first_cluster: u32, size: u32, state: FatSharedStateRef) -> FatFile { pub(crate) fn new(first_cluster: u32, size: u32, state: FatSharedStateRef) -> FatFile {
let first_sector = state.borrow().sector_from_cluster(first_cluster);
FatFile { FatFile {
first_sector, size, state, offset: 0, first_cluster, size, state,
current_cluster: Some(first_cluster),
offset: 0,
} }
} }
} }
impl Read for FatFile { impl Read for FatFile {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let (offset, read_size) = { let mut buf_offset: usize = 0;
let state = self.state.borrow(); let cluster_size = self.state.borrow().get_cluster_size();
let offset = state.offset_from_sector(self.first_sector) + self.offset as u64;
let mut read_size = cmp::min((self.size - self.offset) as usize, buf.len());
// FIXME: allow only one cluster for now
read_size = cmp::min(read_size, (state.get_cluster_size() - self.offset) as usize);
(offset, read_size)
};
let mut state = self.state.borrow_mut(); let mut state = self.state.borrow_mut();
state.rdr.seek(SeekFrom::Start(offset))?; loop {
let size = state.rdr.read(&mut buf[..read_size])?; let offset_in_cluster = self.offset % cluster_size;
self.offset += size as u32; let bytes_left_in_cluster = (cluster_size - offset_in_cluster) as usize;
Ok(size) let bytes_left_in_file = (self.size - self.offset) as usize;
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);
if read_size == 0 {
break;
}
let current_cluster = self.current_cluster.unwrap();
let offset_in_fs = state.offset_from_cluster(current_cluster) + (offset_in_cluster as u64);
state.rdr.seek(SeekFrom::Start(offset_in_fs))?;
let read_bytes = state.rdr.read(&mut buf[buf_offset..buf_offset+read_size])?;
if read_bytes == 0 {
break;
}
self.offset += read_bytes as u32;
buf_offset += read_bytes;
if self.offset % cluster_size == 0 {
self.current_cluster = state.table.get_next_cluster(current_cluster);
}
}
Ok(buf_offset)
} }
} }

View File

@ -48,11 +48,8 @@ impl FatSharedState {
self.boot.bpb.sectors_per_cluster as u32 * self.boot.bpb.bytes_per_sector as u32 self.boot.bpb.sectors_per_cluster as u32 * self.boot.bpb.bytes_per_sector as u32
} }
pub(crate) fn get_root_dir_sector(&self) -> u32 { pub(crate) fn offset_from_cluster(&self, cluser: u32) -> u64 {
match self.fat_type { self.offset_from_sector(self.sector_from_cluster(cluser))
FatType::Fat12 | FatType::Fat16 => self.first_data_sector - self.root_dir_sectors,
_ => self.sector_from_cluster(self.boot.bpb.root_cluster)
}
} }
} }
@ -242,10 +239,6 @@ impl FatFileSystem {
Ok(boot) Ok(boot)
} }
// pub(crate) fn offset_from_cluster(&self, cluser: u32) -> u64 {
// self.offset_from_sector(self.sector_from_cluster(cluser))
// }
pub fn root_dir(&mut self) -> FatDir { pub fn root_dir(&mut self) -> FatDir {
let state = self.state.borrow(); let state = self.state.borrow();
let root_rdr: Box<Read> = match state.fat_type { let root_rdr: Box<Read> = match state.fat_type {
@ -277,12 +270,8 @@ impl FatSlice {
impl Read for FatSlice { impl Read for FatSlice {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let (offset, read_size) = { let offset = self.begin + self.offset;
let state = self.state.borrow(); let read_size = cmp::min((self.size - self.offset) as usize, buf.len());
let offset = self.begin + self.offset;
let mut read_size = cmp::min((self.size - self.offset) as usize, buf.len());
(offset, read_size)
};
let mut state = self.state.borrow_mut(); let mut state = self.state.borrow_mut();
state.rdr.seek(SeekFrom::Start(offset))?; state.rdr.seek(SeekFrom::Start(offset))?;
let size = state.rdr.read(&mut buf[..read_size])?; let size = state.rdr.read(&mut buf[..read_size])?;