diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2023-02-10 12:14:36 +0100 |
|---|---|---|
| committer | Aleksandr Nogikh <wp32pw@gmail.com> | 2023-02-10 14:34:44 +0100 |
| commit | 95871dcc45f6531b4c692ff892aad56bdd95e16f (patch) | |
| tree | 95c8ffe8b8a36b1dc9473cc3a07e7286595e0b2f /pkg/subsystem/match.go | |
| parent | 0ee9f5fa4e372b5a2da4ac27418e6c5bccbcaf7a (diff) | |
pkg/subsystem: restructure the package
Remove the entity and match subpackages.
Regenerate the linux.go file.
Diffstat (limited to 'pkg/subsystem/match.go')
| -rw-r--r-- | pkg/subsystem/match.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/pkg/subsystem/match.go b/pkg/subsystem/match.go new file mode 100644 index 000000000..ad5d3a257 --- /dev/null +++ b/pkg/subsystem/match.go @@ -0,0 +1,73 @@ +// Copyright 2023 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package subsystem + +import ( + "regexp" + "strings" +) + +type PathMatcher struct { + matches []*match +} + +type match struct { + include *regexp.Regexp + exclude *regexp.Regexp + object *Subsystem +} + +func MakePathMatcher(list []*Subsystem) *PathMatcher { + m := &PathMatcher{} + for _, item := range list { + m.register(item) + } + return m +} + +func (p *PathMatcher) register(item *Subsystem) { + onlyInclude := []string{} + list := []PathRule{} + for _, r := range item.PathRules { + if r.ExcludeRegexp == "" { + // It's expected that almost everything will go to this branch. + onlyInclude = append(onlyInclude, r.IncludeRegexp) + } else { + list = append(list, r) + } + } + if len(onlyInclude) > 0 { + list = append(list, PathRule{ + IncludeRegexp: strings.Join(onlyInclude, "|"), + }) + } + for _, rule := range list { + p.matches = append(p.matches, buildMatch(rule, item)) + } +} + +func (p *PathMatcher) Match(path string) []*Subsystem { + ret := []*Subsystem{} + for _, m := range p.matches { + if m.exclude != nil && m.exclude.MatchString(path) { + continue + } + if m.include != nil && !m.include.MatchString(path) { + continue + } + ret = append(ret, m.object) + } + return ret +} + +func buildMatch(rule PathRule, item *Subsystem) *match { + m := &match{object: item} + if rule.IncludeRegexp != "" { + m.include = regexp.MustCompile(rule.IncludeRegexp) + } + if rule.ExcludeRegexp != "" { + m.exclude = regexp.MustCompile(rule.ExcludeRegexp) + } + return m +} |
