Fix dir iterator in case of an error.

Do not resize internal buffer on error, otherwise iterator gets stuck in
infinite loop with zeroed `dirent`s after the first error. Correct
behavior is to return error indefinitely if it persists.
Error can be ENOENT if the directory is unlinked while iterating, for
example.

Signed-off-by: Aleksandr Trukhin <alxtry@gmail.com>
This commit is contained in:
Aleksandr Trukhin
2022-10-10 22:52:03 +03:00
committed by Dan Gohman
parent b72d4291d2
commit 36d79738cc
+6 -2
View File
@@ -136,15 +136,19 @@ impl Dir {
}
fn read_more(&mut self) -> Option<io::Result<()>> {
let og_len = self.buf.len();
// Capacity increment currently chosen by wild guess.
self.buf
.resize(self.buf.capacity() + 32 * size_of::<linux_dirent64>(), 0);
self.pos = 0;
let nread = match crate::backend::fs::syscalls::getdents(self.fd.as_fd(), &mut self.buf) {
Ok(nread) => nread,
Err(err) => return Some(Err(err)),
Err(err) => {
self.buf.resize(og_len, 0);
return Some(Err(err));
}
};
self.buf.resize(nread, 0);
self.pos = 0;
if nread == 0 {
None
} else {