Fix formatting

Immprovements by hand + rustfmt
This commit is contained in:
Rafał Harabień 2018-10-10 21:26:31 +02:00
parent 83b5971045
commit 1724a54536
6 changed files with 50 additions and 22 deletions

View File

@ -473,7 +473,9 @@ pub struct DirIter<'a, T: ReadWriteSeek + 'a> {
impl<'a, T: ReadWriteSeek> DirIter<'a, T> { impl<'a, T: ReadWriteSeek> DirIter<'a, T> {
fn new(stream: DirRawStream<'a, T>, fs: &'a FileSystem<T>, skip_volume: bool) -> Self { fn new(stream: DirRawStream<'a, T>, fs: &'a FileSystem<T>, skip_volume: bool) -> Self {
DirIter { DirIter {
stream, fs, skip_volume, stream,
fs,
skip_volume,
err: false, err: false,
} }
} }

View File

@ -158,8 +158,7 @@ impl DirFileEntryData {
} }
pub(crate) fn first_cluster(&self, fat_type: FatType) -> Option<u32> { pub(crate) fn first_cluster(&self, fat_type: FatType) -> Option<u32> {
let first_cluster_hi = let first_cluster_hi = if fat_type == FatType::Fat32 { self.first_cluster_hi } else { 0 };
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; let n = ((first_cluster_hi as u32) << 16) | self.first_cluster_lo as u32;
if n == 0 { if n == 0 {
None None

View File

@ -305,11 +305,13 @@ impl<'a, T: ReadWriteSeek> Seek for File<'a, T> {
SeekFrom::Current(x) => self.offset as i64 + x, SeekFrom::Current(x) => self.offset as i64 + x,
SeekFrom::Start(x) => x as i64, SeekFrom::Start(x) => x as i64,
SeekFrom::End(x) => { SeekFrom::End(x) => {
self.entry let size = self
.entry
.iter() .iter()
.next() .next()
.map_or(None, |e| e.inner().size()) .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 { if new_pos < 0 {

View File

@ -176,11 +176,13 @@ impl BiosParameterBlock {
Ok(bpb) Ok(bpb)
} }
fn validate(&self) -> io::Result<()> { fn validate(&self) -> io::Result<()> {
// sanity checks // sanity checks
if self.bytes_per_sector.count_ones() != 1 { 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 { } else if self.bytes_per_sector < 512 {
return Err(Error::new(ErrorKind::Other, "invalid bytes_per_sector value in BPB (value < 512)")); return Err(Error::new(ErrorKind::Other, "invalid bytes_per_sector value in BPB (value < 512)"));
} else if self.bytes_per_sector > 4096 { } else if self.bytes_per_sector > 4096 {
@ -188,18 +190,24 @@ impl BiosParameterBlock {
} }
if self.sectors_per_cluster.count_ones() != 1 { 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 { } else if self.sectors_per_cluster < 1 {
return Err(Error::new(ErrorKind::Other, "invalid sectors_per_cluster value in BPB (value < 1)")); return Err(Error::new(ErrorKind::Other, "invalid sectors_per_cluster value in BPB (value < 1)"));
} else if self.sectors_per_cluster > 128 { } 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 // 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 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 // 32k is the largest value to maintain greatest compatibility
// Many implementations appear to support 64k per cluster, and some may support 128k or larger // Many implementations appear to support 64k per cluster, and some may support 128k or larger
// However, >32k is not as thoroughly tested... // 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")); return Err(Error::new(ErrorKind::Other, "invalid reserved_sectors value in BPB"));
} else if !self.is_fat32() && self.reserved_sectors != 1 { } else if !self.is_fat32() && self.reserved_sectors != 1 {
// Microsoft document indicates fat12 and fat16 code exists that presume this value is 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", warn!(
self.reserved_sectors); "fs compatibility: reserved_sectors value '{}' in BPB is not '1', and thus is incompatible with some implementations",
self.reserved_sectors
);
} }
if self.fats == 0 { if self.fats == 0 {
return Err(Error::new(ErrorKind::Other, "invalid fats value in BPB")); return Err(Error::new(ErrorKind::Other, "invalid fats value in BPB"));
} else if self.fats > 2 { } else if self.fats > 2 {
// Microsoft document indicates that few implementations support any values other than 1 or 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", warn!(
self.fats); "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 { if self.fs_version != 0 {
@ -387,13 +399,19 @@ impl FsInfoSector {
let max_valid_cluster_number = total_clusters + RESERVED_FAT_ENTRIES; let max_valid_cluster_number = total_clusters + RESERVED_FAT_ENTRIES;
if let Some(n) = self.free_cluster_count { if let Some(n) = self.free_cluster_count {
if n > total_clusters { 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; self.free_cluster_count = None;
} }
} }
if let Some(n) = self.next_free_cluster { if let Some(n) = self.next_free_cluster {
if n > max_valid_cluster_number { 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; self.next_free_cluster = None;
} }
} }

View File

@ -1,6 +1,6 @@
use io;
use byteorder::LittleEndian; use byteorder::LittleEndian;
use byteorder_ext::{ReadBytesExt, WriteBytesExt}; use byteorder_ext::{ReadBytesExt, WriteBytesExt};
use io;
use fs::{FatType, FsStatusFlags, ReadSeek, ReadWriteSeek}; use fs::{FatType, FsStatusFlags, ReadSeek, ReadWriteSeek};
@ -280,7 +280,10 @@ impl FatTrait for Fat32 {
Ok(match val { Ok(match val {
0 if cluster >= 0x0FFFFFF7 && cluster <= 0x0FFFFFFF => { 0 if cluster >= 0x0FFFFFF7 && cluster <= 0x0FFFFFFF => {
let tmp = if cluster == 0x0FFFFFF7 { "BAD_CLUSTER" } else { "end-of-chain" }; 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 FatValue::Bad // avoid accidental use or allocation into a FAT chain
}, },
0 => FatValue::Free, 0 => FatValue::Free,
@ -304,7 +307,10 @@ impl FatTrait for Fat32 {
// or even have them all store value '4' as their next cluster. // or even have them all store value '4' as their next cluster.
// Some believe only FatValue::Bad should be allowed for this edge case. // Some believe only FatValue::Bad should be allowed for this edge case.
let tmp = if cluster == 0x0FFFFFF7 { "BAD_CLUSTER" } else { "end-of-chain" }; 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 { let raw_val = match value {
FatValue::Free => 0, FatValue::Free => 0,

View File

@ -39,10 +39,11 @@ fn open_filesystem_rw(tmp_path: &str) -> FileSystem {
} }
fn call_with_fs(f: &Fn(FileSystem) -> (), filename: &str, test_seq: u32) { 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); let fs = open_filesystem_rw(tmp_path);
f(fs); f(fs);
}, filename, test_seq); };
call_with_tmp_img(&callback, filename, test_seq);
} }
fn test_write_short_file(fs: FileSystem) { fn test_write_short_file(fs: FileSystem) {