Skip to content

Commit 93432cb

Browse files
committed
refactor(sync): remove unsafe libc::open/close, use safe rustix::fs::open
1 parent a26f0a7 commit 93432cb

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

src/uu/sync/src/sync.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
/* Last synced with: sync (GNU coreutils) 8.13 */
77

88
use clap::{Arg, ArgAction, Command};
9-
#[cfg(any(target_os = "linux", target_os = "android"))]
10-
use nix::errno::Errno;
11-
#[cfg(any(target_os = "linux", target_os = "android"))]
12-
use nix::fcntl::{OFlag, open};
13-
#[cfg(any(target_os = "linux", target_os = "android"))]
14-
use nix::sys::stat::Mode;
159
use std::path::Path;
1610
use uucore::display::Quotable;
1711
use uucore::error::{UResult, USimpleError, get_exit_code, set_exit_code};
@@ -235,13 +229,22 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
235229
#[cfg(any(target_os = "linux", target_os = "android"))]
236230
{
237231
let path = Path::new(&f);
238-
if let Err(e) = open(path, OFlag::O_NONBLOCK, Mode::empty()) {
239-
if e != Errno::EACCES || (e == Errno::EACCES && path.is_dir()) {
240-
show_error!(
241-
"{}",
242-
translate!("sync-error-opening-file", "file" => f.quote(), "err" => e.desc())
243-
);
244-
set_exit_code(1);
232+
match rustix::fs::open(
233+
path,
234+
rustix::fs::OFlags::NONBLOCK,
235+
rustix::fs::Mode::empty(),
236+
) {
237+
Ok(_fd) => { /* OwnedFd auto-closes on drop */ }
238+
Err(e) => {
239+
let is_eacces = e == rustix::io::Errno::ACCESS;
240+
if !is_eacces || path.is_dir() {
241+
let err = std::io::Error::from(e);
242+
show_error!(
243+
"{}",
244+
translate!("sync-error-opening-file", "file" => f.quote(), "err" => err)
245+
);
246+
set_exit_code(1);
247+
}
245248
}
246249
}
247250
}

0 commit comments

Comments
 (0)