Skip to content

Commit 56a07fe

Browse files
committed
use directly raw volume instead of FatVolume. Do not set dirty if already set
Reason: the function can determine it self.
1 parent 9d6865a commit 56a07fe

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/volume_mgr.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ where
134134
) -> Result<Volume<D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>, Error<D::Error>> {
135135
let v = self.open_raw_volume(volume_idx, open_mode)?;
136136
if open_mode != VolumeOpenMode::ReadOnly {
137-
let idx = self.get_volume_by_id(v)?;
138-
let VolumeType::Fat(volume_type) = &self.open_volumes[idx].volume_type;
139-
self.set_volume_status_dirty(volume_type, true)?;
137+
self.set_volume_status_dirty(v, true)?;
140138
}
141139
Ok(v.to_volume(self))
142140
}
@@ -360,8 +358,7 @@ where
360358
}
361359
let volume_idx = self.get_volume_by_id(volume)?;
362360
if self.open_volumes[volume_idx].open_mode != VolumeOpenMode::ReadOnly {
363-
let VolumeType::Fat(volume_type) = &self.open_volumes[volume_idx].volume_type;
364-
self.set_volume_status_dirty(volume_type, false)?;
361+
self.set_volume_status_dirty(volume, false)?;
365362
}
366363

367364
self.open_volumes.swap_remove(volume_idx);
@@ -372,32 +369,38 @@ where
372369
/// Sets the volume status dirty to dirty if true, to not dirty if false
373370
fn set_volume_status_dirty(
374371
&self,
375-
volume: &FatVolume,
372+
volume: RawVolume,
376373
dirty: bool,
377374
) -> Result<(), Error<D::Error>> {
378375
let mut blocks = [Block::new()];
376+
let idx = self.get_volume_by_id(volume)?;
377+
let VolumeType::Fat(volume) = &self.open_volumes[idx].volume_type;
379378
let fat_table1_start = volume.lba_start + volume.fat_start;
380379
self.block_device
381380
.read(&mut blocks, fat_table1_start, "reading fat table")?;
382381
let block = &mut blocks[0];
383382
let mut fat_table =
384383
fat::FatTable::create_from_bytes(&mut block.contents, volume.get_fat_type())
385384
.map_err(Error::FormatError)?;
386-
fat_table.set_dirty(dirty);
387-
if volume.fat_nums == 1 || volume.fat_nums == 2 {
388-
self.block_device.write(&blocks, fat_table1_start)?;
389-
// Synchronize also backup fat table
390-
if volume.fat_nums == 2 {
391-
self.block_device
392-
.write(&blocks, fat_table1_start + volume.fat_size)?
385+
if !fat_table.dirty() {
386+
fat_table.set_dirty(dirty);
387+
if volume.fat_nums == 1 || volume.fat_nums == 2 {
388+
self.block_device.write(&blocks, fat_table1_start)?;
389+
// Synchronize also backup fat table
390+
if volume.fat_nums == 2 {
391+
self.block_device
392+
.write(&blocks, fat_table1_start + volume.fat_size)?
393+
}
393394
}
394395
}
395396
Ok(())
396397
}
397398

398399
/// Checking if the volume is dirty or was unmounted correctly in a previous usage
399-
pub fn volume_status_dirty(&self, volume: &FatVolume) -> Result<bool, Error<D::Error>> {
400+
pub fn volume_status_dirty(&self, volume: RawVolume) -> Result<bool, Error<D::Error>> {
400401
let mut blocks = [Block::new()];
402+
let volume_idx = self.get_volume_by_id(volume)?;
403+
let VolumeType::Fat(volume) = &self.open_volumes[volume_idx].volume_type;
401404
let fat_table1_start = volume.lba_start + volume.fat_start;
402405
self.block_device
403406
.read(&mut blocks, fat_table1_start, "reading fat table")?;
@@ -1816,8 +1819,8 @@ mod tests {
18161819
})
18171820
}
18181821
);
1819-
let VolumeType::Fat(fat_info) = &c.open_volumes[0].volume_type;
1820-
assert_eq!(c.volume_status_dirty(fat_info).unwrap(), false);
1822+
let volume = c.open_volumes[0].volume_id;
1823+
assert_eq!(c.volume_status_dirty(volume).unwrap(), false);
18211824
}
18221825

18231826
#[test]
@@ -1855,8 +1858,8 @@ mod tests {
18551858
})
18561859
}
18571860
);
1858-
let VolumeType::Fat(fat_info) = &c.open_volumes[0].volume_type;
1859-
assert_eq!(c.volume_status_dirty(fat_info).unwrap(), true);
1861+
let volume = c.open_volumes[0].volume_id;
1862+
assert_eq!(c.volume_status_dirty(volume).unwrap(), true);
18601863
}
18611864

18621865
#[test]
@@ -1897,8 +1900,8 @@ mod tests {
18971900
})
18981901
}
18991902
);
1900-
let VolumeType::Fat(fat_info) = &c.open_volumes[0].volume_type;
1901-
assert_eq!(c.volume_status_dirty(fat_info).unwrap(), false);
1903+
let volume = c.open_volumes[0].volume_id;
1904+
assert_eq!(c.volume_status_dirty(volume).unwrap(), false);
19021905
assert_eq!(c.block_device.blocks.borrow()[0], MBR_BLOCK);
19031906
assert_eq!(
19041907
c.block_device.blocks.borrow()[1],
@@ -1912,13 +1915,13 @@ mod tests {
19121915
assert_eq!(c.block_device.blocks.borrow()[7], DUMMY);
19131916
assert_eq!(c.block_device.blocks.borrow()[8].contents[7] & (1 << 3), 8);
19141917

1915-
c.set_volume_status_dirty(fat_info, true).unwrap();
1916-
assert_eq!(c.volume_status_dirty(fat_info).unwrap(), true);
1918+
c.set_volume_status_dirty(volume, true).unwrap();
1919+
assert_eq!(c.volume_status_dirty(volume).unwrap(), true);
19171920
assert_eq!(c.block_device.blocks.borrow()[3].contents[7] & (1 << 3), 0);
19181921
assert_eq!(c.block_device.blocks.borrow()[8].contents[7] & (1 << 3), 0);
19191922

1920-
c.set_volume_status_dirty(fat_info, false).unwrap();
1921-
assert_eq!(c.volume_status_dirty(fat_info).unwrap(), false);
1923+
c.set_volume_status_dirty(volume, false).unwrap();
1924+
assert_eq!(c.volume_status_dirty(volume).unwrap(), false);
19221925
assert_eq!(c.block_device.blocks.borrow()[0], MBR_BLOCK);
19231926
assert_eq!(
19241927
c.block_device.blocks.borrow()[1],

0 commit comments

Comments
 (0)