Skip to content

Commit 640c020

Browse files
sylvestreoech3
authored andcommitted
cat: replace nix with rustix
1 parent 3b2a09c commit 640c020

File tree

5 files changed

+19
-21
lines changed

5 files changed

+19
-21
lines changed

Cargo.lock

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

src/uu/cat/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ uucore = { workspace = true, features = ["fast-inc", "fs", "pipes", "signals"] }
2626
fluent = { workspace = true }
2727

2828
[target.'cfg(unix)'.dependencies]
29-
nix = { workspace = true }
29+
rustix = { workspace = true, features = ["fs"] }
3030

3131
[target.'cfg(windows)'.dependencies]
3232
winapi-util = { workspace = true }

src/uu/cat/src/cat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ enum CatError {
8484
/// Wrapper around `io::Error`
8585
#[error("{}", strip_errno(.0))]
8686
Io(#[from] io::Error),
87-
/// Wrapper around `nix::Error`
87+
/// Wrapper around `rustix::io::Errno`
8888
#[cfg(any(target_os = "linux", target_os = "android"))]
8989
#[error("{0}")]
90-
Nix(#[from] nix::Error),
90+
Rustix(#[from] rustix::io::Errno),
9191
/// Unknown file type; it's not a regular file, socket, etc.
9292
#[error("{}", translate!("cat-error-unknown-filetype", "ft_debug" => .ft_debug))]
9393
UnknownFiletype {

src/uu/cat/src/platform/unix.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
// spell-checker:ignore lseek seekable
77

8-
use nix::fcntl::{FcntlArg, OFlag, fcntl};
9-
use nix::unistd::{Whence, lseek};
8+
use rustix::fs::{OFlags, SeekFrom, fcntl_getfl};
109
use std::os::fd::AsFd;
1110
use uucore::fs::FileInformation;
1211

@@ -31,10 +30,10 @@ pub fn is_unsafe_overwrite<I: AsFd, O: AsFd>(input: &I, output: &O) -> bool {
3130
if file_size == 0 {
3231
return false;
3332
}
34-
// `lseek` returns an error if the file descriptor is closed or it refers to
33+
// `seek` returns an error if the file descriptor is closed or it refers to
3534
// a non-seekable resource (e.g., pipe, socket, or some devices).
36-
let input_pos = lseek(input.as_fd(), 0, Whence::SeekCur);
37-
let output_pos = lseek(output.as_fd(), 0, Whence::SeekCur);
35+
let input_pos = rustix::fs::seek(input, SeekFrom::Current(0)).map(|v| v as i64);
36+
let output_pos = rustix::fs::seek(output, SeekFrom::Current(0)).map(|v| v as i64);
3837
if is_appending(output) {
3938
if let Ok(pos) = input_pos {
4039
if pos >= 0 && (pos as u64) >= file_size {
@@ -54,9 +53,8 @@ pub fn is_unsafe_overwrite<I: AsFd, O: AsFd>(input: &I, output: &O) -> bool {
5453

5554
/// Whether the file is opened with the `O_APPEND` flag
5655
fn is_appending<F: AsFd>(file: &F) -> bool {
57-
let flags_raw = fcntl(file.as_fd(), FcntlArg::F_GETFL).unwrap_or_default();
58-
let flags = OFlag::from_bits_truncate(flags_raw);
59-
flags.contains(OFlag::O_APPEND)
56+
let flags = fcntl_getfl(file).unwrap_or(OFlags::empty());
57+
flags.contains(OFlags::APPEND)
6058
}
6159

6260
#[cfg(test)]

src/uu/cat/src/splice.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// file that was distributed with this source code.
55
use super::{CatResult, FdReadable, InputHandle};
66

7-
use nix::unistd;
7+
use rustix::io::{read, write};
88
use std::os::{fd::AsFd, unix::io::AsRawFd};
99

1010
use uucore::pipes::{pipe, splice, splice_exact};
@@ -52,20 +52,20 @@ pub(super) fn write_fast_using_splice<R: FdReadable, S: AsRawFd + AsFd>(
5252
/// Move exactly `num_bytes` bytes from `read_fd` to `write_fd`.
5353
///
5454
/// Panics if not enough bytes can be read.
55-
fn copy_exact(read_fd: &impl AsFd, write_fd: &impl AsFd, num_bytes: usize) -> nix::Result<()> {
55+
fn copy_exact(read_fd: &impl AsFd, write_fd: &impl AsFd, num_bytes: usize) -> std::io::Result<()> {
5656
let mut left = num_bytes;
5757
let mut buf = [0; BUF_SIZE];
5858
while left > 0 {
59-
let read = unistd::read(read_fd, &mut buf)?;
60-
assert_ne!(read, 0, "unexpected end of pipe");
59+
let n = read(read_fd, &mut buf)?;
60+
assert_ne!(n, 0, "unexpected end of pipe");
6161
let mut written = 0;
62-
while written < read {
63-
match unistd::write(write_fd, &buf[written..read])? {
64-
0 => panic!(),
65-
n => written += n,
62+
while written < n {
63+
match write(write_fd, &buf[written..n])? {
64+
0 => unreachable!("fd should be writable"),
65+
w => written += w,
6666
}
6767
}
68-
left -= read;
68+
left -= n;
6969
}
7070
Ok(())
7171
}

0 commit comments

Comments
 (0)