2017-05-04 00:49:12 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package fsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
|
2017-09-30 12:04:11 -04:00
|
|
|
"github.com/containerd/continuity/sysx"
|
2017-05-04 00:49:12 -04:00
|
|
|
"github.com/pkg/errors"
|
2018-10-05 05:04:52 -04:00
|
|
|
"github.com/tonistiigi/fsutil/types"
|
2017-05-04 00:49:12 -04:00
|
|
|
)
|
|
|
|
|
2018-10-05 05:04:52 -04:00
|
|
|
func loadXattr(origpath string, stat *types.Stat) error {
|
2017-05-04 00:49:12 -04:00
|
|
|
xattrs, err := sysx.LListxattr(origpath)
|
|
|
|
if err != nil {
|
2018-10-05 05:04:52 -04:00
|
|
|
if errors.Cause(err) == syscall.ENOTSUP {
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-04 00:49:12 -04:00
|
|
|
return errors.Wrapf(err, "failed to xattr %s", origpath)
|
|
|
|
}
|
|
|
|
if len(xattrs) > 0 {
|
|
|
|
m := make(map[string][]byte)
|
|
|
|
for _, key := range xattrs {
|
|
|
|
v, err := sysx.LGetxattr(origpath, key)
|
|
|
|
if err == nil {
|
|
|
|
m[key] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stat.Xattrs = m
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 05:04:52 -04:00
|
|
|
func setUnixOpt(fi os.FileInfo, stat *types.Stat, path string, seenFiles map[uint64]string) {
|
2017-05-04 00:49:12 -04:00
|
|
|
s := fi.Sys().(*syscall.Stat_t)
|
|
|
|
|
|
|
|
stat.Uid = s.Uid
|
|
|
|
stat.Gid = s.Gid
|
|
|
|
|
|
|
|
if !fi.IsDir() {
|
|
|
|
if s.Mode&syscall.S_IFBLK != 0 ||
|
|
|
|
s.Mode&syscall.S_IFCHR != 0 {
|
|
|
|
stat.Devmajor = int64(major(uint64(s.Rdev)))
|
|
|
|
stat.Devminor = int64(minor(uint64(s.Rdev)))
|
|
|
|
}
|
|
|
|
|
|
|
|
ino := s.Ino
|
2019-03-20 21:45:16 -04:00
|
|
|
linked := false
|
2018-10-05 05:04:52 -04:00
|
|
|
if seenFiles != nil {
|
|
|
|
if s.Nlink > 1 {
|
|
|
|
if oldpath, ok := seenFiles[ino]; ok {
|
|
|
|
stat.Linkname = oldpath
|
|
|
|
stat.Size_ = 0
|
2019-03-20 21:45:16 -04:00
|
|
|
linked = true
|
2018-10-05 05:04:52 -04:00
|
|
|
}
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
2019-03-20 21:45:16 -04:00
|
|
|
if !linked {
|
|
|
|
seenFiles[ino] = path
|
|
|
|
}
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func major(device uint64) uint64 {
|
|
|
|
return (device >> 8) & 0xfff
|
|
|
|
}
|
|
|
|
|
|
|
|
func minor(device uint64) uint64 {
|
|
|
|
return (device & 0xff) | ((device >> 12) & 0xfff00)
|
|
|
|
}
|