stat: shell-escape control chars in %N output (#9925)#12403
Open
jamesarch wants to merge 1 commit into
Open
Conversation
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>
e88dd21 to
7ef6ac2
Compare
|
GNU testsuite comparison: |
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 `%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 in
stat::quote_file_nameto uucore's shell-escapeimplementation (
uucore::quoting_style::locale_aware_escape_name),which already handles control characters correctly. The local
QuotingStyleenum stays — it's still used to parse theQUOTING_STYLEenv var — but the actual escape work now flows throughuucore. Same code path that
ls --quoting-style=shell-escapeuses, sostat -c %Noutput is now consistent withls.Cargo.toml: enables the
quoting-styleuucore feature foruu_stat.Side effect
QUOTING_STYLE=localeon a file named exactly `'` now emits `"'"`(matching
lsand GNU's locale style in the C locale) instead of theprevious custom `'\''`.
test_quoting_style_localeupdated toreflect this; the change makes stat's behavior consistent with the
other utilities.
Tests
Added:
Updated:
🤖 Generated with Claude Code