@@ -211,7 +211,10 @@ pub(crate) struct FileInfo {
211
211
pub ( crate ) file_id : RawFile ,
212
212
/// The unique ID for the volume this directory is on
213
213
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.
215
218
pub ( crate ) current_cluster : ( u32 , ClusterId ) ,
216
219
/// How far through the file we've read (in bytes).
217
220
pub ( crate ) current_offset : u32 ,
@@ -236,41 +239,30 @@ impl FileInfo {
236
239
237
240
/// Seek to a new position in the file, relative to the start of the file.
238
241
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 ) ;
248
244
}
245
+ self . current_offset = offset;
246
+ Ok ( ( ) )
249
247
}
250
248
251
249
/// Seek to a new position in the file, relative to the end of the file.
252
250
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 ) ;
262
253
}
254
+ self . current_offset = self . entry . size - offset;
255
+ Ok ( ( ) )
263
256
}
264
257
265
258
/// Seek to a new position in the file, relative to the current position.
266
259
pub fn seek_from_current ( & mut self , offset : i32 ) -> Result < ( ) , FileError > {
267
260
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 ) ;
273
263
}
264
+ self . current_offset = new_offset as u32 ;
265
+ Ok ( ( ) )
274
266
}
275
267
276
268
/// Amount of file left to read.
@@ -281,7 +273,6 @@ impl FileInfo {
281
273
/// Update the file's length.
282
274
pub ( crate ) fn update_length ( & mut self , new : u32 ) {
283
275
self . entry . size = new;
284
- self . entry . size = new;
285
276
}
286
277
}
287
278
0 commit comments