diff --git a/src/uu/env/src/native_int_str.rs b/src/uu/env/src/native_int_str.rs index a52686b9dc..b5a0903323 100644 --- a/src/uu/env/src/native_int_str.rs +++ b/src/uu/env/src/native_int_str.rs @@ -13,8 +13,10 @@ // this conversion needs to be done only once in the beginning and at the end. use std::ffi::OsString; -#[cfg(not(target_os = "windows"))] +#[cfg(unix)] use std::os::unix::ffi::{OsStrExt, OsStringExt}; +#[cfg(target_os = "wasi")] +use std::os::wasi::ffi::{OsStrExt, OsStringExt}; #[cfg(target_os = "windows")] use std::os::windows::prelude::*; use std::{borrow::Cow, ffi::OsStr}; diff --git a/src/uucore/src/lib/features/fs.rs b/src/uucore/src/lib/features/fs.rs index 1802cf415f..3774d03d1a 100644 --- a/src/uucore/src/lib/features/fs.rs +++ b/src/uucore/src/lib/features/fs.rs @@ -52,15 +52,13 @@ macro_rules! has { /// Information to uniquely identify a file #[derive(Clone)] pub struct FileInformation( - #[cfg(unix)] rustix::fs::Stat, + #[cfg(any(unix, target_os = "wasi"))] rustix::fs::Stat, #[cfg(windows)] winapi_util::file::Information, - // WASI does not have nix::sys::stat, so we store std::fs::Metadata instead. - #[cfg(target_os = "wasi")] fs::Metadata, ); impl FileInformation { /// Get information from a currently open file - #[cfg(unix)] + #[cfg(any(unix, target_os = "wasi"))] pub fn from_file(file: &impl AsFd) -> IOResult { let stat = rustix::fs::fstat(file)?; Ok(Self(stat)) @@ -78,7 +76,7 @@ impl FileInformation { /// If `path` points to a symlink and `dereference` is true, information about /// the link's target will be returned. pub fn from_path(path: impl AsRef, dereference: bool) -> IOResult { - #[cfg(unix)] + #[cfg(any(unix, target_os = "wasi"))] { let stat = if dereference { rustix::fs::stat(path.as_ref()) @@ -102,20 +100,10 @@ impl FileInformation { let file = open_options.read(true).open(path.as_ref())?; Self::from_file(&file) } - // WASI: use std::fs::metadata / symlink_metadata since nix is not available - #[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 { - #[cfg(unix)] + #[cfg(any(unix, target_os = "wasi"))] { assert!(self.0.st_size >= 0, "File size is negative"); self.0.st_size.try_into().unwrap() @@ -124,10 +112,6 @@ impl FileInformation { { self.0.file_size() } - #[cfg(target_os = "wasi")] - { - self.0.len() - } } #[cfg(windows)] @@ -154,6 +138,8 @@ impl FileInformation { target_pointer_width = "64" ))] return self.0.st_nlink; + #[cfg(target_os = "wasi")] + return self.0.st_nlink; #[cfg(all( unix, any( @@ -178,12 +164,9 @@ impl FileInformation { return self.0.st_nlink.try_into().unwrap(); #[cfg(windows)] return self.0.number_of_links(); - // WASI: nlink is not available in std::fs::Metadata, return 1 - #[cfg(target_os = "wasi")] - return 1; } - #[cfg(unix)] + #[cfg(any(unix, target_os = "wasi"))] pub fn inode(&self) -> u64 { #[cfg(all(not(any(target_os = "netbsd")), target_pointer_width = "64"))] return self.0.st_ino; @@ -193,22 +176,13 @@ impl FileInformation { } } -#[cfg(unix)] +#[cfg(any(unix, target_os = "wasi"))] impl PartialEq for FileInformation { fn eq(&self, other: &Self) -> bool { self.0.st_dev == other.0.st_dev && self.0.st_ino == other.0.st_ino } } -// WASI: compare by file type and size as a basic heuristic since -// device/inode numbers are not available through std::fs::Metadata. -#[cfg(target_os = "wasi")] -impl PartialEq for FileInformation { - fn eq(&self, other: &Self) -> bool { - self.0.file_type() == other.0.file_type() && self.0.len() == other.0.len() - } -} - #[cfg(target_os = "windows")] impl PartialEq for FileInformation { fn eq(&self, other: &Self) -> bool { @@ -221,7 +195,7 @@ impl Eq for FileInformation {} impl Hash for FileInformation { fn hash(&self, state: &mut H) { - #[cfg(unix)] + #[cfg(any(unix, target_os = "wasi"))] { self.0.st_dev.hash(state); self.0.st_ino.hash(state); @@ -231,11 +205,6 @@ 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); - } } } diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs index 3a2e274b21..47e10a1416 100644 --- a/src/uucore/src/lib/features/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -430,7 +430,6 @@ fn mount_dev_id(mount_dir: &OsStr) -> String { } } -#[cfg(not(target_os = "wasi"))] use crate::error::UResult; #[cfg(any( target_os = "freebsd", @@ -461,7 +460,7 @@ use std::ptr; use std::slice; /// Read file system list. -#[cfg(not(target_os = "wasi"))] +#[cfg_attr(target_os = "wasi", allow(clippy::unnecessary_wraps))] pub fn read_fs_list() -> UResult> { #[cfg(any(target_os = "linux", target_os = "android", target_os = "cygwin"))] { @@ -542,6 +541,7 @@ pub fn read_fs_list() -> UResult> { target_os = "redox", target_os = "illumos", target_os = "solaris", + target_os = "wasi" ))] { // No method to read mounts on these platforms @@ -549,13 +549,6 @@ pub fn read_fs_list() -> UResult> { } } -/// Read file system list. -#[cfg(target_os = "wasi")] -pub fn read_fs_list() -> Vec { - // No method to read mounts on WASI - Vec::new() -} - #[derive(Debug, Clone)] pub struct FsUsage { pub blocksize: u64, diff --git a/src/uucore/src/lib/mods/io.rs b/src/uucore/src/lib/mods/io.rs index 530be1ecc1..f63949a44d 100644 --- a/src/uucore/src/lib/mods/io.rs +++ b/src/uucore/src/lib/mods/io.rs @@ -99,14 +99,7 @@ impl OwnedFileDescriptorOrHandle { /// instantiates a corresponding `Stdio` #[cfg(not(target_os = "wasi"))] pub fn into_stdio(self) -> Stdio { - #[cfg(not(target_os = "wasi"))] - { - Stdio::from(self.fx) - } - #[cfg(target_os = "wasi")] - { - Stdio::from(File::from(self.fx)) - } + Stdio::from(self.fx) } /// WASI: Stdio::from(OwnedFd) is not available, convert via File instead. diff --git a/tests/by-util/test_comm.rs b/tests/by-util/test_comm.rs index d082284485..f0b7baed29 100644 --- a/tests/by-util/test_comm.rs +++ b/tests/by-util/test_comm.rs @@ -455,17 +455,12 @@ fn test_sorted() { let at = &scene.fixtures; at.write("comm1", "1\n3"); at.write("comm2", "3\n2"); - let cmd = scene.ucmd().args(&["comm1", "comm2"]).run(); - // WASI's strcoll (C locale only) may not detect unsorted input, - // but the comparison output is still correct. - if std::env::var("UUTESTS_WASM_RUNNER").is_ok() { - cmd.success().stdout_is("1\n\t\t3\n\t2\n"); - } else { - cmd.failure() - .code_is(1) - .stdout_is("1\n\t\t3\n\t2\n") - .stderr_is("comm: file 2 is not in sorted order\ncomm: input is not in sorted order\n"); - } + scene + .ucmd() + .args(&["comm1", "comm2"]) + .fails_with_code(1) + .stdout_is("1\n\t\t3\n\t2\n") + .stderr_is("comm: file 2 is not in sorted order\ncomm: input is not in sorted order\n"); } #[test] @@ -492,19 +487,16 @@ fn test_both_inputs_out_of_order() { at.write("file_a", "3\n1\n0\n"); at.write("file_b", "3\n2\n0\n"); - let cmd = scene.ucmd().args(&["file_a", "file_b"]).run(); - if std::env::var("UUTESTS_WASM_RUNNER").is_ok() { - cmd.success().stdout_is("\t\t3\n1\n0\n\t2\n\t0\n"); - } else { - cmd.failure() - .code_is(1) - .stdout_is("\t\t3\n1\n0\n\t2\n\t0\n") - .stderr_is( - "comm: file 1 is not in sorted order\n\ - comm: file 2 is not in sorted order\n\ - comm: input is not in sorted order\n", - ); - } + scene + .ucmd() + .args(&["file_a", "file_b"]) + .fails_with_code(1) + .stdout_is("\t\t3\n1\n0\n\t2\n\t0\n") + .stderr_is( + "comm: file 1 is not in sorted order\n\ + comm: file 2 is not in sorted order\n\ + comm: input is not in sorted order\n", + ); } #[test] @@ -514,19 +506,16 @@ fn test_both_inputs_out_of_order_last_pair() { at.write("file_a", "3\n1\n"); at.write("file_b", "3\n2\n"); - let cmd = scene.ucmd().args(&["file_a", "file_b"]).run(); - if std::env::var("UUTESTS_WASM_RUNNER").is_ok() { - cmd.success().stdout_is("\t\t3\n1\n\t2\n"); - } else { - cmd.failure() - .code_is(1) - .stdout_is("\t\t3\n1\n\t2\n") - .stderr_is( - "comm: file 1 is not in sorted order\n\ - comm: file 2 is not in sorted order\n\ - comm: input is not in sorted order\n", - ); - } + scene + .ucmd() + .args(&["file_a", "file_b"]) + .fails_with_code(1) + .stdout_is("\t\t3\n1\n\t2\n") + .stderr_is( + "comm: file 1 is not in sorted order\n\ + comm: file 2 is not in sorted order\n\ + comm: input is not in sorted order\n", + ); } #[test]