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" "strings"
"github.com/gobwas/glob" "github.com/gobwas/glob"
"github.com/karrick/godirwalk"
) )
type GlobPath struct { type GlobPath struct {
@ -41,38 +42,42 @@ func Compile(path string) (*GlobPath, error) {
} }
// Match returns all files matching the expression // Match returns all files matching the expression
func (g *GlobPath) Match() map[string]os.FileInfo { // If it's a static path, returns path
out := make(map[string]os.FileInfo) func (g *GlobPath) Match() []string {
if !g.hasMeta { if !g.hasMeta {
info, err := os.Stat(g.path) return []string{g.path}
if err == nil {
out[g.path] = info
}
return out
} }
if !g.HasSuperMeta { if !g.HasSuperMeta {
files, _ := filepath.Glob(g.path) files, _ := filepath.Glob(g.path)
for _, file := range files { return files
info, err := os.Stat(file)
if err == nil {
out[file] = info
}
}
return out
} }
roots, err := filepath.Glob(g.rootGlob) roots, err := filepath.Glob(g.rootGlob)
if err != nil { 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) { if g.g.Match(path) {
out[path] = info out = append(out, path)
} }
return nil return nil
} }
for _, root := range roots { 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 return out
} }

View File

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

View File

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

View File

@ -73,13 +73,17 @@ func (f *FileStat) Gather(acc telegraf.Accumulator) error {
continue continue
} }
for fileName, fileInfo := range files { for _, fileName := range files {
tags := map[string]string{ tags := map[string]string{
"file": fileName, "file": fileName,
} }
fields := map[string]interface{}{ fields := map[string]interface{}{
"exists": int64(1), "exists": int64(1),
} }
fileInfo, err := os.Stat(fileName)
if os.IsNotExist(err) {
fields["exists"] = int64(0)
}
if fileInfo == nil { if fileInfo == nil {
log.Printf("E! Unable to get info for file [%s], possible permissions issue", 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() files := g.Match()
for file := range files { for _, file := range files {
if _, ok := l.tailers[file]; ok { if _, ok := l.tailers[file]; ok {
// we're already tailing this file // we're already tailing this file
continue continue

View File

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