move wasi-c2 into wasi-common
This commit is contained in:
249
crates/wasi-common/cap-std-sync/src/dir.rs
Normal file
249
crates/wasi-common/cap-std-sync/src/dir.rs
Normal file
@@ -0,0 +1,249 @@
|
||||
use crate::file::{filetype_from, File};
|
||||
use cap_fs_ext::{DirExt, MetadataExt, SystemTimeSpec};
|
||||
use std::any::Any;
|
||||
use std::convert::TryInto;
|
||||
use std::path::{Path, PathBuf};
|
||||
use wasi_c2::{
|
||||
dir::{ReaddirCursor, ReaddirEntity, WasiDir},
|
||||
file::{FdFlags, FileCaps, FileType, Filestat, OFlags, WasiFile},
|
||||
Error, ErrorExt,
|
||||
};
|
||||
|
||||
pub struct Dir(cap_std::fs::Dir);
|
||||
|
||||
impl Dir {
|
||||
pub fn from_cap_std(dir: cap_std::fs::Dir) -> Self {
|
||||
Dir(dir)
|
||||
}
|
||||
}
|
||||
|
||||
impl WasiDir for Dir {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn open_file(
|
||||
&self,
|
||||
symlink_follow: bool,
|
||||
path: &str,
|
||||
oflags: OFlags,
|
||||
caps: FileCaps,
|
||||
fdflags: FdFlags,
|
||||
) -> Result<Box<dyn WasiFile>, Error> {
|
||||
use cap_fs_ext::{FollowSymlinks, OpenOptionsFollowExt};
|
||||
|
||||
let mut opts = cap_std::fs::OpenOptions::new();
|
||||
|
||||
if oflags.contains(OFlags::CREATE | OFlags::EXCLUSIVE) {
|
||||
opts.create_new(true);
|
||||
opts.write(true);
|
||||
} else if oflags.contains(OFlags::CREATE) {
|
||||
opts.create(true);
|
||||
opts.write(true);
|
||||
}
|
||||
if oflags.contains(OFlags::TRUNCATE) {
|
||||
opts.truncate(true);
|
||||
}
|
||||
if caps.contains(FileCaps::WRITE)
|
||||
|| caps.contains(FileCaps::DATASYNC)
|
||||
|| caps.contains(FileCaps::ALLOCATE)
|
||||
|| caps.contains(FileCaps::FILESTAT_SET_SIZE)
|
||||
{
|
||||
opts.write(true);
|
||||
} else {
|
||||
// If not opened write, open read. This way the OS lets us open the file.
|
||||
// If FileCaps::READ is not set, read calls will be rejected at the
|
||||
// get_cap check.
|
||||
opts.read(true);
|
||||
}
|
||||
if caps.contains(FileCaps::READ) {
|
||||
opts.read(true);
|
||||
}
|
||||
if fdflags.contains(FdFlags::APPEND) {
|
||||
opts.append(true);
|
||||
}
|
||||
// XXX what about rest of fdflags - dsync, sync become oflags.
|
||||
// what do we do with nonblock?
|
||||
// what do we do with rsync?
|
||||
|
||||
if symlink_follow {
|
||||
opts.follow(FollowSymlinks::Yes);
|
||||
} else {
|
||||
opts.follow(FollowSymlinks::No);
|
||||
}
|
||||
|
||||
let f = self.0.open_with(Path::new(path), &opts)?;
|
||||
Ok(Box::new(File::from_cap_std(f)))
|
||||
}
|
||||
|
||||
fn open_dir(&self, symlink_follow: bool, path: &str) -> Result<Box<dyn WasiDir>, Error> {
|
||||
let d = if symlink_follow {
|
||||
self.0.open_dir(Path::new(path))?
|
||||
} else {
|
||||
self.0.open_dir_nofollow(Path::new(path))?
|
||||
};
|
||||
Ok(Box::new(Dir::from_cap_std(d)))
|
||||
}
|
||||
|
||||
fn create_dir(&self, path: &str) -> Result<(), Error> {
|
||||
self.0.create_dir(Path::new(path))?;
|
||||
Ok(())
|
||||
}
|
||||
fn readdir(
|
||||
&self,
|
||||
cursor: ReaddirCursor,
|
||||
) -> Result<Box<dyn Iterator<Item = Result<(ReaddirEntity, String), 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.
|
||||
let dir_meta = self.0.dir_metadata()?;
|
||||
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))
|
||||
},
|
||||
{
|
||||
// XXX if parent dir is mounted it *might* be possible to give its inode, but we
|
||||
// don't know that in this context.
|
||||
let name = "..".to_owned();
|
||||
let namelen = name.as_bytes().len().try_into().expect("2 wont overflow");
|
||||
Ok((FileType::Directory, dir_meta.ino(), namelen, name))
|
||||
},
|
||||
]
|
||||
.into_iter()
|
||||
.chain(
|
||||
// Now process the `DirEntry`s:
|
||||
self.0.entries()?.map(|entry| {
|
||||
let entry = entry?;
|
||||
let meta = entry.metadata()?;
|
||||
let inode = meta.ino();
|
||||
let filetype = filetype_from(&meta.file_type());
|
||||
let name = entry
|
||||
.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))
|
||||
}),
|
||||
)
|
||||
// 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,
|
||||
},
|
||||
name,
|
||||
)),
|
||||
Err(e) => Err(e),
|
||||
})
|
||||
.skip(u64::from(cursor) as usize);
|
||||
|
||||
Ok(Box::new(rd))
|
||||
}
|
||||
|
||||
fn symlink(&self, src_path: &str, dest_path: &str) -> Result<(), Error> {
|
||||
self.0.symlink(src_path, dest_path)?;
|
||||
Ok(())
|
||||
}
|
||||
fn remove_dir(&self, path: &str) -> Result<(), Error> {
|
||||
self.0.remove_dir(Path::new(path))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn unlink_file(&self, path: &str) -> Result<(), Error> {
|
||||
self.0.remove_file(Path::new(path))?;
|
||||
Ok(())
|
||||
}
|
||||
fn read_link(&self, path: &str) -> Result<PathBuf, Error> {
|
||||
let link = self.0.read_link(Path::new(path))?;
|
||||
Ok(link)
|
||||
}
|
||||
fn get_filestat(&self) -> Result<Filestat, Error> {
|
||||
let meta = self.0.dir_metadata()?;
|
||||
Ok(Filestat {
|
||||
device_id: meta.dev(),
|
||||
inode: meta.ino(),
|
||||
filetype: filetype_from(&meta.file_type()),
|
||||
nlink: meta.nlink(),
|
||||
size: meta.len(),
|
||||
atim: meta.accessed().map(|t| Some(t.into_std())).unwrap_or(None),
|
||||
mtim: meta.modified().map(|t| Some(t.into_std())).unwrap_or(None),
|
||||
ctim: meta.created().map(|t| Some(t.into_std())).unwrap_or(None),
|
||||
})
|
||||
}
|
||||
fn get_path_filestat(&self, path: &str, follow_symlinks: bool) -> Result<Filestat, Error> {
|
||||
let meta = if follow_symlinks {
|
||||
self.0.metadata(Path::new(path))?
|
||||
} else {
|
||||
self.0.symlink_metadata(Path::new(path))?
|
||||
};
|
||||
Ok(Filestat {
|
||||
device_id: meta.dev(),
|
||||
inode: meta.ino(),
|
||||
filetype: filetype_from(&meta.file_type()),
|
||||
nlink: meta.nlink(),
|
||||
size: meta.len(),
|
||||
atim: meta.accessed().map(|t| Some(t.into_std())).unwrap_or(None),
|
||||
mtim: meta.modified().map(|t| Some(t.into_std())).unwrap_or(None),
|
||||
ctim: meta.created().map(|t| Some(t.into_std())).unwrap_or(None),
|
||||
})
|
||||
}
|
||||
fn rename(&self, src_path: &str, dest_dir: &dyn WasiDir, dest_path: &str) -> Result<(), Error> {
|
||||
let dest_dir = dest_dir
|
||||
.as_any()
|
||||
.downcast_ref::<Self>()
|
||||
.ok_or(Error::badf().context("failed downcast to cap-std Dir"))?;
|
||||
self.0
|
||||
.rename(Path::new(src_path), &dest_dir.0, Path::new(dest_path))?;
|
||||
Ok(())
|
||||
}
|
||||
fn hard_link(
|
||||
&self,
|
||||
src_path: &str,
|
||||
target_dir: &dyn WasiDir,
|
||||
target_path: &str,
|
||||
) -> Result<(), Error> {
|
||||
let target_dir = target_dir
|
||||
.as_any()
|
||||
.downcast_ref::<Self>()
|
||||
.ok_or(Error::badf().context("failed downcast to cap-std Dir"))?;
|
||||
let src_path = Path::new(src_path);
|
||||
let target_path = Path::new(target_path);
|
||||
self.0.hard_link(src_path, &target_dir.0, target_path)?;
|
||||
Ok(())
|
||||
}
|
||||
fn set_times(
|
||||
&self,
|
||||
path: &str,
|
||||
atime: Option<wasi_c2::SystemTimeSpec>,
|
||||
mtime: Option<wasi_c2::SystemTimeSpec>,
|
||||
follow_symlinks: bool,
|
||||
) -> Result<(), Error> {
|
||||
if follow_symlinks {
|
||||
self.0.set_times(
|
||||
Path::new(path),
|
||||
convert_systimespec(atime),
|
||||
convert_systimespec(mtime),
|
||||
)?;
|
||||
} else {
|
||||
self.0.set_symlink_times(
|
||||
Path::new(path),
|
||||
convert_systimespec(atime),
|
||||
convert_systimespec(mtime),
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_systimespec(t: Option<wasi_c2::SystemTimeSpec>) -> Option<SystemTimeSpec> {
|
||||
match t {
|
||||
Some(wasi_c2::SystemTimeSpec::Absolute(t)) => Some(SystemTimeSpec::Absolute(t)),
|
||||
Some(wasi_c2::SystemTimeSpec::SymbolicNow) => Some(SystemTimeSpec::SymbolicNow),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user