Simplify code by using while and if let constructions
This commit is contained in:
parent
a760b4b6d0
commit
380add131d
@ -493,10 +493,7 @@ impl LongNameBuilder {
|
||||
fn truncate(&mut self) {
|
||||
// Truncate 0 and 0xFFFF characters from LFN buffer
|
||||
let mut lfn_len = self.buf.len();
|
||||
loop {
|
||||
if lfn_len == 0 {
|
||||
break;
|
||||
}
|
||||
while lfn_len > 0 {
|
||||
match self.buf[lfn_len-1] {
|
||||
0xFFFF | 0 => lfn_len -= 1,
|
||||
_ => break,
|
||||
|
47
src/file.rs
47
src/file.rs
@ -32,27 +32,21 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
|
||||
fn update_size(&mut self) {
|
||||
let offset = self.offset;
|
||||
match self.entry {
|
||||
Some(ref mut e) => {
|
||||
if let Some(ref mut e) = self.entry {
|
||||
e.reset_modified();
|
||||
if e.inner().size().map_or(false, |s| offset > s) {
|
||||
e.set_size(offset);
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
/// Truncate file in current position.
|
||||
pub fn truncate(&mut self) -> io::Result<()> {
|
||||
match self.entry {
|
||||
Some(ref mut e) => {
|
||||
if let Some(ref mut e) = self.entry {
|
||||
e.set_size(self.offset);
|
||||
if self.offset == 0 {
|
||||
e.set_first_cluster(None, self.fs.fat_type());
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
if self.offset > 0 {
|
||||
debug_assert!(self.current_cluster.is_some());
|
||||
@ -60,11 +54,10 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
self.fs.cluster_iter(self.current_cluster.unwrap()).truncate() // SAFE
|
||||
} else {
|
||||
debug_assert!(self.current_cluster.is_none());
|
||||
match self.first_cluster {
|
||||
Some(n) => self.fs.cluster_iter(n).free()?,
|
||||
_ => {},
|
||||
}
|
||||
if let Some(n) = self.first_cluster {
|
||||
self.fs.cluster_iter(n).free()?;
|
||||
self.first_cluster = None;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -84,9 +77,8 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
}
|
||||
|
||||
pub(crate) fn flush_dir_entry(&mut self) -> io::Result<()> {
|
||||
match self.entry {
|
||||
Some(ref mut e) => e.flush(self.fs)?,
|
||||
_ => {},
|
||||
if let Some(ref mut e) = self.entry {
|
||||
e.flush(self.fs)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -95,9 +87,8 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
///
|
||||
/// Note: if chrono feature is enabled (default) library automatically updates all timestamps
|
||||
pub fn set_created(&mut self, date_time: DateTime) {
|
||||
match self.entry {
|
||||
Some(ref mut e) => e.set_created(date_time),
|
||||
_ => {},
|
||||
if let Some(ref mut e) = self.entry {
|
||||
e.set_created(date_time);
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,9 +96,8 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
///
|
||||
/// Note: if chrono feature is enabled (default) library automatically updates all timestamps
|
||||
pub fn set_accessed(&mut self, date: Date) {
|
||||
match self.entry {
|
||||
Some(ref mut e) => e.set_accessed(date),
|
||||
_ => {},
|
||||
if let Some(ref mut e) = self.entry {
|
||||
e.set_accessed(date);
|
||||
}
|
||||
}
|
||||
|
||||
@ -115,9 +105,8 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
///
|
||||
/// Note: if chrono feature is enabled (default) library automatically updates all timestamps
|
||||
pub fn set_modified(&mut self, date_time: DateTime) {
|
||||
match self.entry {
|
||||
Some(ref mut e) => e.set_modified(date_time),
|
||||
_ => {},
|
||||
if let Some(ref mut e) = self.entry {
|
||||
e.set_modified(date_time);
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,9 +119,8 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
|
||||
fn set_first_cluster(&mut self, cluster: u32) {
|
||||
self.first_cluster = Some(cluster);
|
||||
match self.entry {
|
||||
Some(ref mut e) => e.set_first_cluster(self.first_cluster, self.fs.fat_type()),
|
||||
None => {},
|
||||
if let Some(ref mut e) = self.entry {
|
||||
e.set_first_cluster(self.first_cluster, self.fs.fat_type());
|
||||
}
|
||||
}
|
||||
|
||||
@ -143,9 +131,8 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
|
||||
impl<'a, 'b> Drop for File<'a, 'b> {
|
||||
fn drop(&mut self) {
|
||||
match self.flush() {
|
||||
Err(err) => error!("flush failed {}", err),
|
||||
_ => {},
|
||||
if let Err(err) = self.flush() {
|
||||
error!("flush failed {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
54
src/table.rs
54
src/table.rs
@ -68,17 +68,16 @@ pub(crate) fn alloc_cluster(fat: &mut DiskSlice, fat_type: FatType, prev_cluster
|
||||
let end_cluster = total_clusters + RESERVED_FAT_ENTRIES;
|
||||
let start_cluster = match hint {
|
||||
Some(n) if n < end_cluster => n,
|
||||
_ => 2,
|
||||
_ => RESERVED_FAT_ENTRIES,
|
||||
};
|
||||
let new_cluster = match find_free_cluster(fat, fat_type, start_cluster, end_cluster) {
|
||||
Ok(n) => n,
|
||||
Err(_) if start_cluster > 2 => find_free_cluster(fat, fat_type, 2, start_cluster)?,
|
||||
Err(_) if start_cluster > RESERVED_FAT_ENTRIES => find_free_cluster(fat, fat_type, RESERVED_FAT_ENTRIES, start_cluster)?,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
write_fat(fat, fat_type, new_cluster, FatValue::EndOfChain)?;
|
||||
match prev_cluster {
|
||||
Some(n) => write_fat(fat, fat_type, n, FatValue::Data(new_cluster))?,
|
||||
None => {},
|
||||
if let Some(n) = prev_cluster {
|
||||
write_fat(fat, fat_type, n, FatValue::Data(new_cluster))?;
|
||||
}
|
||||
trace!("allocated cluster {}", new_cluster);
|
||||
Ok(new_cluster)
|
||||
@ -187,7 +186,7 @@ impl FatTrait for Fat12 {
|
||||
let mut cluster = RESERVED_FAT_ENTRIES;
|
||||
fat.seek(io::SeekFrom::Start((cluster*3/2) as u64))?;
|
||||
let mut prev_packed_val = 0u16;
|
||||
loop {
|
||||
while cluster < end_cluster {
|
||||
let res = match cluster & 1 {
|
||||
0 => fat.read_u16::<LittleEndian>(),
|
||||
_ => fat.read_u8().map(|n| n as u16),
|
||||
@ -206,9 +205,6 @@ impl FatTrait for Fat12 {
|
||||
count += 1;
|
||||
}
|
||||
cluster += 1;
|
||||
if cluster >= end_cluster {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(count)
|
||||
}
|
||||
@ -245,23 +241,21 @@ impl FatTrait for Fat16 {
|
||||
fn find_free(fat: &mut ReadSeek, start_cluster: u32, end_cluster: u32) -> io::Result<u32> {
|
||||
let mut cluster = start_cluster;
|
||||
fat.seek(io::SeekFrom::Start((cluster*2) as u64))?;
|
||||
loop {
|
||||
while cluster < end_cluster {
|
||||
let val = fat.read_u16::<LittleEndian>()?;
|
||||
if val == 0 {
|
||||
return Ok(cluster);
|
||||
}
|
||||
cluster += 1;
|
||||
if cluster == end_cluster {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "end of FAT reached"));
|
||||
}
|
||||
}
|
||||
Err(io::Error::new(io::ErrorKind::Other, "end of FAT reached"))
|
||||
}
|
||||
|
||||
fn count_free(fat: &mut ReadSeek, end_cluster: u32) -> io::Result<u32> {
|
||||
let mut count = 0;
|
||||
let mut cluster = RESERVED_FAT_ENTRIES;
|
||||
fat.seek(io::SeekFrom::Start((cluster*2) as u64))?;
|
||||
loop {
|
||||
while cluster < end_cluster {
|
||||
match fat.read_u16::<LittleEndian>() {
|
||||
Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => break,
|
||||
Err(err) => return Err(err),
|
||||
@ -269,9 +263,6 @@ impl FatTrait for Fat16 {
|
||||
_ => {},
|
||||
}
|
||||
cluster += 1;
|
||||
if cluster >= end_cluster {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(count)
|
||||
}
|
||||
@ -308,23 +299,21 @@ impl FatTrait for Fat32 {
|
||||
fn find_free(fat: &mut ReadSeek, start_cluster: u32, end_cluster: u32) -> io::Result<u32> {
|
||||
let mut cluster = start_cluster;
|
||||
fat.seek(io::SeekFrom::Start((cluster*4) as u64))?;
|
||||
loop {
|
||||
while cluster < end_cluster {
|
||||
let val = fat.read_u32::<LittleEndian>()? & 0x0FFFFFFF;
|
||||
if val == 0 {
|
||||
return Ok(cluster);
|
||||
}
|
||||
cluster += 1;
|
||||
if cluster == end_cluster {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "end of FAT reached"));
|
||||
}
|
||||
}
|
||||
Err(io::Error::new(io::ErrorKind::Other, "end of FAT reached"))
|
||||
}
|
||||
|
||||
fn count_free(fat: &mut ReadSeek, end_cluster: u32) -> io::Result<u32> {
|
||||
let mut count = 0;
|
||||
let mut cluster = RESERVED_FAT_ENTRIES;
|
||||
fat.seek(io::SeekFrom::Start((cluster*4) as u64))?;
|
||||
loop {
|
||||
while cluster < end_cluster {
|
||||
match fat.read_u32::<LittleEndian>() {
|
||||
Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => break,
|
||||
Err(err) => return Err(err),
|
||||
@ -332,9 +321,6 @@ impl FatTrait for Fat32 {
|
||||
_ => {},
|
||||
}
|
||||
cluster += 1;
|
||||
if cluster >= end_cluster {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(count)
|
||||
}
|
||||
@ -373,13 +359,9 @@ impl <'a, 'b> ClusterIterator<'a, 'b> {
|
||||
}
|
||||
|
||||
pub(crate) fn free(&mut self) -> io::Result<()> {
|
||||
loop {
|
||||
let prev = self.cluster;
|
||||
while let Some(n) = self.cluster {
|
||||
self.next();
|
||||
match prev {
|
||||
Some(n) => write_fat(&mut self.fat, self.fat_type, n, FatValue::Free)?,
|
||||
None => break,
|
||||
};
|
||||
write_fat(&mut self.fat, self.fat_type, n, FatValue::Free)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -392,8 +374,7 @@ impl <'a, 'b> Iterator for ClusterIterator<'a, 'b> {
|
||||
if self.err {
|
||||
return None;
|
||||
}
|
||||
match self.cluster {
|
||||
Some(current_cluster) => {
|
||||
if let Some(current_cluster) = self.cluster {
|
||||
self.cluster = match get_next_cluster(&mut self.fat, self.fat_type, current_cluster) {
|
||||
Ok(next_cluster) => next_cluster,
|
||||
Err(err) => {
|
||||
@ -401,12 +382,7 @@ impl <'a, 'b> Iterator for ClusterIterator<'a, 'b> {
|
||||
return Some(Err(err));
|
||||
},
|
||||
}
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
match self.cluster {
|
||||
Some(n) => Some(Ok(n)),
|
||||
None => None,
|
||||
}
|
||||
self.cluster.map(|n| Ok(n))
|
||||
}
|
||||
}
|
||||
|
@ -130,9 +130,8 @@ impl<T: Read+Write+Seek> Seek for BufStream<T> {
|
||||
|
||||
impl<T: Read+Write+Seek> Drop for BufStream<T> {
|
||||
fn drop(&mut self) {
|
||||
match self.flush() {
|
||||
Err(err) => error!("flush failed {}", err),
|
||||
_ => {},
|
||||
if let Err(err) = self.flush() {
|
||||
error!("flush failed {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user