simplify life for readdir implementors

This commit is contained in:
Pat Hickey
2021-02-03 18:03:40 -08:00
parent 72a8f9235d
commit e2b67aa9a3
3 changed files with 23 additions and 29 deletions

View File

@@ -1,7 +1,6 @@
use crate::file::{filetype_from, File}; use crate::file::{filetype_from, File};
use cap_fs_ext::{DirEntryExt, DirExt, MetadataExt, SystemTimeSpec}; use cap_fs_ext::{DirEntryExt, DirExt, MetadataExt, SystemTimeSpec};
use std::any::Any; use std::any::Any;
use std::convert::TryInto;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use system_interface::fs::GetSetFdFlags; use system_interface::fs::GetSetFdFlags;
use wasi_common::{ use wasi_common::{
@@ -101,7 +100,7 @@ impl WasiDir for Dir {
fn readdir( fn readdir(
&self, &self,
cursor: ReaddirCursor, 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. // 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 // 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. // have enough info to make a ReaddirEntity yet.
@@ -109,13 +108,11 @@ impl WasiDir for Dir {
let rd = vec![ let rd = vec![
{ {
let name = ".".to_owned(); let name = ".".to_owned();
let namelen = name.as_bytes().len().try_into().expect("1 wont overflow"); Ok((FileType::Directory, dir_meta.ino(), name))
Ok((FileType::Directory, dir_meta.ino(), namelen, name))
}, },
{ {
let name = "..".to_owned(); let name = "..".to_owned();
let namelen = name.as_bytes().len().try_into().expect("2 wont overflow"); Ok((FileType::Directory, dir_meta.ino(), name))
Ok((FileType::Directory, dir_meta.ino(), namelen, name))
}, },
] ]
.into_iter() .into_iter()
@@ -130,22 +127,18 @@ impl WasiDir for Dir {
.file_name() .file_name()
.into_string() .into_string()
.map_err(|_| Error::illegal_byte_sequence().context("filename"))?; .map_err(|_| Error::illegal_byte_sequence().context("filename"))?;
let namelen = name.as_bytes().len().try_into()?; Ok((filetype, inode, name))
Ok((filetype, inode, namelen, name))
}), }),
) )
// Enumeration of the iterator makes it possible to define the ReaddirCursor // Enumeration of the iterator makes it possible to define the ReaddirCursor
.enumerate() .enumerate()
.map(|(ix, r)| match r { .map(|(ix, r)| match r {
Ok((filetype, inode, namelen, name)) => Ok(( Ok((filetype, inode, name)) => Ok(ReaddirEntity {
ReaddirEntity {
next: ReaddirCursor::from(ix as u64 + 1), next: ReaddirCursor::from(ix as u64 + 1),
filetype, filetype,
inode, inode,
namelen,
},
name, name,
)), }),
Err(e) => Err(e), Err(e) => Err(e),
}) })
.skip(u64::from(cursor) as usize); .skip(u64::from(cursor) as usize);
@@ -286,8 +279,8 @@ mod test {
.readdir(ReaddirCursor::from(0)) .readdir(ReaddirCursor::from(0))
.expect("readdir succeeds") .expect("readdir succeeds")
{ {
let (entity, name) = readdir_result.expect("readdir entry is valid"); let entity = readdir_result.expect("readdir entry is valid");
out.insert(name, entity); out.insert(entity.name.clone(), entity);
} }
out out
} }

View File

@@ -22,7 +22,7 @@ pub trait WasiDir {
fn readdir( fn readdir(
&self, &self,
cursor: ReaddirCursor, 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 symlink(&self, old_path: &str, new_path: &str) -> Result<(), Error>;
fn remove_dir(&self, path: &str) -> Result<(), Error>; fn remove_dir(&self, path: &str) -> Result<(), Error>;
fn unlink_file(&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 struct ReaddirEntity {
pub next: ReaddirCursor, pub next: ReaddirCursor,
pub inode: u64, pub inode: u64,
pub namelen: u32, pub name: String,
pub filetype: FileType, pub filetype: FileType,
} }

View File

@@ -640,16 +640,16 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
) -> Result<types::Size, Error> { ) -> Result<types::Size, Error> {
let mut bufused = 0; let mut bufused = 0;
let mut buf = buf.clone(); let mut buf = buf.clone();
for pair in self for entity in self
.table() .table()
.get_dir(u32::from(fd))? .get_dir(u32::from(fd))?
.get_cap(DirCaps::READDIR)? .get_cap(DirCaps::READDIR)?
.readdir(ReaddirCursor::from(cookie))? .readdir(ReaddirCursor::from(cookie))?
{ {
let (entity, name) = pair?; let entity = entity?;
let dirent_raw = dirent_bytes(types::Dirent::from(&entity)); let dirent_raw = dirent_bytes(types::Dirent::try_from(&entity)?);
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 = entity.name.as_bytes();
let name_len: types::Size = name_raw.len().try_into()?; 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 // 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 { impl TryFrom<&ReaddirEntity> for types::Dirent {
fn from(e: &ReaddirEntity) -> types::Dirent { type Error = Error;
types::Dirent { fn try_from(e: &ReaddirEntity) -> Result<types::Dirent, Error> {
Ok(types::Dirent {
d_ino: e.inode, 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_type: types::Filetype::from(&e.filetype),
d_next: e.next.into(), d_next: e.next.into(),
} })
} }
} }