Skip to content

Commit 745321e

Browse files
authoredMar 3, 2023
slight now pre-opens the . dir to allow filesystem access (#343)
* slight now pre-opens . to allow filesystem ops Signed-off-by: danbugs <danilochiarlone@gmail.com> * .gitignored test.txt file Signed-off-by: danbugs <danilochiarlone@gmail.com> --------- Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent dd1bf66 commit 745321e

File tree

8 files changed

+77
-3
lines changed

8 files changed

+77
-3
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ target/
33
bindings.rs
44
.idea
55
.DS_Store
6-
**.tar.gz
6+
**.tar.gz
7+
tests/test.txt

‎crates/runtime/src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use async_trait::async_trait;
88
use ctx::SlightCtxBuilder;
99
use resource::{get_host_state, HttpData};
1010
use slight_common::{CapabilityBuilder, WasmtimeBuildable, WasmtimeLinkable};
11-
use wasi_cap_std_sync::WasiCtxBuilder;
11+
use wasi_cap_std_sync::{ambient_authority, Dir, WasiCtxBuilder};
1212
use wasi_common::WasiCtx;
1313
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
1414

@@ -132,7 +132,10 @@ pub fn default_config() -> Result<Config> {
132132

133133
// TODO (Joe): expose the wasmtime wasi context as a capability?
134134
pub fn default_wasi() -> Result<WasiCtx> {
135-
let ctx: WasiCtxBuilder = WasiCtxBuilder::new().inherit_stdio().inherit_args()?;
135+
let ctx: WasiCtxBuilder = WasiCtxBuilder::new()
136+
.preopened_dir(Dir::open_ambient_dir(".", ambient_authority())?, ".")?
137+
.inherit_stdio()
138+
.inherit_args()?;
136139
Ok(ctx.build())
137140
}
138141

‎tests/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ const WIT_DIRECTORY: &str = "wit/*";
77
const KEYVALUE_TEST_PATH: &str = "./keyvalue-test";
88
const HTTP_TEST_PATH: &str = "./http-test";
99
const CONFIGS_TEST_PATH: &str = "./configs-test";
10+
const FILESYSTEM_ACCESS_TEST_PATH: &str = "./filesystem-access-test";
1011

1112
fn main() {
1213
println!("cargo:rerun-if-changed=build.rs");
1314
println!("cargo:rerun-if-changed={WIT_DIRECTORY}");
1415
println!("cargo:rerun-if-changed={KEYVALUE_TEST_PATH}/src/main.rs");
1516
println!("cargo:rerun-if-changed={HTTP_TEST_PATH}/src/main.rs");
1617
println!("cargo:rerun-if-changed={CONFIGS_TEST_PATH}/src/main.rs");
18+
println!("cargo:rerun-if-changed={FILESYSTEM_ACCESS_TEST_PATH}/src/main.rs");
1719

1820
// Check if wasm32-wasi target is installed
1921

@@ -31,6 +33,7 @@ fn main() {
3133
cargo_wasi_build(KEYVALUE_TEST_PATH);
3234
cargo_wasi_build(HTTP_TEST_PATH);
3335
cargo_wasi_build(CONFIGS_TEST_PATH);
36+
cargo_wasi_build(FILESYSTEM_ACCESS_TEST_PATH);
3437
}
3538
}
3639

‎tests/filesystem-access-test/Cargo.lock

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "filesystem-access-test"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = [ "DeisLabs Engineering Team" ]
6+
7+
[[bin]]
8+
name = "filesystem-access-test"
9+
test = false
10+
11+
[dependencies]
12+
anyhow = "1"
13+
14+
[workspace]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
specversion = "0.2"
2+
3+
[[capability]]
4+
resource = "keyvalue.filesystem"
5+
name = "rand"
6+
# This capability does not require any configs
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::fs::OpenOptions;
2+
use std::io::Write;
3+
4+
fn main() -> anyhow::Result<()> {
5+
// Open the file in append mode
6+
let mut file = OpenOptions::new()
7+
.write(true)
8+
.append(true)
9+
.create(true)
10+
.open("./test.txt")?;
11+
12+
// Write the string to the file
13+
writeln!(file, "\nHello, World!")?;
14+
15+
Ok(())
16+
}

‎tests/src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ mod integration_tests {
3636
use crate::{run, slight_path};
3737
use anyhow::Result;
3838

39+
#[test]
40+
fn filesystem_access_test() -> Result<()> {
41+
let out_dir = PathBuf::from(format!("{}/target/wasms", env!("CARGO_MANIFEST_DIR")));
42+
let out_dir = out_dir.join("wasm32-wasi/debug/filesystem-access-test.wasm");
43+
let file_config = &format!(
44+
"{}/filesystem-access-test/slightfile.toml",
45+
env!("CARGO_MANIFEST_DIR")
46+
);
47+
run(
48+
&slight_path(),
49+
vec!["-c", file_config, "run", out_dir.to_str().unwrap()],
50+
);
51+
Ok(())
52+
}
53+
3954
#[test]
4055
fn envvars_test() -> Result<()> {
4156
let out_dir = PathBuf::from(format!("{}/target/wasms", env!("CARGO_MANIFEST_DIR")));

0 commit comments

Comments
 (0)
Please sign in to comment.