forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual.go
298 lines (238 loc) · 5.88 KB
/
virtual.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package accessors
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"time"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/vfilter"
)
type VirtualReadSeekCloser struct {
io.ReadSeeker
}
func (self VirtualReadSeekCloser) Close() error {
return nil
}
type VirtualFileInfo struct {
// Only valid when this is not a directory
RawData []byte
Data_ *ordereddict.Dict
IsDir_ bool
Size_ int64
Path *OSPath
Atime_ time.Time
Mtime_ time.Time
Ctime_ time.Time
Btime_ time.Time
}
func (self *VirtualFileInfo) IsDir() bool {
return self.IsDir_
}
func (self *VirtualFileInfo) OSPath() *OSPath {
return self.Path
}
func (self *VirtualFileInfo) Size() int64 {
if self.Size_ > 0 {
return self.Size_
}
return int64(len(self.RawData))
}
func (self *VirtualFileInfo) Data() *ordereddict.Dict {
return self.Data_
}
func (self *VirtualFileInfo) Name() string {
return self.Path.Basename()
}
func (self *VirtualFileInfo) Sys() interface{} {
return nil
}
func (self *VirtualFileInfo) Mode() os.FileMode {
if self.IsDir_ {
return 0755 | os.ModeDir
}
return 0644
}
func (self *VirtualFileInfo) ModTime() time.Time {
return self.Mtime_
}
func (self *VirtualFileInfo) FullPath() string {
return self.Path.String()
}
func (self *VirtualFileInfo) Btime() time.Time {
return self.Btime_
}
func (self *VirtualFileInfo) Mtime() time.Time {
return self.Mtime_
}
func (self *VirtualFileInfo) Ctime() time.Time {
return self.Ctime_
}
func (self *VirtualFileInfo) Atime() time.Time {
return self.Atime_
}
func (self *VirtualFileInfo) IsLink() bool {
return false
}
func (self *VirtualFileInfo) Debug() string {
return fmt.Sprintf("%v (%v) %s", self.FullPath(), self.Size(), self.Mode())
}
func (self *VirtualFileInfo) GetLink() (*OSPath, error) {
return nil, errors.New("Not implemented")
}
// Mount tree is very sparse so we dont really need a map here -
// linear search is fast enough.
type directory_node struct {
file_info *VirtualFileInfo
// Child directory_nodes
children []*directory_node
}
func (self *directory_node) Debug() string {
res := fmt.Sprintf("directory_node: %v\n", json.MustMarshalString(self.file_info))
for _, c := range self.children {
res += c.Debug()
}
res += "\n"
return res
}
func (self *directory_node) GetChild(name string) *directory_node {
for _, c := range self.children {
if c.file_info != nil &&
c.file_info.Name() == name {
return c
}
}
return nil
}
func (self *directory_node) MakeChild(name string) *directory_node {
if name == "" {
return self
}
for _, c := range self.children {
if c.file_info != nil &&
c.file_info.Name() == name {
return c
}
}
// If we get here there is no child of this name - make it
new_directory_node := &directory_node{
file_info: &VirtualFileInfo{
Path: self.file_info.OSPath().Append(name),
IsDir_: true,
},
}
self.children = append(self.children, new_directory_node)
return new_directory_node
}
// A Virtual Filsystem stores files and directories in memory.
type VirtualFilesystemAccessor struct {
root_path *OSPath
root directory_node
}
func (self VirtualFilesystemAccessor) New(scope vfilter.Scope) (
FileSystemAccessor, error) {
return VirtualFilesystemAccessor{}, nil
}
func (self VirtualFilesystemAccessor) ParsePath(path string) (*OSPath, error) {
return self.root.file_info.OSPath().Parse(path)
}
func (self VirtualFilesystemAccessor) Lstat(path string) (FileInfo, error) {
os_path, err := self.ParsePath(path)
if err != nil {
return nil, err
}
return self.LstatWithOSPath(os_path)
}
func (self VirtualFilesystemAccessor) LstatWithOSPath(
path *OSPath) (FileInfo, error) {
node, err := self.getNode(path)
if err != nil {
return nil, err
}
return node.file_info, nil
}
func (self VirtualFilesystemAccessor) ReadDir(path string) ([]FileInfo, error) {
os_path, err := self.ParsePath(path)
if err != nil {
return nil, err
}
return self.ReadDirWithOSPath(os_path)
}
func (self VirtualFilesystemAccessor) ReadDirWithOSPath(
path *OSPath) ([]FileInfo, error) {
node, err := self.getNode(path)
if err != nil {
return nil, err
}
result := make([]FileInfo, 0, len(node.children))
for _, c := range node.children {
result = append(result, c.file_info)
}
return result, nil
}
func (self VirtualFilesystemAccessor) Open(path string) (
ReadSeekCloser, error) {
os_path, err := self.ParsePath(path)
if err != nil {
return nil, err
}
return self.OpenWithOSPath(os_path)
}
func (self VirtualFilesystemAccessor) OpenWithOSPath(path *OSPath) (
ReadSeekCloser, error) {
node, err := self.getNode(path)
if err != nil {
return nil, os.ErrNotExist
}
return VirtualReadSeekCloser{
ReadSeeker: bytes.NewReader(node.file_info.RawData),
}, nil
}
func (self VirtualFilesystemAccessor) getNode(path *OSPath) (*directory_node, error) {
node := &self.root
for _, c := range path.Components {
if c != "" {
next_node := node.GetChild(c)
if next_node == nil {
return nil, fmt.Errorf("While finding %v: Can not find %v: %w",
path, c, os.ErrNotExist)
}
node = next_node
}
}
return node, nil
}
func (self *VirtualFilesystemAccessor) SetVirtualDirectory(
dir_path *OSPath, file_info *VirtualFileInfo) {
node := &self.root
for _, c := range dir_path.Components {
node = node.MakeChild(c)
}
file_info.Path = dir_path.Copy()
node.file_info = file_info
}
func (self *VirtualFilesystemAccessor) SetVirtualFileInfo(
file_info *VirtualFileInfo) {
node := &self.root
for _, c := range file_info.OSPath().Components {
if c != "" {
node = node.MakeChild(c)
}
}
node.file_info = file_info
}
func NewVirtualFilesystemAccessor(root_path *OSPath) *VirtualFilesystemAccessor {
return &VirtualFilesystemAccessor{
root: directory_node{
file_info: &VirtualFileInfo{
Path: root_path,
IsDir_: true,
},
},
}
}
func init() {
json.RegisterCustomEncoder(&VirtualFileInfo{}, MarshalGlobFileInfo)
}