forked from M-Labs/rust-fatfs
Don't use boxed buffer in FAT cluster reading code.
I assume gived Read object is capable of basic buffering and caching.
This commit is contained in:
parent
94dfe7ffd5
commit
bc835d59b5
20
src/dir.rs
20
src/dir.rs
@ -7,7 +7,7 @@ use std::str;
|
|||||||
use byteorder::{LittleEndian, ReadBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
use chrono::{DateTime, Date, TimeZone, Local};
|
use chrono::{DateTime, Date, TimeZone, Local};
|
||||||
|
|
||||||
use fs::{FatSharedStateRef, FatSlice};
|
use fs::{FatFileSystemRef, FatSlice};
|
||||||
use file::FatFile;
|
use file::FatFile;
|
||||||
|
|
||||||
pub(crate) enum FatDirReader<'a, 'b: 'a> {
|
pub(crate) enum FatDirReader<'a, 'b: 'a> {
|
||||||
@ -33,8 +33,6 @@ impl <'a, 'b> Seek for FatDirReader<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct FatFileAttributes: u8 {
|
pub struct FatFileAttributes: u8 {
|
||||||
@ -89,7 +87,7 @@ enum FatDirEntryData {
|
|||||||
pub struct FatDirEntry<'a, 'b: 'a> {
|
pub struct FatDirEntry<'a, 'b: 'a> {
|
||||||
data: FatDirFileEntryData,
|
data: FatDirFileEntryData,
|
||||||
lfn: Vec<u16>,
|
lfn: Vec<u16>,
|
||||||
state: FatSharedStateRef<'a, 'b>,
|
fs: FatFileSystemRef<'a, 'b>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a, 'b> FatDirEntry<'a, 'b> {
|
impl <'a, 'b> FatDirEntry<'a, 'b> {
|
||||||
@ -127,15 +125,15 @@ impl <'a, 'b> FatDirEntry<'a, 'b> {
|
|||||||
if self.is_dir() {
|
if self.is_dir() {
|
||||||
panic!("This is a directory");
|
panic!("This is a directory");
|
||||||
}
|
}
|
||||||
FatFile::new(self.first_cluster(), Some(self.data.size), self.state)
|
FatFile::new(self.first_cluster(), Some(self.data.size), self.fs)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_dir(&self) -> FatDir<'a, 'b> {
|
pub fn to_dir(&self) -> FatDir<'a, 'b> {
|
||||||
if !self.is_dir() {
|
if !self.is_dir() {
|
||||||
panic!("This is a file");
|
panic!("This is a file");
|
||||||
}
|
}
|
||||||
let file = FatFile::new(self.first_cluster(), None, self.state);
|
let file = FatFile::new(self.first_cluster(), None, self.fs);
|
||||||
FatDir::new(FatDirReader::File(file), self.state)
|
FatDir::new(FatDirReader::File(file), self.fs)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn len(&self) -> u64 {
|
pub fn len(&self) -> u64 {
|
||||||
@ -173,13 +171,13 @@ impl <'a, 'b> fmt::Debug for FatDirEntry<'a, 'b> {
|
|||||||
|
|
||||||
pub struct FatDir<'a, 'b: 'a> {
|
pub struct FatDir<'a, 'b: 'a> {
|
||||||
rdr: FatDirReader<'a, 'b>,
|
rdr: FatDirReader<'a, 'b>,
|
||||||
state: FatSharedStateRef<'a, 'b>,
|
fs: FatFileSystemRef<'a, 'b>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a, 'b> FatDir<'a, 'b> {
|
impl <'a, 'b> FatDir<'a, 'b> {
|
||||||
|
|
||||||
pub(crate) fn new(rdr: FatDirReader<'a, 'b>, state: FatSharedStateRef<'a, 'b>) -> FatDir<'a, 'b> {
|
pub(crate) fn new(rdr: FatDirReader<'a, 'b>, fs: FatFileSystemRef<'a, 'b>) -> FatDir<'a, 'b> {
|
||||||
FatDir { rdr, state }
|
FatDir { rdr, fs }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rewind(&mut self) {
|
pub fn rewind(&mut self) {
|
||||||
@ -296,7 +294,7 @@ impl <'a, 'b> Iterator for FatDir<'a, 'b> {
|
|||||||
return Some(Ok(FatDirEntry {
|
return Some(Ok(FatDirEntry {
|
||||||
data,
|
data,
|
||||||
lfn: lfn_buf,
|
lfn: lfn_buf,
|
||||||
state: self.state.clone(),
|
fs: self.fs,
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
FatDirEntryData::Lfn(data) => {
|
FatDirEntryData::Lfn(data) => {
|
||||||
|
27
src/file.rs
27
src/file.rs
@ -3,7 +3,7 @@ use std::io::prelude::*;
|
|||||||
use std::io::{SeekFrom, ErrorKind};
|
use std::io::{SeekFrom, ErrorKind};
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use fs::FatSharedStateRef;
|
use fs::FatFileSystemRef;
|
||||||
|
|
||||||
|
|
||||||
pub struct FatFile<'a, 'b: 'a> {
|
pub struct FatFile<'a, 'b: 'a> {
|
||||||
@ -11,13 +11,13 @@ pub struct FatFile<'a, 'b: 'a> {
|
|||||||
size: Option<u32>,
|
size: Option<u32>,
|
||||||
offset: u32,
|
offset: u32,
|
||||||
current_cluster: Option<u32>,
|
current_cluster: Option<u32>,
|
||||||
state: FatSharedStateRef<'a, 'b>,
|
fs: FatFileSystemRef<'a, 'b>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a, 'b> FatFile<'a, 'b> {
|
impl <'a, 'b> FatFile<'a, 'b> {
|
||||||
pub(crate) fn new(first_cluster: u32, size: Option<u32>, state: FatSharedStateRef<'a, 'b>) -> Self {
|
pub(crate) fn new(first_cluster: u32, size: Option<u32>, fs: FatFileSystemRef<'a, 'b>) -> Self {
|
||||||
FatFile {
|
FatFile {
|
||||||
first_cluster, size, state,
|
first_cluster, size, fs,
|
||||||
current_cluster: Some(first_cluster),
|
current_cluster: Some(first_cluster),
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}
|
}
|
||||||
@ -27,8 +27,7 @@ impl <'a, 'b> FatFile<'a, 'b> {
|
|||||||
impl <'a, 'b> Read for FatFile<'a, 'b> {
|
impl <'a, 'b> Read for FatFile<'a, 'b> {
|
||||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
let mut buf_offset: usize = 0;
|
let mut buf_offset: usize = 0;
|
||||||
let cluster_size = self.state.borrow().get_cluster_size();
|
let cluster_size = self.fs.get_cluster_size();
|
||||||
let mut state = self.state.borrow_mut();
|
|
||||||
loop {
|
loop {
|
||||||
let offset_in_cluster = self.offset % cluster_size;
|
let offset_in_cluster = self.offset % cluster_size;
|
||||||
let bytes_left_in_cluster = (cluster_size - offset_in_cluster) as usize;
|
let bytes_left_in_cluster = (cluster_size - offset_in_cluster) as usize;
|
||||||
@ -39,16 +38,19 @@ impl <'a, 'b> Read for FatFile<'a, 'b> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let current_cluster = self.current_cluster.unwrap();
|
let current_cluster = self.current_cluster.unwrap();
|
||||||
let offset_in_fs = state.offset_from_cluster(current_cluster) + (offset_in_cluster as u64);
|
let offset_in_fs = self.fs.offset_from_cluster(current_cluster) + (offset_in_cluster as u64);
|
||||||
state.rdr.seek(SeekFrom::Start(offset_in_fs))?;
|
let read_bytes = {
|
||||||
let read_bytes = state.rdr.read(&mut buf[buf_offset..buf_offset+read_size])?;
|
let mut rdr = self.fs.rdr.borrow_mut();
|
||||||
|
rdr.seek(SeekFrom::Start(offset_in_fs))?;
|
||||||
|
rdr.read(&mut buf[buf_offset..buf_offset+read_size])?
|
||||||
|
};
|
||||||
if read_bytes == 0 {
|
if read_bytes == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
self.offset += read_bytes as u32;
|
self.offset += read_bytes as u32;
|
||||||
buf_offset += read_bytes;
|
buf_offset += read_bytes;
|
||||||
if self.offset % cluster_size == 0 {
|
if self.offset % cluster_size == 0 {
|
||||||
self.current_cluster = state.table.cluster_iter(current_cluster).skip(1).next();
|
self.current_cluster = self.fs.cluster_iter(current_cluster).skip(1).next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(buf_offset)
|
Ok(buf_offset)
|
||||||
@ -65,12 +67,11 @@ impl <'a, 'b> Seek for FatFile<'a, 'b> {
|
|||||||
if new_offset < 0 || (self.size.is_some() && new_offset as u64 > self.size.unwrap() as u64) {
|
if new_offset < 0 || (self.size.is_some() && new_offset as u64 > self.size.unwrap() as u64) {
|
||||||
return Err(io::Error::new(ErrorKind::InvalidInput, "invalid seek"));
|
return Err(io::Error::new(ErrorKind::InvalidInput, "invalid seek"));
|
||||||
}
|
}
|
||||||
let cluster_size = self.state.borrow().get_cluster_size();
|
let cluster_size = self.fs.get_cluster_size();
|
||||||
let cluster_count = (new_offset / cluster_size as i64) as usize;
|
let cluster_count = (new_offset / cluster_size as i64) as usize;
|
||||||
let mut new_cluster = Some(self.first_cluster);
|
let mut new_cluster = Some(self.first_cluster);
|
||||||
let state = self.state.borrow_mut();
|
|
||||||
if cluster_count > 0 {
|
if cluster_count > 0 {
|
||||||
new_cluster = state.table.cluster_iter(new_cluster.unwrap()).skip(cluster_count).next();
|
new_cluster = self.fs.cluster_iter(new_cluster.unwrap()).skip(cluster_count).next();
|
||||||
}
|
}
|
||||||
self.offset = new_offset as u32;
|
self.offset = new_offset as u32;
|
||||||
self.current_cluster = new_cluster;
|
self.current_cluster = new_cluster;
|
||||||
|
113
src/fs.rs
113
src/fs.rs
@ -1,5 +1,6 @@
|
|||||||
use core::cell::RefCell;
|
use core::cell::RefCell;
|
||||||
use core::cmp;
|
use core::cmp;
|
||||||
|
use core::iter;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::io::{Error, ErrorKind, SeekFrom};
|
use std::io::{Error, ErrorKind, SeekFrom};
|
||||||
use std::io;
|
use std::io;
|
||||||
@ -8,7 +9,7 @@ use byteorder::{LittleEndian, ReadBytesExt};
|
|||||||
|
|
||||||
use file::FatFile;
|
use file::FatFile;
|
||||||
use dir::{FatDirReader, FatDir};
|
use dir::{FatDirReader, FatDir};
|
||||||
use table::FatTable;
|
use table::FatClusterIterator;
|
||||||
|
|
||||||
// FAT implementation based on:
|
// FAT implementation based on:
|
||||||
// http://wiki.osdev.org/FAT
|
// http://wiki.osdev.org/FAT
|
||||||
@ -22,33 +23,6 @@ pub enum FatType {
|
|||||||
pub trait ReadSeek: Read + Seek {}
|
pub trait ReadSeek: Read + Seek {}
|
||||||
impl<T> ReadSeek for T where T: Read + Seek {}
|
impl<T> ReadSeek for T where T: Read + Seek {}
|
||||||
|
|
||||||
pub(crate) struct FatSharedState<'a> {
|
|
||||||
pub(crate) rdr: &'a mut ReadSeek,
|
|
||||||
pub(crate) fat_type: FatType,
|
|
||||||
pub(crate) boot: FatBootRecord,
|
|
||||||
pub(crate) first_data_sector: u32,
|
|
||||||
pub(crate) root_dir_sectors: u32,
|
|
||||||
pub(crate) table: FatTable,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl <'a> FatSharedState<'a> {
|
|
||||||
pub(crate) fn offset_from_sector(&self, sector: u32) -> u64 {
|
|
||||||
(sector as u64) * self.boot.bpb.bytes_per_sector as u64
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn sector_from_cluster(&self, cluster: u32) -> u32 {
|
|
||||||
((cluster - 2) * self.boot.bpb.sectors_per_cluster as u32) + self.first_data_sector
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn get_cluster_size(&self) -> u32 {
|
|
||||||
self.boot.bpb.sectors_per_cluster as u32 * self.boot.bpb.bytes_per_sector as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn offset_from_cluster(&self, cluser: u32) -> u64 {
|
|
||||||
self.offset_from_sector(self.sector_from_cluster(cluser))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Default, Debug, Clone)]
|
#[derive(Default, Debug, Clone)]
|
||||||
pub(crate) struct FatBiosParameterBlock {
|
pub(crate) struct FatBiosParameterBlock {
|
||||||
@ -102,11 +76,14 @@ impl Default for FatBootRecord {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) type FatSharedStateCell<'a> = RefCell<FatSharedState<'a>>;
|
pub(crate) type FatFileSystemRef<'a, 'b: 'a> = &'a FatFileSystem<'b>;
|
||||||
pub(crate) type FatSharedStateRef<'a, 'b: 'a> = &'a RefCell<FatSharedState<'b>>;
|
|
||||||
|
|
||||||
pub struct FatFileSystem<'a> {
|
pub struct FatFileSystem<'a> {
|
||||||
pub(crate) state: FatSharedStateCell<'a>,
|
pub(crate) rdr: RefCell<&'a mut ReadSeek>,
|
||||||
|
pub(crate) fat_type: FatType,
|
||||||
|
pub(crate) boot: FatBootRecord,
|
||||||
|
pub(crate) first_data_sector: u32,
|
||||||
|
pub(crate) root_dir_sectors: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a> FatFileSystem<'a> {
|
impl <'a> FatFileSystem<'a> {
|
||||||
@ -125,47 +102,36 @@ impl <'a> FatFileSystem<'a> {
|
|||||||
let total_clusters = data_sectors / boot.bpb.sectors_per_cluster as u32;
|
let total_clusters = data_sectors / boot.bpb.sectors_per_cluster as u32;
|
||||||
let fat_type = Self::fat_type_from_clusters(total_clusters);
|
let fat_type = Self::fat_type_from_clusters(total_clusters);
|
||||||
|
|
||||||
let fat_offset = boot.bpb.reserved_sectors * boot.bpb.bytes_per_sector;
|
Ok(FatFileSystem {
|
||||||
rdr.seek(SeekFrom::Start(fat_offset as u64))?;
|
rdr: RefCell::new(rdr),
|
||||||
let fat_size = sectors_per_fat * boot.bpb.bytes_per_sector as u32;
|
|
||||||
let table = FatTable::from_read(rdr, fat_type, fat_size as usize)?;
|
|
||||||
|
|
||||||
let state = FatSharedState {
|
|
||||||
rdr,
|
|
||||||
fat_type,
|
fat_type,
|
||||||
boot,
|
boot,
|
||||||
first_data_sector,
|
first_data_sector,
|
||||||
root_dir_sectors,
|
root_dir_sectors,
|
||||||
table,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(FatFileSystem {
|
|
||||||
state: RefCell::new(state),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fat_type(&self) -> FatType {
|
pub fn fat_type(&self) -> FatType {
|
||||||
self.state.borrow().fat_type
|
self.fat_type
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn volume_id(&self) -> u32 {
|
pub fn volume_id(&self) -> u32 {
|
||||||
self.state.borrow().boot.bpb.volume_id
|
self.boot.bpb.volume_id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn volume_label(&self) -> String {
|
pub fn volume_label(&self) -> String {
|
||||||
str::from_utf8(&self.state.borrow().boot.bpb.volume_label).unwrap().trim_right().to_string()
|
str::from_utf8(&self.boot.bpb.volume_label).unwrap().trim_right().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn root_dir<'b>(&'b self) -> FatDir<'b, 'a> {
|
pub fn root_dir<'b>(&'b self) -> FatDir<'b, 'a> {
|
||||||
let root_rdr = {
|
let root_rdr = {
|
||||||
let state = self.state.borrow();
|
match self.fat_type {
|
||||||
match state.fat_type {
|
|
||||||
FatType::Fat12 | FatType::Fat16 => FatDirReader::Root(FatSlice::from_sectors(
|
FatType::Fat12 | FatType::Fat16 => FatDirReader::Root(FatSlice::from_sectors(
|
||||||
state.first_data_sector - state.root_dir_sectors, state.root_dir_sectors, &self.state)),
|
self.first_data_sector - self.root_dir_sectors, self.root_dir_sectors, self)),
|
||||||
_ => FatDirReader::File(FatFile::new(state.boot.bpb.root_dir_first_cluster, None, &self.state)),
|
_ => FatDirReader::File(FatFile::new(self.boot.bpb.root_dir_first_cluster, None, self)),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
FatDir::new(root_rdr, &self.state)
|
FatDir::new(root_rdr, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_bpb(rdr: &mut Read) -> io::Result<FatBiosParameterBlock> {
|
fn read_bpb(rdr: &mut Read) -> io::Result<FatBiosParameterBlock> {
|
||||||
@ -232,23 +198,50 @@ impl <'a> FatFileSystem<'a> {
|
|||||||
rdr.read(&mut boot.boot_sig)?;
|
rdr.read(&mut boot.boot_sig)?;
|
||||||
Ok(boot)
|
Ok(boot)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn offset_from_sector(&self, sector: u32) -> u64 {
|
||||||
|
(sector as u64) * self.boot.bpb.bytes_per_sector as u64
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn sector_from_cluster(&self, cluster: u32) -> u32 {
|
||||||
|
((cluster - 2) * self.boot.bpb.sectors_per_cluster as u32) + self.first_data_sector
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_cluster_size(&self) -> u32 {
|
||||||
|
self.boot.bpb.sectors_per_cluster as u32 * self.boot.bpb.bytes_per_sector as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn offset_from_cluster(&self, cluser: u32) -> u64 {
|
||||||
|
self.offset_from_sector(self.sector_from_cluster(cluser))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn cluster_iter<'b>(&'b self, cluster: u32) -> iter::Chain<iter::Once<u32>, FatClusterIterator<'b, 'a>> {
|
||||||
|
let bytes_per_sector = self.boot.bpb.bytes_per_sector as u64;
|
||||||
|
let fat_offset = self.boot.bpb.reserved_sectors as u64 * bytes_per_sector;
|
||||||
|
let sectors_per_fat =
|
||||||
|
if self.boot.bpb.sectors_per_fat_16 == 0 { self.boot.bpb.sectors_per_fat_32 }
|
||||||
|
else { self.boot.bpb.sectors_per_fat_16 as u32 };
|
||||||
|
let fat_size = sectors_per_fat as u64 * bytes_per_sector;
|
||||||
|
let fat_slice = FatSlice::new(fat_offset, fat_size, self);
|
||||||
|
FatClusterIterator::new(fat_slice, self.fat_type, cluster)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct FatSlice<'a, 'b: 'a> {
|
pub(crate) struct FatSlice<'a, 'b: 'a> {
|
||||||
begin: u64,
|
begin: u64,
|
||||||
size: u64,
|
size: u64,
|
||||||
offset: u64,
|
offset: u64,
|
||||||
state: FatSharedStateRef<'a, 'b>,
|
fs: &'a FatFileSystem<'b>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a, 'b> FatSlice<'a, 'b> {
|
impl <'a, 'b> FatSlice<'a, 'b> {
|
||||||
pub(crate) fn new(begin: u64, size: u64, state: FatSharedStateRef<'a, 'b>) -> Self {
|
pub(crate) fn new(begin: u64, size: u64, fs: FatFileSystemRef<'a, 'b>) -> Self {
|
||||||
FatSlice { begin, size, state, offset: 0 }
|
FatSlice { begin, size, fs, offset: 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_sectors(first_sector: u32, sectors_count: u32, state: FatSharedStateRef<'a, 'b>) -> Self {
|
pub(crate) fn from_sectors(first_sector: u32, sectors_count: u32, fs: FatFileSystemRef<'a, 'b>) -> Self {
|
||||||
let bytes_per_sector = state.borrow().boot.bpb.bytes_per_sector as u64;
|
let bytes_per_sector = fs.boot.bpb.bytes_per_sector as u64;
|
||||||
Self::new(first_sector as u64 * bytes_per_sector, sectors_count as u64 * bytes_per_sector, state)
|
Self::new(first_sector as u64 * bytes_per_sector, sectors_count as u64 * bytes_per_sector, fs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -256,9 +249,9 @@ impl <'a, 'b> Read for FatSlice<'a, 'b> {
|
|||||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
let offset = self.begin + self.offset;
|
let offset = self.begin + self.offset;
|
||||||
let read_size = cmp::min((self.size - self.offset) as usize, buf.len());
|
let read_size = cmp::min((self.size - self.offset) as usize, buf.len());
|
||||||
let mut state = self.state.borrow_mut();
|
let mut rdr = self.fs.rdr.borrow_mut();
|
||||||
state.rdr.seek(SeekFrom::Start(offset))?;
|
rdr.seek(SeekFrom::Start(offset))?;
|
||||||
let size = state.rdr.read(&mut buf[..read_size])?;
|
let size = rdr.read(&mut buf[..read_size])?;
|
||||||
self.offset += size as u64;
|
self.offset += size as u64;
|
||||||
Ok(size)
|
Ok(size)
|
||||||
}
|
}
|
||||||
|
140
src/table.rs
140
src/table.rs
@ -1,93 +1,22 @@
|
|||||||
use std::io::prelude::*;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use byteorder::{LittleEndian, ReadBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
use fs::FatType;
|
use fs::{FatType, FatSlice, ReadSeek};
|
||||||
use core::iter;
|
use core::iter;
|
||||||
|
|
||||||
pub(crate) struct FatTableData<T> {
|
fn get_next_cluster(rdr: &mut ReadSeek, fat_type: FatType, cluster: u32) -> io::Result<Option<u32>> {
|
||||||
table: Box<[T]>,
|
match fat_type {
|
||||||
}
|
FatType::Fat12 => get_next_cluster_12(rdr, cluster),
|
||||||
|
FatType::Fat16 => get_next_cluster_16(rdr, cluster),
|
||||||
impl <T> FatTableData<T> {
|
FatType::Fat32 => get_next_cluster_32(rdr, cluster),
|
||||||
pub fn new(data: Box<[T]>) -> Self {
|
|
||||||
Self {
|
|
||||||
table: data,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) type FatTable12 = FatTableData<u8>;
|
|
||||||
pub(crate) type FatTable16 = FatTableData<u16>;
|
|
||||||
pub(crate) type FatTable32 = FatTableData<u32>;
|
|
||||||
|
|
||||||
pub(crate) enum FatTable {
|
|
||||||
Fat12(FatTable12),
|
|
||||||
Fat16(FatTable16),
|
|
||||||
Fat32(FatTable32),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FatTable {
|
|
||||||
pub fn from_read(rdr: &mut Read, fat_type: FatType, size: usize) -> io::Result<FatTable> {
|
|
||||||
let table = match fat_type {
|
|
||||||
FatType::Fat12 => FatTable::Fat12(FatTable12::from_read(rdr, size)?),
|
|
||||||
FatType::Fat16 => FatTable::Fat16(FatTable16::from_read(rdr, size)?),
|
|
||||||
FatType::Fat32 => FatTable::Fat32(FatTable32::from_read(rdr, size)?),
|
|
||||||
};
|
|
||||||
Ok(table)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn cluster_iter(&self, cluster: u32) -> iter::Chain<iter::Once<u32>, FatClusterIterator> {
|
|
||||||
let iter = FatClusterIterator {
|
|
||||||
table: self,
|
|
||||||
cluster: Some(cluster),
|
|
||||||
};
|
|
||||||
iter::once(cluster).chain(iter)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait FatNextCluster {
|
fn get_next_cluster_12(rdr: &mut ReadSeek, cluster: u32) -> io::Result<Option<u32>> {
|
||||||
fn get_next_cluster(&self, cluster: u32) -> Option<u32>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FatNextCluster for FatTable {
|
|
||||||
fn get_next_cluster(&self, cluster: u32) -> Option<u32> {
|
|
||||||
match *self {
|
|
||||||
FatTable::Fat12(ref fat) => fat.get_next_cluster(cluster),
|
|
||||||
FatTable::Fat16(ref fat) => fat.get_next_cluster(cluster),
|
|
||||||
FatTable::Fat32(ref fat) => fat.get_next_cluster(cluster),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FatTable12 {
|
|
||||||
pub fn from_read(rdr: &mut Read, size: usize) -> io::Result<Self> {
|
|
||||||
let mut fat = vec![0;size];
|
|
||||||
rdr.read_exact(fat.as_mut())?;
|
|
||||||
Ok(Self::new(fat.into_boxed_slice()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FatTable16 {
|
|
||||||
pub fn from_read(rdr: &mut Read, size: usize) -> io::Result<Self> {
|
|
||||||
let mut fat = vec![0;size/2];
|
|
||||||
rdr.read_u16_into::<LittleEndian>(fat.as_mut())?;
|
|
||||||
Ok(Self::new(fat.into_boxed_slice()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FatTable32 {
|
|
||||||
pub fn from_read(rdr: &mut Read, size: usize) -> io::Result<Self> {
|
|
||||||
let mut fat = vec![0;size/4];
|
|
||||||
rdr.read_u32_into::<LittleEndian>(fat.as_mut())?;
|
|
||||||
Ok(Self::new(fat.into_boxed_slice()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FatNextCluster for FatTable12 {
|
|
||||||
fn get_next_cluster(&self, cluster: u32) -> Option<u32> {
|
|
||||||
let fat_offset = cluster + (cluster / 2);
|
let fat_offset = cluster + (cluster / 2);
|
||||||
let val1 = self.table[fat_offset as usize] as u16;
|
let mut bytes = [0;2];
|
||||||
let val2 = self.table[(fat_offset + 1) as usize] as u16;
|
rdr.seek(io::SeekFrom::Start(fat_offset as u64))?;
|
||||||
|
rdr.read(&mut bytes)?;
|
||||||
|
let (val1, val2) = (bytes[0] as u16, bytes[1] as u16);
|
||||||
|
|
||||||
let val = if cluster & 1 == 1 {
|
let val = if cluster & 1 == 1 {
|
||||||
(val1 >> 4) | (val2 << 4)
|
(val1 >> 4) | (val2 << 4)
|
||||||
@ -95,45 +24,54 @@ impl FatNextCluster for FatTable12 {
|
|||||||
val1 | (val2 & 0x0F)
|
val1 | (val2 & 0x0F)
|
||||||
};
|
};
|
||||||
if val <= 1 || val >= 0xFF7 {
|
if val <= 1 || val >= 0xFF7 {
|
||||||
None
|
Ok(None)
|
||||||
} else {
|
} else {
|
||||||
Some(val as u32)
|
Ok(Some(val as u32))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FatNextCluster for FatTable16 {
|
fn get_next_cluster_16(rdr: &mut ReadSeek, cluster: u32) -> io::Result<Option<u32>> {
|
||||||
fn get_next_cluster(&self, cluster: u32) -> Option<u32> {
|
rdr.seek(io::SeekFrom::Start((cluster*2) as u64))?;
|
||||||
let val = self.table[cluster as usize];
|
let val = rdr.read_u16::<LittleEndian>()?;
|
||||||
if val <= 1 || val >= 0xFFF7 {
|
if val <= 1 || val >= 0xFFF7 {
|
||||||
None
|
Ok(None)
|
||||||
} else {
|
} else {
|
||||||
Some(val as u32)
|
Ok(Some(val as u32))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FatNextCluster for FatTable32 {
|
fn get_next_cluster_32(rdr: &mut ReadSeek, cluster: u32) -> io::Result<Option<u32>> {
|
||||||
fn get_next_cluster(&self, cluster: u32) -> Option<u32> {
|
rdr.seek(io::SeekFrom::Start((cluster*4) as u64))?;
|
||||||
let val = self.table[cluster as usize] & 0x0FFFFFFF;
|
let val = rdr.read_u32::<LittleEndian>()? & 0x0FFFFFFF;
|
||||||
if val <= 1 || val >= 0x0FFFFFF7 {
|
if val <= 1 || val >= 0x0FFFFFF7 {
|
||||||
None
|
Ok(None)
|
||||||
} else {
|
} else {
|
||||||
Some(val)
|
Ok(Some(val))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct FatClusterIterator<'a> {
|
pub(crate) struct FatClusterIterator<'a, 'b: 'a> {
|
||||||
table: &'a FatTable,
|
part: FatSlice<'a, 'b>,
|
||||||
|
fat_type: FatType,
|
||||||
cluster: Option<u32>,
|
cluster: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a> Iterator for FatClusterIterator<'a> {
|
impl <'a, 'b> FatClusterIterator<'a, 'b> {
|
||||||
|
pub(crate) fn new(part: FatSlice<'a, 'b>, fat_type: FatType, cluster: u32) -> iter::Chain<iter::Once<u32>, FatClusterIterator<'a, 'b>> {
|
||||||
|
let iter = FatClusterIterator {
|
||||||
|
part: part,
|
||||||
|
fat_type: fat_type,
|
||||||
|
cluster: Some(cluster),
|
||||||
|
};
|
||||||
|
iter::once(cluster).chain(iter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <'a, 'b> Iterator for FatClusterIterator<'a, 'b> {
|
||||||
type Item = u32;
|
type Item = u32;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
self.cluster = self.table.get_next_cluster(self.cluster.unwrap());
|
self.cluster = get_next_cluster(&mut self.part, self.fat_type, self.cluster.unwrap()).unwrap(); // FIXME: unwrap!
|
||||||
self.cluster
|
self.cluster
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user