Skip to content

Commit

Permalink
Move functions into impl blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Nov 5, 2022
1 parent 65fe601 commit dfb1181
Showing 1 changed file with 38 additions and 43 deletions.
81 changes: 38 additions & 43 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
if options.ignore_dirty_flag {
warn!("BPB is dirty, clearing dirty flag.");
bpb_status_flags.dirty = false;
write_bpb_status_flags(&mut disk, fat_type, bpb_status_flags)?;
Self::write_bpb_status_flags(&mut disk, fat_type, bpb_status_flags)?;
bpb.set_status_flags(bpb_status_flags);
} else {
return Err(Error::DirtyFileSystem);
Expand Down Expand Up @@ -590,9 +590,7 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
Ok(free_cluster_count)
}

/// Unmounts the filesystem.
///
/// Updates the FS Information Sector if needed.
/// Updates the FS Information Sector if needed and unmounts the filesystem.
///
/// # Errors
///
Expand All @@ -606,6 +604,11 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
Ok(disk)
}

/// Updates the FS information sector if needed.
///
/// # Errors
///
/// `Error::Io` will be returned if the underlying storage object returned an I/O error.
pub fn flush(&mut self) -> Result<(), Error<IO::Error>> {
self.flush_fs_info()?;
self.set_dirty_flag(false)?;
Expand All @@ -624,10 +627,40 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
Ok(())
}

fn write_bpb_status_flags<IO: Seek + Write>(
disk: &mut IO,
fat_type: FatType,
status_flags: FsStatusFlags,
) -> Result<(), Error<IO::Error>> {
let encoded = status_flags.encode();

// Note: only one field is written to avoid rewriting entire boot-sector which could be dangerous
// Compute reserver_1 field offset and write new flags
let offset = if fat_type == FatType::Fat32 { 0x041 } else { 0x025 };

disk.seek(io::SeekFrom::Start(offset))?;
disk.write_u8(encoded)?;

Ok(())
}

#[inline]
pub(crate) fn set_dirty_flag(&self, dirty: bool) -> Result<(), Error<IO::Error>> {
let mut disk = self.disk.borrow_mut();
set_dirty_flag(&mut *disk, self.fat_type(), &self.current_status_flags, dirty)

let mut status_flags = self.current_status_flags.get();

if status_flags.dirty == dirty {
// Dirty flag did not change.
return Ok(());
}

status_flags.dirty = dirty;

Self::write_bpb_status_flags(&mut *disk, self.fat_type(), status_flags)?;
self.current_status_flags.set(status_flags);

Ok(())
}

/// Returns a root directory object allowing for futher penetration of a filesystem structure.
Expand Down Expand Up @@ -757,44 +790,6 @@ impl<IO: ReadWriteSeek, TP, OCC> Clone for FsIoAdapter<'_, IO, TP, OCC> {
}
}

fn set_dirty_flag<IO: Seek + Write>(
disk: &mut IO,
fat_type: FatType,
current_status_flags: &Cell<FsStatusFlags>,
dirty: bool,
) -> Result<(), Error<IO::Error>> {
let mut status_flags = current_status_flags.get();

if status_flags.dirty == dirty {
// Dirty flag did not change.
return Ok(());
}

status_flags.dirty = dirty;

write_bpb_status_flags(disk, fat_type, status_flags)?;
current_status_flags.set(status_flags);

Ok(())
}

fn write_bpb_status_flags<IO: Seek + Write>(
disk: &mut IO,
fat_type: FatType,
status_flags: FsStatusFlags,
) -> Result<(), Error<IO::Error>> {
let encoded = status_flags.encode();

// Note: only one field is written to avoid rewriting entire boot-sector which could be dangerous
// Compute reserver_1 field offset and write new flags
let offset = if fat_type == FatType::Fat32 { 0x041 } else { 0x025 };

disk.seek(io::SeekFrom::Start(offset))?;
disk.write_u8(encoded)?;

Ok(())
}

fn fat_slice<B: BorrowMut<S>, E, S: ReadWriteSeek>(io: B, bpb: &BiosParameterBlock) -> DiskSlice<B, E, S> {
let sectors_per_fat = bpb.sectors_per_fat();
let mirroring_enabled = bpb.mirroring_enabled();
Expand Down

0 comments on commit dfb1181

Please sign in to comment.