simplify life for readdir implementors
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
use crate::file::{filetype_from, File};
|
||||
use cap_fs_ext::{DirEntryExt, DirExt, MetadataExt, SystemTimeSpec};
|
||||
use std::any::Any;
|
||||
use std::convert::TryInto;
|
||||
use std::path::{Path, PathBuf};
|
||||
use system_interface::fs::GetSetFdFlags;
|
||||
use wasi_common::{
|
||||
@@ -101,7 +100,7 @@ impl WasiDir for Dir {
|
||||
fn readdir(
|
||||
&self,
|
||||
cursor: ReaddirCursor,
|
||||
) -> Result<Box<dyn Iterator<Item = Result<(ReaddirEntity, String), Error>>>, Error> {
|
||||
) -> Result<Box<dyn Iterator<Item = Result<ReaddirEntity, Error>>>, Error> {
|
||||
// cap_std's read_dir does not include . and .., we should prepend these.
|
||||
// Why does the Ok contain a tuple? We can't construct a cap_std::fs::DirEntry, and we don't
|
||||
// have enough info to make a ReaddirEntity yet.
|
||||
@@ -109,13 +108,11 @@ impl WasiDir for Dir {
|
||||
let rd = vec![
|
||||
{
|
||||
let name = ".".to_owned();
|
||||
let namelen = name.as_bytes().len().try_into().expect("1 wont overflow");
|
||||
Ok((FileType::Directory, dir_meta.ino(), namelen, name))
|
||||
Ok((FileType::Directory, dir_meta.ino(), name))
|
||||
},
|
||||
{
|
||||
let name = "..".to_owned();
|
||||
let namelen = name.as_bytes().len().try_into().expect("2 wont overflow");
|
||||
Ok((FileType::Directory, dir_meta.ino(), namelen, name))
|
||||
Ok((FileType::Directory, dir_meta.ino(), name))
|
||||
},
|
||||
]
|
||||
.into_iter()
|
||||
@@ -130,22 +127,18 @@ impl WasiDir for Dir {
|
||||
.file_name()
|
||||
.into_string()
|
||||
.map_err(|_| Error::illegal_byte_sequence().context("filename"))?;
|
||||
let namelen = name.as_bytes().len().try_into()?;
|
||||
Ok((filetype, inode, namelen, name))
|
||||
Ok((filetype, inode, name))
|
||||
}),
|
||||
)
|
||||
// Enumeration of the iterator makes it possible to define the ReaddirCursor
|
||||
.enumerate()
|
||||
.map(|(ix, r)| match r {
|
||||
Ok((filetype, inode, namelen, name)) => Ok((
|
||||
ReaddirEntity {
|
||||
next: ReaddirCursor::from(ix as u64 + 1),
|
||||
filetype,
|
||||
inode,
|
||||
namelen,
|
||||
},
|
||||
Ok((filetype, inode, name)) => Ok(ReaddirEntity {
|
||||
next: ReaddirCursor::from(ix as u64 + 1),
|
||||
filetype,
|
||||
inode,
|
||||
name,
|
||||
)),
|
||||
}),
|
||||
Err(e) => Err(e),
|
||||
})
|
||||
.skip(u64::from(cursor) as usize);
|
||||
@@ -286,8 +279,8 @@ mod test {
|
||||
.readdir(ReaddirCursor::from(0))
|
||||
.expect("readdir succeeds")
|
||||
{
|
||||
let (entity, name) = readdir_result.expect("readdir entry is valid");
|
||||
out.insert(name, entity);
|
||||
let entity = readdir_result.expect("readdir entry is valid");
|
||||
out.insert(entity.name.clone(), entity);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ pub trait WasiDir {
|
||||
fn readdir(
|
||||
&self,
|
||||
cursor: ReaddirCursor,
|
||||
) -> Result<Box<dyn Iterator<Item = Result<(ReaddirEntity, String), Error>>>, Error>;
|
||||
) -> Result<Box<dyn Iterator<Item = Result<ReaddirEntity, Error>>>, Error>;
|
||||
fn symlink(&self, old_path: &str, new_path: &str) -> Result<(), Error>;
|
||||
fn remove_dir(&self, path: &str) -> Result<(), Error>;
|
||||
fn unlink_file(&self, path: &str) -> Result<(), Error>;
|
||||
@@ -166,7 +166,7 @@ impl TableDirExt for crate::table::Table {
|
||||
pub struct ReaddirEntity {
|
||||
pub next: ReaddirCursor,
|
||||
pub inode: u64,
|
||||
pub namelen: u32,
|
||||
pub name: String,
|
||||
pub filetype: FileType,
|
||||
}
|
||||
|
||||
|
||||
@@ -640,16 +640,16 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
) -> Result<types::Size, Error> {
|
||||
let mut bufused = 0;
|
||||
let mut buf = buf.clone();
|
||||
for pair in self
|
||||
for entity in self
|
||||
.table()
|
||||
.get_dir(u32::from(fd))?
|
||||
.get_cap(DirCaps::READDIR)?
|
||||
.readdir(ReaddirCursor::from(cookie))?
|
||||
{
|
||||
let (entity, name) = pair?;
|
||||
let dirent_raw = dirent_bytes(types::Dirent::from(&entity));
|
||||
let entity = entity?;
|
||||
let dirent_raw = dirent_bytes(types::Dirent::try_from(&entity)?);
|
||||
let dirent_len: types::Size = dirent_raw.len().try_into()?;
|
||||
let name_raw = name.as_bytes();
|
||||
let name_raw = entity.name.as_bytes();
|
||||
let name_len: types::Size = name_raw.len().try_into()?;
|
||||
|
||||
// Copy as many bytes of the dirent as we can, up to the end of the buffer
|
||||
@@ -1438,14 +1438,15 @@ impl From<Filestat> for types::Filestat {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&ReaddirEntity> for types::Dirent {
|
||||
fn from(e: &ReaddirEntity) -> types::Dirent {
|
||||
types::Dirent {
|
||||
impl TryFrom<&ReaddirEntity> for types::Dirent {
|
||||
type Error = Error;
|
||||
fn try_from(e: &ReaddirEntity) -> Result<types::Dirent, Error> {
|
||||
Ok(types::Dirent {
|
||||
d_ino: e.inode,
|
||||
d_namlen: e.namelen,
|
||||
d_namlen: e.name.as_bytes().len().try_into()?,
|
||||
d_type: types::Filetype::from(&e.filetype),
|
||||
d_next: e.next.into(),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user