stat: shell-escape control chars in %N output (#9925)#12403
Open
jamesarch wants to merge 1 commit into
Open
Conversation
e88dd21 to
7ef6ac2
Compare
|
GNU testsuite comparison: |
GNU stat encodes control characters in file names using bash
`$'\\X'` shell-escape sequences inside the `%N` directive:
$ touch $'/tmp/test\nnewline'
$ /usr/bin/stat -c '{"name":"%N"}' $'/tmp/test\nnewline'
{"name":"'/tmp/test'$'\\n''newline'"}
uutils was emitting the newline byte literally between single quotes,
producing invalid shell-quoted output and breaking JSON / log
consumers that parse the result:
{"name":"'/tmp/test
newline'"}
Fix: delegate the quoting to uucore's shell-escape implementation,
which already knows how to encode control characters. The local
`QuotingStyle` enum stays (it's used to parse the `QUOTING_STYLE`
env var), but the actual escape work now flows through
`uucore::quoting_style::locale_aware_escape_name`. Same code path
that `ls --quoting-style=shell-escape` uses, so `stat -c %N`
output is now consistent with `ls`.
Side effect: `QUOTING_STYLE=locale` on a file named exactly `'` now
emits `"'"` (matching `ls` and GNU's locale style in C locale)
instead of the previous custom `'\''`. `test_quoting_style_locale`
updated to reflect this.
Tests added:
- `test_format_n_handles_newline` — exact GNU output for newline.
- `test_format_n_handles_tab` — tab sanity check.
Closes uutils#9925.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
7ef6ac2 to
9ba91c2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #9925.
Problem
GNU
statencodes control characters in file names using bash$'\X'shell-escape sequences in the%Ndirective:uutils was emitting the newline byte literally between single quotes,
producing invalid shell-quoted output and breaking JSON / log consumers
that parse the result:
Fix
For the default
%Nquoting style (ShellEscapeAlways), delegate touucore's shell-escape implementation
(
uucore::quoting_style::locale_aware_escape_name), which alreadyencodes control characters as
$'\X'sequences. Same code path thatls --quoting-style=shell-escapeuses, sostat -c %Noutput is nowconsistent with
ls.QUOTING_STYLE=localeand=shellkeep their previous backslash-escapebehavior so that GNU's
tests/stat/stat-fmt.sh(which asserts'\''for a
'file underlocale) still passes.Cargo.toml: enables thequoting-styleuucore feature foruu_stat.Tests
Added:
test_format_n_handles_newline— exact GNU output for newline.test_format_n_handles_tab— tab sanity check.test_quoting_style_localecontinues to pass unchanged.GNU compatibility:
tests/stat/stat-fmt.shcontinues to pass.🤖 Generated with Claude Code