Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VirtualFile::copy_from_owned_buffer, fixes running mounted comman… #5374

Merged
merged 2 commits into from
Feb 3, 2025
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
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions lib/virtual-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
extern crate pretty_assertions;

use futures::future::BoxFuture;
use shared_buffer::OwnedBuffer;
use std::any::Any;
use std::ffi::OsString;
use std::fmt;
Expand Down Expand Up @@ -398,6 +399,19 @@ pub trait VirtualFile:
})
}

/// This method will copy a file from a source to this destination where
/// the default is to do a straight byte copy however file system implementors
/// may optimize this to cheaply clone and store the OwnedBuffer directly
fn copy_from_owned_buffer(&mut self, src: &OwnedBuffer) -> BoxFuture<'_, std::io::Result<()>> {
let src = src.clone();
Box::pin(async move {
let mut bytes = src.as_slice();
let bytes_written = tokio::io::copy(&mut bytes, self).await?;
tracing::trace!(bytes_written, "Copying file into host filesystem");
Ok(())
})
}

/// Polls the file for when there is data to be read
fn poll_read_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>>;

Expand Down
32 changes: 32 additions & 0 deletions lib/virtual-fs/src/mem_fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,38 @@ impl VirtualFile for FileHandle {
})
}

fn copy_from_owned_buffer(&mut self, src: &OwnedBuffer) -> BoxFuture<'_, std::io::Result<()>> {
let inner = self.filesystem.inner.clone();
let src = src.clone();
Box::pin(async move {
let mut fs = inner.write().unwrap();
let inode = fs.storage.get_mut(self.inode);
match inode {
Some(inode) => {
let metadata = Metadata {
ft: crate::FileType {
file: true,
..Default::default()
},
accessed: 1,
created: 1,
modified: 1,
len: src.len() as u64,
};

*inode = Node::ReadOnlyFile(ReadOnlyFileNode {
inode: inode.inode(),
name: inode.name().to_string_lossy().to_string().into(),
file: ReadOnlyFile { buffer: src },
metadata,
});
Ok(())
}
None => Err(std::io::ErrorKind::InvalidInput.into()),
}
})
}

fn poll_read_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
if !self.readable {
return Poll::Ready(Err(io::Error::new(
Expand Down
2 changes: 2 additions & 0 deletions lib/virtual-fs/src/mem_fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ struct ArcFileNode {
metadata: Metadata,
}

// FIXME: this is broken!!! A `VirtualFile` stores its own offset,
// so a file stored this way can only be read once!
#[derive(Debug)]
struct CustomFileNode {
inode: Inode,
Expand Down
5 changes: 4 additions & 1 deletion lib/wasix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ impl SpawnError {
/// [`NotFound`]: SpawnError::NotFound
#[must_use]
pub fn is_not_found(&self) -> bool {
matches!(self, Self::NotFound { .. } | Self::MissingEntrypoint { .. })
matches!(
self,
Self::NotFound { .. } | Self::MissingEntrypoint { .. } | Self::BinaryNotFound { .. }
)
}
}

Expand Down
11 changes: 5 additions & 6 deletions lib/wasix/src/state/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

use futures::future::BoxFuture;
use rand::Rng;
use virtual_fs::{FileSystem, FsError, StaticFile, VirtualFile};
use virtual_fs::{FileSystem, FsError, VirtualFile};
use virtual_net::DynVirtualNetworking;
use wasmer::{
AsStoreMut, AsStoreRef, FunctionEnvMut, Global, Imports, Instance, Memory, MemoryType,
Expand Down Expand Up @@ -1125,18 +1125,17 @@ impl WasiEnv {
}
}
WasiFsRoot::Backing(fs) => {
// FIXME: we're counting on the fs being a mem_fs here. Otherwise, memory
// usage will be very high.
let mut f = fs.new_open_options().create(true).write(true).open(path)?;
if let Err(e) = f
.copy_reference(Box::new(StaticFile::new(atom.clone())))
.await
{
if let Err(e) = f.copy_from_owned_buffer(&atom).await {
tracing::warn!(
error = &e as &dyn std::error::Error,
"Unable to copy file reference",
);
}
let mut f = fs.new_open_options().create(true).write(true).open(path2)?;
if let Err(e) = f.copy_reference(Box::new(StaticFile::new(atom))).await {
if let Err(e) = f.copy_from_owned_buffer(&atom).await {
tracing::warn!(
error = &e as &dyn std::error::Error,
"Unable to copy file reference",
Expand Down
Loading