-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_pattern.go
100 lines (76 loc) · 1.61 KB
/
node_pattern.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"encoding/hex"
"strings"
"github.com/gobwas/glob"
ntfs "github.com/corebreaker/ntfstool/core"
"github.com/corebreaker/ntfstool/extract"
)
type tNodePattern struct {
tree *extract.Tree
root *extract.Node
ids map[string]bool
globs []glob.Glob
}
func (np *tNodePattern) Match(file *extract.File) bool {
if file.Id == "" {
return false
}
if np.ids[file.Id] {
return true
}
name := file.Name
path := np.tree.GetFilePath(file)
for _, g := range np.globs {
if g.Match(name) || g.Match(path) {
return true
}
}
return false
}
func (np *tNodePattern) getNodes(node *extract.Node, to map[string]*extract.Node) {
for _, n := range node.Children {
np.getNodes(n, to)
}
if np.Match(node.File) {
to[node.File.Id] = node
}
}
func (np *tNodePattern) GetNodes(from *extract.Node) map[string]*extract.Node {
res := make(map[string]*extract.Node)
if from == nil {
from = np.root
}
np.getNodes(from, res)
return res
}
func parseNodePattern(src string, tree *extract.Tree) (*tNodePattern, error) {
parts := strings.Split(src, ",")
ids := make(map[string]bool)
var globs []glob.Glob
for _, part := range parts {
if part[0] == '@' {
id := part[1:]
if _, err := hex.DecodeString(id); err != nil {
return nil, ntfs.WrapError(err)
}
ids[id] = true
continue
}
g, err := glob.Compile(part)
if err != nil {
return nil, ntfs.WrapError(err)
}
globs = append(globs, g)
}
res := &tNodePattern{
tree: tree,
ids: ids,
globs: globs,
root: &extract.Node{
File: new(extract.File),
Children: tree.Roots,
},
}
return res, nil
}