forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessor_linux.go
163 lines (134 loc) · 3.02 KB
/
accessor_linux.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
// +build linux
package glob
import (
"context"
"encoding/json"
"os"
"path/filepath"
"regexp"
"syscall"
"time"
"www.velocidex.com/golang/velociraptor/utils"
)
type OSFileInfo struct {
os.FileInfo
_full_path string
_data interface{}
}
func (self *OSFileInfo) Data() interface{} {
return self._data
}
func (self *OSFileInfo) FullPath() string {
return self._full_path
}
func (self *OSFileInfo) Mtime() TimeVal {
return TimeVal{
Sec: self.sys().Mtim.Sec,
Nsec: self.sys().Mtim.Nsec,
}
}
func (self *OSFileInfo) Ctime() TimeVal {
return TimeVal{
Sec: self.sys().Ctim.Sec,
Nsec: self.sys().Ctim.Nsec,
}
}
func (self *OSFileInfo) Atime() TimeVal {
return TimeVal{
Sec: self.sys().Atim.Sec,
Nsec: self.sys().Atim.Nsec,
}
}
func (self *OSFileInfo) sys() *syscall.Stat_t {
return self.Sys().(*syscall.Stat_t)
}
func (self *OSFileInfo) MarshalJSON() ([]byte, error) {
result, err := json.Marshal(&struct {
FullPath string
Size int64
Mode os.FileMode
ModeStr string
ModTime time.Time
Sys interface{}
Mtime TimeVal
Ctime TimeVal
Atime TimeVal
}{
FullPath: self.FullPath(),
Size: self.Size(),
Mode: self.Mode(),
ModeStr: self.Mode().String(),
ModTime: self.ModTime(),
Sys: self.Sys(),
Mtime: self.Mtime(),
Ctime: self.Ctime(),
Atime: self.Atime(),
})
return result, err
}
func (u *OSFileInfo) UnmarshalJSON(data []byte) error {
return nil
}
// Real implementation for non windows OSs:
type OSFileSystemAccessor struct {
fd_cache map[string]*os.File
}
func (self OSFileSystemAccessor) New(ctx context.Context) FileSystemAccessor {
result := &OSFileSystemAccessor{
fd_cache: make(map[string]*os.File),
}
// When the context is done, close all the files. The files
// must remain open until the entire VQL query is done.
go func() {
select {
case <-ctx.Done():
for _, v := range result.fd_cache {
v.Close()
}
}
}()
return result
}
func (self OSFileSystemAccessor) Lstat(filename string) (FileInfo, error) {
lstat, err := os.Lstat(filename)
if err != nil {
return nil, err
}
return &OSFileInfo{lstat, filename, nil}, nil
}
func (self OSFileSystemAccessor) ReadDir(path string) ([]FileInfo, error) {
files, err := utils.ReadDir(path)
if files != nil {
var result []FileInfo
for _, f := range files {
result = append(result,
&OSFileInfo{f, filepath.Join(path, f.Name()), nil})
}
return result, nil
}
return nil, err
}
func (self OSFileSystemAccessor) Open(path string) (ReadSeekCloser, error) {
fd, pres := self.fd_cache[path]
if pres {
return fd, nil
}
file, err := os.Open(path)
if err != nil {
return nil, err
}
self.fd_cache[path] = file
return file, nil
}
func (self OSFileSystemAccessor) PathSplit() *regexp.Regexp {
return regexp.MustCompile("/")
}
func (self *OSFileSystemAccessor) PathSep() string {
return "/"
}
func (self *OSFileSystemAccessor) GetRoot(path string) (string, string, error) {
return "/", path, nil
}
func init() {
Register("file", &OSFileSystemAccessor{})
}