From 0bcc6a43e0807c064733cd3e364e23b3f3a1948a Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 28 Mar 2026 13:48:57 +0100 Subject: [PATCH] refactor(sync): remove unsafe libc::open/close, use safe rustix::fs::open --- Cargo.lock | 1 + src/uu/sync/Cargo.toml | 1 + src/uu/sync/src/sync.rs | 29 ++++++++++++++++------------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 399fae1c8..df3650107 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4224,6 +4224,7 @@ dependencies = [ "clap", "fluent", "nix", + "rustix", "uucore", "windows-sys 0.61.2", ] diff --git a/src/uu/sync/Cargo.toml b/src/uu/sync/Cargo.toml index 2baad4931..39f86a92c 100644 --- a/src/uu/sync/Cargo.toml +++ b/src/uu/sync/Cargo.toml @@ -26,6 +26,7 @@ fluent = { workspace = true } [target.'cfg(unix)'.dependencies] nix = { workspace = true } +rustix = { workspace = true, features = ["fs"] } [target.'cfg(target_os = "windows")'.dependencies] windows-sys = { workspace = true, features = [ diff --git a/src/uu/sync/src/sync.rs b/src/uu/sync/src/sync.rs index 07f47fc4f..44b823426 100644 --- a/src/uu/sync/src/sync.rs +++ b/src/uu/sync/src/sync.rs @@ -6,12 +6,6 @@ /* Last synced with: sync (GNU coreutils) 8.13 */ use clap::{Arg, ArgAction, Command}; -#[cfg(any(target_os = "linux", target_os = "android"))] -use nix::errno::Errno; -#[cfg(any(target_os = "linux", target_os = "android"))] -use nix::fcntl::{OFlag, open}; -#[cfg(any(target_os = "linux", target_os = "android"))] -use nix::sys::stat::Mode; use std::path::Path; use uucore::display::Quotable; use uucore::error::{UResult, USimpleError, get_exit_code, set_exit_code}; @@ -237,13 +231,22 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { #[cfg(any(target_os = "linux", target_os = "android"))] { let path = Path::new(&f); - if let Err(e) = open(path, OFlag::O_NONBLOCK, Mode::empty()) { - if e != Errno::EACCES || (e == Errno::EACCES && path.is_dir()) { - show_error!( - "{}", - translate!("sync-error-opening-file", "file" => f.quote(), "err" => e.desc()) - ); - set_exit_code(1); + match rustix::fs::open( + path, + rustix::fs::OFlags::NONBLOCK, + rustix::fs::Mode::empty(), + ) { + Ok(_fd) => { /* OwnedFd auto-closes on drop */ } + Err(e) => { + let is_eacces = e == rustix::io::Errno::ACCESS; + if !is_eacces || path.is_dir() { + let err = std::io::Error::from(e); + show_error!( + "{}", + translate!("sync-error-opening-file", "file" => f.quote(), "err" => err) + ); + set_exit_code(1); + } } } }