Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix glob expansion that includes broken symlinks #1567

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions 0.19.0-release-notes.md
Expand Up @@ -21,6 +21,9 @@ and compiled, even if it is not executed:
- Temporary assignment on an unset environment variables no longer leave it
set to an empty string ([#1448](https://b.elv.sh/1448)).

- Glob expansion that includes broken symlinks no longer short-circuits the
expansion ([#1240](https://b.elv.sh/1240)).
Comment on lines +24 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fix but it's also a breaking change - symlinks to directories will now be treated as files, not directories. This breaking change is fine for me - among other things it avoids infinite recursions when expanding ** symlinks introduce loops in the FS tree. But it should be documented as such.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another good point but the existing behavior is flat out wrong and it's unclear how to document the change in behavior.


# Notable new features

- A new `inexact-num` converts its argument to an inexact number.
Expand Down
8 changes: 4 additions & 4 deletions pkg/glob/glob.go
Expand Up @@ -72,19 +72,19 @@ func glob(segs []Segment, dir string, cb func(PathInfo) bool) bool {
elem := segs[0].(Literal).Data
segs = segs[2:]
dir += elem + "/"
if info, err := os.Stat(dir); err != nil || !info.IsDir() {
if info, err := os.Lstat(dir); err != nil || !info.IsDir() {
return true
}
}

if len(segs) == 0 {
if info, err := os.Stat(dir); err == nil {
if info, err := os.Lstat(dir); err == nil {
return cb(PathInfo{dir, info})
}
return true
} else if len(segs) == 1 && IsLiteral(segs[0]) {
path := dir + segs[0].(Literal).Data
if info, err := os.Stat(path); err == nil {
if info, err := os.Lstat(path); err == nil {
return cb(PathInfo{path, info})
}
return true
Expand Down Expand Up @@ -161,7 +161,7 @@ func glob(segs []Segment, dir string, cb func(PathInfo) bool) bool {
name := info.Name()
if matchElement(segs, name) {
dirname := dir + name
info, err := os.Stat(dirname)
info, err := os.Lstat(dirname)
if err != nil {
return true
}
Expand Down
29 changes: 23 additions & 6 deletions pkg/glob/glob_test.go
Expand Up @@ -20,6 +20,15 @@ var (
"lorem", "ipsum",
"d1/e/f/g/X", "d2/e/f/g/X"}
createDots = []string{".x", ".el/x"}
symlinks = []struct {
path string
target string
}{
{"d1/s-f", "f"},
{"s-d", "d2"},
{"s-d-f", "d1/f"},
{"s-bad", "bad"},
}
)

type globCase struct {
Expand All @@ -28,22 +37,21 @@ type globCase struct {
}

var globCases = []globCase{
{"*", []string{"a", "b", "c", "d1", "d2", "dX", "dXY", "lorem", "ipsum"}},
{"*", []string{"a", "b", "c", "d1", "d2", "dX", "dXY", "lorem", "ipsum", "s-bad", "s-d", "s-d-f"}},
{".", []string{"."}},
{"./*", []string{"./a", "./b", "./c", "./d1", "./d2", "./dX", "./dXY", "./lorem", "./ipsum"}},
{"./*", []string{"./a", "./b", "./c", "./d1", "./d2", "./dX", "./dXY", "./lorem", "./ipsum", "./s-bad", "./s-d", "./s-d-f"}},
{"..", []string{".."}},
{"a/..", []string{"a/.."}},
{"a/../*", []string{"a/../a", "a/../b", "a/../c", "a/../d1", "a/../d2", "a/../dX", "a/../dXY", "a/../lorem", "a/../ipsum"}},
{"a/../*", []string{"a/../a", "a/../b", "a/../c", "a/../d1", "a/../d2", "a/../dX", "a/../dXY", "a/../ipsum", "a/../lorem", "a/../s-bad", "a/../s-d", "a/../s-d-f"}},
{"*/", []string{"a/", "b/", "c/", "d1/", "d2/"}},
{"**", append(mkdirs, creates...)},
{"**", []string{"a", "a/X", "a/Y", "b", "b/X", "c", "c/Y", "d1", "d1/e", "d1/e/f", "d1/e/f/g", "d1/e/f/g/X", "d1/s-f", "d2", "d2/e", "d2/e/f", "d2/e/f/g", "d2/e/f/g/X", "dX", "dXY", "ipsum", "lorem", "s-bad", "s-d", "s-d-f"}},
{"*/X", []string{"a/X", "b/X"}},
{"**X", []string{"a/X", "b/X", "dX", "d1/e/f/g/X", "d2/e/f/g/X"}},
{"*/*/*", []string{"d1/e/f", "d2/e/f"}},
{"l*m", []string{"lorem"}},
{"d*", []string{"d1", "d2", "dX", "dXY"}},
{"d*/", []string{"d1/", "d2/"}},
{"d**", []string{"d1", "d1/e", "d1/e/f", "d1/e/f/g", "d1/e/f/g/X",
"d2", "d2/e", "d2/e/f", "d2/e/f/g", "d2/e/f/g/X", "dX", "dXY"}},
{"d**", []string{"d1", "d1/e", "d1/e/f", "d1/e/f/g", "d1/e/f/g/X", "d1/s-f", "d2", "d2/e", "d2/e/f", "d2/e/f/g", "d2/e/f/g/X", "dX", "dXY"}},
{"?", []string{"a", "b", "c"}},
{"??", []string{"d1", "d2", "dX"}},

Expand Down Expand Up @@ -80,6 +88,15 @@ func testGlob(t *testing.T, abs bool) {
}
f.Close()
}
for _, link := range symlinks {
err := os.Symlink(link.target, link.path)
if err != nil {
// Creating symlinks requires a special permission on Windows. If
// the user doesn't have that permission, just skip the whole test.
t.Skip(err)
Comment on lines +94 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skipping the entire test makes sense in the TestPath_Symlink function this is borrowed from, because that entire test is about symlinks. It makes much less sense here, it makes a large part of the globbing code untested on Windows. A better strategy is to just create the symlinks as ordinary files on Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I would argue that both sets of tests should fail if unable to create a symlink on Windows. The CI environment should always have that capability enabled. And anyone running these tests on their local system should also have the capability enabled. The question is whether, and how, to test the behavior on Windows when the symlink capability isn't enabled.

}
}

for _, tc := range globCases {
pattern := tc.pattern
if abs {
Expand Down