forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
170 lines (137 loc) · 3.33 KB
/
utils.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package datastore
import (
"errors"
"sync"
"google.golang.org/protobuf/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/file_store/api"
)
const (
WalkWithDirectories = true
WalkWithoutDirectories = false
)
type MultiGetSubjectRequest struct {
mu sync.Mutex
message proto.Message
Path api.DSPathSpec
Err error
// Free form data that goes with the request.
Data interface{}
}
// Return a copy so there is no race
func (self *MultiGetSubjectRequest) Message() proto.Message {
return proto.Clone(self.message)
}
func (self *MultiGetSubjectRequest) Error() error {
self.mu.Lock()
defer self.mu.Unlock()
return self.Err
}
func NewMultiGetSubjectRequest(message proto.Message, path api.DSPathSpec, data interface{}) *MultiGetSubjectRequest {
return &MultiGetSubjectRequest{
message: proto.Clone(message),
Path: path,
Data: data,
}
}
// A helper function to read multipe subjects at the same time.
func MultiGetSubject(
config_obj *config_proto.Config,
requests []*MultiGetSubjectRequest) error {
db, err := GetDB(config_obj)
if err != nil {
return err
}
var wg sync.WaitGroup
for _, request := range requests {
wg.Add(1)
go func(request *MultiGetSubjectRequest) {
defer wg.Done()
request.mu.Lock()
defer request.mu.Unlock()
request.Err = db.GetSubject(config_obj, request.Path, request.message)
}(request)
}
wg.Wait()
return nil
}
func Walk(config_obj *config_proto.Config,
datastore DataStore, root api.DSPathSpec,
with_directories bool,
walkFn WalkFunc) error {
TraceDirectory(config_obj, "Walk", root)
all_children, err := datastore.ListChildren(config_obj, root)
if err != nil {
return err
}
directories := []api.DSPathSpec{}
files := []api.DSPathSpec{}
for _, child := range all_children {
// Recurse into directories
if child.IsDir() {
directories = append(directories, child)
} else {
files = append(files, child)
}
}
// Depth first walk - first directories then files. This allows us
// to remove empty directories recursively.
for _, d := range directories {
err := Walk(config_obj, datastore, d, with_directories, walkFn)
if err != nil {
// Do not quit the walk early.
}
}
if with_directories {
for _, d := range directories {
err := walkFn(d)
if err == StopIteration {
return nil
}
}
}
for _, f := range files {
err := walkFn(f)
if err == StopIteration {
return nil
}
}
return nil
}
func GetImplementationName(
config_obj *config_proto.Config) (string, error) {
if config_obj.Datastore == nil {
return "", errors.New("Invalid datastore config")
}
if config_obj.Frontend == nil {
return config_obj.Datastore.Implementation, nil
}
if config_obj.Frontend.IsMinion &&
config_obj.Datastore.MinionImplementation != "" {
return config_obj.Datastore.MinionImplementation, nil
}
if config_obj.Datastore.MasterImplementation != "" {
return config_obj.Datastore.MasterImplementation, nil
}
return config_obj.Datastore.Implementation, nil
}
type Flusher interface {
Flush()
}
func FlushDatastore(config_obj *config_proto.Config) error {
var wg sync.WaitGroup
defer wg.Wait()
db, err := GetDB(config_obj)
if err != nil {
return err
}
flusher, ok := db.(Flusher)
if ok {
wg.Add(1)
go func() {
defer wg.Done()
flusher.Flush()
}()
}
return nil
}