Skip to content

Commit 04b1585

Browse files
committed
uucore: use rustix'splice & restrict to Linux
1 parent 3b2a09c commit 04b1585

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fuzz/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uucore/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jiff = { workspace = true, optional = true, features = [
3737
"tzdb-concatenated",
3838
] }
3939
rustc-hash = { workspace = true }
40+
rustix = { workspace = true, features = ["pipe"] }
4041
time = { workspace = true, optional = true, features = [
4142
"formatting",
4243
"local-offset",

src/uucore/src/lib/features/pipes.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
//! Thin pipe-related wrappers around functions from the `nix` crate.
7-
6+
//! Thin zero-copy-related wrappers around functions from the `rustix` crate.
7+
#[cfg(any(target_os = "linux", target_os = "android"))]
88
use std::fs::File;
99
#[cfg(any(target_os = "linux", target_os = "android"))]
1010
use std::os::fd::AsFd;
1111

1212
#[cfg(any(target_os = "linux", target_os = "android"))]
13-
use nix::fcntl::SpliceFFlags;
13+
use rustix::pipe::SpliceFlags;
1414

15-
pub use nix::{Error, Result};
16-
17-
/// A wrapper around [`nix::unistd::pipe`] that ensures the pipe is cleaned up.
15+
/// A wrapper around [`rustix::pipe::pipe`] that ensures the pipe is cleaned up.
1816
///
1917
/// Returns two `File` objects: everything written to the second can be read
2018
/// from the first.
21-
pub fn pipe() -> Result<(File, File)> {
22-
let (read, write) = nix::unistd::pipe()?;
19+
#[cfg(any(target_os = "linux", target_os = "android"))]
20+
pub fn pipe() -> std::io::Result<(File, File)> {
21+
let (read, write) = rustix::pipe::pipe()?;
2322
Ok((File::from(read), File::from(write)))
2423
}
2524

26-
/// Less noisy wrapper around [`nix::fcntl::splice`].
25+
/// Less noisy wrapper around [`rustix::pipe::splice`].
2726
///
2827
/// Up to `len` bytes are moved from `source` to `target`. Returns the number
2928
/// of successfully moved bytes.
@@ -33,8 +32,15 @@ pub fn pipe() -> Result<(File, File)> {
3332
/// a [`pipe`] and then from the pipe into your target (with `splice_exact`):
3433
/// this is still very efficient.
3534
#[cfg(any(target_os = "linux", target_os = "android"))]
36-
pub fn splice(source: &impl AsFd, target: &impl AsFd, len: usize) -> Result<usize> {
37-
nix::fcntl::splice(source, None, target, None, len, SpliceFFlags::empty())
35+
pub fn splice(source: &impl AsFd, target: &impl AsFd, len: usize) -> std::io::Result<usize> {
36+
Ok(rustix::pipe::splice(
37+
source,
38+
None,
39+
target,
40+
None,
41+
len,
42+
SpliceFlags::empty(),
43+
)?)
3844
}
3945

4046
/// Splice wrapper which fully finishes the write.
@@ -43,11 +49,11 @@ pub fn splice(source: &impl AsFd, target: &impl AsFd, len: usize) -> Result<usiz
4349
///
4450
/// Panics if `source` runs out of data before `len` bytes have been moved.
4551
#[cfg(any(target_os = "linux", target_os = "android"))]
46-
pub fn splice_exact(source: &impl AsFd, target: &impl AsFd, len: usize) -> Result<()> {
52+
pub fn splice_exact(source: &impl AsFd, target: &impl AsFd, len: usize) -> std::io::Result<()> {
4753
let mut left = len;
4854
while left != 0 {
4955
let written = splice(source, target, left)?;
50-
assert_ne!(written, 0, "unexpected end of data");
56+
debug_assert_ne!(written, 0, "unexpected end of data");
5157
left -= written;
5258
}
5359
Ok(())

0 commit comments

Comments
 (0)