forked from M-Labs/rust-fatfs
Add FAT table iterator.
This commit is contained in:
parent
d88850624a
commit
f9f192c35e
@ -262,7 +262,7 @@ impl <'a, 'b> FatDir<'a, 'b> {
|
||||
impl <'a, 'b> Iterator for FatDir<'a, 'b> {
|
||||
type Item = io::Result<FatDirEntry<'a, 'b>>;
|
||||
|
||||
fn next(&mut self) -> Option<io::Result<FatDirEntry<'a, 'b>>> {
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let mut lfn_buf = Vec::<u16>::new();
|
||||
loop {
|
||||
let res = self.read_dir_entry_data();
|
||||
|
@ -4,7 +4,6 @@ use std::io::{SeekFrom, ErrorKind};
|
||||
use std::io;
|
||||
|
||||
use fs::FatSharedStateRef;
|
||||
use table::FatNextCluster;
|
||||
|
||||
|
||||
pub struct FatFile<'a, 'b: 'a> {
|
||||
@ -49,7 +48,7 @@ impl <'a, 'b> Read for FatFile<'a, 'b> {
|
||||
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);
|
||||
self.current_cluster = state.table.cluster_iter(current_cluster).skip(1).next();
|
||||
}
|
||||
}
|
||||
Ok(buf_offset)
|
||||
@ -70,8 +69,8 @@ impl <'a, 'b> Seek for FatFile<'a, 'b> {
|
||||
let cluster_count = (new_offset / cluster_size as i64) as usize;
|
||||
let mut new_cluster = Some(self.first_cluster);
|
||||
let state = self.state.borrow_mut();
|
||||
for _ in 0..cluster_count {
|
||||
new_cluster = state.table.get_next_cluster(new_cluster.unwrap());
|
||||
if cluster_count > 0 {
|
||||
new_cluster = state.table.cluster_iter(new_cluster.unwrap()).skip(cluster_count).next();
|
||||
}
|
||||
self.offset = new_offset as u32;
|
||||
self.current_cluster = new_cluster;
|
||||
|
25
src/table.rs
25
src/table.rs
@ -2,6 +2,7 @@ use std::io::prelude::*;
|
||||
use std::io;
|
||||
use byteorder::{LittleEndian, ReadBytesExt};
|
||||
use fs::FatType;
|
||||
use core::iter;
|
||||
|
||||
pub(crate) struct FatTableData<T> {
|
||||
table: Box<[T]>,
|
||||
@ -35,9 +36,17 @@ impl FatTable {
|
||||
};
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait FatNextCluster {
|
||||
trait FatNextCluster {
|
||||
fn get_next_cluster(&self, cluster: u32) -> Option<u32>;
|
||||
}
|
||||
|
||||
@ -115,3 +124,17 @@ impl FatNextCluster for FatTable32 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct FatClusterIterator<'a> {
|
||||
table: &'a FatTable,
|
||||
cluster: Option<u32>,
|
||||
}
|
||||
|
||||
impl <'a> Iterator for FatClusterIterator<'a> {
|
||||
type Item = u32;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.cluster = self.table.get_next_cluster(self.cluster.unwrap());
|
||||
self.cluster
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user