From f9f192c35e913d5068c8636eba341828dc88f417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Sun, 1 Oct 2017 21:31:44 +0200 Subject: [PATCH] Add FAT table iterator. --- src/dir.rs | 2 +- src/file.rs | 7 +++---- src/table.rs | 25 ++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index fb80337..f104679 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -262,7 +262,7 @@ impl <'a, 'b> FatDir<'a, 'b> { impl <'a, 'b> Iterator for FatDir<'a, 'b> { type Item = io::Result>; - fn next(&mut self) -> Option>> { + fn next(&mut self) -> Option { let mut lfn_buf = Vec::::new(); loop { let res = self.read_dir_entry_data(); diff --git a/src/file.rs b/src/file.rs index a2256ff..a1bdad4 100644 --- a/src/file.rs +++ b/src/file.rs @@ -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; diff --git a/src/table.rs b/src/table.rs index 30032c3..a8e002f 100644 --- a/src/table.rs +++ b/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 { table: Box<[T]>, @@ -35,9 +36,17 @@ impl FatTable { }; Ok(table) } + + pub fn cluster_iter(&self, cluster: u32) -> iter::Chain, 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; } @@ -115,3 +124,17 @@ impl FatNextCluster for FatTable32 { } } } + +pub(crate) struct FatClusterIterator<'a> { + table: &'a FatTable, + cluster: Option, +} + +impl <'a> Iterator for FatClusterIterator<'a> { + type Item = u32; + + fn next(&mut self) -> Option { + self.cluster = self.table.get_next_cluster(self.cluster.unwrap()); + self.cluster + } +}