Skip to content

Fix cap-std compilation on WASI. #210

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

Merged
merged 3 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ jobs:
riscv64gc-unknown-linux-gnu
arm-unknown-linux-gnueabihf
aarch64-linux-android
wasm32-wasi
- run: cargo check --workspace --all-targets --all-features --release -vv
- run: cargo check --workspace --all-targets --all-features --release -vv --target=x86_64-unknown-linux-musl
- run: cargo check --workspace --all-targets --all-features --release -vv --target=x86_64-unknown-linux-gnux32
Expand All @@ -163,6 +164,7 @@ jobs:
- run: cargo check --workspace --all-targets --all-features --release -vv --target=riscv64gc-unknown-linux-gnu
- run: cargo check --workspace --all-targets --all-features --release -vv --target=arm-unknown-linux-gnueabihf
- run: cargo check --workspace --all-targets --all-features --release -vv --target=aarch64-linux-android
- run: cd cap-std && cargo check --features=fs_utf8 --release -vv

check_cross_nightly_windows:
name: Check Cross-Compilation on Rust nightly on Windows
Expand Down
3 changes: 3 additions & 0 deletions cap-primitives/src/fs/dir_options.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(not(target_os = "wasi"))]
use crate::fs::DirOptionsExt;

/// Options and flags which can be used to configure how a directory is
Expand All @@ -6,6 +7,7 @@ use crate::fs::DirOptionsExt;
/// This is to `create_dir` what to `OpenOptions` is to `open`.
#[derive(Debug, Clone)]
pub struct DirOptions {
#[cfg(not(target_os = "wasi"))]
#[allow(dead_code)]
pub(crate) ext: DirOptionsExt,
}
Expand All @@ -16,6 +18,7 @@ impl DirOptions {
#[inline]
pub const fn new() -> Self {
Self {
#[cfg(not(target_os = "wasi"))]
ext: DirOptionsExt::new(),
}
}
Expand Down
38 changes: 38 additions & 0 deletions cap-primitives/src/fs/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,44 @@ impl std::os::unix::fs::MetadataExt for Metadata {
}
}

#[cfg(target_os = "wasi")]
impl std::os::wasi::fs::MetadataExt for Metadata {
#[inline]
fn dev(&self) -> u64 {
self.ext.dev()
}

#[inline]
fn ino(&self) -> u64 {
self.ext.ino()
}

#[inline]
fn nlink(&self) -> u64 {
self.ext.nlink()
}

#[inline]
fn size(&self) -> u64 {
self.ext.size()
}

#[inline]
fn atim(&self) -> u64 {
self.ext.atim()
}

#[inline]
fn mtim(&self) -> u64 {
self.ext.mtim()
}

#[inline]
fn ctim(&self) -> u64 {
self.ext.ctim()
}
}

#[cfg(target_os = "vxworks")]
impl std::os::vxworks::fs::MetadataExt for Metadata {
#[inline]
Expand Down
2 changes: 2 additions & 0 deletions cap-primitives/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod remove_file;
mod remove_open_dir;
mod rename;
mod reopen;
#[cfg(not(target_os = "wasi"))]
mod set_permissions;
mod set_times;
mod stat;
Expand Down Expand Up @@ -85,6 +86,7 @@ pub use remove_file::remove_file;
pub use remove_open_dir::{remove_open_dir, remove_open_dir_all};
pub use rename::rename;
pub use reopen::reopen;
#[cfg(not(target_os = "wasi"))]
pub use set_permissions::set_permissions;
pub use set_times::{set_times, set_times_nofollow};
pub use stat::stat;
Expand Down
35 changes: 35 additions & 0 deletions cap-primitives/src/fs/open_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,41 @@ impl std::os::unix::fs::OpenOptionsExt for OpenOptions {
}
}

#[cfg(target_os = "wasi")]
impl std::os::wasi::fs::OpenOptionsExt for OpenOptions {
fn lookup_flags(&mut self, _: u32) -> &mut Self {
todo!()
}
fn directory(&mut self, dir_required: bool) -> &mut Self {
self.dir_required = dir_required;
self
}
fn dsync(&mut self, _: bool) -> &mut Self {
todo!()
}
fn nonblock(&mut self, _: bool) -> &mut Self {
todo!()
}
fn rsync(&mut self, _: bool) -> &mut Self {
todo!()
}
fn sync(&mut self, _: bool) -> &mut Self {
todo!()
}
fn fs_rights_base(&mut self, _: u64) -> &mut Self {
todo!()
}
fn fs_rights_inheriting(&mut self, _: u64) -> &mut Self {
todo!()
}
fn open_at<P>(&self, dirfd: &std::fs::File, path: P) -> Result<std::fs::File, std::io::Error>
where
P: AsRef<std::path::Path>,
{
crate::fs::open(dirfd, path.as_ref(), self)
}
}

