Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/uu/sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ uucore = { workspace = true, features = ["wide"] }
fluent = { workspace = true }

[target.'cfg(unix)'.dependencies]
nix = { workspace = true }
libc = { workspace = true }
rustix = { workspace = true, features = ["fs"] }

[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { workspace = true, features = [
Expand Down
62 changes: 33 additions & 29 deletions src/uu/sync/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -28,14 +22,11 @@ static ARG_FILES: &str = "files";

#[cfg(unix)]
mod platform {
#[cfg(any(target_os = "linux", target_os = "android"))]
use nix::fcntl::{FcntlArg, OFlag, fcntl};
use nix::unistd::sync;
#[cfg(any(target_os = "linux", target_os = "android"))]
use nix::unistd::{fdatasync, syncfs};
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::fs::{File, OpenOptions};
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::os::fd::AsFd;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::os::unix::fs::OpenOptionsExt;
#[cfg(any(target_os = "linux", target_os = "android"))]
use uucore::display::Quotable;
Expand All @@ -51,7 +42,7 @@ mod platform {
reason = "fn sig must match on all platforms"
)]
pub fn do_sync() -> UResult<()> {
sync();
rustix::fs::sync();
Ok(())
}

Expand All @@ -62,17 +53,17 @@ mod platform {
fn open_and_reset_nonblock(path: &str) -> UResult<File> {
let f = OpenOptions::new()
.read(true)
.custom_flags(OFlag::O_NONBLOCK.bits())
.custom_flags(libc::O_NONBLOCK)
.open(path)
.map_err_context(|| path.to_string())?;
// Reset O_NONBLOCK flag if it was set (matches GNU behavior)
// This is non-critical, so we log errors but don't fail
if let Err(e) = fcntl(&f, FcntlArg::F_SETFL(OFlag::empty())) {
if let Err(e) = rustix::fs::fcntl_setfl(f.as_fd(), rustix::fs::OFlags::empty()) {
use std::io::{Write, stderr};
let _ = writeln!(
stderr(),
"sync: {}",
translate!("sync-warning-fcntl-failed", "file" => path, "error" => e.to_string())
translate!("sync-warning-fcntl-failed", "file" => path, "error" => std::io::Error::from(e).to_string())
);
uucore::error::set_exit_code(1);
}
Expand All @@ -83,9 +74,11 @@ mod platform {
pub fn do_syncfs(files: Vec<String>) -> UResult<()> {
for path in files {
let f = open_and_reset_nonblock(&path)?;
syncfs(f).map_err_context(
|| translate!("sync-error-syncing-file", "file" => path.quote()),
)?;
rustix::fs::syncfs(f.as_fd())
.map_err(std::io::Error::from)
.map_err_context(
|| translate!("sync-error-syncing-file", "file" => path.quote()),
)?;
}
Ok(())
}
Expand All @@ -94,9 +87,11 @@ mod platform {
pub fn do_fdatasync(files: Vec<String>) -> UResult<()> {
for path in files {
let f = open_and_reset_nonblock(&path)?;
fdatasync(f).map_err_context(
|| translate!("sync-error-syncing-file", "file" => path.quote()),
)?;
rustix::fs::fdatasync(f.as_fd())
.map_err(std::io::Error::from)
.map_err_context(
|| translate!("sync-error-syncing-file", "file" => path.quote()),
)?;
}
Ok(())
}
Expand Down Expand Up @@ -231,17 +226,26 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}

for f in &files {
// Use the Nix open to be able to set the NONBLOCK flags for fifo files
// Use open with O_NONBLOCK to be able to handle fifo files
#[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);
}
}
}
}
Expand Down
Loading