implemented read

This commit is contained in:
Moritz Gmeiner 2025-08-01 01:08:48 +02:00
commit 2b01b9ff0e
6 changed files with 149 additions and 23 deletions

View file

@ -22,7 +22,7 @@ impl<'a> ClusterChainReader<'a> {
}
}
fn next_cluster(&mut self) -> bool {
fn move_to_next_cluster(&mut self) -> bool {
let Some(next_cluster) = self.next_cluster else {
return false;
};
@ -37,12 +37,33 @@ impl<'a> ClusterChainReader<'a> {
true
}
pub fn skip(&mut self, n: u64) -> u64 {
let mut bytes_to_skip = n;
while bytes_to_skip > self.sub_slice.len() as u64 {
bytes_to_skip -= self.sub_slice.len() as u64;
if !self.move_to_next_cluster() {
// ran out of bytes to seek
return n - bytes_to_skip;
}
}
if bytes_to_skip != 0 {
bytes_to_skip -= self.sub_slice.skip(bytes_to_skip as usize) as u64;
}
// n should absolutely be zero here
assert_eq!(bytes_to_skip, 0);
n
}
}
impl<'a> Read for ClusterChainReader<'a> {
impl Read for ClusterChainReader<'_> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
if self.sub_slice.is_empty() {
if !self.next_cluster() {
if !self.move_to_next_cluster() {
return Ok(0);
}
}

View file

@ -11,7 +11,7 @@ mod datetime;
pub mod dir;
pub mod fat;
pub mod fs_info;
mod iter;
pub mod iter;
mod subslice;
mod utils;
@ -240,7 +240,7 @@ impl FatFs {
Ok(data)
}
fn chain_reader(&self, first_cluster: u32) -> impl Read {
fn chain_reader(&'_ self, first_cluster: u32) -> iter::ClusterChainReader<'_> {
iter::ClusterChainReader::new(self, first_cluster)
}
@ -274,4 +274,10 @@ impl FatFs {
DirIter::new(Box::new(cluster_iter))
}
pub fn file_reader(&self, first_cluster: u32) -> iter::ClusterChainReader<'_> {
assert!(first_cluster >= 2);
self.chain_reader(first_cluster)
}
}

View file

@ -104,16 +104,21 @@ impl SubSlice<'_> {
self.fat_fs
}
pub fn fat_fs_mut(&self) -> &FatFs {
self.fat_fs
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
self.len
}
pub fn skip(&mut self, n: usize) -> usize {
let n = n.min(self.len());
self.offset += n as u64;
self.len -= n;
n
}
}
impl<'a> SubSlice<'a> {