fat-rs/fat-bits/src/iter.rs

141 lines
3.5 KiB
Rust
Raw Normal View History

2025-08-01 18:09:45 +02:00
use std::io::{Read, Write};
2025-07-26 15:51:21 +02:00
use crate::FatFs;
2025-08-01 18:09:45 +02:00
use crate::subslice::{SubSlice, SubSliceMut};
2025-07-26 15:51:21 +02:00
pub struct ClusterChainReader<'a> {
2025-08-01 18:09:45 +02:00
fat_fs: &'a FatFs,
sub_slice: SubSlice,
2025-07-26 15:51:21 +02:00
next_cluster: Option<u32>,
}
impl<'a> ClusterChainReader<'a> {
pub fn new(fat_fs: &'a FatFs, first_cluster: u32) -> ClusterChainReader<'a> {
2025-07-26 15:51:21 +02:00
let next_cluster = fat_fs.next_cluster(first_cluster).unwrap_or(None);
let sub_slice = fat_fs.cluster_as_subslice(first_cluster);
ClusterChainReader {
2025-08-01 18:09:45 +02:00
fat_fs,
2025-07-26 15:51:21 +02:00
sub_slice,
next_cluster,
}
}
2025-08-01 01:08:48 +02:00
fn move_to_next_cluster(&mut self) -> bool {
2025-07-26 15:51:21 +02:00
let Some(next_cluster) = self.next_cluster else {
return false;
};
2025-08-01 18:09:45 +02:00
self.next_cluster = self.fat_fs.next_cluster(next_cluster).unwrap_or(None);
self.sub_slice = self.fat_fs.cluster_as_subslice(next_cluster);
2025-07-26 15:51:21 +02:00
true
}
2025-08-01 01:08:48 +02:00
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
}
2025-07-26 15:51:21 +02:00
}
2025-08-01 01:08:48 +02:00
impl Read for ClusterChainReader<'_> {
2025-07-26 15:51:21 +02:00
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
if self.sub_slice.is_empty() {
2025-08-01 01:08:48 +02:00
if !self.move_to_next_cluster() {
2025-07-26 15:51:21 +02:00
return Ok(0);
}
}
self.sub_slice.read(buf)
}
}
2025-08-01 18:09:45 +02:00
pub struct ClusterChainWriter<'a> {
fat_fs: &'a FatFs,
sub_slice: SubSliceMut,
next_cluster: Option<u32>,
}
impl<'a> ClusterChainWriter<'a> {
pub fn new(fat_fs: &'a FatFs, first_cluster: u32) -> ClusterChainWriter<'a> {
let next_cluster = fat_fs.next_cluster(first_cluster).unwrap_or(None);
let sub_slice = fat_fs.cluster_as_subslice_mut(first_cluster);
ClusterChainWriter {
fat_fs,
sub_slice,
next_cluster,
}
}
fn move_to_next_cluster(&mut self) -> bool {
// TODO: should allocate a new cluster here!
let Some(next_cluster) = self.next_cluster else {
return false;
};
self.next_cluster = self.fat_fs.next_cluster(next_cluster).unwrap_or(None);
self.fat_fs.cluster_as_subslice_mut(next_cluster);
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 Write for ClusterChainWriter<'_> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if self.sub_slice.is_empty() {
if !(self.move_to_next_cluster()) {
return Ok(0);
}
}
self.sub_slice.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}