Skip to content

Commit

Permalink
Use godirwalk in globpath (influxdata#5145)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel-BF authored and otherpirate committed Mar 15, 2019
1 parent d90663b commit d7b9317
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 30 deletions.
41 changes: 23 additions & 18 deletions internal/globpath/globpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/gobwas/glob"
"github.com/karrick/godirwalk"
)

type GlobPath struct {
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 4 additions & 5 deletions internal/globpath/globpath_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package globpath

import (
"os"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions plugins/inputs/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion plugins/inputs/filestat/filestat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/logparser/logparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d7b9317

Please sign in to comment.