forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
99 lines (80 loc) · 2.02 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
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"
)
type MultiGetSubjectRequest struct {
Path api.DSPathSpec
Message proto.Message
Err error
// Free form data that goes with the request.
Data interface{}
}
// A helper function to read multipe subjects at the same time.
func MultiGetSubject(
config_obj *config_proto.Config,
requests []*MultiGetSubjectRequest) error {
var mu sync.Mutex
db, err := GetDB(config_obj)
if err != nil {
return err
}
var wg sync.WaitGroup
mu.Lock()
for _, request := range requests {
wg.Add(1)
go func(request *MultiGetSubjectRequest) {
mu.Lock()
defer mu.Unlock()
request.Err = db.GetSubject(config_obj, request.Path, request.Message)
wg.Done()
}(request)
}
mu.Unlock()
wg.Wait()
return nil
}
func Walk(config_obj *config_proto.Config,
datastore DataStore, root api.DSPathSpec, walkFn WalkFunc) error {
TraceDirectory(config_obj, "Walk", root)
all_children, err := datastore.ListChildren(config_obj, root)
if err != nil {
return err
}
for _, child := range all_children {
// Recurse into directories
if child.IsDir() {
err := Walk(config_obj, datastore, child, walkFn)
if err != nil {
// Do not quit the walk early.
}
} else {
err := walkFn(child)
if err == StopIteration {
return nil
}
continue
}
}
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
}