head: fix -c0 and -n0 on directories to match GNU behavior#12217
Conversation
|
GNU testsuite comparison: |
191d756 to
4030cc2
Compare
|
I don't know if you have seen it: the new tests fail on Windows :| |
4030cc2 to
1bed4e9
Compare
| .no_stdout() | ||
| .no_stderr(); |
There was a problem hiding this comment.
A detail:
| .no_stdout() | |
| .no_stderr(); | |
| .no_output(); |
| .no_stdout() | ||
| .no_stderr(); |
There was a problem hiding this comment.
Same here:
| .no_stdout() | |
| .no_stderr(); | |
| .no_output(); |
| // fails with "Permission denied"). | ||
| let zero_output = matches!(options.mode, Mode::FirstBytes(0) | Mode::FirstLines(0)); | ||
| let is_dir = Path::new(file).is_dir(); | ||
| if is_dir && !zero_output { |
There was a problem hiding this comment.
I would move the !zero_output check into the block. Something like:
if is_dir {
if !zero_output {
// here comes the show! call
}
continue;
}It allows you to get rid of the ifs on line 464 and 485.
There was a problem hiding this comment.
Wait, this is not correct, at least if we want the same behavior as GNU head :| If there are multiple directories and a non-zero input, GNU head shows the directories on stdout whereas we don't:
$ head -c 1 . .
==> . <==
head: error reading '.': Is a directory
==> . <==
head: error reading '.': Is a directory
$ cargo run -q head -c 1 . .
head: error reading '.': Is a directory
head: error reading '.': Is a directory
| 1, | ||
| translate!("head-error-reading-file", "name" => file.quote(), "err" => "Is a directory") | ||
| )); | ||
| continue; |
There was a problem hiding this comment.
I think this continue is incorrect, see my previous comment.
| continue; |
When zero bytes or zero lines are requested, there is nothing to read, so we should not check whether the path is a directory. GNU head succeeds in this case because it never attempts to open/read the file. Previously, uutils head would stat the file and reject directories even when no reading was needed, causing a spurious 'Is a directory' error. Fixes #12215
1bed4e9 to
82ce536
Compare
|
It is a bit strange to write |
When zero bytes or zero lines are requested, there is nothing to read, so we should not check whether the path is a directory. GNU head succeeds in this case because it never attempts to open/read the file.
Previously, uutils head would stat the file and reject directories even when no reading was needed, causing a spurious 'Is a directory' error.
Fixes #12215
Changes
is_dir()check when-c0or-n0is specifiedtest_zero_bytes_on_directory_succeedsandtest_zero_lines_on_directory_succeeds