Don't recalculate cluster in seek if not needed.

This commit is contained in:
Rafał Harabień 2017-10-15 16:17:41 +02:00
parent e2374c08cc
commit b2c3eb1823

View File

@ -270,19 +270,21 @@ impl<'a, 'b> Seek for File<'a, 'b> {
return Ok(self.offset as u64); return Ok(self.offset as u64);
} }
let cluster_size = self.fs.get_cluster_size(); let cluster_size = self.fs.get_cluster_size();
let new_cluster = if new_pos == 0 { // get number of clusters to seek (favoring previous cluster in corner case)
let cluster_count = ((new_pos + cluster_size as i64 - 1) / cluster_size as i64 - 1) as isize;
let old_cluster_count = ((self.offset as i64 + cluster_size as i64 - 1) / cluster_size as i64 - 1) as isize;
let new_cluster = if new_pos == 0 {
None None
} else if cluster_count == old_cluster_count {
self.current_cluster
} else { } else {
// get number of clusters to seek (favoring previous cluster in corner case)
let cluster_count = ((new_pos - 1) / cluster_size as i64) as isize;
match self.first_cluster { match self.first_cluster {
Some(n) => { Some(n) => {
let mut cluster = n; let mut cluster = n;
let mut iter = self.fs.cluster_iter(n); let mut iter = self.fs.cluster_iter(n);
for i in 0..cluster_count { for i in 0..cluster_count {
cluster = match iter.next() { cluster = match iter.next() {
Some(Err(err)) => return Err(err), Some(r) => r?,
Some(Ok(n)) => n,
None => { None => {
// chain ends before new position - seek to end of last cluster // chain ends before new position - seek to end of last cluster
new_pos = (i + 1) as i64 * cluster_size as i64; new_pos = (i + 1) as i64 * cluster_size as i64;