diff --git a/src/dir.rs b/src/dir.rs index ce59d08..4dae8ca 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -473,7 +473,9 @@ pub struct DirIter<'a, T: ReadWriteSeek + 'a> { impl<'a, T: ReadWriteSeek> DirIter<'a, T> { fn new(stream: DirRawStream<'a, T>, fs: &'a FileSystem, skip_volume: bool) -> Self { DirIter { - stream, fs, skip_volume, + stream, + fs, + skip_volume, err: false, } } diff --git a/src/dir_entry.rs b/src/dir_entry.rs index 566b8a0..01a0c24 100644 --- a/src/dir_entry.rs +++ b/src/dir_entry.rs @@ -158,8 +158,7 @@ impl DirFileEntryData { } pub(crate) fn first_cluster(&self, fat_type: FatType) -> Option { - let first_cluster_hi = - if fat_type == FatType::Fat32 { self.first_cluster_hi } else { 0 }; + let first_cluster_hi = if fat_type == FatType::Fat32 { self.first_cluster_hi } else { 0 }; let n = ((first_cluster_hi as u32) << 16) | self.first_cluster_lo as u32; if n == 0 { None diff --git a/src/file.rs b/src/file.rs index 3175009..3046bd0 100644 --- a/src/file.rs +++ b/src/file.rs @@ -305,11 +305,13 @@ impl<'a, T: ReadWriteSeek> Seek for File<'a, T> { SeekFrom::Current(x) => self.offset as i64 + x, SeekFrom::Start(x) => x as i64, SeekFrom::End(x) => { - self.entry + let size = self + .entry .iter() .next() .map_or(None, |e| e.inner().size()) - .expect("cannot seek from end if size is unknown") as i64 + x + .expect("cannot seek from end if size is unknown") as i64; + size + x }, }; if new_pos < 0 { diff --git a/src/fs.rs b/src/fs.rs index d5acd9b..4ee15b0 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -176,11 +176,13 @@ impl BiosParameterBlock { Ok(bpb) } - fn validate(&self) -> io::Result<()> { // sanity checks if self.bytes_per_sector.count_ones() != 1 { - return Err(Error::new(ErrorKind::Other, "invalid bytes_per_sector value in BPB (not power of two)")); + return Err(Error::new( + ErrorKind::Other, + "invalid bytes_per_sector value in BPB (not power of two)", + )); } else if self.bytes_per_sector < 512 { return Err(Error::new(ErrorKind::Other, "invalid bytes_per_sector value in BPB (value < 512)")); } else if self.bytes_per_sector > 4096 { @@ -188,18 +190,24 @@ impl BiosParameterBlock { } if self.sectors_per_cluster.count_ones() != 1 { - return Err(Error::new(ErrorKind::Other, "invalid sectors_per_cluster value in BPB (not power of two)")); + return Err(Error::new( + ErrorKind::Other, + "invalid sectors_per_cluster value in BPB (not power of two)", + )); } else if self.sectors_per_cluster < 1 { return Err(Error::new(ErrorKind::Other, "invalid sectors_per_cluster value in BPB (value < 1)")); } else if self.sectors_per_cluster > 128 { - return Err(Error::new(ErrorKind::Other, "invalid sectors_per_cluster value in BPB (value > 128)")); + return Err(Error::new( + ErrorKind::Other, + "invalid sectors_per_cluster value in BPB (value > 128)", + )); } // bytes per sector is u16, sectors per cluster is u8, so guaranteed no overflow in multiplication let bytes_per_cluster = self.bytes_per_sector as u32 * self.sectors_per_cluster as u32; - let maximum_compatibility_bytes_per_cluster : u32 = 32 * 1024; + let maximum_compatibility_bytes_per_cluster: u32 = 32 * 1024; - if bytes_per_cluster > maximum_compatibility_bytes_per_cluster { + if bytes_per_cluster > maximum_compatibility_bytes_per_cluster { // 32k is the largest value to maintain greatest compatibility // Many implementations appear to support 64k per cluster, and some may support 128k or larger // However, >32k is not as thoroughly tested... @@ -211,16 +219,20 @@ impl BiosParameterBlock { return Err(Error::new(ErrorKind::Other, "invalid reserved_sectors value in BPB")); } else if !self.is_fat32() && self.reserved_sectors != 1 { // Microsoft document indicates fat12 and fat16 code exists that presume this value is 1 - warn!("fs compatibility: reserved_sectors value '{}' in BPB is not '1', and thus is incompatible with some implementations", - self.reserved_sectors); + warn!( + "fs compatibility: reserved_sectors value '{}' in BPB is not '1', and thus is incompatible with some implementations", + self.reserved_sectors + ); } if self.fats == 0 { return Err(Error::new(ErrorKind::Other, "invalid fats value in BPB")); } else if self.fats > 2 { // Microsoft document indicates that few implementations support any values other than 1 or 2 - warn!("fs compatibility: numbers of FATs '{}' in BPB is greater than '2', and thus is incompatible with some implementations", - self.fats); + warn!( + "fs compatibility: numbers of FATs '{}' in BPB is greater than '2', and thus is incompatible with some implementations", + self.fats + ); } if self.fs_version != 0 { @@ -387,13 +399,19 @@ impl FsInfoSector { let max_valid_cluster_number = total_clusters + RESERVED_FAT_ENTRIES; if let Some(n) = self.free_cluster_count { if n > total_clusters { - warn!("invalid free_cluster_count ({}) in fs_info exceeds total cluster count ({})", n, total_clusters); + warn!( + "invalid free_cluster_count ({}) in fs_info exceeds total cluster count ({})", + n, total_clusters + ); self.free_cluster_count = None; } } if let Some(n) = self.next_free_cluster { if n > max_valid_cluster_number { - warn!("invalid free_cluster_count ({}) in fs_info exceeds maximum cluster number ({})", n, max_valid_cluster_number); + warn!( + "invalid free_cluster_count ({}) in fs_info exceeds maximum cluster number ({})", + n, max_valid_cluster_number + ); self.next_free_cluster = None; } } diff --git a/src/table.rs b/src/table.rs index 0d34418..746278a 100644 --- a/src/table.rs +++ b/src/table.rs @@ -1,6 +1,6 @@ -use io; use byteorder::LittleEndian; use byteorder_ext::{ReadBytesExt, WriteBytesExt}; +use io; use fs::{FatType, FsStatusFlags, ReadSeek, ReadWriteSeek}; @@ -280,7 +280,10 @@ impl FatTrait for Fat32 { Ok(match val { 0 if cluster >= 0x0FFFFFF7 && cluster <= 0x0FFFFFFF => { let tmp = if cluster == 0x0FFFFFF7 { "BAD_CLUSTER" } else { "end-of-chain" }; - warn!("cluster number {} is a special value in FAT to indicate {}; it should never be seen as free", cluster, tmp); + warn!( + "cluster number {} is a special value in FAT to indicate {}; it should never be seen as free", + cluster, tmp + ); FatValue::Bad // avoid accidental use or allocation into a FAT chain }, 0 => FatValue::Free, @@ -304,7 +307,10 @@ impl FatTrait for Fat32 { // or even have them all store value '4' as their next cluster. // Some believe only FatValue::Bad should be allowed for this edge case. let tmp = if cluster == 0x0FFFFFF7 { "BAD_CLUSTER" } else { "end-of-chain" }; - panic!("cluster number {} is a special value in FAT to indicate {}; it should never be set as free", cluster, tmp); + panic!( + "cluster number {} is a special value in FAT to indicate {}; it should never be set as free", + cluster, tmp + ); }; let raw_val = match value { FatValue::Free => 0, diff --git a/tests/write.rs b/tests/write.rs index 529f093..814b57a 100644 --- a/tests/write.rs +++ b/tests/write.rs @@ -39,10 +39,11 @@ fn open_filesystem_rw(tmp_path: &str) -> FileSystem { } fn call_with_fs(f: &Fn(FileSystem) -> (), filename: &str, test_seq: u32) { - call_with_tmp_img(&|tmp_path| { + let callback = |tmp_path: &str| { let fs = open_filesystem_rw(tmp_path); f(fs); - }, filename, test_seq); + }; + call_with_tmp_img(&callback, filename, test_seq); } fn test_write_short_file(fs: FileSystem) {