From 681c0eca374f25e24e9628f789ed2b45d1ad2b73 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 3 Apr 2026 12:15:13 +0200 Subject: [PATCH] uucore: add WASI support for FileInformation and io module (#11568) --- Cargo.toml | 1 + src/uu/sort/Cargo.toml | 2 +- src/uu/sort/src/tmp_dir.rs | 4 ++-- src/uucore/src/lib/features/fs.rs | 1 + src/uucore/src/lib/features/fsext.rs | 4 +++- src/uucore/src/lib/mods/io.rs | 9 ++++++++- 6 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f606eae28..cb27c7806 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -215,6 +215,7 @@ feat_wasm = [ "shred", "shuf", "sleep", + "sort", "sum", "tail", "tee", diff --git a/src/uu/sort/Cargo.toml b/src/uu/sort/Cargo.toml index d9bc86a68..bf7c514bf 100644 --- a/src/uu/sort/Cargo.toml +++ b/src/uu/sort/Cargo.toml @@ -47,7 +47,7 @@ uucore = { workspace = true, features = [ fluent = { workspace = true } foldhash = { workspace = true } -[target.'cfg(not(target_os = "redox"))'.dependencies] +[target.'cfg(not(any(target_os = "redox", target_os = "wasi")))'.dependencies] ctrlc = { workspace = true } [target.'cfg(unix)'.dependencies] diff --git a/src/uu/sort/src/tmp_dir.rs b/src/uu/sort/src/tmp_dir.rs index a9b93ceb7..cf484e2ff 100644 --- a/src/uu/sort/src/tmp_dir.rs +++ b/src/uu/sort/src/tmp_dir.rs @@ -54,7 +54,7 @@ fn should_install_signal_handler() -> bool { open_fds.saturating_add(CTRL_C_FDS + RESERVED_FOR_MERGE) <= limit } -#[cfg(not(target_os = "redox"))] +#[cfg(not(any(target_os = "redox", target_os = "wasi")))] fn ensure_signal_handler_installed(state: Arc>) -> UResult<()> { // This shared state must originate from `HANDLER_STATE` so the handler always sees // the current lock/path pair and can clean up the active temp directory on SIGINT. @@ -104,7 +104,7 @@ fn ensure_signal_handler_installed(state: Arc>) -> UR Ok(()) } -#[cfg(target_os = "redox")] +#[cfg(any(target_os = "redox", target_os = "wasi"))] fn ensure_signal_handler_installed(_state: Arc>) -> UResult<()> { Ok(()) } diff --git a/src/uucore/src/lib/features/fs.rs b/src/uucore/src/lib/features/fs.rs index ede2d07ee..f56c069f5 100644 --- a/src/uucore/src/lib/features/fs.rs +++ b/src/uucore/src/lib/features/fs.rs @@ -225,6 +225,7 @@ impl Hash for FileInformation { #[cfg(target_os = "wasi")] { self.0.len().hash(state); + self.0.file_type().is_dir().hash(state); } } } diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs index 26070b108..b118cea1f 100644 --- a/src/uucore/src/lib/features/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -28,6 +28,7 @@ use crate::os_str_from_bytes; #[cfg(windows)] use crate::show_warning; +#[cfg(not(target_os = "wasi"))] use std::ffi::OsStr; #[cfg(unix)] use std::os::unix::ffi::OsStrExt; @@ -64,13 +65,14 @@ use libc::{ }; #[cfg(unix)] use std::ffi::{CStr, CString}; +#[cfg(not(target_os = "wasi"))] use std::io::Error as IOError; #[cfg(unix)] use std::mem; #[cfg(windows)] use std::path::Path; use std::time::SystemTime; -#[cfg(not(windows))] +#[cfg(unix)] use std::time::UNIX_EPOCH; use std::{borrow::Cow, ffi::OsString}; diff --git a/src/uucore/src/lib/mods/io.rs b/src/uucore/src/lib/mods/io.rs index a9e4a7bcb..40bd74324 100644 --- a/src/uucore/src/lib/mods/io.rs +++ b/src/uucore/src/lib/mods/io.rs @@ -75,7 +75,14 @@ impl OwnedFileDescriptorOrHandle { /// instantiates a corresponding `Stdio` #[cfg(not(target_os = "wasi"))] pub fn into_stdio(self) -> Stdio { - Stdio::from(self.fx) + #[cfg(not(target_os = "wasi"))] + { + Stdio::from(self.fx) + } + #[cfg(target_os = "wasi")] + { + Stdio::from(File::from(self.fx)) + } } /// WASI: Stdio::from(OwnedFd) is not available, convert via File instead.