From b9a48075ea5db92eec3f2fba28ad5a89e80f8eea Mon Sep 17 00:00:00 2001 From: Charlie Vieth Date: Sat, 12 Mar 2022 19:46:56 -0500 Subject: [PATCH] internal/gopathwalk: remove unnecessary call to os.Lstat internal/gopathwalk.walk remove an unnecessary call to os.Lstat when checking if a symlink should be traversed. And remove a call to filepath.EvalSymlinks that should be unnecessary as we are using os.Stat to compare files. Change-Id: Ie5ad1cb99cbd6d62f08f2b482a3e84b6fe41114d Reviewed-on: https://go-review.googlesource.com/c/tools/+/392095 Reviewed-by: Heschi Kreinick Run-TryBot: Heschi Kreinick Auto-Submit: Heschi Kreinick gopls-CI: kokoro TryBot-Result: Gopher Robot Trust: Ian Lance Taylor --- internal/gopathwalk/walk.go | 20 +++++--------------- internal/gopathwalk/walk_test.go | 2 +- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/internal/gopathwalk/walk.go b/internal/gopathwalk/walk.go index 925ff53560..1684053226 100644 --- a/internal/gopathwalk/walk.go +++ b/internal/gopathwalk/walk.go @@ -175,8 +175,8 @@ func (w *walker) shouldSkipDir(fi os.FileInfo, dir string) bool { // walk walks through the given path. func (w *walker) walk(path string, typ os.FileMode) error { - dir := filepath.Dir(path) if typ.IsRegular() { + dir := filepath.Dir(path) if dir == w.root.Path && (w.root.Type == RootGOROOT || w.root.Type == RootGOPATH) { // Doesn't make sense to have regular files // directly in your $GOPATH/src or $GOROOT/src. @@ -209,12 +209,7 @@ func (w *walker) walk(path string, typ os.FileMode) error { // Emacs noise. return nil } - fi, err := os.Lstat(path) - if err != nil { - // Just ignore it. - return nil - } - if w.shouldTraverse(dir, fi) { + if w.shouldTraverse(path) { return fastwalk.ErrTraverseLink } } @@ -224,13 +219,8 @@ func (w *walker) walk(path string, typ os.FileMode) error { // shouldTraverse reports whether the symlink fi, found in dir, // should be followed. It makes sure symlinks were never visited // before to avoid symlink loops. -func (w *walker) shouldTraverse(dir string, fi os.FileInfo) bool { - path := filepath.Join(dir, fi.Name()) - target, err := filepath.EvalSymlinks(path) - if err != nil { - return false - } - ts, err := os.Stat(target) +func (w *walker) shouldTraverse(path string) bool { + ts, err := os.Stat(path) if err != nil { fmt.Fprintln(os.Stderr, err) return false @@ -238,7 +228,7 @@ func (w *walker) shouldTraverse(dir string, fi os.FileInfo) bool { if !ts.IsDir() { return false } - if w.shouldSkipDir(ts, dir) { + if w.shouldSkipDir(ts, filepath.Dir(path)) { return false } // Check for symlink loops by statting each directory component diff --git a/internal/gopathwalk/walk_test.go b/internal/gopathwalk/walk_test.go index 2d887a655f..fa4ebdc32b 100644 --- a/internal/gopathwalk/walk_test.go +++ b/internal/gopathwalk/walk_test.go @@ -81,7 +81,7 @@ func TestShouldTraverse(t *testing.T) { continue } var w walker - got := w.shouldTraverse(filepath.Join(dir, tt.dir), fi) + got := w.shouldTraverse(filepath.Join(dir, tt.dir, fi.Name())) if got != tt.want { t.Errorf("%d. shouldTraverse(%q, %q) = %v; want %v", i, tt.dir, tt.file, got, tt.want) }