Skip to content
Draft
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
4 changes: 4 additions & 0 deletions crates/wasi/src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ pub(crate) enum DescriptorType {
SymbolicLink,
/// The descriptor refers to a regular file inode.
RegularFile,
/// The descriptor refers to a FIFO
Fifo,
}

impl From<cap_std::fs::FileType> for DescriptorType {
Expand All @@ -268,6 +270,8 @@ impl From<cap_std::fs::FileType> for DescriptorType {
DescriptorType::CharacterDevice
} else if ft.is_file() {
DescriptorType::RegularFile
} else if ft.is_fifo() {
DescriptorType::Fifo
} else {
DescriptorType::Unknown
}
Expand Down
8 changes: 4 additions & 4 deletions crates/wasi/src/p0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ wiggle::from_witx!({
fd_filestat_set_times, fd_read, fd_pread, fd_seek, fd_sync, fd_readdir, fd_write,
fd_pwrite, poll_oneoff, path_create_directory, path_filestat_get,
path_filestat_set_times, path_link, path_open, path_readlink, path_remove_directory,
path_rename, path_symlink, path_unlink_file
path_rename, path_symlink, path_unlink_file, fd_tell,
}
},
errors: { errno => trappable Error },
Expand All @@ -50,7 +50,7 @@ mod sync {
fd_filestat_set_times, fd_read, fd_pread, fd_seek, fd_sync, fd_readdir, fd_write,
fd_pwrite, poll_oneoff, path_create_directory, path_filestat_get,
path_filestat_set_times, path_link, path_open, path_readlink, path_remove_directory,
path_rename, path_symlink, path_unlink_file
path_rename, path_symlink, path_unlink_file, fd_tell,
}
},
errors: { errno => trappable Error },
Expand Down Expand Up @@ -325,12 +325,12 @@ impl<T: Snapshot1 + Send> wasi_unstable::WasiUnstable for T {
Ok(())
}

fn fd_tell(
async fn fd_tell(
&mut self,
memory: &mut GuestMemory<'_>,
fd: types::Fd,
) -> Result<types::Filesize, Error> {
Ok(Snapshot1::fd_tell(self, memory, fd.into())?)
Ok(Snapshot1::fd_tell(self, memory, fd.into()).await?)
}

async fn fd_readdir(
Expand Down
24 changes: 17 additions & 7 deletions crates/wasi/src/p1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,19 @@ impl Transaction<'_> {
/// # Errors
///
/// Returns [`types::Errno::Spipe`] if the descriptor corresponds to stdio
fn get_seekable(&self, fd: types::Fd) -> Result<&File> {
async fn get_seekable(&mut self, fd: types::Fd) -> Result<&File> {
let fd = fd.into();
match self.descriptors.used.get(&fd) {
Some(Descriptor::File(file)) => Ok(file),
Some(Descriptor::File(file)) => {
let descriptor_type = self.view.filesystem().get_type(file.fd.borrowed()).await?;
if matches!(
descriptor_type,
filesystem::DescriptorType::CharacterDevice | filesystem::DescriptorType::Fifo
) {
return Err(types::Errno::Spipe.into());
}
Ok(file)
}
Some(
Descriptor::Stdin { .. } | Descriptor::Stdout { .. } | Descriptor::Stderr { .. },
) => {
Expand Down Expand Up @@ -863,7 +872,7 @@ wiggle::from_witx!({
fd_filestat_set_times, fd_read, fd_pread, fd_seek, fd_sync, fd_readdir, fd_write,
fd_pwrite, poll_oneoff, path_create_directory, path_filestat_get,
path_filestat_set_times, path_link, path_open, path_readlink, path_remove_directory,
path_rename, path_symlink, path_unlink_file
path_rename, path_symlink, path_unlink_file, fd_tell,
}
},
errors: { errno => trappable Error },
Expand All @@ -882,7 +891,7 @@ pub(crate) mod sync {
fd_filestat_set_times, fd_read, fd_pread, fd_seek, fd_sync, fd_readdir, fd_write,
fd_pwrite, poll_oneoff, path_create_directory, path_filestat_get,
path_filestat_set_times, path_link, path_open, path_readlink, path_remove_directory,
path_rename, path_symlink, path_unlink_file
path_rename, path_symlink, path_unlink_file, fd_tell,
}
},
errors: { errno => trappable Error },
Expand Down Expand Up @@ -1888,8 +1897,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiP1Ctx {
offset: types::Filedelta,
whence: types::Whence,
) -> Result<types::Filesize, types::Error> {
let t = self.transact()?;
let File { fd, position, .. } = t.get_seekable(fd)?;
let mut t = self.transact()?;
let File { fd, position, .. } = t.get_seekable(fd).await?;
let fd = fd.borrowed();
let position = position.clone();
drop(t);
Expand Down Expand Up @@ -1927,14 +1936,15 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiP1Ctx {
/// Return the current offset of a file descriptor.
/// NOTE: This is similar to `lseek(fd, 0, SEEK_CUR)` in POSIX.
#[instrument(skip(self, _memory))]
fn fd_tell(
async fn fd_tell(
&mut self,
_memory: &mut GuestMemory<'_>,
fd: types::Fd,
) -> Result<types::Filesize, types::Error> {
let pos = self
.transact()?
.get_seekable(fd)
.await
.map(|File { position, .. }| position.load(Ordering::Relaxed))?;
Ok(pos)
}
Expand Down
1 change: 1 addition & 0 deletions crates/wasi/src/p2/host/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ impl From<crate::filesystem::DescriptorType> for types::DescriptorType {
crate::filesystem::DescriptorType::Directory => Self::Directory,
crate::filesystem::DescriptorType::SymbolicLink => Self::SymbolicLink,
crate::filesystem::DescriptorType::RegularFile => Self::RegularFile,
crate::filesystem::DescriptorType::Fifo => Self::Fifo,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/wasi/src/p3/filesystem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ impl From<crate::filesystem::DescriptorType> for types::DescriptorType {
crate::filesystem::DescriptorType::Directory => Self::Directory,
crate::filesystem::DescriptorType::SymbolicLink => Self::SymbolicLink,
crate::filesystem::DescriptorType::RegularFile => Self::RegularFile,
crate::filesystem::DescriptorType::Fifo => Self::Fifo,
}
}
}
Expand All @@ -278,6 +279,8 @@ impl From<cap_std::fs::FileType> for types::DescriptorType {
Self::CharacterDevice
} else if ft.is_file() {
Self::RegularFile
} else if ft.is_fifo() {
Self::Fifo
} else {
Self::Other(None)
}
Expand Down
Loading