Skip to content

Commit

Permalink
Tune negative patterns to behave more like positive patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanohoro committed Nov 3, 2023
1 parent 082893c commit a1c8016
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
3 changes: 2 additions & 1 deletion configs/go/fileshot/fileshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ files:
positive:
- '/glob/to/your/files/*'
- '/superglob/to/your/files/**'
# negative # Does not support doublestar **
# negative
# - '/*/badfiles/*'
# - '/**/temp-*.log'
minsize: 1 # Limit minimum file size in bytes
maxsize: 999999999 # Limit max file size in bytes
limitcapacity: 999999999 # Limit max file bandwidth in bytes
Expand Down
20 changes: 8 additions & 12 deletions src/go/cmd/strelka-fileshot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,13 @@ func checkRecentlyModified(modTime time.Time, modified int, verbose bool) (bool,

// wildCardToRegexp converts a wildcard pattern to a regular expression pattern.
func wildCardToRegexp(pattern string) string {
var result strings.Builder
for i, literal := range strings.Split(pattern, "*") {

// Replace * with .*
if i > 0 {
result.WriteString(".*")
}
result := regexp.QuoteMeta(pattern)
result = strings.Replace(result, "\\*\\*", ".*", -1)
result = strings.Replace(result, "\\*", "[^\\\\]*", -1)

return result

// Quote any regular expression meta characters in the
// literal text.
result.WriteString(regexp.QuoteMeta(literal))
}
return result.String()
}

func sortMatches(matches []matchRich, field string, order string) []matchRich {
Expand Down Expand Up @@ -503,7 +497,9 @@ func getFilePaths(conf structs.FileShot, verbose *bool, hashes []string) []strin
}

for _, negativePattern := range conf.Files.Patterns.Negative {
result, _ := regexp.MatchString(wildCardToRegexp(negativePattern), filePath)
re, _ := regexp.Compile("^" + wildCardToRegexp(negativePattern) + "$")

result := re.Match(([]byte)(filePath))

if result {
if *verbose {
Expand Down

0 comments on commit a1c8016

Please sign in to comment.