1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/vendor/github.com/tonistiigi/fsutil/hardlinks.go
Tonis Tiigi 7cfcf74a90 vendor: update deps for fssession
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2017-06-22 11:22:42 -07:00

46 lines
839 B
Go

package fsutil
import (
"os"
"github.com/pkg/errors"
)
// 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().(*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
}