Commit 03f27d5f authored by Daniel Martí's avatar Daniel Martí

path/filepath: fix escaped chars in Glob on non-Windows

Backslashes are ignored in Match and Glob on Windows, since those
collide with the separator character. However, they should still work in
both functions on other operating systems.

hasMeta did not reflect this logic - it always treated a backslash as a
non-special character. Do that only on Windows.

Assuming this is what the TODO was referring to, remove it. There are no
other characters that scanChunk treats especially.

Fixes #23418.

Change-Id: Ie0bd795812e0ed9d8c8c1bbc3137f29d960cba84
Reviewed-on: https://go-review.googlesource.com/87455
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 821b04da
...@@ -339,6 +339,9 @@ func glob(dir, pattern string, matches []string) (m []string, e error) { ...@@ -339,6 +339,9 @@ func glob(dir, pattern string, matches []string) (m []string, e error) {
// hasMeta reports whether path contains any of the magic characters // hasMeta reports whether path contains any of the magic characters
// recognized by Match. // recognized by Match.
func hasMeta(path string) bool { func hasMeta(path string) bool {
// TODO(niemeyer): Should other magic characters be added here? magicChars := `*?[`
return strings.ContainsAny(path, "*?[") if runtime.GOOS != "windows" {
magicChars = `*?[\`
}
return strings.ContainsAny(path, magicChars)
} }
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
. "path/filepath" . "path/filepath"
"reflect"
"runtime" "runtime"
"sort" "sort"
"strings" "strings"
...@@ -371,3 +372,18 @@ func TestWindowsGlob(t *testing.T) { ...@@ -371,3 +372,18 @@ func TestWindowsGlob(t *testing.T) {
} }
} }
} }
func TestNonWindowsGlobEscape(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skipf("skipping non-windows specific test")
}
pattern := `\match.go`
want := []string{"match.go"}
matches, err := Glob(pattern)
if err != nil {
t.Fatalf("Glob error for %q: %s", pattern, err)
}
if !reflect.DeepEqual(matches, want) {
t.Fatalf("Glob(%#q) = %v want %v", pattern, matches, want)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment