Skip to content
Open
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.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 5 additions & 11 deletions src/uu/cat/src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,31 +480,25 @@ fn get_input_type(path: &OsString) -> CatResult<InputType> {
/// Writes handle to stdout with no configuration. This allows a
/// simple memory copy.
fn print_fast<R: FdReadable>(handle: &mut InputHandle<R>) -> CatResult<()> {
let stdout = io::stdout();
#[cfg(any(target_os = "linux", target_os = "android"))]
let mut stdout = stdout;
#[cfg(any(target_os = "linux", target_os = "android"))]
{
// If we're on Linux or Android, try to use the splice() system call
// for faster writing. If it works, we're done.
if !splice::write_fast_using_splice(handle, &mut stdout)? {
if !splice::write_fast_using_splice(handle, &mut *uucore::stdio::stdout_raw())? {
return Ok(());
}
}
// If we're not on Linux or Android, or the splice() call failed,
// fall back on slower writing.
print_unbuffered(handle, stdout)
print_unbuffered(handle)
}

#[cfg_attr(any(target_os = "linux", target_os = "android"), inline(never))] // splice fast-path does not require this allocation
fn print_unbuffered<R: FdReadable>(
handle: &mut InputHandle<R>,
stdout: io::Stdout,
) -> CatResult<()> {
fn print_unbuffered<R: FdReadable>(handle: &mut InputHandle<R>) -> CatResult<()> {
#[cfg(any(unix, target_os = "wasi"))]
let mut stdout = uucore::io::RawWriter(stdout); // use raw syscall to remove buffering
let mut stdout = uucore::stdio::stdout_raw();
#[cfg(not(any(unix, target_os = "wasi")))]
let mut stdout = stdout.lock();
let mut stdout = io::stdout().lock();
let mut buf = [0; 1024 * 64];
loop {
match handle.reader.read(&mut buf) {
Expand Down
1 change: 1 addition & 0 deletions src/uu/dd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ doctest = false
clap = { workspace = true }
gcd = { workspace = true }
libc = { workspace = true }
rustix = { workspace = true }
uucore = { workspace = true, features = [
"format",
"parser-size",
Expand Down
8 changes: 7 additions & 1 deletion src/uucore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ jiff = { workspace = true, optional = true, features = [
"tzdb-concatenated",
] }
rustc-hash = { workspace = true }
rustix = { workspace = true, features = ["fs", "net", "pipe", "process"] }
rustix = { workspace = true, features = [
"fs",
"net",
"pipe",
"process",
"stdio",
] }
time = { workspace = true, optional = true, features = [
"formatting",
"local-offset",
Expand Down
2 changes: 2 additions & 0 deletions src/uucore/src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub use crate::mods::locale;
pub use crate::mods::os;
pub use crate::mods::panic;
pub use crate::mods::posix;
#[cfg(any(unix, target_os = "wasi"))]
pub use crate::mods::stdio;

// * feature-gated modules
#[cfg(feature = "backup-control")]
Expand Down
2 changes: 2 additions & 0 deletions src/uucore/src/lib/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ pub mod locale;
pub mod os;
pub mod panic;
pub mod posix;
#[cfg(any(unix, target_os = "wasi"))]
pub mod stdio;
55 changes: 55 additions & 0 deletions src/uucore/src/lib/mods/stdio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

//! Abstractions for raw access to stdin and stdout, without buffering.

use core::ops::{Deref, DerefMut};
use std::{fs::File, mem::ManuallyDrop};

pub struct StdinRaw(ManuallyDrop<File>);

pub struct StdoutRaw(ManuallyDrop<File>);

pub fn stdin_raw() -> StdinRaw {
// SAFETY: We ensure that the file descriptor is never closed by
// wrapping the `File` in `ManuallyDrop`.
let fd = unsafe { rustix::stdio::take_stdin() };
StdinRaw(ManuallyDrop::new(File::from(fd)))
}

pub fn stdout_raw() -> StdoutRaw {
// SAFETY: We ensure that the file descriptor is never closed by
// wrapping the `File` in `ManuallyDrop`.
let fd = unsafe { rustix::stdio::take_stdout() };
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let fd = unsafe { rustix::stdio::take_stdout() };
let fd = unsafe { OwnedFd::from_raw_fd(libc::STDOUT_FILENO as RawFd) }

We could avoid the rustix dependency here if so desired.

StdoutRaw(ManuallyDrop::new(File::from(fd)))
}

impl Deref for StdinRaw {
type Target = File;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for StdinRaw {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl Deref for StdoutRaw {
type Target = File;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for StdoutRaw {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
Loading