File: hardlinks.go

package info (click to toggle)
golang-github-tonistiigi-fsutil 0.0~git20200331.f427cf1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 400 kB
  • sloc: makefile: 7
file content (47 lines) | stat: -rw-r--r-- 883 bytes parent folder | download | duplicates (3)
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
package fsutil

import (
	"os"

	"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.Errorf("invalid change without stat info: %s", p)
	}

	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
}