From e7ce063f8acb437cc86cc5fadf91598c46b5a48c Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Fri, 21 Jul 2017 14:28:14 -0700 Subject: [PATCH] Don't match pattern on any error (#3040) This prevents a pattern with no wildcards from matching in case permissions is denied. (cherry picked from commit 31ce98fa91ff8a71474969133965ae6b3083d890) --- internal/globpath/globpath.go | 4 ++-- internal/globpath/globpath_test.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/globpath/globpath.go b/internal/globpath/globpath.go index 22ae92721d415..6067f65b26c0b 100644 --- a/internal/globpath/globpath.go +++ b/internal/globpath/globpath.go @@ -45,7 +45,7 @@ func (g *GlobPath) Match() map[string]os.FileInfo { if !g.hasMeta { out := make(map[string]os.FileInfo) info, err := os.Stat(g.path) - if !os.IsNotExist(err) { + if err == nil { out[g.path] = info } return out @@ -55,7 +55,7 @@ func (g *GlobPath) Match() map[string]os.FileInfo { files, _ := filepath.Glob(g.path) for _, file := range files { info, err := os.Stat(file) - if !os.IsNotExist(err) { + if err == nil { out[file] = info } } diff --git a/internal/globpath/globpath_test.go b/internal/globpath/globpath_test.go index 7205724113f12..20bfbcbb9283e 100644 --- a/internal/globpath/globpath_test.go +++ b/internal/globpath/globpath_test.go @@ -1,6 +1,7 @@ package globpath import ( + "os" "runtime" "strings" "testing" @@ -70,3 +71,20 @@ func getTestdataDir() string { _, filename, _, _ := runtime.Caller(1) return strings.Replace(filename, "globpath_test.go", "testdata", 1) } + +func TestMatch_ErrPermission(t *testing.T) { + tests := []struct { + input string + expected map[string]os.FileInfo + }{ + {"/root/foo", map[string]os.FileInfo{}}, + {"/root/f*", map[string]os.FileInfo{}}, + } + + for _, test := range tests { + glob, err := Compile(test.input) + require.NoError(t, err) + actual := glob.Match() + require.Equal(t, test.expected, actual) + } +}