elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.65k stars 299 forks source link

Don't stop glob expansion when path stat fails #1677

Closed krader1961 closed 1 year ago

krader1961 commented 1 year ago

Specifically, ignore EPERM errors returned by os.Lstat() since

a) that is not documented as a valid error on any system I currently have access to (which includes macOS, Linux, and FreeBSD), and

b) on macOS EPERM is only returned due to security restrictions on gathering information about the path and which should otherwise not short-circuit glob expansion by treating it as a fatal error.

We only care about EPERM if there is explicit filtering of the glob expansion; e.g., put **[type:regular]. Otherwise we should include the path name in the results and not prematurely terminate inclusion of glob matches.

Fixes #1674

xiaq commented 1 year ago

This fixes the very problem in #1674 but is problematic for a key reason: getting EPERM from Lstat does not mean that the file exists.

This is how Elvish behaves now:

~> mkdir a b
~> touch b/foo
~> chmod -x a
~> echo */foo
b/foo

But this PR will change the behavior of echo */foo to also output a/foo, because */ matches a/, and lstat'ing a/foo returns EPERM.

There's a much simpler way to fix the original issue, that is to simply change line 172 in glob/glob.go to continue. This results in glob expansions skipping files that cannot be lstat'ed, which actually matches what #1674 is proposing (ignoring those files).