forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
123 lines (98 loc) · 3.35 KB
/
factory.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
/*
Velociraptor - Hunting Evil
Copyright (C) 2019 Velocidex Innovations.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package glob
import (
"path/filepath"
"regexp"
"sync"
errors "github.com/pkg/errors"
"www.velocidex.com/golang/vfilter"
)
var (
mu sync.Mutex
handlers map[string]FileSystemAccessorFactory
)
// Interface for accessing the filesystem. Used for dependency
// injection.
type FileSystemAccessor interface {
ReadDir(path string) ([]FileInfo, error)
Open(path string) (ReadSeekCloser, error)
Lstat(filename string) (FileInfo, error)
// Produce a function which splits a path into components.
PathSplit(path string) []string
PathJoin(root, stem string) string
// Split a path into a glob root and a sub path
// component. This is required when the accessor uses a prefix
// which contains path separators but should not be considered
// as part of the glob. For example, the ntfs accessor uses a
// device path as the root, which already contains path
// separators:
// \\.\c:\Windows\System32\notepad.exe ->
// root = \\.\c:
// path = \Windows\System32\notepad.exe
GetRoot(path string) (root, subpath string, err error)
}
type NullFileSystemAccessor struct{}
func (self NullFileSystemAccessor) New(scope *vfilter.Scope) FileSystemAccessor {
return self
}
func (self NullFileSystemAccessor) ReadDir(path string) ([]FileInfo, error) {
return nil, errors.New("Not supported")
}
func (self NullFileSystemAccessor) Open(path string) (ReadSeekCloser, error) {
return nil, errors.New("Not supported")
}
func (self NullFileSystemAccessor) Lstat(path string) (FileInfo, error) {
return nil, errors.New("Not supported")
}
func (self NullFileSystemAccessor) GetRoot(path string) (string, string, error) {
return "/", path, nil
}
func (self NullFileSystemAccessor) PathSplit(path string) []string {
re := regexp.MustCompile("/")
return re.Split(path, -1)
}
func (self NullFileSystemAccessor) PathJoin(root, stem string) string {
return filepath.Join(root, stem)
}
func GetAccessor(scheme string, scope *vfilter.Scope) (
FileSystemAccessor, error) {
mu.Lock()
defer mu.Unlock()
// Fallback to the file handler - this should work
// because there needs to be at least a file handler
// registered.
if scheme == "" {
scheme = "file"
}
handler, pres := handlers[scheme]
if pres {
res, err := handler.New(scope)
return res, err
}
return nil, errors.New("Unknown filesystem accessor")
}
// A factory for new accessors
type FileSystemAccessorFactory interface {
New(scope *vfilter.Scope) (FileSystemAccessor, error)
}
func Register(scheme string, accessor FileSystemAccessorFactory) {
mu.Lock()
defer mu.Unlock()
if handlers == nil {
handlers = make(map[string]FileSystemAccessorFactory)
}
handlers[scheme] = accessor
}