| 1 | //go:build !windows |
| 2 | // +build !windows |
| 3 | |
| 4 | package fsutil |
| 5 | |
| 6 | import ( |
| 7 | "os" |
| 8 | "strings" |
| 9 | "syscall" |
| 10 | |
| 11 | "github.com/containerd/continuity/sysx" |
| 12 | "github.com/pkg/errors" |
| 13 | "github.com/tonistiigi/fsutil/types" |
| 14 | ) |
| 15 | |
| 16 | const xattrApplePrefix = "com.apple." |
| 17 | |
| 18 | func loadXattr(origpath string, stat *types.Stat) error { |
| 19 | xattrs, err := sysx.LListxattr(origpath) |
| 20 | if err != nil { |
| 21 | if errors.Is(err, syscall.ENOTSUP) { |
| 22 | return nil |
| 23 | } |
| 24 | return errors.Wrapf(err, "failed to xattr %s", origpath) |
| 25 | } |
| 26 | if len(xattrs) > 0 { |
| 27 | m := make(map[string][]byte) |
| 28 | for _, key := range xattrs { |
| 29 | if skipXattr(key) { |
| 30 | continue |
| 31 | } |
| 32 | |
| 33 | if v, err := sysx.LGetxattr(origpath, key); err == nil { |
| 34 | m[key] = v |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if len(m) > 0 { |
| 39 | stat.Xattrs = m |
| 40 | } |
| 41 | } |
| 42 | return nil |
| 43 | } |
| 44 | |
| 45 | func skipXattr(key string) bool { |
| 46 | if strings.HasPrefix(key, xattrApplePrefix) { |
| 47 | return true |
| 48 | } |
| 49 | return false |
| 50 | } |
| 51 | |
| 52 | func setUnixOpt(fi os.FileInfo, stat *types.Stat, path string, seenFiles map[uint64]string) { |
| 53 | s := fi.Sys().(*syscall.Stat_t) |
| 54 | |
| 55 | stat.Uid = s.Uid |
| 56 | stat.Gid = s.Gid |
| 57 | |
| 58 | if !fi.IsDir() { |
| 59 | if s.Mode&syscall.S_IFBLK != 0 || |
| 60 | s.Mode&syscall.S_IFCHR != 0 { |
| 61 | stat.Devmajor = int64(major(uint64(s.Rdev))) |
| 62 | stat.Devminor = int64(minor(uint64(s.Rdev))) |
| 63 | } |
| 64 | |
| 65 | ino := s.Ino |
| 66 | linked := false |
| 67 | if seenFiles != nil { |
| 68 | if s.Nlink > 1 { |
| 69 | if oldpath, ok := seenFiles[ino]; ok { |
| 70 | stat.Linkname = oldpath |
| 71 | stat.Size = 0 |
| 72 | linked = true |
| 73 | } |
| 74 | } |
| 75 | if !linked { |
| 76 | seenFiles[ino] = path |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func major(device uint64) uint64 { |
| 83 | return (device >> 8) & 0xfff |
| 84 | } |
| 85 | |
| 86 | func minor(device uint64) uint64 { |
| 87 | return (device & 0xff) | ((device >> 12) & 0xfff00) |
| 88 | } |