#[cfg(target_os = "vxworks")]
impl std::os::vxworks::fs::OpenOptionsExt for OpenOptions {
#[inline]
Expand Down
11 changes: 10 additions & 1 deletion cap-primitives/src/fs/permissions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(any(unix, target_os = "vxworks"))]
#[cfg(not(windows))]
use crate::fs::PermissionsExt;
#[cfg(unix)]
use rustix::fs::RawMode;
Expand Down Expand Up @@ -52,6 +52,15 @@ impl Permissions {
Ok(fs::Permissions::from_mode(self.ext.mode()))
}

#[cfg(target_os = "wasi")]
#[inline]
#[allow(clippy::unnecessary_wraps)]
fn _into_std(self, file: &fs::File) -> io::Result<fs::Permissions> {
let mut permissions = file.metadata()?.permissions();
permissions.set_readonly(self.readonly());
Ok(permissions)
}

#[cfg(windows)]
#[inline]
fn _into_std(self, file: &fs::File) -> io::Result<fs::Permissions> {
Expand Down
20 changes: 20 additions & 0 deletions cap-primitives/src/rustix/fs/copy_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn open_from(start: &fs::File, path: &Path) -> io::Result<(fs::File, fs::Metadat
Ok((reader, metadata))
}

#[cfg(not(target_os = "wasi"))]
fn open_to_and_set_permissions(
start: &fs::File,
path: &Path,
Expand Down Expand Up @@ -54,6 +55,25 @@ fn open_to_and_set_permissions(
Ok((writer, writer_metadata))
}

#[cfg(target_os = "wasi")]
fn open_to_and_set_permissions(
start: &fs::File,
path: &Path,
reader_metadata: fs::Metadata,
) -> io::Result<(fs::File, fs::Metadata)> {
let writer = open(
start,
path,
OpenOptions::new()
// create the file with the correct mode right away
.write(true)
.create(true)
.truncate(true),
)?;
let writer_metadata = writer.metadata()?;
Ok((writer, writer_metadata))
}

#[cfg(not(any(
target_os = "linux",
target_os = "android",
Expand Down
11 changes: 6 additions & 5 deletions cap-primitives/src/rustix/fs/create_dir_unchecked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ pub(crate) fn create_dir_unchecked(
path: &Path,
options: &DirOptions,
) -> io::Result<()> {
Ok(mkdirat(
start,
path,
Mode::from_bits(options.ext.mode as RawMode).unwrap(),
)?)
#[cfg(not(target_os = "wasi"))]
let raw_mode = options.ext.mode as RawMode;
#[cfg(target_os = "wasi")]
let raw_mode = 0;

Ok(mkdirat(start, path, Mode::from_bits(raw_mode).unwrap())?)
}
14 changes: 9 additions & 5 deletions cap-primitives/src/rustix/fs/dir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::Deref;
#[cfg(unix)]
use std::os::unix::{ffi::OsStrExt, fs::OpenOptionsExt};
#[cfg(target_os = "wasi")]
use std::os::wasi::{ffi::OsStrExt, fs::OpenOptionsExt};
use std::os::wasi::ffi::OsStrExt;
use std::path::Path;
#[cfg(racy_asserts)]
use std::{ffi::OsString, os::unix::ffi::OsStringExt, path::PathBuf};
Expand Down Expand Up @@ -104,10 +104,13 @@ pub(crate) fn open_ambient_dir_impl(path: &Path, _: AmbientAuthority) -> io::Res
// `O_DIRECTORY` manually.
let flags = OFlags::DIRECTORY | target_o_path();

fs::OpenOptions::new()
.read(true)
.custom_flags(flags.bits() as i32)
.open(&path)
let mut options = fs::OpenOptions::new();
options.read(true);

#[cfg(not(target_os = "wasi"))]
options.custom_flags(flags.bits() as i32);

options.open(&path)
}

/// Use `O_PATH` on platforms which have it, or none otherwise.
Expand All @@ -131,6 +134,7 @@ pub(crate) const fn target_o_path() -> OFlags {
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
target_os = "wasi",
))]
{
OFlags::empty()
Expand Down
2 changes: 0 additions & 2 deletions cap-primitives/src/rustix/fs/file_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use std::ffi::OsString;
use std::fs;
#[cfg(unix)]
use std::os::unix::ffi::OsStringExt;
#[cfg(target_os = "wasi")]
use std::os::wasi::ffi::OsStringExt;
use std::path::PathBuf;

pub(crate) fn file_path_by_ttyname_or_seaching(file: &fs::File) -> Option<PathBuf> {
Expand Down
14 changes: 9 additions & 5 deletions cap-primitives/src/rustix/fs/file_type_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ impl FileTypeExt {
FileType::ext(Self::BlockDevice)
} else if std.is_char_device() {
FileType::ext(Self::CharDevice)
} else if std.is_fifo() {
FileType::ext(Self::Fifo)
} else if std.is_socket() {
FileType::ext(Self::Socket)
} else {
FileType::unknown()
#[cfg(not(target_os = "wasi"))]
if std.is_fifo() {
return FileType::ext(Self::Fifo);
}
if std.is_socket() {
FileType::ext(Self::Socket)
} else {
FileType::unknown()
}
}
}

Expand Down
Loading