forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessor_darwin.go
149 lines (125 loc) · 2.88 KB
/
accessor_darwin.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
// +build darwin
package glob
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"syscall"
"time"
)
type OSFileInfo struct {
os.FileInfo
_full_path string
}
func (self *OSFileInfo) FullPath() string {
return self._full_path
}
func (self *OSFileInfo) Mtime() TimeVal {
return TimeVal{
Sec: int64(self.sys().Mtimespec.Sec),
Nsec: int64(self.sys().Mtimespec.Nsec),
}
}
func (self *OSFileInfo) Ctime() TimeVal {
return TimeVal{
Sec: int64(self.sys().Ctimespec.Sec),
Nsec: int64(self.sys().Ctimespec.Nsec),
}
}
func (self *OSFileInfo) Atime() TimeVal {
return TimeVal{
Sec: int64(self.sys().Atimespec.Sec),
Nsec: int64(self.sys().Atimespec.Nsec),
}
}
func (self *OSFileInfo) Data() interface{} {
return nil
}
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
}
func (self OSFileSystemAccessor) ReadDir(path string) ([]FileInfo, error) {
files, err := ioutil.ReadDir(path)
if err == nil {
var result []FileInfo
for _, f := range files {
result = append(result,
&OSFileInfo{f, filepath.Join(path, f.Name())})
}
return result, nil
}
return nil, err
}
func (self OSFileSystemAccessor) Open(path string) (ReadSeekCloser, error) {
file, err := os.Open(path)
return file, err
}
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{})
}