Skip to content

Commit e46bfa9

Browse files
sylvestreoech3
authored andcommitted
sync: replace nix by rustix
1 parent acea804 commit e46bfa9

3 files changed

Lines changed: 37 additions & 31 deletions

File tree

Cargo.lock

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

src/uu/sync/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ uucore = { workspace = true, features = ["wide"] }
2424
fluent = { workspace = true }
2525

2626
[target.'cfg(unix)'.dependencies]
27-
nix = { workspace = true }
27+
libc = { workspace = true }
28+
rustix = { workspace = true, features = ["fs"] }
2829

2930
[target.'cfg(target_os = "windows")'.dependencies]
3031
windows-sys = { workspace = true, features = [

src/uu/sync/src/sync.rs

Lines changed: 33 additions & 29 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};
@@ -28,14 +22,11 @@ static ARG_FILES: &str = "files";
2822

2923
#[cfg(unix)]
3024
mod platform {
31-
#[cfg(any(target_os = "linux", target_os = "android"))]
32-
use nix::fcntl::{FcntlArg, OFlag, fcntl};
33-
use nix::unistd::sync;
34-
#[cfg(any(target_os = "linux", target_os = "android"))]
35-
use nix::unistd::{fdatasync, syncfs};
3625
#[cfg(any(target_os = "linux", target_os = "android"))]
3726
use std::fs::{File, OpenOptions};
3827
#[cfg(any(target_os = "linux", target_os = "android"))]
28+
use std::os::fd::AsFd;
29+
#[cfg(any(target_os = "linux", target_os = "android"))]
3930
use std::os::unix::fs::OpenOptionsExt;
4031
#[cfg(any(target_os = "linux", target_os = "android"))]
4132
use uucore::display::Quotable;
@@ -51,7 +42,7 @@ mod platform {
5142
reason = "fn sig must match on all platforms"
5243
)]
5344
pub fn do_sync() -> UResult<()> {
54-
sync();
45+
rustix::fs::sync();
5546
Ok(())
5647
}
5748

@@ -62,17 +53,17 @@ mod platform {
6253
fn open_and_reset_nonblock(path: &str) -> UResult<File> {
6354
let f = OpenOptions::new()
6455
.read(true)
65-
.custom_flags(OFlag::O_NONBLOCK.bits())
56+
.custom_flags(libc::O_NONBLOCK)
6657
.open(path)
6758
.map_err_context(|| path.to_string())?;
6859
// Reset O_NONBLOCK flag if it was set (matches GNU behavior)
6960
// This is non-critical, so we log errors but don't fail
70-
if let Err(e) = fcntl(&f, FcntlArg::F_SETFL(OFlag::empty())) {
61+
if let Err(e) = rustix::fs::fcntl_setfl(f.as_fd(), rustix::fs::OFlags::empty()) {
7162
use std::io::{Write, stderr};
7263
let _ = writeln!(
7364
stderr(),
7465
"sync: {}",
75-
translate!("sync-warning-fcntl-failed", "file" => path, "error" => e.to_string())
66+
translate!("sync-warning-fcntl-failed", "file" => path, "error" => std::io::Error::from(e).to_string())
7667
);
7768
uucore::error::set_exit_code(1);
7869
}
@@ -83,9 +74,11 @@ mod platform {
8374
pub fn do_syncfs(files: Vec<String>) -> UResult<()> {
8475
for path in files {
8576
let f = open_and_reset_nonblock(&path)?;
86-
syncfs(f).map_err_context(
87-
|| translate!("sync-error-syncing-file", "file" => path.quote()),
88-
)?;
77+
rustix::fs::syncfs(f.as_fd())
78+
.map_err(std::io::Error::from)
79+
.map_err_context(
80+
|| translate!("sync-error-syncing-file", "file" => path.quote()),
81+
)?;
8982
}
9083
Ok(())
9184
}
@@ -94,9 +87,11 @@ mod platform {
9487
pub fn do_fdatasync(files: Vec<String>) -> UResult<()> {
9588
for path in files {
9689
let f = open_and_reset_nonblock(&path)?;
97-
fdatasync(f).map_err_context(
98-
|| translate!("sync-error-syncing-file", "file" => path.quote()),
99-
)?;
90+
rustix::fs::fdatasync(f.as_fd())
91+
.map_err(std::io::Error::from)
92+
.map_err_context(
93+
|| translate!("sync-error-syncing-file", "file" => path.quote()),
94+
)?;
10095
}
10196
Ok(())
10297
}
@@ -231,17 +226,26 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
231226
}
232227

233228
for f in &files {
234-
// Use the Nix open to be able to set the NONBLOCK flags for fifo files
229+
// Use open with O_NONBLOCK to be able to handle fifo files
235230
#[cfg(any(target_os = "linux", target_os = "android"))]
236231
{
237232
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);
233+
match rustix::fs::open(
234+
path,
235+
rustix::fs::OFlags::NONBLOCK,
236+
rustix::fs::Mode::empty(),
237+
) {
238+
Ok(_fd) => { /* OwnedFd auto-closes on drop */ }
239+
Err(e) => {
240+
let is_eacces = e == rustix::io::Errno::ACCESS;
241+
if !is_eacces || path.is_dir() {
242+
let err = std::io::Error::from(e);
243+
show_error!(
244+
"{}",
245+
translate!("sync-error-opening-file", "file" => f.quote(), "err" => err)
246+
);
247+
set_exit_code(1);
248+
}
245249
}
246250
}
247251
}

0 commit comments

Comments
 (0)