Path: blob/main/crates/wasi/src/p2/host/filesystem/sync.rs
3068 views
use crate::filesystem::WasiFilesystemCtxView;1use crate::p2::bindings::filesystem::types as async_filesystem;2use crate::p2::bindings::sync::filesystem::types as sync_filesystem;3use crate::p2::bindings::sync::io::streams;4use crate::p2::{FsError, FsResult};5use crate::runtime::in_tokio;6use wasmtime::component::Resource;78impl sync_filesystem::Host for WasiFilesystemCtxView<'_> {9fn convert_error_code(&mut self, err: FsError) -> wasmtime::Result<sync_filesystem::ErrorCode> {10Ok(async_filesystem::Host::convert_error_code(self, err)?.into())11}1213fn filesystem_error_code(14&mut self,15err: Resource<streams::Error>,16) -> wasmtime::Result<Option<sync_filesystem::ErrorCode>> {17Ok(async_filesystem::Host::filesystem_error_code(self, err)?.map(|e| e.into()))18}19}2021impl sync_filesystem::HostDescriptor for WasiFilesystemCtxView<'_> {22fn advise(23&mut self,24fd: Resource<sync_filesystem::Descriptor>,25offset: sync_filesystem::Filesize,26len: sync_filesystem::Filesize,27advice: sync_filesystem::Advice,28) -> FsResult<()> {29in_tokio(async {30async_filesystem::HostDescriptor::advise(self, fd, offset, len, advice.into()).await31})32}3334fn sync_data(&mut self, fd: Resource<sync_filesystem::Descriptor>) -> FsResult<()> {35in_tokio(async { async_filesystem::HostDescriptor::sync_data(self, fd).await })36}3738fn get_flags(39&mut self,40fd: Resource<sync_filesystem::Descriptor>,41) -> FsResult<sync_filesystem::DescriptorFlags> {42Ok(in_tokio(async { async_filesystem::HostDescriptor::get_flags(self, fd).await })?.into())43}4445fn get_type(46&mut self,47fd: Resource<sync_filesystem::Descriptor>,48) -> FsResult<sync_filesystem::DescriptorType> {49Ok(in_tokio(async { async_filesystem::HostDescriptor::get_type(self, fd).await })?.into())50}5152fn set_size(53&mut self,54fd: Resource<sync_filesystem::Descriptor>,55size: sync_filesystem::Filesize,56) -> FsResult<()> {57in_tokio(async { async_filesystem::HostDescriptor::set_size(self, fd, size).await })58}5960fn set_times(61&mut self,62fd: Resource<sync_filesystem::Descriptor>,63atim: sync_filesystem::NewTimestamp,64mtim: sync_filesystem::NewTimestamp,65) -> FsResult<()> {66in_tokio(async {67async_filesystem::HostDescriptor::set_times(self, fd, atim.into(), mtim.into()).await68})69}7071fn read(72&mut self,73fd: Resource<sync_filesystem::Descriptor>,74len: sync_filesystem::Filesize,75offset: sync_filesystem::Filesize,76) -> FsResult<(Vec<u8>, bool)> {77in_tokio(async { async_filesystem::HostDescriptor::read(self, fd, len, offset).await })78}7980fn write(81&mut self,82fd: Resource<sync_filesystem::Descriptor>,83buf: Vec<u8>,84offset: sync_filesystem::Filesize,85) -> FsResult<sync_filesystem::Filesize> {86in_tokio(async { async_filesystem::HostDescriptor::write(self, fd, buf, offset).await })87}8889fn read_directory(90&mut self,91fd: Resource<sync_filesystem::Descriptor>,92) -> FsResult<Resource<sync_filesystem::DirectoryEntryStream>> {93in_tokio(async { async_filesystem::HostDescriptor::read_directory(self, fd).await })94}9596fn sync(&mut self, fd: Resource<sync_filesystem::Descriptor>) -> FsResult<()> {97in_tokio(async { async_filesystem::HostDescriptor::sync(self, fd).await })98}99100fn create_directory_at(101&mut self,102fd: Resource<sync_filesystem::Descriptor>,103path: String,104) -> FsResult<()> {105in_tokio(async {106async_filesystem::HostDescriptor::create_directory_at(self, fd, path).await107})108}109110fn stat(111&mut self,112fd: Resource<sync_filesystem::Descriptor>,113) -> FsResult<sync_filesystem::DescriptorStat> {114Ok(in_tokio(async { async_filesystem::HostDescriptor::stat(self, fd).await })?.into())115}116117fn stat_at(118&mut self,119fd: Resource<sync_filesystem::Descriptor>,120path_flags: sync_filesystem::PathFlags,121path: String,122) -> FsResult<sync_filesystem::DescriptorStat> {123Ok(in_tokio(async {124async_filesystem::HostDescriptor::stat_at(self, fd, path_flags.into(), path).await125})?126.into())127}128129fn set_times_at(130&mut self,131fd: Resource<sync_filesystem::Descriptor>,132path_flags: sync_filesystem::PathFlags,133path: String,134atim: sync_filesystem::NewTimestamp,135mtim: sync_filesystem::NewTimestamp,136) -> FsResult<()> {137in_tokio(async {138async_filesystem::HostDescriptor::set_times_at(139self,140fd,141path_flags.into(),142path,143atim.into(),144mtim.into(),145)146.await147})148}149150fn link_at(151&mut self,152fd: Resource<sync_filesystem::Descriptor>,153// TODO delete the path flags from this function154old_path_flags: sync_filesystem::PathFlags,155old_path: String,156new_descriptor: Resource<sync_filesystem::Descriptor>,157new_path: String,158) -> FsResult<()> {159in_tokio(async {160async_filesystem::HostDescriptor::link_at(161self,162fd,163old_path_flags.into(),164old_path,165new_descriptor,166new_path,167)168.await169})170}171172fn open_at(173&mut self,174fd: Resource<sync_filesystem::Descriptor>,175path_flags: sync_filesystem::PathFlags,176path: String,177oflags: sync_filesystem::OpenFlags,178flags: sync_filesystem::DescriptorFlags,179) -> FsResult<Resource<sync_filesystem::Descriptor>> {180in_tokio(async {181async_filesystem::HostDescriptor::open_at(182self,183fd,184path_flags.into(),185path,186oflags.into(),187flags.into(),188)189.await190})191}192193fn drop(&mut self, fd: Resource<sync_filesystem::Descriptor>) -> wasmtime::Result<()> {194async_filesystem::HostDescriptor::drop(self, fd)195}196197fn readlink_at(198&mut self,199fd: Resource<sync_filesystem::Descriptor>,200path: String,201) -> FsResult<String> {202in_tokio(async { async_filesystem::HostDescriptor::readlink_at(self, fd, path).await })203}204205fn remove_directory_at(206&mut self,207fd: Resource<sync_filesystem::Descriptor>,208path: String,209) -> FsResult<()> {210in_tokio(async {211async_filesystem::HostDescriptor::remove_directory_at(self, fd, path).await212})213}214215fn rename_at(216&mut self,217fd: Resource<sync_filesystem::Descriptor>,218old_path: String,219new_fd: Resource<sync_filesystem::Descriptor>,220new_path: String,221) -> FsResult<()> {222in_tokio(async {223async_filesystem::HostDescriptor::rename_at(self, fd, old_path, new_fd, new_path).await224})225}226227fn symlink_at(228&mut self,229fd: Resource<sync_filesystem::Descriptor>,230src_path: String,231dest_path: String,232) -> FsResult<()> {233in_tokio(async {234async_filesystem::HostDescriptor::symlink_at(self, fd, src_path, dest_path).await235})236}237238fn unlink_file_at(239&mut self,240fd: Resource<sync_filesystem::Descriptor>,241path: String,242) -> FsResult<()> {243in_tokio(async { async_filesystem::HostDescriptor::unlink_file_at(self, fd, path).await })244}245246fn read_via_stream(247&mut self,248fd: Resource<sync_filesystem::Descriptor>,249offset: sync_filesystem::Filesize,250) -> FsResult<Resource<streams::InputStream>> {251Ok(async_filesystem::HostDescriptor::read_via_stream(252self, fd, offset,253)?)254}255256fn write_via_stream(257&mut self,258fd: Resource<sync_filesystem::Descriptor>,259offset: sync_filesystem::Filesize,260) -> FsResult<Resource<streams::OutputStream>> {261Ok(async_filesystem::HostDescriptor::write_via_stream(262self, fd, offset,263)?)264}265266fn append_via_stream(267&mut self,268fd: Resource<sync_filesystem::Descriptor>,269) -> FsResult<Resource<streams::OutputStream>> {270Ok(async_filesystem::HostDescriptor::append_via_stream(271self, fd,272)?)273}274275fn is_same_object(276&mut self,277a: Resource<sync_filesystem::Descriptor>,278b: Resource<sync_filesystem::Descriptor>,279) -> wasmtime::Result<bool> {280in_tokio(async { async_filesystem::HostDescriptor::is_same_object(self, a, b).await })281}282fn metadata_hash(283&mut self,284fd: Resource<sync_filesystem::Descriptor>,285) -> FsResult<sync_filesystem::MetadataHashValue> {286Ok(287in_tokio(async { async_filesystem::HostDescriptor::metadata_hash(self, fd).await })?288.into(),289)290}291fn metadata_hash_at(292&mut self,293fd: Resource<sync_filesystem::Descriptor>,294path_flags: sync_filesystem::PathFlags,295path: String,296) -> FsResult<sync_filesystem::MetadataHashValue> {297Ok(in_tokio(async {298async_filesystem::HostDescriptor::metadata_hash_at(self, fd, path_flags.into(), path)299.await300})?301.into())302}303}304305impl sync_filesystem::HostDirectoryEntryStream for WasiFilesystemCtxView<'_> {306fn read_directory_entry(307&mut self,308stream: Resource<sync_filesystem::DirectoryEntryStream>,309) -> FsResult<Option<sync_filesystem::DirectoryEntry>> {310Ok(in_tokio(async {311async_filesystem::HostDirectoryEntryStream::read_directory_entry(self, stream).await312})?313.map(|e| e.into()))314}315316fn drop(317&mut self,318stream: Resource<sync_filesystem::DirectoryEntryStream>,319) -> wasmtime::Result<()> {320async_filesystem::HostDirectoryEntryStream::drop(self, stream)321}322}323324impl From<async_filesystem::ErrorCode> for sync_filesystem::ErrorCode {325fn from(other: async_filesystem::ErrorCode) -> Self {326use async_filesystem::ErrorCode;327match other {328ErrorCode::Access => Self::Access,329ErrorCode::WouldBlock => Self::WouldBlock,330ErrorCode::Already => Self::Already,331ErrorCode::BadDescriptor => Self::BadDescriptor,332ErrorCode::Busy => Self::Busy,333ErrorCode::Deadlock => Self::Deadlock,334ErrorCode::Quota => Self::Quota,335ErrorCode::Exist => Self::Exist,336ErrorCode::FileTooLarge => Self::FileTooLarge,337ErrorCode::IllegalByteSequence => Self::IllegalByteSequence,338ErrorCode::InProgress => Self::InProgress,339ErrorCode::Interrupted => Self::Interrupted,340ErrorCode::Invalid => Self::Invalid,341ErrorCode::Io => Self::Io,342ErrorCode::IsDirectory => Self::IsDirectory,343ErrorCode::Loop => Self::Loop,344ErrorCode::TooManyLinks => Self::TooManyLinks,345ErrorCode::MessageSize => Self::MessageSize,346ErrorCode::NameTooLong => Self::NameTooLong,347ErrorCode::NoDevice => Self::NoDevice,348ErrorCode::NoEntry => Self::NoEntry,349ErrorCode::NoLock => Self::NoLock,350ErrorCode::InsufficientMemory => Self::InsufficientMemory,351ErrorCode::InsufficientSpace => Self::InsufficientSpace,352ErrorCode::NotDirectory => Self::NotDirectory,353ErrorCode::NotEmpty => Self::NotEmpty,354ErrorCode::NotRecoverable => Self::NotRecoverable,355ErrorCode::Unsupported => Self::Unsupported,356ErrorCode::NoTty => Self::NoTty,357ErrorCode::NoSuchDevice => Self::NoSuchDevice,358ErrorCode::Overflow => Self::Overflow,359ErrorCode::NotPermitted => Self::NotPermitted,360ErrorCode::Pipe => Self::Pipe,361ErrorCode::ReadOnly => Self::ReadOnly,362ErrorCode::InvalidSeek => Self::InvalidSeek,363ErrorCode::TextFileBusy => Self::TextFileBusy,364ErrorCode::CrossDevice => Self::CrossDevice,365}366}367}368369impl From<sync_filesystem::Advice> for async_filesystem::Advice {370fn from(other: sync_filesystem::Advice) -> Self {371use sync_filesystem::Advice;372match other {373Advice::Normal => Self::Normal,374Advice::Sequential => Self::Sequential,375Advice::Random => Self::Random,376Advice::WillNeed => Self::WillNeed,377Advice::DontNeed => Self::DontNeed,378Advice::NoReuse => Self::NoReuse,379}380}381}382383impl From<async_filesystem::DescriptorFlags> for sync_filesystem::DescriptorFlags {384fn from(other: async_filesystem::DescriptorFlags) -> Self {385let mut out = Self::empty();386if other.contains(async_filesystem::DescriptorFlags::READ) {387out |= Self::READ;388}389if other.contains(async_filesystem::DescriptorFlags::WRITE) {390out |= Self::WRITE;391}392if other.contains(async_filesystem::DescriptorFlags::FILE_INTEGRITY_SYNC) {393out |= Self::FILE_INTEGRITY_SYNC;394}395if other.contains(async_filesystem::DescriptorFlags::DATA_INTEGRITY_SYNC) {396out |= Self::DATA_INTEGRITY_SYNC;397}398if other.contains(async_filesystem::DescriptorFlags::REQUESTED_WRITE_SYNC) {399out |= Self::REQUESTED_WRITE_SYNC;400}401if other.contains(async_filesystem::DescriptorFlags::MUTATE_DIRECTORY) {402out |= Self::MUTATE_DIRECTORY;403}404out405}406}407408impl From<async_filesystem::DescriptorType> for sync_filesystem::DescriptorType {409fn from(other: async_filesystem::DescriptorType) -> Self {410use async_filesystem::DescriptorType;411match other {412DescriptorType::RegularFile => Self::RegularFile,413DescriptorType::Directory => Self::Directory,414DescriptorType::BlockDevice => Self::BlockDevice,415DescriptorType::CharacterDevice => Self::CharacterDevice,416DescriptorType::Fifo => Self::Fifo,417DescriptorType::Socket => Self::Socket,418DescriptorType::SymbolicLink => Self::SymbolicLink,419DescriptorType::Unknown => Self::Unknown,420}421}422}423424impl From<async_filesystem::DirectoryEntry> for sync_filesystem::DirectoryEntry {425fn from(other: async_filesystem::DirectoryEntry) -> Self {426Self {427type_: other.type_.into(),428name: other.name,429}430}431}432433impl From<async_filesystem::DescriptorStat> for sync_filesystem::DescriptorStat {434fn from(other: async_filesystem::DescriptorStat) -> Self {435Self {436type_: other.type_.into(),437link_count: other.link_count,438size: other.size,439data_access_timestamp: other.data_access_timestamp,440data_modification_timestamp: other.data_modification_timestamp,441status_change_timestamp: other.status_change_timestamp,442}443}444}445446impl From<sync_filesystem::PathFlags> for async_filesystem::PathFlags {447fn from(other: sync_filesystem::PathFlags) -> Self {448let mut out = Self::empty();449if other.contains(sync_filesystem::PathFlags::SYMLINK_FOLLOW) {450out |= Self::SYMLINK_FOLLOW;451}452out453}454}455456impl From<sync_filesystem::NewTimestamp> for async_filesystem::NewTimestamp {457fn from(other: sync_filesystem::NewTimestamp) -> Self {458use sync_filesystem::NewTimestamp;459match other {460NewTimestamp::NoChange => Self::NoChange,461NewTimestamp::Now => Self::Now,462NewTimestamp::Timestamp(datetime) => Self::Timestamp(datetime),463}464}465}466467impl From<sync_filesystem::OpenFlags> for async_filesystem::OpenFlags {468fn from(other: sync_filesystem::OpenFlags) -> Self {469let mut out = Self::empty();470if other.contains(sync_filesystem::OpenFlags::CREATE) {471out |= Self::CREATE;472}473if other.contains(sync_filesystem::OpenFlags::DIRECTORY) {474out |= Self::DIRECTORY;475}476if other.contains(sync_filesystem::OpenFlags::EXCLUSIVE) {477out |= Self::EXCLUSIVE;478}479if other.contains(sync_filesystem::OpenFlags::TRUNCATE) {480out |= Self::TRUNCATE;481}482out483}484}485impl From<sync_filesystem::DescriptorFlags> for async_filesystem::DescriptorFlags {486fn from(other: sync_filesystem::DescriptorFlags) -> Self {487let mut out = Self::empty();488if other.contains(sync_filesystem::DescriptorFlags::READ) {489out |= Self::READ;490}491if other.contains(sync_filesystem::DescriptorFlags::WRITE) {492out |= Self::WRITE;493}494if other.contains(sync_filesystem::DescriptorFlags::FILE_INTEGRITY_SYNC) {495out |= Self::FILE_INTEGRITY_SYNC;496}497if other.contains(sync_filesystem::DescriptorFlags::DATA_INTEGRITY_SYNC) {498out |= Self::DATA_INTEGRITY_SYNC;499}500if other.contains(sync_filesystem::DescriptorFlags::REQUESTED_WRITE_SYNC) {501out |= Self::REQUESTED_WRITE_SYNC;502}503if other.contains(sync_filesystem::DescriptorFlags::MUTATE_DIRECTORY) {504out |= Self::MUTATE_DIRECTORY;505}506out507}508}509impl From<async_filesystem::MetadataHashValue> for sync_filesystem::MetadataHashValue {510fn from(other: async_filesystem::MetadataHashValue) -> Self {511Self {512lower: other.lower,513upper: other.upper,514}515}516}517518519