Skip to content

Implement preopened_dir using set_mapped_directories setting #427

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions ext/src/ruby_api/wasi_ctx_builder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use super::{root, WasiCtx};
use crate::error;
use crate::helpers::OutputLimitedBuffer;
use cap_std::fs::Dir;
use magnus::{
class, function, gc::Marker, method, typed_data::Obj, value::Opaque, DataTypeFunctions, Error,
Module, Object, RArray, RHash, RString, Ruby, TryConvert, TypedData,
};
use rb_sys::ruby_rarray_flags::RARRAY_EMBED_FLAG;
use std::cell::RefCell;
use std::path::Path;
use std::{fs::File, path::PathBuf};
use wasi_common::pipe::{ReadPipe, WritePipe};

Expand Down Expand Up @@ -47,6 +50,7 @@ struct WasiCtxBuilderInner {
stderr: Option<WriteStream>,
env: Option<Opaque<RHash>>,
args: Option<Opaque<RArray>>,
mapped_directories: Option<Opaque<RArray>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When holding references to Ruby objects, we must GC mark them, otherwise the GC might collect the object. See WasiCtxBuilderInner::mark on L56 for how it's done.

}

impl WasiCtxBuilderInner {
Expand All @@ -66,6 +70,9 @@ impl WasiCtxBuilderInner {
if let Some(v) = self.args.as_ref() {
marker.mark(*v);
}
if let Some(v) = self.mapped_directories.as_ref() {
marker.mark(*v);
}
}
}

Expand Down Expand Up @@ -222,6 +229,31 @@ impl WasiCtxBuilder {
rb_self
}

/// @yard
/// Set mapped directory for host path and guest path.
/// @param host_path [String]
/// @param guest_path [String]
/// @def set_mapped_directory(host_path, guest_path)
/// @return [WasiCtxBuilder] +self+
pub fn set_mapped_directory(
rb_self: RbSelf,
host_path: RString,
guest_path: RString,
) -> RbSelf {
let mut inner = rb_self.inner.borrow_mut();
if inner.mapped_directories.is_none() {
inner.mapped_directories = Some(RArray::new().into());
}
let mapped_directory = RArray::new();
mapped_directory.push(host_path).unwrap();
mapped_directory.push(guest_path).unwrap();

let ruby = Ruby::get().unwrap();
let mapped_directories = ruby.get_inner(inner.mapped_directories.unwrap());
mapped_directories.push(mapped_directory).unwrap();
rb_self
}

pub fn build(ruby: &Ruby, rb_self: RbSelf) -> Result<WasiCtx, Error> {
let mut builder = wasi_common::sync::WasiCtxBuilder::new();
let inner = rb_self.inner.borrow();
Expand Down Expand Up @@ -281,6 +313,25 @@ impl WasiCtxBuilder {
builder.envs(&env_vec).map_err(|e| error!("{}", e))?;
}

if let Some(mapped_directories) = inner.mapped_directories.as_ref() {
for item in unsafe { ruby.get_inner(*mapped_directories).as_slice() } {
let mapped_directory = RArray::try_convert(*item)?;
if mapped_directory.len() == 2 {
let host_path =
RString::try_convert(mapped_directory.entry(0)?)?.to_string()?;
let guest_path =
RString::try_convert(mapped_directory.entry(1)?)?.to_string()?;

let host_path_dir = Dir::from_std_file(File::open(host_path).unwrap());
let guest_path_path = PathBuf::from(guest_path.as_str());

builder
.preopened_dir(host_path_dir, guest_path_path)
.map_err(|e| error!("{}", e))?;
}
}
}

let ctx = WasiCtx::from_inner(builder.build());
Ok(ctx)
}
Expand Down Expand Up @@ -339,6 +390,11 @@ pub fn init() -> Result<(), Error> {

class.define_method("set_argv", method!(WasiCtxBuilder::set_argv, 1))?;

class.define_method(
"set_mapped_directory",
method!(WasiCtxBuilder::set_mapped_directory, 2),
)?;

class.define_method("build", method!(WasiCtxBuilder::build, 0))?;

Ok(())
Expand Down
Loading