@@ -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
0 commit comments