Merge pull request #339 from colincross/optimize_glob

Remove redundant stat/lstat calls from glob
This commit is contained in:
colincross 2021-01-20 13:34:51 -08:00 committed by GitHub
commit 49a8b95d4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 17 deletions

View file

@ -538,10 +538,22 @@ func listDirsRecursiveRelative(fs FileSystem, name string, follow ShouldFollowSy
continue
}
f = filepath.Join(name, f)
if isSymlink, _ := fs.IsSymlink(f); isSymlink && follow == DontFollowSymlinks {
continue
var info os.FileInfo
if follow == DontFollowSymlinks {
info, err = fs.Lstat(f)
if err != nil {
continue
}
if info.Mode()&os.ModeSymlink != 0 {
continue
}
} else {
info, err = fs.Stat(f)
if err != nil {
continue
}
}
if isDir, _ := fs.IsDir(f); isDir {
if info.Mode()&os.ModeDir != 0 {
dirs = append(dirs, f)
subDirs, err := listDirsRecursiveRelative(fs, f, follow, depth)
if err != nil {

View file

@ -76,24 +76,18 @@ func startGlob(fs FileSystem, pattern string, excludes []string,
}
for i, match := range matches {
isSymlink, err := fs.IsSymlink(match)
var info os.FileInfo
if follow == DontFollowSymlinks {
info, err = fs.Lstat(match)
} else {
info, err = fs.Stat(match)
}
if err != nil {
return nil, nil, err
}
if !(isSymlink && follow == DontFollowSymlinks) {
isDir, err := fs.IsDir(match)
if os.IsNotExist(err) {
if isSymlink {
return nil, nil, fmt.Errorf("%s: dangling symlink", match)
}
}
if err != nil {
return nil, nil, fmt.Errorf("%s: %s", match, err.Error())
}
if isDir {
matches[i] = match + "/"
}
if info.Mode()&os.ModeDir != 0 {
matches[i] = match + "/"
}
}