1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #3592 from anastygnome/fork

Implement the --sync flag for df. (fixes #3564)
This commit is contained in:
Sylvestre Ledru 2022-06-06 10:05:51 +02:00 committed by GitHub
commit 63347abd19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View file

@ -87,6 +87,9 @@ struct Options {
/// types will *not* be listed. /// types will *not* be listed.
exclude: Option<Vec<String>>, exclude: Option<Vec<String>>,
/// Whether to sync before operating.
sync: bool,
/// Whether to show a final row comprising the totals for each column. /// Whether to show a final row comprising the totals for each column.
show_total: bool, show_total: bool,
@ -104,6 +107,7 @@ impl Default for Options {
header_mode: Default::default(), header_mode: Default::default(),
include: Default::default(), include: Default::default(),
exclude: Default::default(), exclude: Default::default(),
sync: Default::default(),
show_total: Default::default(), show_total: Default::default(),
columns: vec![ columns: vec![
Column::Source, Column::Source,
@ -178,6 +182,7 @@ impl Options {
Ok(Self { Ok(Self {
show_local_fs: matches.is_present(OPT_LOCAL), show_local_fs: matches.is_present(OPT_LOCAL),
show_all_fs: matches.is_present(OPT_ALL), show_all_fs: matches.is_present(OPT_ALL),
sync: matches.is_present(OPT_SYNC),
block_size: read_block_size(matches).map_err(|e| match e { block_size: read_block_size(matches).map_err(|e| match e {
ParseSizeError::InvalidSuffix(s) => OptionsError::InvalidSuffix(s), ParseSizeError::InvalidSuffix(s) => OptionsError::InvalidSuffix(s),
ParseSizeError::SizeTooBig(_) => OptionsError::BlockSizeTooLarge( ParseSizeError::SizeTooBig(_) => OptionsError::BlockSizeTooLarge(
@ -325,9 +330,21 @@ fn filter_mount_list(vmi: Vec<MountInfo>, opt: &Options) -> Vec<MountInfo> {
/// Get all currently mounted filesystems. /// Get all currently mounted filesystems.
/// ///
/// `opt` excludes certain filesystems from consideration; see /// `opt` excludes certain filesystems from consideration and allows for the synchronization of filesystems before running; see
/// [`Options`] for more information. /// [`Options`] for more information.
fn get_all_filesystems(opt: &Options) -> Vec<Filesystem> { fn get_all_filesystems(opt: &Options) -> Vec<Filesystem> {
// Run a sync call before any operation if so instructed.
if opt.sync {
#[cfg(not(windows))]
unsafe {
#[cfg(not(target_os = "android"))]
uucore::libc::sync();
#[cfg(target_os = "android")]
uucore::libc::syscall(uucore::libc::SYS_sync);
}
}
// The list of all mounted filesystems. // The list of all mounted filesystems.
// //
// Filesystems excluded by the command-line options are // Filesystems excluded by the command-line options are
@ -559,7 +576,7 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(OPT_SYNC) Arg::new(OPT_SYNC)
.long("sync") .long("sync")
.overrides_with_all(&[OPT_NO_SYNC, OPT_SYNC]) .overrides_with_all(&[OPT_NO_SYNC, OPT_SYNC])
.help("invoke sync before getting usage info"), .help("invoke sync before getting usage info (non-windows only)"),
) )
.arg( .arg(
Arg::new(OPT_TYPE) Arg::new(OPT_TYPE)

View file

@ -28,6 +28,11 @@ fn test_df_compatible_si() {
new_ucmd!().arg("-aH").succeeds(); new_ucmd!().arg("-aH").succeeds();
} }
#[test]
fn test_df_compatible_sync() {
new_ucmd!().arg("--sync").succeeds();
}
#[test] #[test]
fn test_df_arguments_override_themselves() { fn test_df_arguments_override_themselves() {
new_ucmd!().args(&["--help", "--help"]).succeeds(); new_ucmd!().args(&["--help", "--help"]).succeeds();