2017-05-15 17:13:34 -04:00
|
|
|
package fsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-11-17 05:31:07 -05:00
|
|
|
"syscall"
|
2017-05-15 17:13:34 -04:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-10-05 05:05:42 -04:00
|
|
|
"github.com/tonistiigi/fsutil/types"
|
2017-05-15 17:13:34 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-10-05 05:05:42 -04:00
|
|
|
stat, ok := fi.Sys().(*types.Stat)
|
2017-05-15 17:13:34 -04:00
|
|
|
if !ok {
|
2020-11-17 05:31:07 -05:00
|
|
|
return errors.WithStack(&os.PathError{Path: p, Err: syscall.EBADMSG, Op: "change without stat info"})
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|