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

Add support for WASI #205

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 15 additions & 9 deletions src/dent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct DirEntry {
/// The depth at which this entry was generated relative to the root.
depth: usize,
/// The underlying inode number (Unix only).
#[cfg(unix)]
#[cfg(any(unix, target_os = "wasi"))]
ino: u64,
/// The underlying metadata (Windows only). We store this on Windows
/// because this comes for free while reading a directory.
Expand Down Expand Up @@ -196,12 +196,15 @@ impl DirEntry {
Ok(DirEntry { path, ty, follow_link: false, depth, metadata: md })
}

#[cfg(unix)]
#[cfg(any(unix, target_os = "wasi"))]
pub(crate) fn from_entry(
depth: usize,
ent: &fs::DirEntry,
) -> Result<DirEntry> {
#[cfg(unix)]
use std::os::unix::fs::DirEntryExt;
#[cfg(target_os = "wasi")]
use std::os::wasi::fs::DirEntryExt;

let ty = ent
.file_type()
Expand All @@ -215,7 +218,7 @@ impl DirEntry {
})
}

#[cfg(not(any(unix, windows)))]
#[cfg(not(any(unix, windows, target_os = "wasi")))]
pub(crate) fn from_entry(
depth: usize,
ent: &fs::DirEntry,
Expand Down Expand Up @@ -248,13 +251,16 @@ impl DirEntry {
})
}

#[cfg(unix)]
#[cfg(any(unix, target_os = "wasi"))]
pub(crate) fn from_path(
depth: usize,
pb: PathBuf,
follow: bool,
) -> Result<DirEntry> {
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
#[cfg(target_os = "wasi")]
use std::os::wasi::fs::MetadataExt;

let md = if follow {
fs::metadata(&pb)
Expand All @@ -272,7 +278,7 @@ impl DirEntry {
})
}

#[cfg(not(any(unix, windows)))]
#[cfg(not(any(unix, windows, target_os = "wasi")))]
pub(crate) fn from_path(
depth: usize,
pb: PathBuf,
Expand Down Expand Up @@ -306,7 +312,7 @@ impl Clone for DirEntry {
}
}

#[cfg(unix)]
#[cfg(any(unix, target_os = "wasi"))]
fn clone(&self) -> DirEntry {
DirEntry {
path: self.path.clone(),
Expand All @@ -317,7 +323,7 @@ impl Clone for DirEntry {
}
}

#[cfg(not(any(unix, windows)))]
#[cfg(not(any(unix, windows, target_os = "wasi")))]
fn clone(&self) -> DirEntry {
DirEntry {
path: self.path.clone(),
Expand All @@ -335,14 +341,14 @@ impl fmt::Debug for DirEntry {
}

/// Unix-specific extension methods for `walkdir::DirEntry`
#[cfg(unix)]
#[cfg(any(unix, target_os = "wasi"))]
pub trait DirEntryExt {
/// Returns the underlying `d_ino` field in the contained `dirent`
/// structure.
fn ino(&self) -> u64;
}

#[cfg(unix)]
#[cfg(any(unix, target_os = "wasi"))]
impl DirEntryExt for DirEntry {
/// Returns the underlying `d_ino` field in the contained `dirent`
/// structure.
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ for entry in walker.filter_entry(|e| !is_hidden(e)) {

#![deny(missing_docs)]
#![allow(unknown_lints)]
// https://github.com/rust-lang/rust/issues/130323
#![cfg_attr(all(target_os = "wasi", target_env = "p2"), feature(wasip2))]
#![cfg_attr(target_os = "wasi", feature(wasi_ext))]

#[cfg(doctest)]
doc_comment::doctest!("../README.md");
Expand All @@ -121,7 +124,7 @@ use std::vec;
use same_file::Handle;

pub use crate::dent::DirEntry;
#[cfg(unix)]
#[cfg(any(unix, target_os = "wasi"))]
pub use crate::dent::DirEntryExt;
pub use crate::error::Error;

Expand Down
12 changes: 12 additions & 0 deletions src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ impl Dir {
symlink(src, link_name)
}

#[cfg(target_os = "wasi")]
fn imp(src: &Path, link_name: &Path) -> io::Result<()> {
use std::os::wasi::fs::symlink_path;
symlink_path(src, link_name)
}

let (src, link_name) = (self.join(src), self.join(link_name));
imp(&src, &link_name)
.map_err(|e| {
Expand Down Expand Up @@ -191,6 +197,12 @@ impl Dir {
symlink(src, link_name)
}

#[cfg(target_os = "wasi")]
fn imp(src: &Path, link_name: &Path) -> io::Result<()> {
use std::os::wasi::fs::symlink_path;
symlink_path(src, link_name)
}

let (src, link_name) = (self.join(src), self.join(link_name));
imp(&src, &link_name)
.map_err(|e| {
Expand Down