File: hardlinks.go

package info (click to toggle)
golang-github-tonistiigi-fsutil 0.0~git20240925.a340068-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 644 kB
  • sloc: sh: 21; makefile: 5
file content (116 lines) | stat: -rw-r--r-- 2,462 bytes parent folder | download | duplicates (13)
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
package fsutil

import (
	"context"
	"io"
	gofs "io/fs"
	"os"
	"syscall"

	"github.com/pkg/errors"
	"github.com/tonistiigi/fsutil/types"
)

// Hardlinks validates that all targets for links were part of the changes

type Hardlinks struct {
	seenFiles map[string]struct{}
}

func (v *Hardlinks) HandleChange(kind ChangeKind, p string, fi os.FileInfo, err error) error {
	if err != nil {
		return err
	}

	if v.seenFiles == nil {
		v.seenFiles = make(map[string]struct{})
	}

	if kind == ChangeKindDelete {
		return nil
	}

	stat, ok := fi.Sys().(*types.Stat)
	if !ok {
		return errors.WithStack(&os.PathError{Path: p, Err: syscall.EBADMSG, Op: "change without stat info"})
	}

	if fi.IsDir() || fi.Mode()&os.ModeSymlink != 0 {
		return nil
	}

	if len(stat.Linkname) > 0 {
		if _, ok := v.seenFiles[stat.Linkname]; !ok {
			return errors.Errorf("invalid link %s to unknown path: %q", p, stat.Linkname)
		}
	} else {
		v.seenFiles[p] = struct{}{}
	}

	return nil
}

// WithHardlinkReset returns a FS that fixes hardlinks for FS that has been filtered
// so that original hardlink sources might be missing
func WithHardlinkReset(fs FS) FS {
	return &hardlinkFilter{fs: fs}
}

type hardlinkFilter struct {
	fs FS
}

var _ FS = &hardlinkFilter{}

func (r *hardlinkFilter) Walk(ctx context.Context, target string, fn gofs.WalkDirFunc) error {
	seenFiles := make(map[string]string)
	return r.fs.Walk(ctx, target, func(path string, entry gofs.DirEntry, err error) error {
		if err != nil {
			return err
		}

		fi, err := entry.Info()
		if err != nil {
			return err
		}

		if fi.IsDir() || fi.Mode()&os.ModeSymlink != 0 {
			return fn(path, entry, nil)
		}

		stat, ok := fi.Sys().(*types.Stat)
		if !ok {
			return errors.WithStack(&os.PathError{Path: path, Err: syscall.EBADMSG, Op: "fileinfo without stat info"})
		}

		if stat.Linkname != "" {
			if v, ok := seenFiles[stat.Linkname]; !ok {
				seenFiles[stat.Linkname] = stat.Path
				stat.Linkname = ""
				entry = &dirEntryWithStat{DirEntry: entry, stat: stat}
			} else {
				if v != stat.Path {
					stat.Linkname = v
					entry = &dirEntryWithStat{DirEntry: entry, stat: stat}
				}
			}
		}

		seenFiles[path] = stat.Path

		return fn(path, entry, nil)
	})
}

func (r *hardlinkFilter) Open(p string) (io.ReadCloser, error) {
	return r.fs.Open(p)
}

type dirEntryWithStat struct {
	gofs.DirEntry
	stat *types.Stat
}

func (d *dirEntryWithStat) Info() (gofs.FileInfo, error) {
	return &StatInfo{d.stat}, nil
}