Skip to content

Commit c54fa28

Browse files
committed
Account for current position in File::size_snapshot
1 parent 430681f commit c54fa28

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/libstd/fs.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,15 @@ impl Read for File {
452452
fn size_snapshot(&self) -> Option<usize> {
453453
if let Ok(meta) = self.metadata() {
454454
let len = meta.len();
455-
let size = len as usize;
456-
// Don't trust a length of zero. For example, "pseudofiles" on Linux
457-
// like /proc/meminfo report a size of 0.
458-
if size != 0 && size as u64 == len {
459-
return Some(size);
455+
if let Ok(position) = self.inner.seek(SeekFrom::Current(0)) {
456+
if let Some(distance) = len.checked_sub(position) {
457+
let size = distance as usize;
458+
// Don't trust a length of zero. For example, "pseudofiles"
459+
// on Linux like /proc/meminfo report a size of 0.
460+
if size != 0 && size as u64 == distance {
461+
return Some(size);
462+
}
463+
}
460464
}
461465
}
462466
None

0 commit comments

Comments
 (0)