glob

package
v1.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	TypeEpsilon = iota
	TypeLiteral
	TypeAny   // ?
	TypeClass // [abc] or [!abc]
)
View Source
const (
	GlobPossibleMaxValues = 512 // this should match POSSIBLE_MAX_VALUES in bpf/glob/bpf_glob_multi.h
	GlobTableSize         = 256

	BitmapShift = 6
	BitmapMask  = 63
	BitsPerByte = 8
)
View Source
const Other = -999

Helper for "Other" transitions in DFA

Variables

View Source
var GlobTestCases = []TestCase{
	{Pattern: "/home/*/.bash*", Path: "/home/user/.bash_profile", Match: true},
	{Pattern: "home/*/.bash*", Path: "/home/user/.bash_profile", Match: false},
	{Pattern: "*.txt", Path: "/home/.bash_profile.txt", Match: true},
	{Pattern: "*.txt", Path: "tasos.txta", Match: false},
	{Pattern: "/home/*/.bash*", Path: "/home/user/.bas_profile/.bash_profile", Match: true},
	{Pattern: "*.*", Path: "test.txt", Match: true},
	{Pattern: "*.*", Path: "test", Match: false},
	{Pattern: "/home/*/.bash*t", Path: "/home/user/.bas_profile/.bash_profilea/.bash_profilet", Match: true},
	{Pattern: "*abcd*", Path: "qabczabuuuuabcde", Match: true},
	{Pattern: "a*b*ab", Path: "aabaabaab", Match: true},
	{Pattern: "a*", Path: "aabaabaab", Match: true},
	{Pattern: "aa*", Path: "aabaabaab", Match: true},
	{Pattern: "aaba*", Path: "aabaabaab", Match: true},
	{Pattern: "*d", Path: "abcdd", Match: true},
	{Pattern: "*d*", Path: "abcdd", Match: true},
	{Pattern: "a*baab", Path: "aabaabqqbaab", Match: true},
	{Pattern: "/foo/*/blig", Path: "/foo/bar/baz/xlig/fig/blig", Match: true},
	{Pattern: "/foo/*/blig", Path: "/foo/bar/baz/blig/fig/blig", Match: true},
	{Pattern: "a*baab", Path: "aabaabaab", Match: true},
	{Pattern: "*.txt", Path: "a.tx.txt", Match: true},
	{Pattern: "*", Path: "a.tx.txt", Match: true},
	{Pattern: "*", Path: "ababa", Match: true},
	{Pattern: "*a*b*c*d*e*f*g", Path: "ababadlufgasijkldhfgakljhsdgfjklagsdkfjlhhagsdkjfghasdkjfgakjsdhgfkajsdgfkayueirgfjkhzsdvbcajkhgdevfrkjuyagvda", Match: false},
	{Pattern: "?", Path: "a", Match: true},
	{Pattern: "?", Path: "aa", Match: false},
	{Pattern: "a?a", Path: "aba", Match: true},
	{Pattern: "a?a", Path: "abba", Match: false},
	{Pattern: "a?a*", Path: "abadsafgadsjkhfgahsjk", Match: true},
	{Pattern: "*.t?t", Path: "test.txt", Match: true},
	{Pattern: "*.t?t", Path: "test.tat", Match: true},
	{Pattern: "*.t?t", Path: "test.txxt", Match: false},
	{Pattern: "*.t?t", Path: "test.tx", Match: false},
	{Pattern: "a*b?c", Path: "aabcc", Match: true},
	{Pattern: "a*b?c", Path: "aabbcc", Match: true},
	{Pattern: "a*b?c", Path: "abc", Match: false},
	{Pattern: "a*b?c", Path: "aabbbc", Match: true},
	{Pattern: "a*b?c", Path: "aabbbbc", Match: true},
	{Pattern: "/home/user/.cache/go-build/*.go", Path: "/usr/local/go/src/math/j0.go", Match: false},
	{Pattern: "a*", Path: "a", Match: true},
	{Pattern: "a*b", Path: "ab", Match: true},
	{Pattern: "*a", Path: "a", Match: true},
	{Pattern: "a*?boo", Path: "axboo", Match: true},

	{Pattern: "config[0-9].yaml", Path: "config0.yaml", Match: true},
	{Pattern: "config[0-9].yaml", Path: "config9.yaml", Match: true},
	{Pattern: "config[0-9].yaml", Path: "config10.yaml", Match: false},
	{Pattern: "config[0-9].yaml", Path: "configa.yaml", Match: false},
	{Pattern: "config[!0-9].yaml", Path: "configa.yaml", Match: true},
	{Pattern: "config[!0-9].yaml", Path: "config5.yaml", Match: false},
	{Pattern: "file[._-]bak", Path: "file.bak", Match: true},
	{Pattern: "file[._-]bak", Path: "file_bak", Match: true},
	{Pattern: "file[._-]bak", Path: "file-bak", Match: true},
	{Pattern: "file[._-]bak", Path: "filexbak", Match: false},
	{Pattern: "[a-c][0-2]", Path: "a0", Match: true},
	{Pattern: "[a-c][0-2]", Path: "c2", Match: true},
	{Pattern: "[a-c][0-2]", Path: "d2", Match: false},
	{Pattern: "[a-c][0-2]", Path: "c3", Match: false},

	{Pattern: "*.{TEMP,backup,bak,old,tar.gz,tgz,temp,tmp}", Path: "/tmp/rules.TEMP", Match: true},
	{Pattern: "*.{TEMP,backup,bak,old,tar.gz,tgz,temp,tmp}", Path: "/tmp/rules.backup", Match: true},
	{Pattern: "*.{TEMP,backup,bak,old,tar.gz,tgz,temp,tmp}", Path: "/tmp/rules.bak", Match: true},
	{Pattern: "*.{TEMP,backup,bak,old,tar.gz,tgz,temp,tmp}", Path: "/tmp/rules.old", Match: true},
	{Pattern: "*.{TEMP,backup,bak,old,tar.gz,tgz,temp,tmp}", Path: "/tmp/rules.tar.gz", Match: true},
	{Pattern: "*.{TEMP,backup,bak,old,tar.gz,tgz,temp,tmp}", Path: "/tmp/rules.tgz", Match: true},
	{Pattern: "*.{TEMP,backup,bak,old,tar.gz,tgz,temp,tmp}", Path: "/tmp/rules.temp", Match: true},
	{Pattern: "*.{TEMP,backup,bak,old,tar.gz,tgz,temp,tmp}", Path: "/tmp/rules.tmp", Match: true},
	{Pattern: "*.{TEMP,backup,bak,old,tar.gz,tgz,temp,tmp}", Path: "/tmp/rules.gz", Match: false},
	{Pattern: "*.{TEMP,backup,bak,old,tar.gz,tgz,temp,tmp}", Path: "/tmp/rules.tmp.more", Match: false},
	{Pattern: "/{bin,etc,lib,lib64,opt,sbin,usr}/*", Path: "/bin/sh", Match: true},
	{Pattern: "/{bin,etc,lib,lib64,opt,sbin,usr}/*", Path: "/etc/passwd", Match: true},
	{Pattern: "/{bin,etc,lib,lib64,opt,sbin,usr}/*", Path: "/lib/modules/kernel", Match: true},
	{Pattern: "/{bin,etc,lib,lib64,opt,sbin,usr}/*", Path: "/lib64/libc.so", Match: true},
	{Pattern: "/{bin,etc,lib,lib64,opt,sbin,usr}/*", Path: "/opt/app/config", Match: true},
	{Pattern: "/{bin,etc,lib,lib64,opt,sbin,usr}/*", Path: "/sbin/init", Match: true},
	{Pattern: "/{bin,etc,lib,lib64,opt,sbin,usr}/*", Path: "/usr/bin/bash", Match: true},
	{Pattern: "/{bin,etc,lib,lib64,opt,sbin,usr}/*", Path: "/root/.ssh/authorized_keys", Match: false},
	{Pattern: "/srv/{bin,etc,lib,lib64,home/user/.ssh,sbin,usr}/*", Path: "/srv/bin/sh", Match: true},
	{Pattern: "/srv/{bin,etc,lib,lib64,home/user/.ssh,sbin,usr}/*", Path: "/srv/etc/config", Match: true},
	{Pattern: "/srv/{bin,etc,lib,lib64,home/user/.ssh,sbin,usr}/*", Path: "/srv/lib/libfoo.so", Match: true},
	{Pattern: "/srv/{bin,etc,lib,lib64,home/user/.ssh,sbin,usr}/*", Path: "/srv/lib64/libfoo.so", Match: true},
	{Pattern: "/srv/{bin,etc,lib,lib64,home/user/.ssh,sbin,usr}/*", Path: "/srv/home/user/.ssh/authorized_keys", Match: true},
	{Pattern: "/srv/{bin,etc,lib,lib64,home/user/.ssh,sbin,usr}/*", Path: "/srv/sbin/init", Match: true},
	{Pattern: "/srv/{bin,etc,lib,lib64,home/user/.ssh,sbin,usr}/*", Path: "/srv/usr/bin/bash", Match: true},
	{Pattern: "/srv/{bin,etc,lib,lib64,home/user/.ssh,sbin,usr}/*", Path: "/srv/home/user/authorized_keys", Match: false},

	{Pattern: "/var/log/*.{[1-9],1[0-9],20,gz}", Path: "/var/log/messages.1", Match: true},
	{Pattern: "/var/log/*.{[1-9],1[0-9],20,gz}", Path: "/var/log/messages.9", Match: true},
	{Pattern: "/var/log/*.{[1-9],1[0-9],20,gz}", Path: "/var/log/messages.10", Match: true},
	{Pattern: "/var/log/*.{[1-9],1[0-9],20,gz}", Path: "/var/log/messages.19", Match: true},
	{Pattern: "/var/log/*.{[1-9],1[0-9],20,gz}", Path: "/var/log/messages.20", Match: true},
	{Pattern: "/var/log/*.{[1-9],1[0-9],20,gz}", Path: "/var/log/messages.gz", Match: true},
	{Pattern: "/var/log/*.{[1-9],1[0-9],20,gz}", Path: "/var/log/messages.0", Match: false},
	{Pattern: "/var/log/*.{[1-9],1[0-9],20,gz}", Path: "/var/log/messages.21", Match: false},
	{Pattern: "/var/log/*.{[1-9],1[0-9],20,gz}", Path: "/var/log/messages.100", Match: false},
	{Pattern: "/var/log/*.{[1-9],1[0-9],20,gz}", Path: "/var/log/messages.GZ", Match: false},
}
View Source
var GlobTestCasesMulti = []TestCaseMulti{
	{
		Patterns: map[string][]int32{
			"abc*":    {1, 3},
			"a?c":     {2},
			"a[cd]b":  {6},
			"a[!cd]c": {8},
		},
		Tests: []struct {
			Path   string
			Values []int32
		}{
			{Path: "abc", Values: []int32{1, 2, 3, 8}},
			{Path: "acb", Values: []int32{6}},
			{Path: "azc", Values: []int32{2, 8}},
			{Path: "pizza", Values: []int32{}},
		},
	},
	{
		Patterns: map[string][]int32{
			"foo*":   {1},
			"*bar":   {2},
			"foobar": {3},
			"f*r":    {4},
		},
		Tests: []struct {
			Path   string
			Values []int32
		}{
			{Path: "foobar", Values: []int32{1, 2, 3, 4}},
			{Path: "foor", Values: []int32{1, 4}},
			{Path: "fbar", Values: []int32{2, 4}},
		},
	},
	{
		Patterns: map[string][]int32{
			"*":  {100},
			"":   {200},
			"a*": {300},
			"*z": {400},
		},
		Tests: []struct {
			Path   string
			Values []int32
		}{
			{Path: "", Values: []int32{100, 200}},
			{Path: "az", Values: []int32{100, 300, 400}},
			{Path: "supercalifragilistic", Values: []int32{100}},
		},
	},
	{
		Patterns: map[string][]int32{
			"id-[a-f]":   {1},
			"val-[2-9]":  {2},
			"val-[!0-5]": {3},
			"val-[01]":   {4},
		},
		Tests: []struct {
			Path   string
			Values []int32
		}{
			{Path: "id-c", Values: []int32{1}},
			{Path: "id-g", Values: []int32{}},
			{Path: "val-0", Values: []int32{4}},
			{Path: "val-1", Values: []int32{4}},
			{Path: "val-2", Values: []int32{2}},
			{Path: "val-3", Values: []int32{2}},
			{Path: "val-4", Values: []int32{2}},
			{Path: "val-5", Values: []int32{2}},
			{Path: "val-6", Values: []int32{2, 3}},
			{Path: "val-7", Values: []int32{2, 3}},
			{Path: "val-8", Values: []int32{2, 3}},
			{Path: "val-9", Values: []int32{2, 3}},
		},
	},
	{
		Patterns: map[string][]int32{
			"*.{TEMP,backup,bak,old,tar.gz,tgz,temp,tmp}":        {1},
			"/var/log/*.{[1-9],1[0-9],20,gz}":                    {2},
			"/{bin,etc,lib,lib64,opt,sbin,usr}/*":                {3},
			"/srv/{bin,etc,lib,lib64,home/user/.ssh,sbin,usr}/*": {4},
		},
		Tests: []struct {
			Path   string
			Values []int32
		}{
			{Path: "/tmp/rules.tmp", Values: []int32{1}},
			{Path: "/var/log/messages.gz", Values: []int32{2}},
			{Path: "/var/log/messages.9", Values: []int32{2}},
			{Path: "/var/log/messages.19", Values: []int32{2}},
			{Path: "/var/log/messages.21", Values: []int32{}},
			{Path: "/opt/app/config", Values: []int32{3}},
			{Path: "/srv/usr/bin/bash", Values: []int32{4}},
			{Path: "/srv/home/user/.ssh/authorized_keys", Values: []int32{4}},
			{Path: "/srv/home/user/authorized_keys", Values: []int32{}},
		},
	},
}

