port https://github.com/bytecodealliance/wasmtime/pull/2620 into rewrite
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
use more_asserts::assert_gt;
|
use more_asserts::assert_gt;
|
||||||
use std::{cmp::min, env, mem, process, slice, str};
|
use std::{env, mem, process, slice, str};
|
||||||
use wasi_tests::open_scratch_directory;
|
use wasi_tests::open_scratch_directory;
|
||||||
|
|
||||||
const BUF_LEN: usize = 256;
|
const BUF_LEN: usize = 256;
|
||||||
@@ -58,8 +58,8 @@ unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> (Vec<DirEntr
|
|||||||
let mut buf: [u8; BUF_LEN] = [0; BUF_LEN];
|
let mut buf: [u8; BUF_LEN] = [0; BUF_LEN];
|
||||||
let bufused =
|
let bufused =
|
||||||
wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir");
|
wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir");
|
||||||
|
assert!(bufused <= BUF_LEN);
|
||||||
let sl = slice::from_raw_parts(buf.as_ptr(), min(BUF_LEN, bufused));
|
let sl = slice::from_raw_parts(buf.as_ptr(), bufused);
|
||||||
let dirs: Vec<_> = ReadDir::from_slice(sl).collect();
|
let dirs: Vec<_> = ReadDir::from_slice(sl).collect();
|
||||||
let eof = bufused < BUF_LEN;
|
let eof = bufused < BUF_LEN;
|
||||||
(dirs, eof)
|
(dirs, eof)
|
||||||
|
|||||||
@@ -650,9 +650,6 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
|||||||
let dirent_len: types::Size = dirent_raw.len().try_into()?;
|
let dirent_len: types::Size = dirent_raw.len().try_into()?;
|
||||||
let name_raw = name.as_bytes();
|
let name_raw = name.as_bytes();
|
||||||
let name_len: types::Size = name_raw.len().try_into()?;
|
let name_len: types::Size = name_raw.len().try_into()?;
|
||||||
let offset = dirent_len
|
|
||||||
.checked_add(name_len)
|
|
||||||
.ok_or_else(|| Error::overflow())?;
|
|
||||||
|
|
||||||
// Copy as many bytes of the dirent as we can, up to the end of the buffer
|
// Copy as many bytes of the dirent as we can, up to the end of the buffer
|
||||||
let dirent_copy_len = std::cmp::min(dirent_len, buf_len - bufused);
|
let dirent_copy_len = std::cmp::min(dirent_len, buf_len - bufused);
|
||||||
@@ -666,6 +663,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buf = buf.add(dirent_copy_len)?;
|
buf = buf.add(dirent_copy_len)?;
|
||||||
|
bufused += dirent_copy_len;
|
||||||
|
|
||||||
// Copy as many bytes of the name as we can, up to the end of the buffer
|
// Copy as many bytes of the name as we can, up to the end of the buffer
|
||||||
let name_copy_len = std::cmp::min(name_len, buf_len - bufused);
|
let name_copy_len = std::cmp::min(name_len, buf_len - bufused);
|
||||||
@@ -680,7 +678,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buf = buf.add(name_copy_len)?;
|
buf = buf.add(name_copy_len)?;
|
||||||
bufused += offset;
|
bufused += name_copy_len;
|
||||||
}
|
}
|
||||||
Ok(bufused)
|
Ok(bufused)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user