Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 0979f44

Browse files
authored
Merge pull request #1221 from jmank88/pkgtree-ls-pkg
pkgtree: add os.IsPermission check for walkFn err arg
2 parents 914a0bf + c1da7b8 commit 0979f44

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

internal/gps/pkgtree/pkgtree.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ func ListPackages(fileRoot, importRoot string) (PackageTree, error) {
7373

7474
err = filepath.Walk(fileRoot, func(wp string, fi os.FileInfo, err error) error {
7575
if err != nil && err != filepath.SkipDir {
76+
if os.IsPermission(err) {
77+
return filepath.SkipDir
78+
}
7679
return err
7780
}
7881
if !fi.IsDir() {
@@ -97,25 +100,29 @@ func ListPackages(fileRoot, importRoot string) (PackageTree, error) {
97100
return filepath.SkipDir
98101
}
99102

100-
// The entry error is nil when visiting a directory that itself is
101-
// untraversable, as it's still governed by the parent directory's
102-
// perms. We have to check readability of the dir here, because
103-
// otherwise we'll have an empty package entry when we fail to read any
104-
// of the dir's contents.
105-
//
106-
// If we didn't check here, then the next time this closure is called it
107-
// would have an err with the same path as is called this time, as only
108-
// then will filepath.Walk have attempted to descend into the directory
109-
// and encountered an error.
110-
var f *os.File
111-
f, err = os.Open(wp)
112-
if err != nil {
113-
if os.IsPermission(err) {
114-
return filepath.SkipDir
103+
{
104+
// For Go 1.9 and earlier:
105+
//
106+
// The entry error is nil when visiting a directory that itself is
107+
// untraversable, as it's still governed by the parent directory's
108+
// perms. We have to check readability of the dir here, because
109+
// otherwise we'll have an empty package entry when we fail to read any
110+
// of the dir's contents.
111+
//
112+
// If we didn't check here, then the next time this closure is called it
113+
// would have an err with the same path as is called this time, as only
114+
// then will filepath.Walk have attempted to descend into the directory
115+
// and encountered an error.
116+
var f *os.File
117+
f, err = os.Open(wp)
118+
if err != nil {
119+
if os.IsPermission(err) {
120+
return filepath.SkipDir
121+
}
122+
return err
115123
}
116-
return err
124+
f.Close()
117125
}
118-
f.Close()
119126

120127
// Compute the import path. Run the result through ToSlash(), so that
121128
// windows file paths are normalized to slashes, as is expected of

0 commit comments

Comments
 (0)