Functions

func ExportToDOT

func ExportToDOT(w io.Writer, start *DFAState) error

func GetLiterals

func GetLiterals(patterns map[string][]int32) ([]int, map[int]bool)

func MatchString

func MatchString(dfa *DFAState, input string, knownLiterals map[int]bool) []int32

Types

type Bitmap

type Bitmap struct {
	V [GlobPossibleMaxValues / BitsPerByte / unsafe.Sizeof(uint64(0))]uint64 // one bit per state
}

func (*Bitmap) Set

func (b *Bitmap) Set(n int)

type DFAState

type DFAState struct {
	ID          int
	Transitions map[int]*DFAState
	Matches     []int32
}

func ToDFA

func ToDFA(nfa *NFA, literals []int) *DFAState

type Data

type Data struct {
	// contains filtered or unexported fields
}

func GenerateAndPopulateData

func GenerateAndPopulateData(allPatterns map[string][]int32) Data

func (Data) GenerateFinalStatesMap

func (g Data) GenerateFinalStatesMap(m *ebpf.Map) error

func (Data) GenerateStateTransitionsMap

func (g Data) GenerateStateTransitionsMap(m *ebpf.Map) error

func (Data) GetFinalStatesMapSize

func (g Data) GetFinalStatesMapSize() int

func (Data) GetStateTransitionsMapSize

func (g Data) GetStateTransitionsMapSize() int

type NFA

type NFA struct {
	Start     *State
	NumStates int
}

func BuildMultiNFA

func BuildMultiNFA(patterns map[string][]int32) *NFA

type State

type State struct {
	ID       int
	Type     int
	Char     rune
	Class    map[rune]bool
	NotClass bool
	Out      *State
	Out1     *State
	MatchID  []int32
}

type StateTransitions

type StateTransitions struct {
	Next [GlobTableSize]int32
}

func GetAll

func GetAll(start *DFAState, knownMap map[int]bool) ([]StateTransitions, map[int32][]int32)

type TestCase

type TestCase struct {
	Pattern string
	Path    string
	Match   bool
}

type TestCaseMulti

type TestCaseMulti struct {
	Patterns map[string][]int32
	Tests    []struct {
		Path   string
		Values []int32
	}
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL