Skip to content
Open
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
1 change: 0 additions & 1 deletion src/uu/fmt/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
1 change: 0 additions & 1 deletion src/uu/fmt/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
39 changes: 20 additions & 19 deletions src/uu/fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ fn process_file(
fmt_opts: &FmtOptions,
ostream: &mut BufWriter<Stdout>,
) -> UResult<()> {
let mut fp = BufReader::new(if file_name == "-" {
Box::new(stdin()) as Box<dyn Read + 'static>
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.
Expand All @@ -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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how was this list picked? was it checked on the freebsd CI job?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the FreeBSD Tests job passed on 6c81de6: https://github.com/uutils/coreutils/actions/runs/46557164454/job/142335034390

DragonFlyBSD and NetBSD are on the list because their UFS implementations can allow directory reads. FreeBSD also allows them on supported filesystems when running as root with security.bsd.allow_read_dir enabled. The CI job runs as a regular user, so it covers the normal FreeBSD case. WASI Preview 1 is included because Wasmtime can return EBADF when reading a directory. In each case, the check lets fmt report “Is a directory.”

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<dyn Read + 'static>
Box::new(f)
});

let p_stream = ParagraphStream::new(fmt_opts, &mut fp);
Expand Down
36 changes: 36 additions & 0 deletions tests/by-util/test_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand Down
Loading