Skip to content

Commit

Permalink
alias . to cwd in wasi env (#5244)
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej authored Nov 13, 2024
2 parents 62f16d7 + de638a1 commit c057723
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 12 deletions.
23 changes: 12 additions & 11 deletions lib/wasix/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ impl WasiRunner {
None
};

if self.wasi.is_home_mapped {
builder.set_current_dir(MAPPED_CURRENT_DIR_DEFAULT_PATH);
}

if let Some(current_dir) = &self.wasi.current_dir {
builder.set_current_dir(current_dir.clone());
}

if let Some(cwd) = &wasi.cwd {
builder.set_current_dir(cwd);
}

self.wasi
.prepare_webc_env(&mut builder, container_fs, wasi, root_fs)?;

Expand All @@ -275,13 +287,6 @@ impl WasiRunner {
builder.set_stderr(Box::new(stderr.clone()));
}

if self.wasi.is_home_mapped {
builder.set_current_dir(MAPPED_CURRENT_DIR_DEFAULT_PATH);
}
if let Some(current_dir) = &self.wasi.current_dir {
builder.set_current_dir(current_dir.clone());
}

Ok(builder)
}

Expand Down Expand Up @@ -377,10 +382,6 @@ impl crate::runners::Runner for WasiRunner {
}
}

if let Some(cwd) = &wasi.cwd {
env.set_current_dir(cwd);
}

let env = env.build()?;
let store = runtime.new_store();

Expand Down
3 changes: 2 additions & 1 deletion lib/wasix/src/runners/wasi_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ impl CommonWasiOptions {

if self.mounts.iter().all(|m| m.guest != ".") {
// The user hasn't mounted "." to anything, so let's map it to "/"
builder.add_map_dir(".", "/")?;
let path = builder.get_current_dir().unwrap_or(PathBuf::from("/"));
builder.add_map_dir(".", path)?;
}

builder.set_fs(Box::new(fs));
Expand Down
4 changes: 4 additions & 0 deletions lib/wasix/src/state/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,10 @@ impl WasiEnvBuilder {
self.journals.push(journal);
}

pub fn get_current_dir(&mut self) -> Option<PathBuf> {
self.current_dir.clone()
}

pub fn set_current_dir(&mut self, dir: impl Into<PathBuf>) {
self.current_dir = Some(dir.into());
}
Expand Down
34 changes: 34 additions & 0 deletions tests/wasix/create-dir-at-cwd-with-chdir/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

// the difference between this test and the one in create-dir-at-cwd is the
// presence of chdir.

// this will force chdir to be linked with this binary which in turn will change
// the behavior of rel_path logic the libc.
//
// for more info see: libc-find-relpath.h in wasix-libc
int (*dummy_chdir_ref)(const char *path) = chdir;

int main() {
int status = EXIT_FAILURE;

const char *dirName1 = "test1";
if (mkdir(dirName1, 0755) != 0) {
goto end;
}

const char *dirName2 = "./test2";
if (mkdir(dirName2, 0755) != 0) {
goto end;
}

status = EXIT_SUCCESS;

end:
printf("%d", status);
return 0;
}
5 changes: 5 additions & 0 deletions tests/wasix/create-dir-at-cwd-with-chdir/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

$WASMER -q run main.wasm --dir . > output

rmdir test1 test2 2>/dev/null && printf "0" | diff -u output - 1>/dev/null
24 changes: 24 additions & 0 deletions tests/wasix/create-dir-at-cwd/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

int main() {
int status = EXIT_FAILURE;

const char *dirName1 = "test1";
if (mkdir(dirName1, 0755) != 0) {
goto end;
}

const char *dirName2 = "./test2";
if (mkdir(dirName2, 0755) != 0) {
goto end;
}

status = EXIT_SUCCESS;

end:
printf("%d", status);
return 0;
}
5 changes: 5 additions & 0 deletions tests/wasix/create-dir-at-cwd/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

$WASMER -q run main.wasm --dir . > output

rmdir test1 test2 2>/dev/null && printf "0" | diff -u output - 1>/dev/null

0 comments on commit c057723

Please sign in to comment.