Skip to content

Commit 19a19f0

Browse files
authored
Merge pull request #6854 from sylvestre/fmt
fmt: generate an error if the input is a directory
2 parents bf05a3d + ab77eae commit 19a19f0

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/uu/fmt/src/fmt.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ fn process_file(
189189
_ => {
190190
let f = File::open(file_name)
191191
.map_err_context(|| format!("cannot open {} for reading", file_name.quote()))?;
192+
if f.metadata()
193+
.map_err_context(|| format!("cannot get metadata for {}", file_name.quote()))?
194+
.is_dir()
195+
{
196+
return Err(USimpleError::new(1, "read error".to_string()));
197+
}
198+
192199
Box::new(f) as Box<dyn Read + 'static>
193200
}
194201
});

tests/by-util/test_fmt.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ fn test_invalid_arg() {
99
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
1010
}
1111

12+
#[test]
13+
fn test_invalid_input() {
14+
new_ucmd!().arg(".").fails().code_is(1);
15+
}
16+
1217
#[test]
1318
fn test_fmt() {
1419
new_ucmd!()

0 commit comments

Comments
 (0)