Skip to content

Commit a3c895b

Browse files
committed
Fix read_file_backwards.
We weren't actually reading backwards because we always did "seek from start". Now we do some "seek from current" with a negative offset, and a "seek from end".
1 parent 07d7bd2 commit a3c895b

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

tests/read_file.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,27 +150,31 @@ fn read_file_backwards() {
150150

151151
const CHUNK_SIZE: u32 = 100;
152152
let length = volume_mgr.file_length(test_file).expect("file length");
153-
let mut offset = length - CHUNK_SIZE;
154153
let mut read = 0;
155154

155+
// go to end
156+
volume_mgr.file_seek_from_end(test_file, 0).expect("seek");
157+
156158
// We're going to read the file backwards in chunks of 100 bytes. This
157159
// checks we didn't make any assumptions about only going forwards.
158160
while read < length {
161+
// go to start of next chunk
159162
volume_mgr
160-
.file_seek_from_start(test_file, offset)
163+
.file_seek_from_current(test_file, -(CHUNK_SIZE as i32))
161164
.expect("seek");
165+
// read chunk
162166
let mut buffer = [0u8; CHUNK_SIZE as usize];
163167
let len = volume_mgr.read(test_file, &mut buffer).expect("read");
164168
assert_eq!(len, CHUNK_SIZE as usize);
165169
contents.push_front(buffer.to_vec());
166170
read += CHUNK_SIZE;
167-
if offset >= CHUNK_SIZE {
168-
offset -= CHUNK_SIZE;
169-
}
171+
// go to start of chunk we just read
172+
volume_mgr
173+
.file_seek_from_current(test_file, -(CHUNK_SIZE as i32))
174+
.expect("seek");
170175
}
171176

172177
assert_eq!(read, length);
173-
assert_eq!(offset, 0);
174178

175179
let flat: Vec<u8> = contents.iter().flatten().copied().collect();
176180

0 commit comments

Comments
 (0)