Refactor globpath pkg to return a map

this is so that we don't call os.Stat twice for every file matched
by Match(). Also changing the behavior to _not_ return the name of a
file that doesn't exist if it's not a glob.
This commit is contained in:
Cameron Sparr 2016-04-24 14:37:44 -06:00
parent d3a25e4dc1
commit 07728d7425
3 changed files with 41 additions and 25 deletions

View File

@ -39,20 +39,25 @@ func Compile(path string) (*GlobPath, error) {
return &out, nil
}
func (g *GlobPath) Match() []string {
func (g *GlobPath) Match() map[string]os.FileInfo {
if !g.hasMeta {
return []string{g.path}
out := make(map[string]os.FileInfo)
info, err := os.Stat(g.path)
if !os.IsNotExist(err) {
out[g.path] = info
}
return out
}
return walkFilePath(g.root, g.g)
}
// walk the filepath from the given root and return a list of files that match
// the given glob.
func walkFilePath(root string, g glob.Glob) []string {
matchedFiles := []string{}
walkfn := func(path string, _ os.FileInfo, _ error) error {
func walkFilePath(root string, g glob.Glob) map[string]os.FileInfo {
matchedFiles := make(map[string]os.FileInfo)
walkfn := func(path string, info os.FileInfo, _ error) error {
if g.Match(path) {
matchedFiles = append(matchedFiles, path)
matchedFiles[path] = info
}
return nil
}

View File

@ -11,12 +11,21 @@ import (
func TestCompileAndMatch(t *testing.T) {
dir := getTestdataDir()
// test super asterisk
g1, err := Compile(dir + "/**")
require.NoError(t, err)
// test single asterisk
g2, err := Compile(dir + "/*.log")
require.NoError(t, err)
// test no meta characters (file exists)
g3, err := Compile(dir + "/log1.log")
require.NoError(t, err)
// test file that doesn't exist
g4, err := Compile(dir + "/i_dont_exist.log")
require.NoError(t, err)
// test super asterisk that doesn't exist
g5, err := Compile(dir + "/dir_doesnt_exist/**")
require.NoError(t, err)
matches := g1.Match()
assert.Len(t, matches, 3)
@ -24,6 +33,10 @@ func TestCompileAndMatch(t *testing.T) {
assert.Len(t, matches, 2)
matches = g3.Match()
assert.Len(t, matches, 1)
matches = g4.Match()
assert.Len(t, matches, 0)
matches = g5.Match()
assert.Len(t, matches, 0)
}
func TestFindRootDir(t *testing.T) {

View File

@ -55,31 +55,29 @@ func (f *FileStat) Gather(acc telegraf.Accumulator) error {
f.globs[filepath] = g
}
for _, file := range g.Match() {
files := g.Match()
if len(files) == 0 {
acc.AddFields("filestat",
map[string]interface{}{
"exists": int64(0),
},
map[string]string{
"file": filepath,
})
continue
}
for fileName, fileInfo := range files {
tags := map[string]string{
"file": file,
"file": fileName,
}
fields := map[string]interface{}{
"exists": int64(0),
"exists": int64(1),
"size_bytes": fileInfo.Size(),
}
// Get file stats
fileInfo, err := os.Stat(file)
if os.IsNotExist(err) {
// file doesn't exist, so move on to the next
acc.AddFields("filestat", fields, tags)
continue
}
if err != nil {
errS += err.Error() + " "
continue
}
// file exists and no errors encountered
fields["exists"] = int64(1)
fields["size_bytes"] = fileInfo.Size()
if f.Md5 {
md5, err := getMd5(file)
md5, err := getMd5(fileName)
if err != nil {
errS += err.Error() + " "
} else {