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

Move back to the main nightly channel #723

Closed
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Don't use libc in wasi-tests
We don't know what version of the wasi api it's compiled against, so
let's avoid it.
  • Loading branch information
alexcrichton committed Dec 16, 2019
commit 226a8af09069fea3e9d716fd5a06b41f0cb4d5fe
36 changes: 20 additions & 16 deletions crates/test-programs/wasi-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
pub mod utils;
pub mod wasi_wrappers;

use libc;
use std::ffi::CString;
use std::io;
use wasi_old::wasi_unstable;

/// Opens a fresh file descriptor for `path` where `path` should be a preopened
/// directory. This is intended to be used with `wasi_unstable`, not with
/// `wasi_snapshot_preview1`. This is getting phased out and will likely be
/// deleted soon.
pub fn open_scratch_directory(path: &str) -> Result<wasi_unstable::Fd, String> {
// Open the scratch directory.
let dir_fd: wasi_unstable::Fd = unsafe {
let cstr = CString::new(path.as_bytes()).unwrap();
libc::open(cstr.as_ptr(), libc::O_RDONLY | libc::O_DIRECTORY)
} as wasi_unstable::Fd;
unsafe {
for i in 3.. {
let stat = match wasi_unstable::fd_prestat_get(i) {
Ok(s) => s,
Err(_) => break,
};
if stat.pr_type != wasi::PREOPENTYPE_DIR {
continue;
}
let mut dst = Vec::with_capacity(stat.u.dir.pr_name_len);
if wasi::fd_prestat_dir_name(i, dst.as_mut_ptr(), dst.capacity()).is_err() {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, is it OK to mix wasi_unstable and wasi for the syscalls here? I mean, first, we call wasi_unstable::fd_prestat_get just to later on call wasi::fd_prestat_dir_name. It might be OK, but definitely looks confusing!

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh oops this was a mistake! I'll switch this as well to match the above

continue;
}
dst.set_len(stat.u.dir.pr_name_len);
if dst == path.as_bytes() {
return Ok(wasi::path_open(i, 0, ".", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
Copy link
Member

Choose a reason for hiding this comment

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

Oh, and here as well, we call wasi::path_open instead of wasi_unstable::path_open.

.expect("failed to open dir"));
}
}

if (dir_fd as std::os::raw::c_int) < 0 {
Err(format!(
"error opening scratch directory '{}': {}",
path,
io::Error::last_os_error()
))
} else {
Ok(dir_fd)
Err(format!("failed to find scratch dir"))
}
}

Expand Down