Skip to content

Commit

Permalink
Merge #1660
Browse files Browse the repository at this point in the history
1660: Allow mapdir aliases with starting `/` r=MarkMcCaskey a=MarkMcCaskey

Stripping leading `/` is valid because we mount mapped dirs at `/` regardless.

Resolves #1503 

This solution is a bit hacky, there may be a more elegant solution.  I'll look for one tomorrow morning when I'm more mentally fresh.

- [ ] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Mark McCaskey <mark@wasmer.io>
  • Loading branch information
bors[bot] and Mark McCaskey authored Oct 1, 2020
2 parents d924639 + 7e097b4 commit 49a65cc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/wasi/src/state/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,10 @@ impl PreopenDirBuilder {

/// Make this preopened directory appear to the WASI program as `alias`
pub fn alias(&mut self, alias: &str) -> &mut Self {
self.alias = Some(alias.to_string());
// We mount at preopened dirs at `/` by default and multiple `/` in a row
// are equal to a single `/`.
let alias = alias.trim_start_matches(b'/');
self.alias = Some(alias);

self
}
Expand Down
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/wasi-wast/wasi/snapshot1/mapdir_with_leading_slash.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
;; This file was generated by https://github.com/wasmerio/wasi-tests

(wasi_test "mapdir_with_leading_slash.wasm"
(map_dirs "/hamlet:test_fs/hamlet")
(assert_return (i64.const 0))
(assert_stdout "File exists? true\nSCENE III. A room in the castle.\n\n Enter KING CLAUDIUS, ROSENCRANTZ, and GUILDENSTERN \n\nKING CLAUDIUS\n\n I like him not, nor stands it safe with us\n To let his madness range. Therefore prepare you;\n I your commission will forthwith dispatch,\n \n")
)
23 changes: 23 additions & 0 deletions tests/wasi-wast/wasi/tests/mapdir_with_leading_slash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// WASI:
// mapdir: /hamlet:test_fs/hamlet

use std::fs;
use std::io::Read;
use std::path::PathBuf;

fn main() {
#[cfg(not(target_os = "wasi"))]
let mut base = PathBuf::from("test_fs/hamlet");
#[cfg(target_os = "wasi")]
let mut base = PathBuf::from("hamlet");

base.push("act3/scene3.txt");

println!("File exists? {}", base.exists());

let mut f = fs::File::open(&base).unwrap();
let mut s = String::new();
f.read_to_string(&mut s).unwrap();

println!("{}", s.chars().take(256).collect::<String>());
}
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/wasi-wast/wasi/unstable/mapdir_with_leading_slash.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
;; This file was generated by https://github.com/wasmerio/wasi-tests

(wasi_test "mapdir_with_leading_slash.wasm"
(map_dirs "/hamlet:test_fs/hamlet")
(assert_return (i64.const 0))
(assert_stdout "File exists? true\nSCENE III. A room in the castle.\n\n Enter KING CLAUDIUS, ROSENCRANTZ, and GUILDENSTERN \n\nKING CLAUDIUS\n\n I like him not, nor stands it safe with us\n To let his madness range. Therefore prepare you;\n I your commission will forthwith dispatch,\n \n")
)

0 comments on commit 49a65cc

Please sign in to comment.