diff --git a/src/dir.rs b/src/dir.rs index 656fa7b..03564a3 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -76,7 +76,7 @@ impl Display for RegularDirEntry { write!( f, - "{} {: <16} created: {} modified: {}", + "DirEntry {{ {} {: <16} created: {} modified: {} }}", self.attr, name, self.create_time().format("%a %b %d %H:%M:%S%.3f %Y"), @@ -167,30 +167,6 @@ impl RegularDirEntry { self.attr.contains(Attr::Directory) && !self.attr.intersects(Attr::System | Attr::VolumeId) } - pub fn is_dot(&self) -> bool { - if !self.is_dir() { - return false; - } - - // &self.name[..2] == &[b'.', b' '] - - self.name[0] == b'.' && &self.name[1..] == &[b' '; 10] - } - - pub fn is_dotdot(&self) -> bool { - if !self.is_dir() { - return false; - } - - // &self.name[..3] == &[b'.', b'.', b' '] - - &self.name[..2] == &[b'.', b'.'] && &self.name[2..] == &[b' '; 9] - } - - pub fn is_hidden(&self) -> bool { - self.is_dot() || self.is_dotdot() || self.attr.contains(Attr::Hidden) - } - pub fn name(&self) -> &[u8] { &self.name } diff --git a/src/dump.rs b/src/dump.rs index 0633697..43c2517 100644 --- a/src/dump.rs +++ b/src/dump.rs @@ -1,8 +1,7 @@ use std::io::Read; -use fat_rs::dir::{DirIter, RegularDirEntry}; +use fat_rs::FatFs; use fat_rs::fat::Fatty as _; -use fat_rs::{FatFs, SliceLike}; pub fn main() -> anyhow::Result<()> { let args = std::env::args(); @@ -39,45 +38,5 @@ pub fn main() -> anyhow::Result<()> { println!("{}", dir_entry); } - println!(); - println!(); - - tree(&fat_fs); - Ok(()) } - -fn tree(fat_fs: &FatFs) { - fn do_indent(indent: u32) { - for _ in 0..indent { - print!(" "); - } - } - - fn tree_impl( - fat_fs: &FatFs, - iter: impl Iterator, - indent: u32, - ) { - for dir_entry in iter.filter(|x| !x.is_hidden()) { - do_indent(indent); - - println!("{}", dir_entry); - - if dir_entry.is_dot() || dir_entry.is_dotdot() { - // do not descent into . and .. - continue; - } - - if dir_entry.is_dir() { - let reader = fat_fs.chain_reader(dir_entry.first_cluster()); - - let iter = DirIter::new(reader); - - tree_impl(fat_fs, iter, indent + 1); - } - } - } - - tree_impl(fat_fs, fat_fs.root_dir_iter(), 0); -} diff --git a/src/iter.rs b/src/iter.rs index 2e34765..0e0922e 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -1,5 +1,6 @@ use std::io::Read; +use crate::fat::Fatty; use crate::subslice::SubSlice; use crate::utils::replace; use crate::{FatFs, SliceLike}; @@ -11,7 +12,7 @@ pub struct ClusterChainReader<'a, S: SliceLike> { } impl<'a, S: SliceLike> ClusterChainReader<'a, S> { - pub fn new(fat_fs: &'a FatFs, first_cluster: u32) -> ClusterChainReader<'a, S> { + pub fn new(fat_fs: &'a mut FatFs, first_cluster: u32) -> ClusterChainReader<'a, S> { let next_cluster = fat_fs.next_cluster(first_cluster).unwrap_or(None); let sub_slice = fat_fs.cluster_as_subslice(first_cluster); diff --git a/src/lib.rs b/src/lib.rs index a0f086c..e948f4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,4 @@ -use std::cell::RefCell; -use std::io::{Read, Seek, SeekFrom, Write}; -use std::rc::Rc; +use std::io::{Read as _, Seek as _, SeekFrom, Write as _}; use crate::dir::{DirIter, RegularDirEntry}; use crate::fat::{FatError, Fatty}; @@ -96,7 +94,7 @@ impl SliceLike for std::fs::File { #[allow(dead_code)] pub struct FatFs { - inner: Rc>, + data: S, fat_offset: u64, fat_size: usize, @@ -152,10 +150,8 @@ impl FatFs { let bytes_per_cluster = bpb.bytes_per_cluster(); - let data = Rc::new(RefCell::new(data)); - Ok(FatFs { - inner: data, + data, fat_offset, fat_size, root_dir_offset, @@ -198,7 +194,7 @@ impl FatFs { SubSliceMut::new(self, offset, self.bytes_per_cluster) } - pub fn cluster_as_subslice(&self, cluster: u32) -> SubSlice<'_, S> { + pub fn cluster_as_subslice(&mut self, cluster: u32) -> SubSlice<'_, S> { let offset = self.data_cluster_to_offset(cluster); SubSlice::new(self, offset, self.bytes_per_cluster) @@ -219,20 +215,20 @@ impl FatFs { let mut data = vec![0; self.bytes_per_cluster]; - let mut inner = self.inner.borrow_mut(); - - inner.read_at_offset(self.data_cluster_to_offset(cluster), &mut data)?; + self.data + .read_at_offset(self.data_cluster_to_offset(cluster), &mut data)?; while let Ok(Some(next_cluster)) = self.next_cluster(cluster) { cluster = next_cluster; - inner.read_at_offset(self.data_cluster_to_offset(cluster), &mut data)?; + self.data + .read_at_offset(self.data_cluster_to_offset(cluster), &mut data)?; } Ok(data) } - pub fn root_dir_iter(&self) -> Box + '_> { + pub fn root_dir_iter(&mut self) -> Box + '_> { // TODO: maybe wrap this in another RootDirIter enum, so we don't have to Box if let Some(root_dir_offset) = self.root_dir_offset { @@ -252,8 +248,4 @@ impl FatFs { Box::new(DirIter::new(cluster_iter)) } - - pub fn chain_reader(&self, first_cluster: u32) -> impl Read { - iter::ClusterChainReader::new(self, first_cluster) - } } diff --git a/src/subslice.rs b/src/subslice.rs index 821081c..4dbb43c 100644 --- a/src/subslice.rs +++ b/src/subslice.rs @@ -44,8 +44,7 @@ impl Read for SubSliceMut<'_, S> { let bytes_to_read = self.len.min(buf.len()); self.fat_fs - .inner - .borrow_mut() + .data .read_at_offset(self.offset, &mut buf[..bytes_to_read])?; self.offset += bytes_to_read as u64; @@ -60,8 +59,7 @@ impl Write for SubSliceMut<'_, S> { let bytes_to_write = self.len.min(buf.len()); self.fat_fs - .inner - .borrow_mut() + .data .write_at_offset(self.offset, &buf[..bytes_to_write])?; self.offset += bytes_to_write as u64; @@ -76,7 +74,7 @@ impl Write for SubSliceMut<'_, S> { } pub struct SubSlice<'a, S: SliceLike> { - fat_fs: &'a FatFs, + fat_fs: &'a mut FatFs, offset: u64, len: usize, @@ -92,7 +90,7 @@ impl Debug for SubSlice<'_, S> { } impl SubSlice<'_, S> { - pub fn new(fat_fs: &FatFs, offset: u64, len: usize) -> SubSlice<'_, S> { + pub fn new(fat_fs: &mut FatFs, offset: u64, len: usize) -> SubSlice<'_, S> { SubSlice { fat_fs, offset, @@ -117,7 +115,7 @@ impl SubSlice<'_, S> { } impl<'a, S: SliceLike> SubSlice<'a, S> { - pub fn release(self) -> &'a FatFs { + pub fn release(self) -> &'a mut FatFs { self.fat_fs } } @@ -127,8 +125,7 @@ impl Read for SubSlice<'_, S> { let bytes_to_read = self.len.min(buf.len()); self.fat_fs - .inner - .borrow_mut() + .data .read_at_offset(self.offset, &mut buf[..bytes_to_read])?; self.offset += bytes_to_read as u64;