Skip to content

Commit

Permalink
Merge pull request #138 from RReverser/wasi
Browse files Browse the repository at this point in the history
Add [limited] support for WASI
  • Loading branch information
Stebalien authored Feb 2, 2021
2 parents a7d19db + 55ad1e2 commit 0fc7e36
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ rust:
os:
- linux
- osx
env:
- CARGO_FLAGS=
script:
- cargo test --verbose $CARGO_FLAGS
jobs:
include:
- rust: 1.40.0
os: linux
env: CARGO_FLAGS=--target wasm32-wasi
- rust: nightly
os: linux
env: CARGO_FLAGS=--target wasm32-wasi --features nightly
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cfg-if = "1"
rand = "0.8"
remove_dir_all = "0.5"

[target.'cfg(unix)'.dependencies]
[target.'cfg(any(unix, target_os = "wasi"))'.dependencies]
libc = "0.2.27"

[target.'cfg(windows)'.dependencies.winapi]
Expand All @@ -34,3 +34,6 @@ features = [

[target.'cfg(target_os = "redox")'.dependencies]
redox_syscall = "0.2"

[features]
nightly = []
2 changes: 1 addition & 1 deletion src/file/imp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cfg_if! {
if #[cfg(any(unix, target_os = "redox"))] {
if #[cfg(any(unix, target_os = "redox", target_os = "wasi"))] {
mod unix;
pub use self::unix::*;
} else if #[cfg(windows)] {
Expand Down
37 changes: 28 additions & 9 deletions src/file/imp/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ use std::env;
use std::ffi::{CString, OsStr};
use std::fs::{self, File, OpenOptions};
use std::io;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::{MetadataExt, OpenOptionsExt};
use std::path::Path;
cfg_if! {
if #[cfg(not(target_os = "wasi"))] {
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::{MetadataExt, OpenOptionsExt};
} else {
use std::os::wasi::ffi::OsStrExt;
#[cfg(feature = "nightly")]
use std::os::wasi::fs::MetadataExt;
}
}
use crate::util;
use std::path::Path;

#[cfg(not(target_os = "redox"))]
use libc::{c_char, c_int, link, rename, unlink};
Expand Down Expand Up @@ -33,12 +41,14 @@ pub fn cstr(path: &Path) -> io::Result<CString> {
}

pub fn create_named(path: &Path, open_options: &mut OpenOptions) -> io::Result<File> {
open_options
.read(true)
.write(true)
.create_new(true)
.mode(0o600)
.open(path)
open_options.read(true).write(true).create_new(true);

#[cfg(not(target_os = "wasi"))]
{
open_options.mode(0o600);
}

open_options.open(path)
}

fn create_unlinked(path: &Path) -> io::Result<File> {
Expand Down Expand Up @@ -90,6 +100,7 @@ fn create_unix(dir: &Path) -> io::Result<File> {
)
}

#[cfg(any(not(target_os = "wasi"), feature = "nightly"))]
pub fn reopen(file: &File, path: &Path) -> io::Result<File> {
let new_file = OpenOptions::new().read(true).write(true).open(path)?;
let old_meta = file.metadata()?;
Expand All @@ -103,6 +114,14 @@ pub fn reopen(file: &File, path: &Path) -> io::Result<File> {
Ok(new_file)
}

#[cfg(all(target_os = "wasi", not(feature = "nightly")))]
pub fn reopen(file: &File, path: &Path) -> io::Result<File> {
return Err(io::Error::new(
io::ErrorKind::Other,
"this operation is supported on WASI only on nightly Rust (with `nightly` feature enabled)",
));
}

#[cfg(not(target_os = "redox"))]
pub fn persist(old_path: &Path, new_path: &Path, overwrite: bool) -> io::Result<()> {
unsafe {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
#![cfg_attr(test, deny(warnings))]
#![deny(rust_2018_idioms)]
#![allow(clippy::redundant_field_names)]
#![cfg_attr(feature = "nightly", feature(wasi_ext))]

#[macro_use]
extern crate cfg_if;
Expand Down

0 comments on commit 0fc7e36

Please sign in to comment.