-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
84 lines (66 loc) · 1.64 KB
/
environment.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
package types
import (
"fmt"
"io"
"os"
"github.com/avahidi/molly/util"
)
// Env is the current environment during scanning
type Env struct {
m *Molly
// Input is valid while we are scanning a file
Reader io.ReadSeeker
Current *FileData
// Scope is valid while we are scanning a file and a rule
Scope *Scope
}
func NewEnv(m *Molly) *Env {
return &Env{
m: m,
}
}
func (e *Env) StartRule(rule *Rule) {
e.Scope = NewScope(rule, nil)
}
func (e *Env) PushRule(newrule *Rule) {
e.Scope = NewScope(newrule, e.Scope)
}
func (e *Env) PopRule() {
if e.Scope == nil || e.Scope.Parent == nil {
util.RegisterFatalf("Internal error: no scope or scope hierarchy")
}
e.Scope = e.Scope.Parent
}
func (e Env) String() string {
if e.Current != nil {
return fmt.Sprintf("{%s:%s}", e.Scope.Rule.ID, e.Current.Filename)
}
return fmt.Sprintf("{%s}", e.Scope.Rule.ID)
}
func (e *Env) SetInput(r io.ReadSeeker, d *FileData) {
e.Reader = r
e.Current = d
}
func (e Env) GetFile() string {
return e.Current.Filename
}
func (e Env) GetSize() uint64 {
return uint64(e.Current.Filesize)
}
func (e *Env) New(name string, islog bool) (*FileData, error) {
return e.m.New(e.Current, name, false, islog)
}
func (e *Env) Create(name string) (*os.File, *FileData, error) {
return e.m.CreateFile(e.Current, name, false)
}
func (e *Env) Mkdir(path string) (*FileData, error) {
return e.m.CreateDir(e.Current, path)
}
// CreateLog creates a new log
func (e *Env) CreateLog(name string) (*os.File, error) {
newfile, _, err := e.m.CreateFile(e.Current, name, true)
return newfile, err
}
func (e Env) HasPermission(p Permission) bool {
return e.m.Config.HasPermission(p)
}