Skip to content

find: escape unusual characters in -ls and -fls file names - #1072

Open
andretorresdg wants to merge 1 commit into
uutils:mainfrom
andretorresdg:ls-escape-names
Open

andretorresdg wants to merge 1 commit into
uutils:mainfrom
andretorresdg:ls-escape-names

Conversation

@andretorresdg

Copy link
Copy Markdown
Contributor

GNU find escapes the file name (and the symlink target) in -ls/-fls output, as described under "UNUSUAL FILENAMES" in find(1): white space, backslash and double quote get a C-style escape (\ , \t, \n, \\, \", ...), any other byte outside printable ASCII is written as a three-digit octal escape, and the rest is copied as is.

We printed the name unchanged through to_string_lossy, so a name containing a newline split one entry over two lines and invalid UTF-8 bytes became U+FFFD:

$ touch 'sp ace' "$(printf 'nl\nx')"; ln -s 'sp ace' link
$ find . -ls          # GNU 4.11 (name column only)
./nl\nx
./link -> sp\ ace
./sp\ ace
$ target/debug/find . -ls   # before
./nl
x
./link -> sp ace
./sp ace

The line is now built as bytes on Unix and the name and link target go through a small escaping helper. The non-Unix branch is left as is (escaping \ there would double every Windows path separator).

This is the same fix #935 attempted (closed by its author because it was written with GNU's source in view). This one is written only from find(1) and by comparing output with GNU find 4.11; it also adds the unit test for the escaping helper suggested in that review, covering \b, \r, \f and 0x7f.

Closes #1062

How I tested

  • New push_escaped_name_escapes_unusual_bytes unit test in ls.rs covering every branch of the helper.
  • New find_ls_escapes_unusual_characters test in tests/test_find.rs (fails before, passes after), covering -ls and -fls, control characters, "/\, invalid UTF-8, a non-ASCII UTF-8 name and a symlink target.
  • Compared -ls output with GNU find 4.11.0 for files named with every byte 0x01-0xff (except /): identical.
  • cargo test, cargo fmt --all -- --check, cargo clippy --all-targets -- -D warnings.

GNU find escapes the file name and symlink target in -ls/-fls output:
white space, backslash and double quote get a C-style escape (\ , \t,
\n, \\, \", ...), and other bytes outside printable ASCII are written
as three-digit octal escapes. We printed the name as is, through
to_string_lossy, so a name with a newline split the listing over two
lines and invalid UTF-8 bytes turned into U+FFFD.

Build the line as bytes and escape the name and link target the same
way on Unix.
@codspeed

codspeed Bot commented Nov 28, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 15 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 20 untouched benchmarks


Comparing andretorresdg:ls-escape-names (43d37f7) with main (0172cc6)

Open in CodSpeed

@codecov

codecov Bot commented Nov 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.44444% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.48%. Comparing base (0172cc6) to head (43d37f7).

Files with missing lines Patch % Lines
src/find/matchers/ls.rs 94.44% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1072      +/-   ##
==========================================
+ Coverage   92.44%   92.48%   +0.04%     
==========================================
  Files          38       38              
  Lines        8894     8920      +26     
  Branches      440      440              
==========================================
+ Hits         8222     8250      +28     
+ Misses        532      531       -1     
+ Partials      140      139       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

find: -ls / -fls print unusual characters in file names unescaped

1 participant