From 66f0c32d8bf6568c16beb3d5427e9dc2503d7ac0 Mon Sep 17 00:00:00 2001 From: mattsu Date: Tue, 10 Mar 2026 20:28:12 +0900 Subject: [PATCH 1/3] refactor(df): replace unsafe libc sync with nix crate Replace the unsafe libc sync call with the nix crate's sync_filesystems function for safer filesystem synchronization. This change improves code safety by using a well-maintained Rust wrapper instead of direct unsafe libc calls. --- Cargo.lock | 1 + src/uu/df/Cargo.toml | 3 +++ src/uu/df/src/df.rs | 9 +++------ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 62277a370f8..20cc053047c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3367,6 +3367,7 @@ dependencies = [ "clap", "codspeed-divan-compat", "fluent", + "nix", "tempfile", "thiserror 2.0.18", "unicode-width 0.2.2", diff --git a/src/uu/df/Cargo.toml b/src/uu/df/Cargo.toml index 48b793145e0..9b99baa9287 100644 --- a/src/uu/df/Cargo.toml +++ b/src/uu/df/Cargo.toml @@ -25,6 +25,9 @@ unicode-width = { workspace = true } thiserror = { workspace = true } fluent = { workspace = true } +[target.'cfg(unix)'.dependencies] +nix = { workspace = true, features = ["fs"] } + [dev-dependencies] divan = { workspace = true } tempfile = { workspace = true } diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index e0133805f97..d213dbc1403 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -19,6 +19,8 @@ use uucore::translate; use uucore::{format_usage, show, show_warning}; use clap::{Arg, ArgAction, ArgMatches, Command, parser::ValueSource}; +#[cfg(not(any(windows, target_os = "redox")))] +use nix::unistd::sync as sync_filesystems; use std::ffi::OsString; use std::io::stdout; @@ -301,12 +303,7 @@ fn get_all_filesystems(opt: &Options) -> UResult> { // Run a sync call before any operation if so instructed. if opt.sync { #[cfg(not(any(windows, target_os = "redox")))] - unsafe { - #[cfg(not(target_os = "android"))] - uucore::libc::sync(); - #[cfg(target_os = "android")] - uucore::libc::syscall(uucore::libc::SYS_sync); - } + sync_filesystems(); } let mut mounts = vec![]; From 424d3171353173fa8a4aa6ead52b7d45ef50bea9 Mon Sep 17 00:00:00 2001 From: mattsu <35655889+mattsu2020@users.noreply.github.com> Date: Wed, 11 Mar 2026 08:06:19 +0900 Subject: [PATCH 2/3] Update src/uu/df/src/df.rs Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com> --- src/uu/df/src/df.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index d213dbc1403..2b87ea3c32f 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -303,7 +303,7 @@ fn get_all_filesystems(opt: &Options) -> UResult> { // Run a sync call before any operation if so instructed. if opt.sync { #[cfg(not(any(windows, target_os = "redox")))] - sync_filesystems(); + nix::unistd::sync(); } let mut mounts = vec![]; From b5b375073069b6505a5acb97080bfcf86ec5ff11 Mon Sep 17 00:00:00 2001 From: mattsu <35655889+mattsu2020@users.noreply.github.com> Date: Wed, 11 Mar 2026 08:20:45 +0900 Subject: [PATCH 3/3] Update src/uu/df/src/df.rs Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com> --- src/uu/df/src/df.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 2b87ea3c32f..51b950dd460 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -19,8 +19,6 @@ use uucore::translate; use uucore::{format_usage, show, show_warning}; use clap::{Arg, ArgAction, ArgMatches, Command, parser::ValueSource}; -#[cfg(not(any(windows, target_os = "redox")))] -use nix::unistd::sync as sync_filesystems; use std::ffi::OsString; use std::io::stdout;