Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Implement host file flags and type functions. #42

Merged
merged 1 commit into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 71 additions & 20 deletions host/src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
};
use wasi_common::{
dir::TableDirExt,
file::{FileStream, TableFileExt},
file::{FdFlags, FileStream, TableFileExt},
WasiDir, WasiFile,
};

Expand Down Expand Up @@ -101,44 +101,63 @@ fn convert(error: wasi_common::Error) -> wasmtime::component::Error<wasi_filesys
}
}

impl Into<wasi_common::file::OFlags> for wasi_filesystem::OFlags {
fn into(self) -> wasi_common::file::OFlags {
impl From<wasi_filesystem::OFlags> for wasi_common::file::OFlags {
fn from(oflags: wasi_filesystem::OFlags) -> Self {
let mut flags = wasi_common::file::OFlags::empty();
if contains(self, wasi_filesystem::OFlags::CREATE) {
if contains(oflags, wasi_filesystem::OFlags::CREATE) {
flags |= wasi_common::file::OFlags::CREATE;
}
if contains(self, wasi_filesystem::OFlags::DIRECTORY) {
if contains(oflags, wasi_filesystem::OFlags::DIRECTORY) {
flags |= wasi_common::file::OFlags::DIRECTORY;
}
if contains(self, wasi_filesystem::OFlags::EXCL) {
if contains(oflags, wasi_filesystem::OFlags::EXCL) {
flags |= wasi_common::file::OFlags::EXCLUSIVE;
}
if contains(self, wasi_filesystem::OFlags::TRUNC) {
if contains(oflags, wasi_filesystem::OFlags::TRUNC) {
flags |= wasi_common::file::OFlags::TRUNCATE;
}
flags
}
}

impl Into<wasi_common::file::FdFlags> for wasi_filesystem::DescriptorFlags {
fn into(self) -> wasi_common::file::FdFlags {
let mut flags = wasi_common::file::FdFlags::empty();
if contains(self, wasi_filesystem::DescriptorFlags::DSYNC) {
flags |= wasi_common::file::FdFlags::DSYNC;
impl From<FdFlags> for wasi_filesystem::DescriptorFlags {
fn from(fdflags: FdFlags) -> Self {
let mut flags = wasi_filesystem::DescriptorFlags::empty();
if contains(fdflags, FdFlags::DSYNC) {
flags |= wasi_filesystem::DescriptorFlags::DSYNC;
}
if contains(self, wasi_filesystem::DescriptorFlags::NONBLOCK) {
flags |= wasi_common::file::FdFlags::NONBLOCK;
if contains(fdflags, FdFlags::NONBLOCK) {
flags |= wasi_filesystem::DescriptorFlags::NONBLOCK;
}
if contains(self, wasi_filesystem::DescriptorFlags::RSYNC) {
flags |= wasi_common::file::FdFlags::RSYNC;
if contains(fdflags, FdFlags::RSYNC) {
flags |= wasi_filesystem::DescriptorFlags::RSYNC;
}
if contains(self, wasi_filesystem::DescriptorFlags::SYNC) {
flags |= wasi_common::file::FdFlags::SYNC;
if contains(fdflags, FdFlags::SYNC) {
flags |= wasi_filesystem::DescriptorFlags::SYNC;
}
flags
}
}

impl From<wasi_filesystem::DescriptorFlags> for FdFlags {
fn from(flags: wasi_filesystem::DescriptorFlags) -> FdFlags {
let mut fdflags = FdFlags::empty();
if contains(flags, wasi_filesystem::DescriptorFlags::DSYNC) {
fdflags |= FdFlags::DSYNC;
}
if contains(flags, wasi_filesystem::DescriptorFlags::NONBLOCK) {
fdflags |= FdFlags::NONBLOCK;
}
if contains(flags, wasi_filesystem::DescriptorFlags::RSYNC) {
fdflags |= FdFlags::RSYNC;
}
if contains(flags, wasi_filesystem::DescriptorFlags::SYNC) {
fdflags |= FdFlags::SYNC;
}
fdflags
}
}

impl From<wasi_common::file::FileType> for wasi_filesystem::DescriptorType {
fn from(type_: wasi_common::file::FileType) -> Self {
match type_ {
Expand Down Expand Up @@ -204,14 +223,46 @@ impl wasi_filesystem::WasiFilesystem for WasiCtx {
&mut self,
fd: wasi_filesystem::Descriptor,
) -> HostResult<wasi_filesystem::DescriptorFlags, wasi_filesystem::Errno> {
todo!()
let table = self.table();
if table.is::<Box<dyn WasiFile>>(fd) {
Ok(table
.get_file(fd)
.map_err(convert)?
.get_fdflags()
.await
.map_err(convert)?
.into())
} else if table.is::<Box<dyn WasiDir>>(fd) {
Ok(table
.get_dir(fd)
.map_err(convert)?
.get_fdflags()
.await
.map_err(convert)?
.into())
} else {
Err(wasi_filesystem::Errno::Badf.into())
}
}

async fn todo_type(
&mut self,
fd: wasi_filesystem::Descriptor,
) -> HostResult<wasi_filesystem::DescriptorType, wasi_filesystem::Errno> {
todo!()
let table = self.table();
if table.is::<Box<dyn WasiFile>>(fd) {
Ok(table
.get_file(fd)
.map_err(convert)?
.get_filetype()
.await
.map_err(convert)?
.into())
} else if table.is::<Box<dyn WasiDir>>(fd) {
Ok(wasi_filesystem::DescriptorType::Directory)
} else {
Err(wasi_filesystem::Errno::Badf.into())
}
}

async fn set_flags(
Expand Down
7 changes: 6 additions & 1 deletion wasi-common/cap-std-sync/src/dir.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::file::{filetype_from, File};
use crate::file::{filetype_from, get_fd_flags, File};
use cap_fs_ext::{DirEntryExt, DirExt, MetadataExt, SystemTimeSpec};
use std::any::Any;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -80,6 +80,11 @@ impl Dir {
Ok(File::from_cap_std(f))
}

pub fn get_fdflags(&self) -> Result<FdFlags, Error> {
let fdflags = get_fd_flags(&self.0)?;
Ok(fdflags)
}

pub fn open_dir_(&self, symlink_follow: bool, path: &str) -> Result<Self, Error> {
let d = if symlink_follow {
self.0.open_dir(Path::new(path))?
Expand Down
4 changes: 4 additions & 0 deletions wasi-common/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub trait WasiDir: Send + Sync {
Err(Error::not_supported())
}

async fn get_fdflags(&self) -> Result<FdFlags, Error> {
Ok(FdFlags::empty())
}

async fn create_dir(&self, _path: &str) -> Result<(), Error> {
Err(Error::not_supported())
}
Expand Down