Skip to content
Draft
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ feat_wasm = [
"shred",
"shuf",
"sleep",
"sort",
"sum",
"tee",
"true",
Expand Down
2 changes: 1 addition & 1 deletion src/uu/sort/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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]
Expand Down
4 changes: 2 additions & 2 deletions src/uu/sort/src/tmp_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<HandlerRegistration>>) -> 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.
Expand Down Expand Up @@ -104,7 +104,7 @@ fn ensure_signal_handler_installed(state: Arc<Mutex<HandlerRegistration>>) -> UR
Ok(())
}

#[cfg(target_os = "redox")]
#[cfg(any(target_os = "redox", target_os = "wasi"))]
fn ensure_signal_handler_installed(_state: Arc<Mutex<HandlerRegistration>>) -> UResult<()> {
Ok(())
}
Expand Down
42 changes: 38 additions & 4 deletions src/uucore/src/lib/features/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ macro_rules! has {
pub struct FileInformation(
#[cfg(unix)] nix::sys::stat::FileStat,
#[cfg(windows)] winapi_util::file::Information,
#[cfg(target_os = "wasi")] std::fs::Metadata,
);

impl FileInformation {
Expand Down Expand Up @@ -91,6 +92,15 @@ impl FileInformation {
let file = open_options.read(true).open(path.as_ref())?;
Self::from_file(&file)
}
#[cfg(target_os = "wasi")]
{
let metadata = if dereference {
fs::metadata(path.as_ref())
} else {
fs::symlink_metadata(path.as_ref())
}?;
Ok(Self(metadata))
}
}

pub fn file_size(&self) -> u64 {
Expand All @@ -103,6 +113,10 @@ impl FileInformation {
{
self.0.file_size()
}
#[cfg(target_os = "wasi")]
{
self.0.len()
}
}

#[cfg(windows)]
Expand Down Expand Up @@ -153,6 +167,8 @@ impl FileInformation {
return self.0.st_nlink.try_into().unwrap();
#[cfg(windows)]
return self.0.number_of_links();
#[cfg(target_os = "wasi")]
return 1;
}

#[cfg(unix)]
Expand Down Expand Up @@ -180,6 +196,13 @@ impl PartialEq for FileInformation {
}
}

#[cfg(target_os = "wasi")]
impl PartialEq for FileInformation {
fn eq(&self, _other: &Self) -> bool {
false
}
}

impl Eq for FileInformation {}

impl Hash for FileInformation {
Expand All @@ -194,6 +217,11 @@ impl Hash for FileInformation {
self.0.volume_serial_number().hash(state);
self.0.file_index().hash(state);
}
#[cfg(target_os = "wasi")]
{
self.0.len().hash(state);
self.0.file_type().is_dir().hash(state);
}
}
}

Expand Down Expand Up @@ -769,7 +797,7 @@ pub fn is_stdin_directory(stdin: &Stdin) -> bool {
mode & S_IFMT == S_IFDIR
}

#[cfg(windows)]
#[cfg(target_os = "windows")]
{
use std::os::windows::io::AsRawHandle;
let handle = stdin.as_raw_handle();
Expand All @@ -778,11 +806,17 @@ pub fn is_stdin_directory(stdin: &Stdin) -> bool {
}
false
}

#[cfg(target_os = "wasi")]
{
let _ = stdin;
false
}
}

pub mod sane_blksize {

#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
use std::{fs::metadata, path::Path};

Expand All @@ -809,12 +843,12 @@ pub mod sane_blksize {
#[cfg(unix)] metadata: &std::fs::Metadata,
#[cfg(not(unix))] _: &std::fs::Metadata,
) -> u64 {
#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
{
sane_blksize(metadata.blksize())
}

#[cfg(target_os = "windows")]
#[cfg(not(unix))]
{
DEFAULT
}
Expand Down
9 changes: 8 additions & 1 deletion src/uucore/src/lib/mods/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ impl OwnedFileDescriptorOrHandle {

/// instantiates a corresponding `Stdio`
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))
}
}

/// clones self. useful when needing another
Expand Down
Loading