Skip to content

Commit

Permalink
path/filepath: Glob fails if it has a wildcard and ends with /
Browse files Browse the repository at this point in the history
The Glob tries to get the parent directory of the given directory.
When a trailing slash is present, filepath.Split returns the path as it is as it can not split it further.
This fix cleans the path first by remoivng any trailing slash before getting the parent directory.

Fixes golang#33617
  • Loading branch information
jamdagni86 committed Nov 19, 2019
1 parent 8cf5293 commit ed9e127
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/path/filepath/match.go
Expand Up @@ -239,13 +239,9 @@ func Glob(pattern string) (matches []string, err error) {
return []string{pattern}, nil
}

dir, file := Split(pattern)
volumeLen := 0
if runtime.GOOS == "windows" {
volumeLen, dir = cleanGlobPathWindows(dir)
} else {
dir = cleanGlobPath(dir)
}
_, dir := cleanPath(pattern)
dir, file := Split(dir)
volumeLen, dir := cleanPath(dir)

if !hasMeta(dir[volumeLen:]) {
return glob(dir, file, nil)
Expand All @@ -270,6 +266,15 @@ func Glob(pattern string) (matches []string, err error) {
return
}

// cleanPath cleans the path depending on GOOS
func cleanPath(dir string) (int, string) {
if runtime.GOOS != "windows" {
return cleanGlobPathWindows(dir)
}

return 0, cleanGlobPath(dir)
}

// cleanGlobPath prepares path for glob matching.
func cleanGlobPath(path string) string {
switch path {
Expand All @@ -279,7 +284,10 @@ func cleanGlobPath(path string) string {
// do nothing to the path
return path
default:
return path[0 : len(path)-1] // chop off trailing separator
if os.IsPathSeparator(path[len(path)-1]) {
return path[0 : len(path)-1] // chop off trailing separator
}
return path
}
}

Expand All @@ -298,7 +306,10 @@ func cleanGlobPathWindows(path string) (prefixLen int, cleaned string) {
if vollen >= len(path) {
vollen = len(path) - 1
}
return vollen, path[0 : len(path)-1] // chop off trailing separator
if os.IsPathSeparator(path[len(path)-1]) {
return vollen, path[0 : len(path)-1] // chop off trailing separator
}
return vollen, path
}
}

Expand Down
1 change: 1 addition & 0 deletions src/path/filepath/match_test.go
Expand Up @@ -123,6 +123,7 @@ var globTests = []struct {
{"mat?h.go", "match.go"},
{"*", "match.go"},
{"../*/match.go", "../filepath/match.go"},
{"../../*/filepath/", "../../path/filepath"},
}

func TestGlob(t *testing.T) {
Expand Down

0 comments on commit ed9e127

Please sign in to comment.