|
6 | 6 | /* Last synced with: sync (GNU coreutils) 8.13 */ |
7 | 7 |
|
8 | 8 | use clap::{Arg, ArgAction, Command}; |
9 | | -#[cfg(any(target_os = "linux", target_os = "android"))] |
10 | | -use std::ffi::CString; |
11 | 9 | use std::path::Path; |
12 | 10 | use uucore::display::Quotable; |
13 | 11 | use uucore::error::{UResult, USimpleError, get_exit_code, set_exit_code}; |
@@ -229,22 +227,23 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { |
229 | 227 | #[cfg(any(target_os = "linux", target_os = "android"))] |
230 | 228 | { |
231 | 229 | let path = Path::new(&f); |
232 | | - let c_path = CString::new(f.as_str()).unwrap(); |
233 | | - // SAFETY: c_path is a valid null-terminated C string |
234 | | - let fd = unsafe { libc::open(c_path.as_ptr(), libc::O_NONBLOCK) }; |
235 | | - if fd < 0 { |
236 | | - let err = std::io::Error::last_os_error(); |
237 | | - let is_eacces = err.raw_os_error() == Some(libc::EACCES); |
238 | | - if !is_eacces || path.is_dir() { |
239 | | - show_error!( |
240 | | - "{}", |
241 | | - translate!("sync-error-opening-file", "file" => f.quote(), "err" => err) |
242 | | - ); |
243 | | - set_exit_code(1); |
| 230 | + match rustix::fs::open( |
| 231 | + path, |
| 232 | + rustix::fs::OFlags::NONBLOCK, |
| 233 | + rustix::fs::Mode::empty(), |
| 234 | + ) { |
| 235 | + Ok(_fd) => { /* OwnedFd auto-closes on drop */ } |
| 236 | + Err(e) => { |
| 237 | + let is_eacces = e == rustix::io::Errno::ACCESS; |
| 238 | + if !is_eacces || path.is_dir() { |
| 239 | + let err = std::io::Error::from(e); |
| 240 | + show_error!( |
| 241 | + "{}", |
| 242 | + translate!("sync-error-opening-file", "file" => f.quote(), "err" => err) |
| 243 | + ); |
| 244 | + set_exit_code(1); |
| 245 | + } |
244 | 246 | } |
245 | | - } else { |
246 | | - // SAFETY: fd is a valid open file descriptor |
247 | | - unsafe { libc::close(fd) }; |
248 | 247 | } |
249 | 248 | } |
250 | 249 | #[cfg(not(any(target_os = "linux", target_os = "android")))] |
|
0 commit comments