Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
97
internal/globpath/globpath.go
Normal file
97
internal/globpath/globpath.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package globpath
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/bmatcuk/doublestar/v3"
|
||||
"github.com/gobwas/glob"
|
||||
)
|
||||
|
||||
type GlobPath struct {
|
||||
path string
|
||||
hasMeta bool
|
||||
HasSuperMeta bool
|
||||
rootGlob string
|
||||
g glob.Glob
|
||||
}
|
||||
|
||||
func Compile(path string) (*GlobPath, error) {
|
||||
out := GlobPath{
|
||||
hasMeta: hasMeta(path),
|
||||
HasSuperMeta: hasSuperMeta(path),
|
||||
path: filepath.FromSlash(path),
|
||||
}
|
||||
|
||||
// if there are no glob meta characters in the path, don't bother compiling
|
||||
// a glob object
|
||||
if !out.hasMeta || !out.HasSuperMeta {
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
// find the root elements of the object path, the entry point for recursion
|
||||
// when you have a super-meta in your path (which are :
|
||||
// glob(/your/expression/until/first/star/of/super-meta))
|
||||
out.rootGlob = path[:strings.Index(path, "**")+1]
|
||||
var err error
|
||||
if out.g, err = glob.Compile(path, os.PathSeparator); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
// Match returns all files matching the expression.
|
||||
// If it's a static path, returns path.
|
||||
// All returned path will have the host platform separator.
|
||||
func (g *GlobPath) Match() []string {
|
||||
// This string replacement is for backwards compatibility support
|
||||
// The original implementation allowed **.txt but the double star package requires **/**.txt
|
||||
g.path = strings.ReplaceAll(g.path, "**/**", "**")
|
||||
g.path = strings.ReplaceAll(g.path, "**", "**/**")
|
||||
|
||||
//nolint:errcheck // pattern is known
|
||||
files, _ := doublestar.Glob(g.path)
|
||||
return files
|
||||
}
|
||||
|
||||
// MatchString tests the path string against the glob. The path should contain
|
||||
// the host platform separator.
|
||||
func (g *GlobPath) MatchString(path string) bool {
|
||||
if !g.HasSuperMeta {
|
||||
//nolint:errcheck // pattern is known
|
||||
res, _ := filepath.Match(g.path, path)
|
||||
return res
|
||||
}
|
||||
return g.g.Match(path)
|
||||
}
|
||||
|
||||
// GetRoots returns a list of files and directories which should be optimal
|
||||
// prefixes of matching files when you have a super-meta in your expression :
|
||||
// - any directory under these roots may contain a matching file
|
||||
// - no file outside of these roots can match the pattern
|
||||
// Note that it returns both files and directories.
|
||||
// All returned path will have the host platform separator.
|
||||
func (g *GlobPath) GetRoots() []string {
|
||||
if !g.hasMeta {
|
||||
return []string{g.path}
|
||||
}
|
||||
if !g.HasSuperMeta {
|
||||
//nolint:errcheck // pattern is known
|
||||
matches, _ := filepath.Glob(g.path)
|
||||
return matches
|
||||
}
|
||||
//nolint:errcheck // pattern is known
|
||||
roots, _ := filepath.Glob(g.rootGlob)
|
||||
return roots
|
||||
}
|
||||
|
||||
// hasMeta reports whether path contains any magic glob characters.
|
||||
func hasMeta(path string) bool {
|
||||
return strings.ContainsAny(path, "*?[")
|
||||
}
|
||||
|
||||
// hasSuperMeta reports whether path contains any super magic glob characters (**).
|
||||
func hasSuperMeta(path string) bool {
|
||||
return strings.Contains(path, "**")
|
||||
}
|
124
internal/globpath/globpath_test.go
Normal file
124
internal/globpath/globpath_test.go
Normal file
|
@ -0,0 +1,124 @@
|
|||
//go:build !windows
|
||||
|
||||
// TODO: Windows - should be enabled for Windows when super asterisk is fixed on Windows
|
||||
// https://github.com/influxdata/telegraf/issues/6248
|
||||
|
||||
package globpath
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var (
|
||||
testdataDir = getTestdataDir()
|
||||
)
|
||||
|
||||
func TestCompileAndMatch(t *testing.T) {
|
||||
type test struct {
|
||||
path string
|
||||
matches int
|
||||
}
|
||||
|
||||
tests := []test{
|
||||
// test super asterisk
|
||||
{path: filepath.Join(testdataDir, "**"), matches: 7},
|
||||
// test single asterisk
|
||||
{path: filepath.Join(testdataDir, "*.log"), matches: 3},
|
||||
// test no meta characters (file exists)
|
||||
{path: filepath.Join(testdataDir, "log1.log"), matches: 1},
|
||||
// test file that doesn't exist
|
||||
{path: filepath.Join(testdataDir, "i_dont_exist.log"), matches: 0},
|
||||
// test super asterisk that doesn't exist
|
||||
{path: filepath.Join(testdataDir, "dir_doesnt_exist", "**"), matches: 0},
|
||||
// test exclamation mark creates non-matching list with a range
|
||||
{path: filepath.Join(testdataDir, "log[!1-2]*"), matches: 1},
|
||||
// test caret creates non-matching list
|
||||
{path: filepath.Join(testdataDir, "log[^1-2]*"), matches: 1},
|
||||
// test exclamation mark creates non-matching list without a range
|
||||
{path: filepath.Join(testdataDir, "log[!2]*"), matches: 2},
|
||||
// test exclamation mark creates non-matching list without a range
|
||||
//nolint:gocritic // filepathJoin - '\\' used to escape in glob, not path separator
|
||||
{path: filepath.Join(testdataDir, "log\\[!*"), matches: 1},
|
||||
// test exclamation mark creates non-matching list without a range
|
||||
//nolint:gocritic // filepathJoin - '\\' used to escape in glob, not path separator
|
||||
{path: filepath.Join(testdataDir, "log\\[^*"), matches: 0},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
g, err := Compile(tc.path)
|
||||
require.NoError(t, err)
|
||||
matches := g.Match()
|
||||
require.Len(t, matches, tc.matches)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRootGlob(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
output string
|
||||
}{
|
||||
{filepath.Join(testdataDir, "**"), filepath.Join(testdataDir, "*")},
|
||||
{filepath.Join(testdataDir, "nested?", "**"), filepath.Join(testdataDir, "nested?", "*")},
|
||||
{filepath.Join(testdataDir, "ne**", "nest*"), filepath.Join(testdataDir, "ne*")},
|
||||
{filepath.Join(testdataDir, "nested?", "*"), ""},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
actual, err := Compile(test.input)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, actual.rootGlob, test.output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindNestedTextFile(t *testing.T) {
|
||||
// test super asterisk
|
||||
g1, err := Compile(filepath.Join(testdataDir, "**.txt"))
|
||||
require.NoError(t, err)
|
||||
|
||||
matches := g1.Match()
|
||||
require.Len(t, matches, 1)
|
||||
}
|
||||
|
||||
func TestMatch_ErrPermission(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected []string
|
||||
}{
|
||||
{"/root/foo", []string(nil)},
|
||||
{"/root/f*", []string(nil)},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
glob, err := Compile(test.input)
|
||||
require.NoError(t, err)
|
||||
actual := glob.Match()
|
||||
require.Equal(t, test.expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWindowsSeparator(t *testing.T) {
|
||||
//nolint:staticcheck // Silence linter for now as we plan to reenable tests for Windows later
|
||||
if runtime.GOOS != "windows" {
|
||||
t.Skip("Skipping Windows only test")
|
||||
}
|
||||
|
||||
glob, err := Compile("testdata/nested1")
|
||||
require.NoError(t, err)
|
||||
ok := glob.MatchString("testdata\\nested1")
|
||||
require.True(t, ok)
|
||||
}
|
||||
|
||||
func getTestdataDir() string {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
// if we cannot even establish the test directory, further progress is meaningless
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return filepath.Join(dir, "testdata")
|
||||
}
|
0
internal/globpath/testdata/log1.log
vendored
Normal file
0
internal/globpath/testdata/log1.log
vendored
Normal file
0
internal/globpath/testdata/log2.log
vendored
Normal file
0
internal/globpath/testdata/log2.log
vendored
Normal file
0
internal/globpath/testdata/log[!.log
vendored
Normal file
0
internal/globpath/testdata/log[!.log
vendored
Normal file
0
internal/globpath/testdata/nested1/nested2/nested.txt
vendored
Normal file
0
internal/globpath/testdata/nested1/nested2/nested.txt
vendored
Normal file
5
internal/globpath/testdata/test.conf
vendored
Normal file
5
internal/globpath/testdata/test.conf
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
# this is a fake testing config file
|
||||
# for testing the filestat plugin
|
||||
|
||||
option1 = "foo"
|
||||
option2 = "bar"
|
Loading…
Add table
Add a link
Reference in a new issue