Skip to content

Commit

Permalink
expand: fix globbing with symlinks
Browse files Browse the repository at this point in the history
This was causing a test to fail on Mac, since the temporary dir used
there was under /var, which is a symlink to a directory.

This does require an extra syscall in that edge case. We might get rid
of that at some point. For now, fix the bug.
  • Loading branch information
mvdan committed Dec 20, 2018
1 parent 13a9691 commit dffd4f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 12 additions & 2 deletions expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,20 @@ func (cfg *Config) globDir(base, dir string, rx *regexp.Regexp, wantDir bool, ma
return nil, err
}
for _, info := range infos {
if wantDir && !info.IsDir() {
name := info.Name()
if !wantDir {
// no filtering
} else if mode := info.Mode(); mode&os.ModeSymlink != 0 {
// TODO: is there a way to do this without the
// extra syscall?
if _, err := cfg.ReadDir(filepath.Join(fullDir, name)); err != nil {
// symlink pointing to non-directory
continue
}
} else if !mode.IsDir() {
// definitely not a directory
continue
}
name := info.Name()
if !strings.HasPrefix(rx.String(), `^\.`) && name[0] == '.' {
continue
}
Expand Down
8 changes: 8 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,14 @@ set +o pipefail
"mkdir x-d; touch x-f; test -d $PWD/x-*/",
"",
},
{
"mkdir -p foo/bar; ln -s foo sym; echo sy*/; echo sym/b*",
"sym/\nsym/bar\n",
},
{
"touch foo; ln -s foo sym; echo sy*; echo sy*/",
"sym\nsy*/\n",
},

// brace expansion; more exhaustive tests in the syntax package
{"echo a}b", "a}b\n"},
Expand Down

0 comments on commit dffd4f7

Please sign in to comment.