diff --git a/src/dent.rs b/src/dent.rs index adf54f7..7d86e15 100644 --- a/src/dent.rs +++ b/src/dent.rs @@ -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. @@ -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 { + #[cfg(unix)] use std::os::unix::fs::DirEntryExt; + #[cfg(target_os = "wasi")] + use std::os::wasi::fs::DirEntryExt; let ty = ent .file_type() @@ -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, @@ -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 { + #[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) @@ -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, @@ -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(), @@ -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(), @@ -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. diff --git a/src/lib.rs b/src/lib.rs index 4b96df4..0fd62ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"); @@ -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; diff --git a/src/tests/util.rs b/src/tests/util.rs index fdf06f5..6533ed1 100644 --- a/src/tests/util.rs +++ b/src/tests/util.rs @@ -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| { @@ -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| {