-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathfs.go
More file actions
231 lines (193 loc) · 5.26 KB
/
fs.go
File metadata and controls
231 lines (193 loc) · 5.26 KB
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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package tar
import (
"archive/tar"
"bufio"
"errors"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/rs/zerolog/log"
"github.com/spf13/afero"
"go.mondoo.com/cnquery/v12/providers/os/connection/shared"
"go.mondoo.com/cnquery/v12/providers/os/fsutil"
)
var _ shared.FileSearch = (*FS)(nil)
func NewFs(source string) *FS {
return &FS{
Source: source,
FileMap: make(map[string]*tar.Header),
}
}
type FS struct {
Source string
FileMap map[string]*tar.Header
}
func (fs *FS) Name() string {
return "tarfs"
}
func (fs *FS) Create(name string) (afero.File, error) {
return nil, errors.New("create not implemented")
}
func (fs *FS) Mkdir(name string, perm os.FileMode) error {
return errors.New("mkdir not implemented")
}
func (fs *FS) MkdirAll(path string, perm os.FileMode) error {
return errors.New("mkdirall not implemented")
}
func (fs *FS) Open(path string) (afero.File, error) {
h, ok := fs.FileMap[path]
if !ok {
return nil, os.ErrNotExist
}
if h.Typeflag == tar.TypeSymlink {
resolvedPath := fs.resolveSymlink(h)
log.Debug().Str("path", path).Str("resolved", Abs(resolvedPath)).Msg("file is a symlink, resolved it")
h, ok = fs.FileMap[Abs(resolvedPath)]
if !ok {
return nil, os.ErrNotExist
}
}
reader, err := fs.open(h)
if err != nil {
return nil, err
}
return &File{
path: path,
header: h,
Fs: fs,
reader: reader,
}, nil
}
func (fs *FS) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
return nil, errors.New("openfile not implemented")
}
func (fs *FS) Remove(name string) error {
return errors.New("remove not implemented")
}
func (fs *FS) RemoveAll(path string) error {
return errors.New("removeall not implemented")
}
func (fs *FS) Rename(oldname, newname string) error {
return errors.New("rename not implemented")
}
func (fs *FS) Stat(name string) (os.FileInfo, error) {
h, ok := fs.FileMap[name]
if !ok {
return nil, os.ErrNotExist
}
return fs.stat(h)
}
func (fs *FS) Chmod(name string, mode os.FileMode) error {
return errors.New("chmod not implemented")
}
func (fs *FS) Chtimes(name string, atime time.Time, mtime time.Time) error {
return errors.New("chtimes not implemented")
}
func (fs *FS) Chown(name string, uid, gid int) error {
return errors.New("chown not implemented")
}
func (fs *FS) stat(header *tar.Header) (os.FileInfo, error) {
statHeader := header
if header.Typeflag == tar.TypeSymlink {
path := fs.resolveSymlink(header)
h, ok := fs.FileMap[Abs(path)]
if !ok {
return nil, errors.New("could not find " + path)
}
statHeader = h
}
return statHeader.FileInfo(), nil
}
// resolve symlink file
func (fs *FS) resolveSymlink(header *tar.Header) string {
dest := header.Name
link := header.Linkname
var path string
if filepath.IsAbs(link) {
var err error
// we need to remove the root / then
path, err = filepath.Rel("/", link)
if err != nil {
log.Error().Str("link", link).Msg("could not determine the relative root path")
}
} else {
path = Clean(join(dest, "..", link))
}
log.Debug().Str("link", link).Str("file", dest).Str("path", path).Msg("tar> is symlink")
return path
}
func (fs *FS) open(header *tar.Header) (*bufio.Reader, error) {
log.Debug().Str("file", header.Name).Msg("tar> load file content")
// open tar file
f, err := os.Open(fs.Source)
if err != nil {
return nil, err
}
defer f.Close()
path := header.Name
if header.Typeflag == tar.TypeSymlink {
path = fs.resolveSymlink(header)
}
// extract file from tar stream
reader, err := fsutil.ExtractFileFromTarStream(path, f)
if err != nil {
return nil, err
}
return reader, nil
}
func (fs *FS) tar(path string, header *tar.Header) (io.ReadCloser, error) {
fReader, err := fs.open(header)
if err != nil {
return nil, err
}
// create a pipe
tarReader, tarWriter := io.Pipe()
// get file info, header my just include symlink fileinfo
fi, err := fs.stat(header)
if err != nil {
return nil, err
}
// convert raw stream to tar stream
go fsutil.StreamFileAsTar(header.Name, fi, io.NopCloser(fReader), tarWriter)
// return the reader
return tarReader, nil
}
// Find searches for files and returns the file info, regex can be nil
func (fs *FS) Find(from string, r *regexp.Regexp, typ string, perm *uint32, depth *int) ([]string, error) {
list := []string{}
for k := range fs.FileMap {
p := strings.HasPrefix(k, from)
m := true
if r != nil {
m = r.MatchString(k)
}
if !depthMatch(from, k, depth) {
continue
}
log.Trace().Str("path", k).Str("from", from).Str("prefix", from).Bool("prefix", p).Bool("m", m).Msg("check if matches")
if p && m {
entry := fs.FileMap[k]
if (typ == "directory" && entry.Typeflag == tar.TypeDir) || (typ == "file" && entry.Typeflag == tar.TypeReg) || typ == "" {
list = append(list, k)
log.Debug().Msg("matches")
continue
}
}
}
return list, nil
}
func depthMatch(from, filepath string, depth *int) bool {
if depth == nil {
return true
}
trimmed := strings.TrimPrefix(filepath, from)
// WalkDir always uses slash for separating, ignoring the OS separator. This is why we need to replace it.
normalized := strings.ReplaceAll(trimmed, string(os.PathSeparator), "/")
fileDepth := strings.Count(normalized, "/")
return fileDepth <= *depth
}