Skip to content

Commit

Permalink
Clear FAT dirty flag before creating FileSystem.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Jun 13, 2022
1 parent 34c59d1 commit d05dca7
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,31 +424,33 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
// Validate the numbers stored in the free_cluster_count and next_free_cluster are within bounds for volume
fs_info.validate_and_fix(total_clusters);

let mut fs = Self {
disk: RefCell::new(disk),
options,
fat_type,
bpb,
first_data_sector,
root_dir_sectors,
total_clusters,
fs_info: RefCell::new(fs_info),
current_status_flags: Cell::new(bpb_status_flags),
};

let mut fat_status_flags = fs.read_fat_status_flags()?;
let mut fat_status_flags = read_fat_flags(&mut fat_slice::<&mut IO, IO::Error, IO>(&mut disk, &bpb), fat_type)?;
if fat_status_flags.dirty() {
if fs.options.ignore_dirty_flag {
if options.ignore_dirty_flag {
warn!("FAT is dirty, clearing dirty flag.");
fat_status_flags.dirty = false;
fs.set_fat_status_flags(fat_status_flags)?;
write_fat_flags(
&mut fat_slice::<&mut IO, IO::Error, IO>(&mut disk, &bpb),
fat_type,
fat_status_flags,
)?;
} else {
return Err(Error::DirtyFileSystem);
}
}

trace!("FileSystem::new end");
Ok(fs)
Ok(Self {
disk: RefCell::new(disk),
options,
fat_type,
bpb,
first_data_sector,
root_dir_sectors,
total_clusters,
fs_info: RefCell::new(fs_info),
current_status_flags: Cell::new(bpb_status_flags),
})
}

/// Returns a type of File Allocation Table (FAT) used by this filesystem.
Expand Down Expand Up @@ -553,7 +555,7 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
/// `Error::Io` will be returned if the underlying storage object returned an I/O error.
pub fn read_status_flags(&self) -> Result<FsStatusFlags, Error<IO::Error>> {
let bpb_status = self.current_status_flags.get();
let fat_status = self.read_fat_status_flags()?;
let fat_status = read_fat_flags(&mut self.fat_slice(), self.fat_type)?;
Ok(FsStatusFlags {
dirty: bpb_status.dirty || fat_status.dirty,
io_error: bpb_status.io_error || fat_status.io_error,
Expand Down Expand Up @@ -619,31 +621,10 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
Ok(())
}

#[inline]
pub(crate) fn set_dirty_flag(&self, dirty: bool) -> Result<(), Error<IO::Error>> {
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;

let mut disk = self.disk.borrow_mut();
write_bpb_status_flags(&mut *disk, self.fat_type(), status_flags)?;
self.current_status_flags.set(status_flags);

Ok(())
}

#[inline]
fn read_fat_status_flags(&self) -> Result<FsStatusFlags, Error<IO::Error>> {
read_fat_flags(&mut self.fat_slice(), self.fat_type)
}

#[inline]
fn set_fat_status_flags(&self, status_flags: FsStatusFlags) -> Result<(), Error<IO::Error>> {
write_fat_flags(&mut self.fat_slice(), self.fat_type, status_flags)
set_dirty_flag(&mut *disk, self.fat_type(), &self.current_status_flags, dirty)
}

/// Returns a root directory object allowing for futher penetration of a filesystem structure.
Expand Down Expand Up @@ -773,6 +754,27 @@ 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,
Expand Down

0 comments on commit d05dca7

Please sign in to comment.