diff --git a/src/uu/fmt/locales/en-US.ftl b/src/uu/fmt/locales/en-US.ftl index a65fd392411..16013f6751e 100644 --- a/src/uu/fmt/locales/en-US.ftl +++ b/src/uu/fmt/locales/en-US.ftl @@ -28,6 +28,5 @@ fmt-error-first-option-width = invalid option -- {$option}; -WIDTH is recognized fmt-error-reading-is-directory = error reading {$file}: Is a directory fmt-error-invalid-width-malformed = invalid width: {$width} fmt-error-cannot-open-for-reading = cannot open {$file} for reading -fmt-error-cannot-get-metadata = cannot get metadata for {$file} fmt-error-failed-to-write-output = failed to write output fmt-error-read = error reading {$file} diff --git a/src/uu/fmt/locales/fr-FR.ftl b/src/uu/fmt/locales/fr-FR.ftl index 7b4763011aa..e457366e88c 100644 --- a/src/uu/fmt/locales/fr-FR.ftl +++ b/src/uu/fmt/locales/fr-FR.ftl @@ -28,6 +28,5 @@ fmt-error-first-option-width = option invalide -- {$option} ; -WIDTH n'est recon fmt-error-reading-is-directory = erreur de lecture de {$file} : C'est un répertoire fmt-error-invalid-width-malformed = largeur invalide : {$width} fmt-error-cannot-open-for-reading = impossible d'ouvrir {$file} en lecture -fmt-error-cannot-get-metadata = impossible d'obtenir les métadonnées pour {$file} fmt-error-failed-to-write-output = échec de l'écriture de sortie fmt-error-read = erreur de lecture de {$file} diff --git a/src/uu/fmt/src/fmt.rs b/src/uu/fmt/src/fmt.rs index fde4db409ba..d2629bf0959 100644 --- a/src/uu/fmt/src/fmt.rs +++ b/src/uu/fmt/src/fmt.rs @@ -203,8 +203,8 @@ fn process_file( fmt_opts: &FmtOptions, ostream: &mut BufWriter, ) -> UResult<()> { - let mut fp = BufReader::new(if file_name == "-" { - Box::new(stdin()) as Box + let mut fp = FileOrStdReader::new(if file_name == "-" { + Box::new(stdin()) } else { let path = Path::new(file_name); // Like GNU fmt, report a file that can't be read and go on with the next one. @@ -217,24 +217,25 @@ fn process_file( return Ok(()); } }; - match f.metadata() { - Ok(m) if m.is_dir() => { - show!(USimpleError::new( - 1, - translate!("fmt-error-reading-is-directory", "file" => path.quote()) - )); - return Ok(()); - } - Ok(_) => {} - Err(e) => { - show!(e.map_err_context( - || translate!("fmt-error-cannot-get-metadata", "file" => path.quote()) - )); - return Ok(()); - } + // A read may succeed on a UFS directory on DragonFly BSD and NetBSD. + // FreeBSD also permits this on supported filesystems for root when + // security.bsd.allow_read_dir is enabled (see read(2)). Wasmtime's + // WASI Preview 1 fd_read can return EBADF for a directory. Check the + // file type here so fmt consistently reports "Is a directory". + #[cfg(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + all(target_os = "wasi", target_env = "p1") + ))] + if f.metadata().is_ok_and(|m| m.is_dir()) { + show!(USimpleError::new( + 1, + translate!("fmt-error-reading-is-directory", "file" => path.quote()) + )); + return Ok(()); } - - Box::new(f) as Box + Box::new(f) }); let p_stream = ParagraphStream::new(fmt_opts, &mut fp); diff --git a/tests/by-util/test_fmt.rs b/tests/by-util/test_fmt.rs index e44f8544b95..6bb5aed1b7b 100644 --- a/tests/by-util/test_fmt.rs +++ b/tests/by-util/test_fmt.rs @@ -301,6 +301,42 @@ fn test_fmt_continues_after_directory() { ); } +#[cfg(target_os = "linux")] +#[uutests::wasi_ignore(NoSubprocessSpawning)] +#[test] +fn test_fmt_reads_input_when_stat_fails() { + use std::process::Command; + + let (at, scene) = uutests::at_and_ts!(); + let input = at.plus_as_string("one-word-per-line.txt"); + let trace_args = [ + "-o", + "/dev/null", + "-e", + "inject=%%stat:error=EOVERFLOW", + "-P", + &input, + ]; + + if !Command::new("strace") + .args(trace_args) + .arg("true") + .output() + .is_ok_and(|probe| probe.status.success()) + { + eprintln!("skipping: strace fault injection is unavailable"); + return; + } + + scene + .cmd("strace") + .args(&trace_args) + .arg(&scene.bin_path) + .args(&["fmt", &input]) + .succeeds() + .stdout_only("this is a file with one word per line\n"); +} + #[test] fn test_fmt_continues_after_missing_file() { let (at, mut ucmd) = at_and_ucmd!();