Skip to content

Commit 035476c

Browse files
committed
Adapt to API changes in nix
1 parent fd4c315 commit 035476c

File tree

9 files changed

+43
-44
lines changed

9 files changed

+43
-44
lines changed

src/uu/cat/src/cat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::io::{self, BufWriter, IsTerminal, Read, Write};
1010
#[cfg(unix)]
1111
use std::net::Shutdown;
1212
#[cfg(unix)]
13-
use std::os::fd::{AsFd, AsRawFd};
13+
use std::os::fd::AsFd;
1414
#[cfg(unix)]
1515
use std::os::unix::fs::FileTypeExt;
1616
#[cfg(unix)]
@@ -372,7 +372,7 @@ fn cat_handle<R: FdReadable>(
372372
#[cfg(unix)]
373373
fn is_appending() -> bool {
374374
let stdout = io::stdout();
375-
let Ok(flags) = fcntl(stdout.as_raw_fd(), FcntlArg::F_GETFL) else {
375+
let Ok(flags) = fcntl(stdout.as_fd(), FcntlArg::F_GETFL) else {
376376
return false;
377377
};
378378
// TODO Replace `1 << 10` with `nix::fcntl::Oflag::O_APPEND`.

src/uu/cat/src/splice.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
use super::{CatResult, FdReadable, InputHandle};
66

77
use nix::unistd;
8-
use std::os::{
9-
fd::AsFd,
10-
unix::io::{AsRawFd, RawFd},
11-
};
8+
use std::os::{fd::AsFd, unix::io::AsRawFd};
129

1310
use uucore::pipes::{pipe, splice, splice_exact};
1411

@@ -41,7 +38,7 @@ pub(super) fn write_fast_using_splice<R: FdReadable, S: AsRawFd + AsFd>(
4138
// we can recover by copying the data that we have from the
4239
// intermediate pipe to stdout using normal read/write. Then
4340
// we tell the caller to fall back.
44-
copy_exact(pipe_rd.as_raw_fd(), write_fd, n)?;
41+
copy_exact(&pipe_rd, write_fd, n)?;
4542
return Ok(true);
4643
}
4744
}
@@ -55,7 +52,7 @@ pub(super) fn write_fast_using_splice<R: FdReadable, S: AsRawFd + AsFd>(
5552
/// Move exactly `num_bytes` bytes from `read_fd` to `write_fd`.
5653
///
5754
/// Panics if not enough bytes can be read.
58-
fn copy_exact(read_fd: RawFd, write_fd: &impl AsFd, num_bytes: usize) -> nix::Result<()> {
55+
fn copy_exact(read_fd: &impl AsFd, write_fd: &impl AsFd, num_bytes: usize) -> nix::Result<()> {
5956
let mut left = num_bytes;
6057
let mut buf = [0; BUF_SIZE];
6158
while left > 0 {

src/uu/dd/src/dd.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use std::ffi::OsString;
3131
use std::fs::{File, OpenOptions};
3232
use std::io::{self, Read, Seek, SeekFrom, Stdout, Write};
3333
#[cfg(any(target_os = "linux", target_os = "android"))]
34+
use std::os::fd::AsFd;
35+
#[cfg(any(target_os = "linux", target_os = "android"))]
3436
use std::os::unix::fs::OpenOptionsExt;
3537
#[cfg(unix)]
3638
use std::os::unix::{
@@ -279,7 +281,7 @@ impl Source {
279281
match self {
280282
Self::File(f) => {
281283
let advice = PosixFadviseAdvice::POSIX_FADV_DONTNEED;
282-
posix_fadvise(f.as_raw_fd(), offset, len, advice)
284+
posix_fadvise(f.as_fd(), offset, len, advice)
283285
}
284286
_ => Err(Errno::ESPIPE), // "Illegal seek"
285287
}
@@ -649,7 +651,7 @@ impl Dest {
649651
match self {
650652
Self::File(f, _) => {
651653
let advice = PosixFadviseAdvice::POSIX_FADV_DONTNEED;
652-
posix_fadvise(f.as_raw_fd(), offset, len, advice)
654+
posix_fadvise(f.as_fd(), offset, len, advice)
653655
}
654656
_ => Err(Errno::ESPIPE), // "Illegal seek"
655657
}
@@ -784,7 +786,7 @@ impl<'a> Output<'a> {
784786
#[cfg(any(target_os = "linux", target_os = "android"))]
785787
if let Some(libc_flags) = make_linux_oflags(&settings.oflags) {
786788
nix::fcntl::fcntl(
787-
fx.as_raw().as_raw_fd(),
789+
fx.as_raw().as_fd(),
788790
F_SETFL(OFlag::from_bits_retain(libc_flags)),
789791
)?;
790792
}

src/uu/tee/src/tee.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,12 @@ pub fn ensure_stdout_not_broken() -> Result<bool> {
390390
poll::{PollFd, PollFlags, PollTimeout},
391391
sys::stat::{SFlag, fstat},
392392
};
393-
use std::os::fd::{AsFd, AsRawFd};
393+
use std::os::fd::AsFd;
394394

395395
let out = stdout();
396396

397397
// First, check that stdout is a fifo and return true if it's not the case
398-
let stat = fstat(out.as_raw_fd())?;
398+
let stat = fstat(out.as_fd())?;
399399
if !SFlag::from_bits_truncate(stat.st_mode).contains(SFlag::S_IFIFO) {
400400
return Ok(true);
401401
}

src/uu/wc/src/count_fast.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use libc::{_SC_PAGESIZE, S_IFREG, sysconf};
1818
use nix::sys::stat;
1919
#[cfg(unix)]
2020
use std::io::{Seek, SeekFrom};
21-
#[cfg(any(target_os = "linux", target_os = "android"))]
21+
#[cfg(unix)]
2222
use std::os::fd::{AsFd, AsRawFd};
2323
#[cfg(windows)]
2424
use std::os::windows::fs::MetadataExt;
@@ -48,9 +48,7 @@ fn count_bytes_using_splice(fd: &impl AsFd) -> Result<usize, usize> {
4848
.write(true)
4949
.open("/dev/null")
5050
.map_err(|_| 0_usize)?;
51-
let null_rdev = stat::fstat(null_file.as_raw_fd())
52-
.map_err(|_| 0_usize)?
53-
.st_rdev as libc::dev_t;
51+
let null_rdev = stat::fstat(null_file.as_fd()).map_err(|_| 0_usize)?.st_rdev as libc::dev_t;
5452
if (libc::major(null_rdev), libc::minor(null_rdev)) != (1, 3) {
5553
// This is not a proper /dev/null, writing to it is probably bad
5654
// Bit of an edge case, but it has been known to happen
@@ -92,7 +90,7 @@ pub(crate) fn count_bytes_fast<T: WordCountable>(handle: &mut T) -> (usize, Opti
9290

9391
#[cfg(unix)]
9492
{
95-
let fd = handle.as_raw_fd();
93+
let fd = handle.as_fd();
9694
if let Ok(stat) = stat::fstat(fd) {
9795
// If the file is regular, then the `st_size` should hold
9896
// the file's size in bytes.
@@ -132,7 +130,10 @@ pub(crate) fn count_bytes_fast<T: WordCountable>(handle: &mut T) -> (usize, Opti
132130
// However, the raw file descriptor in this situation would be equal to `0`
133131
// for STDIN in both invocations.
134132
// Therefore we cannot rely of `st_size` here and should fall back on full read.
135-
if fd > 0 && (stat.st_mode as libc::mode_t & S_IFREG) != 0 && stat.st_size > 0 {
133+
if fd.as_raw_fd() > 0
134+
&& (stat.st_mode as libc::mode_t & S_IFREG) != 0
135+
&& stat.st_size > 0
136+
{
136137
let sys_page_size = unsafe { sysconf(_SC_PAGESIZE) as usize };
137138
if stat.st_size as usize % sys_page_size > 0 {
138139
// regular file or file from /proc, /sys and similar pseudo-filesystems

src/uucore/src/lib/features/buf_copy.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ mod tests {
3737
},
3838
};
3939

40-
#[cfg(any(target_os = "linux", target_os = "android"))]
41-
use std::os::fd::AsRawFd;
42-
4340
use std::io::{Read, Write};
4441

4542
#[cfg(unix)]
@@ -62,7 +59,7 @@ mod tests {
6259
let n = pipe_write.write(data).unwrap();
6360
assert_eq!(n, data.len());
6461
let mut buf = [0; 1024];
65-
let n = copy_exact(pipe_read.as_raw_fd(), &pipe_write, data.len()).unwrap();
62+
let n = copy_exact(&pipe_read, &pipe_write, data.len()).unwrap();
6663
let n2 = pipe_read.read(&mut buf).unwrap();
6764
assert_eq!(n, n2);
6865
assert_eq!(&buf[..n], data);

src/uucore/src/lib/features/buf_copy/linux.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
/// Buffer-based copying utilities for unix (excluding Linux).
1414
use std::{
1515
io::{Read, Write},
16-
os::fd::{AsFd, AsRawFd, RawFd},
16+
os::fd::{AsFd, AsRawFd},
1717
};
1818

1919
use super::common::Error;
@@ -105,7 +105,7 @@ where
105105
// we can recover by copying the data that we have from the
106106
// intermediate pipe to stdout using normal read/write. Then
107107
// we tell the caller to fall back.
108-
copy_exact(pipe_rd.as_raw_fd(), dest, n)?;
108+
copy_exact(&pipe_rd, dest, n)?;
109109
return Ok((bytes, true));
110110
}
111111

@@ -122,7 +122,7 @@ where
122122
/// and `write` calls.
123123
#[cfg(any(target_os = "linux", target_os = "android"))]
124124
pub(crate) fn copy_exact(
125-
read_fd: RawFd,
125+
read_fd: &impl AsFd,
126126
write_fd: &impl AsFd,
127127
num_bytes: usize,
128128
) -> std::io::Result<usize> {

src/uucore/src/lib/features/fs.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ use std::fs::read_dir;
2424
use std::hash::Hash;
2525
use std::io::Stdin;
2626
use std::io::{Error, ErrorKind, Result as IOResult};
27+
use std::os::fd::AsFd;
2728
#[cfg(unix)]
28-
use std::os::unix::{fs::MetadataExt, io::AsRawFd};
29+
use std::os::unix::fs::MetadataExt;
2930
use std::path::{Component, MAIN_SEPARATOR, Path, PathBuf};
3031
#[cfg(target_os = "windows")]
3132
use winapi_util::AsHandleRef;
@@ -50,8 +51,8 @@ pub struct FileInformation(
5051
impl FileInformation {
5152
/// Get information from a currently open file
5253
#[cfg(unix)]
53-
pub fn from_file(file: &impl AsRawFd) -> IOResult<Self> {
54-
let stat = nix::sys::stat::fstat(file.as_raw_fd())?;
54+
pub fn from_file(file: &impl AsFd) -> IOResult<Self> {
55+
let stat = nix::sys::stat::fstat(file)?;
5556
Ok(Self(stat))
5657
}
5758

@@ -717,7 +718,7 @@ pub fn is_stdin_directory(stdin: &Stdin) -> bool {
717718
#[cfg(unix)]
718719
{
719720
use nix::sys::stat::fstat;
720-
let mode = fstat(stdin.as_raw_fd()).unwrap().st_mode as mode_t;
721+
let mode = fstat(stdin.as_fd()).unwrap().st_mode as mode_t;
721722
has!(mode, S_IFDIR)
722723
}
723724

tests/by-util/test_ls.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ use std::collections::HashMap;
1919
use std::ffi::OsStr;
2020
#[cfg(target_os = "linux")]
2121
use std::os::unix::ffi::OsStrExt;
22-
#[cfg(all(unix, feature = "chmod"))]
23-
use std::os::unix::io::IntoRawFd;
2422
use std::path::Path;
2523
#[cfg(not(windows))]
2624
use std::path::PathBuf;
@@ -544,50 +542,53 @@ fn test_ls_io_errors() {
544542

545543
#[cfg(unix)]
546544
{
545+
use std::os::fd::AsRawFd;
546+
547547
at.touch("some-dir4/bad-fd.txt");
548-
let fd1 = at.open("some-dir4/bad-fd.txt").into_raw_fd();
549-
let fd2 = dup(dbg!(fd1)).unwrap();
548+
let fd1 = at.open("some-dir4/bad-fd.txt");
549+
let fd2 = dup(dbg!(&fd1)).unwrap();
550550
close(fd1).unwrap();
551551

552552
// on the mac and in certain Linux containers bad fds are typed as dirs,
553553
// however sometimes bad fds are typed as links and directory entry on links won't fail
554-
if PathBuf::from(format!("/dev/fd/{fd2}")).is_dir() {
554+
if PathBuf::from(format!("/dev/fd/{}", fd2.as_raw_fd())).is_dir() {
555555
scene
556556
.ucmd()
557557
.arg("-alR")
558-
.arg(format!("/dev/fd/{fd2}"))
558+
.arg(format!("/dev/fd/{}", fd2.as_raw_fd()))
559559
.fails()
560560
.stderr_contains(format!(
561-
"cannot open directory '/dev/fd/{fd2}': Bad file descriptor"
561+
"cannot open directory '/dev/fd/{}': Bad file descriptor",
562+
fd2.as_raw_fd()
562563
))
563-
.stdout_does_not_contain(format!("{fd2}:\n"));
564+
.stdout_does_not_contain(format!("{}:\n", fd2.as_raw_fd()));
564565

565566
scene
566567
.ucmd()
567568
.arg("-RiL")
568-
.arg(format!("/dev/fd/{fd2}"))
569+
.arg(format!("/dev/fd/{}", fd2.as_raw_fd()))
569570
.fails()
570-
.stderr_contains(format!("cannot open directory '/dev/fd/{fd2}': Bad file descriptor"))
571+
.stderr_contains(format!("cannot open directory '/dev/fd/{}': Bad file descriptor", fd2.as_raw_fd()))
571572
// don't double print bad fd errors
572-
.stderr_does_not_contain(format!("ls: cannot open directory '/dev/fd/{fd2}': Bad file descriptor\nls: cannot open directory '/dev/fd/{fd2}': Bad file descriptor"));
573+
.stderr_does_not_contain(format!("ls: cannot open directory '/dev/fd/{0}': Bad file descriptor\nls: cannot open directory '/dev/fd/{0}': Bad file descriptor", fd2.as_raw_fd()));
573574
} else {
574575
scene
575576
.ucmd()
576577
.arg("-alR")
577-
.arg(format!("/dev/fd/{fd2}"))
578+
.arg(format!("/dev/fd/{}", fd2.as_raw_fd()))
578579
.succeeds();
579580

580581
scene
581582
.ucmd()
582583
.arg("-RiL")
583-
.arg(format!("/dev/fd/{fd2}"))
584+
.arg(format!("/dev/fd/{}", fd2.as_raw_fd()))
584585
.succeeds();
585586
}
586587

587588
scene
588589
.ucmd()
589590
.arg("-alL")
590-
.arg(format!("/dev/fd/{fd2}"))
591+
.arg(format!("/dev/fd/{}", fd2.as_raw_fd()))
591592
.succeeds();
592593

593594
let _ = close(fd2);

0 commit comments

Comments
 (0)