Skip to content

Commit 59e4c2f

Browse files
committed
Code cleanups.
1 parent 0de58d3 commit 59e4c2f

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

src/filesystem/files.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ pub(crate) struct FileInfo {
211211
pub(crate) file_id: RawFile,
212212
/// The unique ID for the volume this directory is on
213213
pub(crate) volume_id: RawVolume,
214-
/// The current cluster, and how many bytes that short-cuts us
214+
/// The last cluster we accessed, and how many bytes that short-cuts us.
215+
///
216+
/// This saves us walking from the very start of the FAT chain when we move
217+
/// forward through a file.
215218
pub(crate) current_cluster: (u32, ClusterId),
216219
/// How far through the file we've read (in bytes).
217220
pub(crate) current_offset: u32,
@@ -236,41 +239,30 @@ impl FileInfo {
236239

237240
/// Seek to a new position in the file, relative to the start of the file.
238241
pub fn seek_from_start(&mut self, offset: u32) -> Result<(), FileError> {
239-
if offset <= self.entry.size {
240-
self.current_offset = offset;
241-
if offset < self.current_cluster.0 {
242-
// Back to start
243-
self.current_cluster = (0, self.entry.cluster);
244-
}
245-
Ok(())
246-
} else {
247-
Err(FileError::InvalidOffset)
242+
if offset > self.entry.size {
243+
return Err(FileError::InvalidOffset);
248244
}
245+
self.current_offset = offset;
246+
Ok(())
249247
}
250248

251249
/// Seek to a new position in the file, relative to the end of the file.
252250
pub fn seek_from_end(&mut self, offset: u32) -> Result<(), FileError> {
253-
if offset <= self.entry.size {
254-
self.current_offset = self.entry.size - offset;
255-
if self.current_offset < self.current_cluster.0 {
256-
// Back to start
257-
self.current_cluster = (0, self.entry.cluster);
258-
}
259-
Ok(())
260-
} else {
261-
Err(FileError::InvalidOffset)
251+
if offset > self.entry.size {
252+
return Err(FileError::InvalidOffset);
262253
}
254+
self.current_offset = self.entry.size - offset;
255+
Ok(())
263256
}
264257

265258
/// Seek to a new position in the file, relative to the current position.
266259
pub fn seek_from_current(&mut self, offset: i32) -> Result<(), FileError> {
267260
let new_offset = i64::from(self.current_offset) + i64::from(offset);
268-
if new_offset >= 0 && new_offset <= i64::from(self.entry.size) {
269-
self.current_offset = new_offset as u32;
270-
Ok(())
271-
} else {
272-
Err(FileError::InvalidOffset)
261+
if new_offset < 0 || new_offset > i64::from(self.entry.size) {
262+
return Err(FileError::InvalidOffset);
273263
}
264+
self.current_offset = new_offset as u32;
265+
Ok(())
274266
}
275267

276268
/// Amount of file left to read.
@@ -281,7 +273,6 @@ impl FileInfo {
281273
/// Update the file's length.
282274
pub(crate) fn update_length(&mut self, new: u32) {
283275
self.entry.size = new;
284-
self.entry.size = new;
285276
}
286277
}
287278

src/volume_mgr.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ where
10451045

10461046
/// This function turns `desired_offset` into an appropriate block to be
10471047
/// read. It either calculates this based on the start of the file, or
1048-
/// from the last cluster we read - whichever is better.
1048+
/// from the given start point - whichever is better.
10491049
///
10501050
/// Returns:
10511051
///
@@ -1062,15 +1062,16 @@ where
10621062
let bytes_per_cluster = match &self.open_volumes[volume_idx].volume_type {
10631063
VolumeType::Fat(fat) => fat.bytes_per_cluster(),
10641064
};
1065-
// How many clusters forward do we need to go?
1065+
// do we need to be before our start point?
10661066
if desired_offset < start.0 {
10671067
// user wants to go backwards - start from the beginning of the file
10681068
// because the FAT is only a singly-linked list.
10691069
start.0 = 0;
10701070
start.1 = file_start;
10711071
}
1072-
// walk through the FAT chain
1072+
// How many clusters forward do we need to go?
10731073
let offset_from_cluster = desired_offset - start.0;
1074+
// walk through the FAT chain
10741075
let num_clusters = offset_from_cluster / bytes_per_cluster;
10751076
let mut block_cache = BlockCache::empty();
10761077
for _ in 0..num_clusters {

0 commit comments

Comments
 (0)