Use godirwalk in globpath (#5145)

This commit is contained in:
Samuel-BF 2018-12-18 23:23:25 +01:00 committed by Daniel Nelson
parent 234975bcac
commit f530ca6e7c
6 changed files with 35 additions and 30 deletions

View File

@ -6,6 +6,7 @@ import (
"strings"
"github.com/gobwas/glob"
"github.com/karrick/godirwalk"
)
type GlobPath struct {
@ -41,38 +42,42 @@ func Compile(path string) (*GlobPath, error) {
}
// Match returns all files matching the expression
func (g *GlobPath) Match() map[string]os.FileInfo {
out := make(map[string]os.FileInfo)
// If it's a static path, returns path
func (g *GlobPath) Match() []string {
if !g.hasMeta {
info, err := os.Stat(g.path)
if err == nil {
out[g.path] = info
}
return out
return []string{g.path}
}
if !g.HasSuperMeta {
files, _ := filepath.Glob(g.path)
for _, file := range files {
info, err := os.Stat(file)
if err == nil {
out[file] = info
}
}
return out
return files
}
roots, err := filepath.Glob(g.rootGlob)
if err != nil {
return out
return []string{}
}
walkfn := func(path string, info os.FileInfo, _ error) error {
out := []string{}
walkfn := func(path string, _ *godirwalk.Dirent) error {
if g.g.Match(path) {
out[path] = info
out = append(out, path)
}
return nil
}
for _, root := range roots {
filepath.Walk(root, walkfn)
fileinfo, err := os.Stat(root)
if err != nil {
continue
}
if !fileinfo.IsDir() {
if g.MatchString(root) {
out = append(out, root)
}
continue
}
godirwalk.Walk(root, &godirwalk.Options{
Callback: walkfn,
Unsorted: true,
})
}
return out
}

View File

@ -1,7 +1,6 @@
package globpath
import (
"os"
"runtime"
"strings"
"testing"
@ -34,7 +33,7 @@ func TestCompileAndMatch(t *testing.T) {
matches = g3.Match()
require.Len(t, matches, 1)
matches = g4.Match()
require.Len(t, matches, 0)
require.Len(t, matches, 1)
matches = g5.Match()
require.Len(t, matches, 0)
}
@ -75,10 +74,10 @@ func getTestdataDir() string {
func TestMatch_ErrPermission(t *testing.T) {
tests := []struct {
input string
expected map[string]os.FileInfo
expected []string
}{
{"/root/foo", map[string]os.FileInfo{}},
{"/root/f*", map[string]os.FileInfo{}},
{"/root/foo", []string{"/root/foo"}},
{"/root/f*", []string(nil)},
}
for _, test := range tests {

View File

@ -75,10 +75,7 @@ func (f *File) refreshFilePaths() error {
if len(files) <= 0 {
return fmt.Errorf("could not find file: %v", file)
}
for k := range files {
allFiles = append(allFiles, k)
}
allFiles = append(allFiles, files...)
}
f.filenames = allFiles

View File

@ -73,13 +73,17 @@ func (f *FileStat) Gather(acc telegraf.Accumulator) error {
continue
}
for fileName, fileInfo := range files {
for _, fileName := range files {
tags := map[string]string{
"file": fileName,
}
fields := map[string]interface{}{
"exists": int64(1),
}
fileInfo, err := os.Stat(fileName)
if os.IsNotExist(err) {
fields["exists"] = int64(0)
}
if fileInfo == nil {
log.Printf("E! Unable to get info for file [%s], possible permissions issue",

View File

@ -182,7 +182,7 @@ func (l *LogParserPlugin) tailNewfiles(fromBeginning bool) error {
}
files := g.Match()
for file := range files {
for _, file := range files {
if _, ok := l.tailers[file]; ok {
// we're already tailing this file
continue

View File

@ -111,7 +111,7 @@ func (t *Tail) tailNewFiles(fromBeginning bool) error {
if err != nil {
t.acc.AddError(fmt.Errorf("E! Error Glob %s failed to compile, %s", filepath, err))
}
for file := range g.Match() {
for _, file := range g.Match() {
if _, ok := t.tailers[file]; ok {
// we're already tailing this file
continue