-
Notifications
You must be signed in to change notification settings - Fork 104
/
vfs.go
293 lines (237 loc) · 6.46 KB
/
vfs.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
// Copyright 2022, 2023 Chainguard, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vfs
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"golang.org/x/sys/unix"
)
// An INode contains overlay metadata for a filesystem entry that would
// otherwise be represented by a traditional inode on disk.
type INode struct {
Filename string
Children map[string]*INode
UnderlayINode unix.Stat_t
}
// parseElements parses a path into components, and returns the current
// path component, followed by the remaining path components, and a hint
// as to whether or not the path list is terminated or not.
func parseElements(path string) (string, string, bool) {
pathElements := strings.Split(path, "/")
currentElement := pathElements[0]
if len(pathElements) == 1 {
return currentElement, "", true
}
otherElements := filepath.Join(pathElements[1:]...)
if pathElements[0] == "." {
currentElement = pathElements[1]
otherElements = ""
if len(pathElements) > 2 {
otherElements = filepath.Join(pathElements[2:]...)
}
}
return currentElement, otherElements, len(pathElements) == 1
}
func (i *INode) walk(path string) (*INode, error) {
currentElement, otherElements, terminated := parseElements(path)
// We are at the end of the path, return ourselves.
if terminated {
if currentElement == "" {
return i, nil
}
if child, ok := i.Children[currentElement]; ok {
return child, nil
}
return nil, fmt.Errorf("no underlay inode found")
}
// We are not at the end of the path, traverse downward.
if child, ok := i.Children[currentElement]; ok {
return child.walk(otherElements)
}
// We do not have any overlay INode. Pass through to owning FS.
return nil, fmt.Errorf("no underlay inode found")
}
// Stat looks up a child INode in the VFS or returns nothing.
// Note: We intentionally do not implement support for the `..` directory
// entry for security reasons.
func (i *INode) Stat(path string) (os.FileInfo, error) {
child, err := i.walk(path)
if err != nil {
return INode{}, err
}
return *child, nil
}
// Create creates a new underlay INode.
func (i *INode) Create(path string) (*INode, error) {
currentElement, otherElements, terminated := parseElements(path)
// We are at the end of the path, return ourselves.
if terminated {
if child, ok := i.Children[currentElement]; ok {
return child, nil
}
if i.Children == nil {
i.Children = make(map[string]*INode)
}
child := &INode{Filename: currentElement}
i.Children[currentElement] = child
return child, nil
}
// We are not at the end of the path, traverse downward.
if child, ok := i.Children[currentElement]; ok {
return child.Create(otherElements)
}
// We do not yet have an overlay INode, create one and
// continue downward.
if i.Children == nil {
i.Children = make(map[string]*INode)
}
child := &INode{Filename: currentElement}
i.Children[currentElement] = child
return child.Create(otherElements)
}
func (i *INode) walkOrCreate(path string) (*INode, error) {
node, err := i.walk(path)
if err != nil {
// No overlay node, create a new one.
node, err = i.Create(path)
if err != nil {
return nil, err
}
}
return node, nil
}
// Chmod updates the permissions on an INode.
func (i *INode) Chmod(path string, mode fs.FileMode) error {
node, err := i.walkOrCreate(path)
if err != nil {
return err
}
isDir := node.IsDir()
newMode := mode
if isDir {
newMode |= fs.ModeDir
}
node.UnderlayINode.Mode = fileModeToStatMode(newMode)
return nil
}
// Chown updates the ownership on an INode.
func (i *INode) Chown(path string, uid, gid uint32) error {
node, err := i.walkOrCreate(path)
if err != nil {
return err
}
node.UnderlayINode.Uid = uid
node.UnderlayINode.Gid = gid
return nil
}
func (i INode) IsDir() bool {
return i.Mode()&fs.ModeDir == fs.ModeDir
}
func (i INode) Mode() fs.FileMode {
return fs.FileMode(i.UnderlayINode.Mode)
}
func (i INode) ModTime() time.Time {
ts, tns := i.UnderlayINode.Mtim.Unix()
return time.Unix(ts, tns).UTC()
}
func (i INode) Name() string {
return i.Filename
}
func (i INode) Size() int64 {
return i.UnderlayINode.Size
}
func (i INode) Sys() any {
return &i.UnderlayINode
}
func (i INode) Info() (os.FileInfo, error) {
return i, nil
}
func (i INode) Type() fs.FileMode {
return i.Mode()
}
// BaseFS is the required interfaces for an underlay filesystem
// which is used with VFS.
type BaseFS interface {
fs.FS
fs.ReadDirFS
fs.ReadFileFS
fs.StatFS
Create(path string) (io.WriteCloser, error)
Remove(path string) error
RemoveAll(path string) error
}
// VFS is an overlay virtual filesystem which tracks an underlying
// BaseFS.
//
// It allows for things like permission and ownership changes that
// do not require physical root access, because it is tracked at
// the VFS level instead.
type VFS struct {
FS BaseFS
Root *INode
}
func (vfs *VFS) Stat(path string) (os.FileInfo, error) {
inode, err := vfs.Root.Stat(path)
if err == nil {
return inode, err
}
return vfs.FS.Stat(path)
}
func (vfs *VFS) Create(path string) (io.WriteCloser, error) {
return vfs.FS.Create(path)
}
func (vfs *VFS) Open(path string) (fs.File, error) {
return vfs.FS.Open(path)
}
func (vfs *VFS) ReadFile(path string) ([]byte, error) {
return vfs.FS.ReadFile(path)
}
func (vfs *VFS) ReadDir(path string) ([]fs.DirEntry, error) {
de, err := vfs.FS.ReadDir(path)
if err != nil {
return []fs.DirEntry{}, err
}
baseINode, err := vfs.Root.walk(path)
if err != nil {
return de, nil
}
out := []fs.DirEntry{}
for _, dentry := range de {
if patchedINode, ok := baseINode.Children[dentry.Name()]; ok {
out = append(out, patchedINode)
} else {
out = append(out, dentry)
}
}
return out, nil
}
func (vfs *VFS) Chmod(path string, mode fs.FileMode) error {
return vfs.Root.Chmod(path, mode)
}
func (vfs *VFS) Chown(path string, uid, gid uint32) error {
return vfs.Root.Chown(path, uid, gid)
}
func New(base BaseFS) (*VFS, error) {
return &VFS{
FS: base,
Root: &INode{
Filename: ".",
},
}, nil
}