Skip to content

Commit

Permalink
globpath: only walk tree if ** is defined
Browse files Browse the repository at this point in the history
closes #1517
  • Loading branch information
sparrc committed Sep 28, 2016
1 parent e7e39df commit 5213455
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- [#1768](https://github.com/influxdata/telegraf/pull/1768): Speed up statsd parsing.
- [#1751](https://github.com/influxdata/telegraf/issues/1751): Fix powerdns integer parse error handling.
- [#1752](https://github.com/influxdata/telegraf/issues/1752): Fix varnish plugin defaults not being used.
- [#1517](https://github.com/influxdata/telegraf/issues/1517): Fix windows glob paths.

## v1.0.1 [unreleased]

Expand Down
32 changes: 25 additions & 7 deletions internal/globpath/globpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@ import (
var sepStr = fmt.Sprintf("%v", string(os.PathSeparator))

type GlobPath struct {
path string
hasMeta bool
g glob.Glob
root string
path string
hasMeta bool
hasSuperMeta bool
g glob.Glob
root string
}

func Compile(path string) (*GlobPath, error) {
out := GlobPath{
hasMeta: hasMeta(path),
path: path,
hasMeta: hasMeta(path),
hasSuperMeta: hasSuperMeta(path),
path: path,
}

// if there are no glob meta characters in the path, don't bother compiling
// a glob object or finding the root directory. (see short-circuit in Match)
if !out.hasMeta {
if !out.hasMeta || !out.hasSuperMeta {
return &out, nil
}

Expand All @@ -48,6 +50,17 @@ func (g *GlobPath) Match() map[string]os.FileInfo {
}
return out
}
if !g.hasSuperMeta {

This comment has been minimized.

Copy link
@molon

molon Oct 20, 2017

I'm learning about the wonderful code~

Why not use filepath.Glob directly?
Using gobwas/glob for performance?

This comment has been minimized.

Copy link
@danielnelson

danielnelson Oct 20, 2017

Contributor

We use gobwas/glob mostly for the the extra syntax options and the ability to match non-paths in other places of the code. This code is not particularly performance sensitive.

out := make(map[string]os.FileInfo)
files, _ := filepath.Glob(g.path)
for _, file := range files {
info, err := os.Stat(file)
if !os.IsNotExist(err) {
out[file] = info
}
}
return out
}
return walkFilePath(g.root, g.g)
}

Expand Down Expand Up @@ -96,3 +109,8 @@ func findRootDir(path string) string {
func hasMeta(path string) bool {
return strings.IndexAny(path, "*?[") >= 0
}

// hasSuperMeta reports whether path contains any super magic glob characters (**).
func hasSuperMeta(path string) bool {
return strings.Index(path, "**") >= 0
}

0 comments on commit 5213455

Please sign in to comment.