forked from containerd/stargz-snapshotter
/
logger.go
525 lines (462 loc) · 13.3 KB
/
logger.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
Copyright The containerd Authors.
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 logger
import (
"archive/tar"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/ktock/stargz-snapshotter/cmd/ctr-remote/util"
"golang.org/x/sys/unix"
)
const (
DefaultBlockSize = 4096
whiteoutPrefix = ".wh."
whiteoutOpaqueDir = whiteoutPrefix + whiteoutPrefix + ".opq"
xattrPAXRecordsPrefix = "SCHILY.xattr."
)
func Mount(mountPoint string, tarfile io.ReaderAt, monitor Monitor) (func() error, error) {
// Mount filesystem.
root := newRoot(tarfile, monitor)
conn := nodefs.NewFileSystemConnector(root, &nodefs.Options{
NegativeTimeout: 0,
AttrTimeout: time.Second,
EntryTimeout: time.Second,
Owner: nil, // preserve owners.
})
server, err := fuse.NewServer(conn.RawFS(), mountPoint, &fuse.MountOptions{AllowOther: true})
if err != nil {
return nil, fmt.Errorf("Error: Failed failed: %v", err)
}
if err := root.InitNodes(); err != nil {
return nil, fmt.Errorf("Error: Failed to initialize nodes: %v", err)
}
go server.Serve()
if err := server.WaitMount(); err != nil {
return nil, fmt.Errorf("Error: Failed to mount filesystem: %v", err)
}
return func() error { return server.Unmount() }, nil
}
// Nodes
func newRoot(tarfile io.ReaderAt, monitor Monitor) *node {
root := &node{
Node: nodefs.NewDefaultNode(),
tr: tarfile,
monitor: monitor,
}
root.root = root
now := time.Now()
root.attr.SetTimes(&now, &now, &now)
root.attr.Mode = fuse.S_IFDIR | 0777
return root
}
type node struct {
nodefs.Node // Embedded default node.
fullname string
attr fuse.Attr
r io.ReaderAt
link string // Symbolic link.
xattr map[string][]byte
root *node
tr io.ReaderAt
monitor Monitor
}
func (n *node) OnMount(conn *nodefs.FileSystemConnector) {
n.monitor.OnMount()
}
func (n *node) OnUnmount() {
n.monitor.OnUnmount()
}
func (n *node) Lookup(out *fuse.Attr, name string, context *fuse.Context) (*nodefs.Inode, fuse.Status) {
c := n.Inode().GetChild(name)
if c == nil {
return nil, fuse.ENOENT
}
if s := c.Node().GetAttr(out, nil, context); s != fuse.OK {
return nil, s
}
n.monitor.OnLookup(n.fullname)
return c, fuse.OK
}
func (n *node) Deletable() bool {
// Undeletable because of read-only filesystem
return false
}
func (n *node) OnForget() {
// Do nothing because the node is undeletable.
n.monitor.OnForget(n.fullname)
}
func (n *node) Access(mode uint32, context *fuse.Context) (code fuse.Status) {
if context.Owner.Uid == 0 { // root can do anything.
return fuse.OK
}
if mode == 0 { // Requires nothing.
return fuse.OK
}
var attr fuse.Attr
s := n.GetAttr(&attr, nil, context)
if s != fuse.OK {
return s
}
var shift uint32
if attr.Owner.Uid == context.Owner.Uid {
shift = 6
} else if attr.Owner.Gid == context.Owner.Gid {
shift = 3
} else {
shift = 0
}
if mode<<shift&attr.Mode != 0 {
n.monitor.OnAccess(n.fullname)
return fuse.OK
}
return fuse.EPERM
}
func (n *node) Readlink(c *fuse.Context) ([]byte, fuse.Status) {
n.monitor.OnReadlink(n.fullname, n.link)
return []byte(n.link), fuse.OK
}
func (n *node) Open(flags uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) {
n.monitor.OnOpen(n.fullname)
// It is OK to return (nil, OK) here. In that case,
// the Node need to implement "Read" function.
return nil, fuse.OK
}
func (n *node) Read(file nodefs.File, dest []byte, off int64, context *fuse.Context) (fuse.ReadResult, fuse.Status) {
if _, err := n.r.ReadAt(dest, off); err != nil && err != io.EOF {
fmt.Printf("Error: Failed to read %s: %v", n.fullname, err)
return nil, fuse.EIO
}
n.monitor.OnRead(n.fullname, off, int64(len(dest)))
return fuse.ReadResultData(dest), fuse.OK
}
func (n *node) GetAttr(out *fuse.Attr, file nodefs.File, context *fuse.Context) (code fuse.Status) {
n.dumpAttr(out)
n.monitor.OnGetAttr(n.fullname)
return fuse.OK
}
func (n *node) GetXAttr(attribute string, context *fuse.Context) (data []byte, code fuse.Status) {
if v, ok := n.xattr[attribute]; ok {
return v, fuse.OK
}
return nil, fuse.ENOATTR
}
func (n *node) ListXAttr(ctx *fuse.Context) (attrs []string, code fuse.Status) {
for k := range n.xattr {
attrs = append(attrs, k)
}
return attrs, fuse.OK
}
func (n *node) StatFs() *fuse.StatfsOut {
// http://man7.org/linux/man-pages/man2/statfs.2.html
return &fuse.StatfsOut{
Blocks: 0, // dummy
Bfree: 0,
Bavail: 0,
Files: 0, // dummy
Ffree: 0,
Bsize: 0, // dummy
NameLen: 1<<32 - 1,
Frsize: 0, // dummy
Padding: 0,
Spare: [6]uint32{},
}
}
func (n *node) dumpAttr(out *fuse.Attr) {
out.Ino = n.attr.Ino
out.Size = n.attr.Size
out.Blocks = n.attr.Blocks
out.Atime = n.attr.Atime
out.Mtime = n.attr.Mtime
out.Ctime = n.attr.Ctime
out.Atimensec = n.attr.Atimensec
out.Mtimensec = n.attr.Mtimensec
out.Ctimensec = n.attr.Ctimensec
out.Mode = n.attr.Mode
out.Nlink = n.attr.Nlink
out.Owner = n.attr.Owner
out.Rdev = n.attr.Rdev
out.Blksize = n.attr.Blksize
out.Padding = n.attr.Padding
}
func (n *node) headerToAttr(h *tar.Header) fuse.Attr {
out := fuse.Attr{}
out.Size = uint64(h.Size)
out.Blocks = uint64(h.Size/DefaultBlockSize + 1)
out.Atime = uint64(h.AccessTime.Unix())
out.Mtime = uint64(h.ModTime.Unix())
out.Ctime = uint64(h.ChangeTime.Unix())
out.Atimensec = uint32(h.AccessTime.UnixNano())
out.Mtimensec = uint32(h.ModTime.UnixNano())
out.Ctimensec = uint32(h.ChangeTime.UnixNano())
out.Mode = fileModeToSystemMode(h.FileInfo().Mode())
out.Blksize = uint32(DefaultBlockSize)
out.Owner = fuse.Owner{Uid: uint32(h.Uid), Gid: uint32(h.Gid)}
out.Rdev = uint32(unix.Mkdev(uint32(h.Devmajor), uint32(h.Devminor)))
// ad-hoc
out.Ino = 0
out.Nlink = 1
out.Padding = 0
return out
}
func (n *node) InitNodes() error {
pw, err := util.NewPositionWatcher(n.tr)
if err != nil {
return fmt.Errorf("Failed to make position watcher: %v", err)
}
tr := tar.NewReader(pw)
// Walk functions for nodes
getormake := func(n *nodefs.Inode, base string) (c *nodefs.Inode, err error) {
if c = n.GetChild(base); c == nil {
// Make temporary dummy node.
c = n.NewChild(base, true, &node{Node: nodefs.NewDefaultNode()})
}
return
}
getnode := func(n *nodefs.Inode, base string) (c *nodefs.Inode, err error) {
if c = n.GetChild(base); c == nil {
// the original node isn't found. We do not allow it.
return nil, fmt.Errorf("Node %q not found", base)
}
return
}
var whiteouts []string
// Walk through all nodes.
for {
// Fetch and parse next header.
h, err := tr.Next()
if err != nil {
if err != io.EOF {
return fmt.Errorf("Failed to parse tar file: %v", err)
}
break
}
var (
fullname = filepath.Clean(h.Name)
dir, base = filepath.Split(fullname)
parentDir *nodefs.Inode
existingNode *nodefs.Inode
xattrs = make(map[string][]byte)
)
if parentDir, err = n.walkDown(dir, getormake); err != nil {
return fmt.Errorf("failed to make dummy nodes: %v", err)
}
existingNode = parentDir.GetChild(base)
if h.PAXRecords != nil {
for k, v := range h.PAXRecords {
if strings.HasPrefix(k, xattrPAXRecordsPrefix) {
xattrs[k[len(xattrPAXRecordsPrefix):]] = []byte(v)
}
}
}
switch {
case existingNode != nil:
if !existingNode.IsDir() {
return fmt.Errorf("node \"%s\" is placeholder but not a directory",
fullname)
}
// This is "placeholder node" which has been registered in previous loop as
// an intermediate directory. Now we update it with real one.
ph, ok := existingNode.Node().(*node)
if !ok {
return fmt.Errorf("invalid placeholder node %q", fullname)
}
ph.Node = nodefs.NewDefaultNode()
ph.fullname = fullname
ph.attr = n.headerToAttr(h)
ph.link = h.Linkname
ph.xattr = overrideXattr(ph.xattr, xattrs) // preserve previously assigned xattr
ph.root = n.root
ph.tr = n.tr
ph.monitor = n.monitor
case strings.HasPrefix(base, whiteoutPrefix):
if base == whiteoutOpaqueDir {
// This node is opaque directory indicator so we append opaque xattr to
// the parent directory.
pn, ok := parentDir.Node().(*node)
if !ok {
return fmt.Errorf("parent node %q isn't valid node", dir)
}
if pn.xattr == nil {
pn.xattr = make(map[string][]byte)
}
pn.xattr["trusted.overlay.opaque"] = []byte("y")
} else {
// This node is a whiteout, so we don't want to show it.
// We record it now and then hide the target node later.
whiteouts = append(whiteouts, fullname)
}
case h.Typeflag == tar.TypeLink:
if h.Linkname == "" {
return fmt.Errorf("Linkname of hardlink %s is not found", fullname)
}
// This node is a hardlink. Same as major tar tools(GNU tar etc.),
// we pretend that the target node of this hard link has already been appeared.
target, err := n.walkDown(filepath.Clean(h.Linkname), getnode)
if err != nil {
return fmt.Errorf("hardlink(%q ==> %q) is not found: %v",
fullname, h.Linkname, err)
}
n, ok := target.Node().(*node)
if !ok {
return fmt.Errorf("original node %q isn't valid node", h.Linkname)
}
n.attr.Nlink++
// We register the target node as name "base". When we query this hardlink node,
// we can easily get the target name by seeing target's fullname field.
parentDir.AddChild(base, target)
default:
// Normal node so simply create it.
_ = parentDir.NewChild(base, h.FileInfo().IsDir(), &node{
Node: nodefs.NewDefaultNode(),
fullname: fullname,
attr: n.headerToAttr(h),
r: io.NewSectionReader(n.tr, pw.CurrentPos(), h.Size),
link: h.Linkname,
xattr: xattrs,
root: n.root,
tr: n.tr,
monitor: n.monitor,
})
}
continue
}
// Add whiteout nodes if necessary. If an entry exists as both of a
// whiteout file and a normal entry, we simply prioritize the normal entry.
for _, w := range whiteouts {
dir, base := filepath.Split(w)
if _, err := n.walkDown(filepath.Join(dir, base[len(whiteoutPrefix):]), getnode); err != nil {
p, err := n.walkDown(dir, getnode)
if err != nil {
return fmt.Errorf("parent node of whiteout %q is not found", w)
}
p.NewChild(base[len(whiteoutPrefix):], false, &whiteout{nodefs.NewDefaultNode()})
}
}
return nil
}
type walkFunc func(n *nodefs.Inode, base string) (*nodefs.Inode, error)
func (n *node) walkDown(path string, walkFn walkFunc) (ino *nodefs.Inode, err error) {
ino = n.root.Inode()
for _, comp := range strings.Split(path, "/") {
if len(comp) == 0 {
continue
}
if ino == nil {
return nil, fmt.Errorf("corresponding node of %q is not found", comp)
}
if ino, err = walkFn(ino, comp); err != nil {
return nil, err
}
}
return
}
type whiteout struct {
nodefs.Node
}
func (w *whiteout) GetAttr(out *fuse.Attr, file nodefs.File, context *fuse.Context) (code fuse.Status) {
out.Ino = 0 // TODO
out.Size = 0
out.Blksize = uint32(DefaultBlockSize)
out.Blocks = 0
out.Mode = syscall.S_IFCHR
out.Owner = fuse.Owner{Uid: 0, Gid: 0}
out.Rdev = uint32(unix.Mkdev(0, 0))
out.Nlink = 1
out.Padding = 0 // TODO
return fuse.OK
}
// monitor
type Monitor interface {
OnMount()
OnUnmount()
OnLookup(name string)
OnForget(name string)
OnAccess(name string)
OnReadlink(name, linkname string)
OnOpen(name string)
OnRead(name string, off, size int64)
OnGetAttr(name string)
DumpLog() []string
}
func NewOpenReadMonitor() Monitor {
return &OpenReadMonitor{}
}
type OpenReadMonitor struct {
log []string
}
func (m *OpenReadMonitor) OnOpen(name string) {
m.log = append(m.log, name)
}
func (m *OpenReadMonitor) OnRead(name string, off, size int64) {
m.log = append(m.log, name)
}
func (m *OpenReadMonitor) DumpLog() []string {
return m.log
}
func (m *OpenReadMonitor) OnMount() {}
func (m *OpenReadMonitor) OnUnmount() {}
func (m *OpenReadMonitor) OnLookup(name string) {}
func (m *OpenReadMonitor) OnForget(name string) {}
func (m *OpenReadMonitor) OnAccess(name string) {}
func (m *OpenReadMonitor) OnReadlink(name, linkname string) {}
func (m *OpenReadMonitor) OnGetAttr(name string) {}
// Utilities
func overrideXattr(a, b map[string][]byte) map[string][]byte {
if b == nil {
return a
}
if a == nil {
return b
}
for k, v := range b {
a[k] = v
}
return a
}
func fileModeToSystemMode(m os.FileMode) uint32 {
// Convert os.FileMode to system's native bitmap.
sm := uint32(m & 0777)
switch m & os.ModeType {
case os.ModeDevice:
sm |= syscall.S_IFBLK
case os.ModeDevice | os.ModeCharDevice:
sm |= syscall.S_IFCHR
case os.ModeDir:
sm |= syscall.S_IFDIR
case os.ModeNamedPipe:
sm |= syscall.S_IFIFO
case os.ModeSymlink:
sm |= syscall.S_IFLNK
case os.ModeSocket:
sm |= syscall.S_IFSOCK
default: // regular file.
sm |= syscall.S_IFREG
}
if m&os.ModeSetgid != 0 {
sm |= syscall.S_ISGID
}
if m&os.ModeSetuid != 0 {
sm |= syscall.S_ISUID
}
if m&os.ModeSticky != 0 {
sm |= syscall.S_ISVTX
}
return sm
}