From 95871dcc45f6531b4c692ff892aad56bdd95e16f Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Fri, 10 Feb 2023 12:14:36 +0100 Subject: pkg/subsystem: restructure the package Remove the entity and match subpackages. Regenerate the linux.go file. --- pkg/subsystem/match.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 pkg/subsystem/match.go (limited to 'pkg/subsystem/match.go') 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 +} -- cgit mrf-deployment