-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.go
344 lines (305 loc) · 8.11 KB
/
layer.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package ocifs
import (
"errors"
"io"
"io/fs"
"path"
"strings"
"github.com/stealthrocket/fslink"
)
const (
whiteoutPrefix = ".wh."
whiteoutOpaque = ".wh..wh..opq"
)
// LayerFS constructs a read-only overlay file system by stacking layers of OCI
// images.
//
// Because OCI images are immutable objects, the resulting layered file system
// is read-only. Write permissions are removed on all files exposed by the file
// system and the fs.File instances have no methods to allow writes.
//
// The underlying fs.FS layers are expected to support symlinks by exposing a
// ReadLink method with this signature:
//
// type ReadLinkFS interface {
// ReadLink(name string) (string, error)
// }
//
// Files opened by a layered file system implement fs.ReadFileFS, io.ReaderAt,
// and io.Seeker. If the underlying files do not support these extensions of the
// fs.File interface, and fs.PathError wrapping fs.ErrInvalid is returned.
func LayerFS(layers ...fs.FS) fs.FS {
layers = append([]fs.FS{}, layers...)
// Reverse the layers so we can use range loops to iterate the list in the
// right priority order.
for i, j := 0, len(layers)-1; i < j; {
layers[i], layers[j] = layers[j], layers[i]
i++
j--
}
return layerFS(layers)
}
type layerFS []fs.FS
func (layers layerFS) Open(name string) (fs.File, error) {
visibleLayers, err := layers.lookup("open", name)
if err != nil {
return nil, err
}
files := make([]fs.File, 0, len(layers))
defer func() {
for _, f := range files {
f.Close()
}
}()
for _, layer := range visibleLayers {
f, err := layer.Open(name)
if err != nil {
return nil, err
}
files = append(files, f)
}
defer func() { files = nil }()
return &layerFile{layers: files, name: name}, nil
}
func (layers layerFS) Sub(name string) (fs.FS, error) {
visibleLayers, err := layers.lookup("open", name)
if err != nil {
return nil, err
}
for i, layer := range visibleLayers {
layer, err := fslink.Sub(layer, name)
if err != nil {
return nil, err
}
visibleLayers[i] = layer
}
return layerFS(visibleLayers), nil
}
func (layers layerFS) ReadLink(name string) (string, error) {
visibleLayers, err := layers.lookup("readlink", name)
if err != nil {
return "", err
}
for _, layer := range visibleLayers {
link, err := fslink.ReadLink(layer, name)
switch {
case err == nil:
return link, nil
case errors.Is(err, fs.ErrNotExist):
case errors.Is(err, fs.ErrInvalid):
default:
return "", err
}
}
return "", &fs.PathError{"readlink", name, fs.ErrNotExist}
}
func (layers layerFS) lookup(op, name string) ([]fs.FS, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{op, name, fs.ErrNotExist}
}
if name == "." {
return layers, nil
}
visibleLayers := append([]fs.FS{}, layers...)
// To determine if a layer is masking the ones below, we have to walk
// through each element of the path and determine if any of the upper
// layer has whiteout files that would mask the lower layers.
path := name
walk := 0
for walk < len(path) && len(visibleLayers) > 0 {
if i := strings.IndexByte(path[walk:], '/'); i < 0 {
walk = len(path)
} else {
walk = walk + i
}
whiteoutOne, whiteoutAll := whiteout(path[:walk])
for i := 0; i < len(visibleLayers); {
s, err := fs.Stat(visibleLayers[i], path[:walk])
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
// The layer does not have the file, it cannot be part of the
// visible layers.
n := copy(visibleLayers[i:], visibleLayers[i+1:])
visibleLayers = visibleLayers[:i+n]
continue
} else if !s.IsDir() {
// The layer is not a directory, it will mask all the files in
// layers below. However, if this is not the top most layer it
// indicates that the previous layers contained directories and
// therefore the current layer cannot be included.
if i == 0 {
i++
}
visibleLayers = visibleLayers[:i]
break
}
if exist, err := hasOneOf(visibleLayers[i], whiteoutOne, whiteoutAll); err != nil {
return nil, err
} else if exist {
// The layer has whiteout files that mask all the layers below,
// we strip them out of the list of visible layers.
visibleLayers = visibleLayers[:i+1]
break
}
i++
}
walk++
}
if len(visibleLayers) == 0 {
return nil, &fs.PathError{op, name, fs.ErrNotExist}
}
return visibleLayers, nil
}
var (
_ fs.SubFS = (layerFS)(nil)
_ fslink.ReadLinkFS = (layerFS)(nil)
)
func whiteout(name string) (whiteoutOne, whiteoutAll string) {
dir, base := path.Split(name)
whiteoutOne = path.Join(dir, whiteoutPrefix+base)
whiteoutAll = path.Join(dir, whiteoutOpaque)
return
}
func hasOneOf(fsys fs.FS, names ...string) (bool, error) {
for _, name := range names {
_, err := fs.Stat(fsys, name)
if err == nil {
return true, nil
}
if !errors.Is(err, fs.ErrNotExist) {
return false, err
}
}
return false, nil
}
type layerFile struct {
layers []fs.File
name string
// lazily allocated by ReadDir
dirReader *dirReader
}
func (f *layerFile) Close() error {
for _, layer := range f.layers {
layer.Close()
}
return nil
}
func (f *layerFile) Stat() (fs.FileInfo, error) {
s, err := f.layers[0].Stat()
if err != nil {
return nil, err
}
return &layerInfo{s}, nil
}
func (f *layerFile) Read(b []byte) (int, error) {
return f.layers[0].Read(b)
}
func (f *layerFile) ReadAt(b []byte, offset int64) (int, error) {
if r, ok := f.layers[0].(io.ReaderAt); ok {
return r.ReadAt(b, offset)
}
return 0, &fs.PathError{"read", f.name, fs.ErrInvalid}
}
func (f *layerFile) Seek(offset int64, whence int) (int64, error) {
if s, ok := f.layers[0].(io.Seeker); ok {
offset, err := s.Seek(offset, whence)
if err != nil {
return offset, err
}
if offset == 0 && f.dirReader != nil {
// Using lseek to reset the directory position is supported by posix
// so we be good citizens and comply, assuming that if we get to
// this stage when Seek was called on a directory we must be running
// on a posix complient environment.
f.dirReader = nil
}
return offset, nil
}
return 0, &fs.PathError{"seek", f.name, fs.ErrInvalid}
}
func (f *layerFile) ReadDir(n int) ([]fs.DirEntry, error) {
if f.dirReader == nil {
files := make([]fs.ReadDirFile, 0, len(f.layers))
for _, layer := range f.layers {
if f, ok := layer.(fs.ReadDirFile); ok {
files = append(files, f)
}
}
f.dirReader = &dirReader{files: files}
}
if n < 0 {
n = 0
}
ret := make([]fs.DirEntry, 0, n)
err := f.dirReader.scan(n, func(e fs.DirEntry) error {
ret = append(ret, e)
return nil
})
return ret, err
}
var (
_ fs.ReadDirFile = (*layerFile)(nil)
_ io.ReaderAt = (*layerFile)(nil)
_ io.Seeker = (*layerFile)(nil)
)
type layerInfo struct{ fs.FileInfo }
func (info *layerInfo) Mode() fs.FileMode {
// Layers are read-only, so mask all write permissions on the files to let
// the application know that it is not allowed to write those layers.
mode := info.FileInfo.Mode()
mode &= ^fs.FileMode(0222)
return mode
}
type dirReader struct {
files []fs.ReadDirFile
names []string
masks map[string]struct{}
}
func (dir *dirReader) scan(n int, f func(fs.DirEntry) error) error {
if dir.masks == nil {
dir.masks = make(map[string]struct{})
}
dirents := 0
for len(dir.files) > 0 {
for {
entries, err := dir.files[0].ReadDir(n - dirents)
for _, entry := range entries {
name := entry.Name()
if _, seen := dir.masks[name]; seen {
continue
}
switch {
case name == whiteoutOpaque:
dir.files = dir.files[:1]
case strings.HasPrefix(name, whiteoutPrefix):
dir.names = append(dir.names, name[len(whiteoutPrefix):])
default:
dir.names = append(dir.names, name)
if err := f(entry); err != nil {
return err
}
dirents++
}
}
if n == 0 || err == io.EOF {
break
}
if n == dirents || err != nil {
return err
}
}
// Apply names after completing iteration of the layer otherwise
// it could end up mistakenly masking its own entries.
for _, name := range dir.names {
dir.masks[name] = struct{}{}
}
dir.names = dir.names[:0]
dir.files = dir.files[1:]
}
if dirents < n {
return io.EOF
}
return nil
}