|
| 1 | +package unionbackfill |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "io/fs" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/containers/storage/pkg/archive" |
| 12 | + "github.com/containers/storage/pkg/idtools" |
| 13 | + "github.com/containers/storage/pkg/system" |
| 14 | +) |
| 15 | + |
| 16 | +// NewBackfiller supplies a backfiller whose Backfill method provides the |
| 17 | +// ownership/permissions/attributes of a directory from a lower layer so that |
| 18 | +// we don't have to create it in an upper layer using default values that will |
| 19 | +// be mistaken for a reason that the directory was pulled up to that layer. |
| 20 | +func NewBackfiller(idmap *idtools.IDMappings, lowerDiffDirs []string) *backfiller { |
| 21 | + if idmap != nil { |
| 22 | + uidMaps, gidMaps := idmap.UIDs(), idmap.GIDs() |
| 23 | + if len(uidMaps) > 0 || len(gidMaps) > 0 { |
| 24 | + idmap = idtools.NewIDMappingsFromMaps(append([]idtools.IDMap{}, uidMaps...), append([]idtools.IDMap{}, gidMaps...)) |
| 25 | + } |
| 26 | + } |
| 27 | + return &backfiller{idmap: idmap, lowerDiffDirs: append([]string{}, lowerDiffDirs...)} |
| 28 | +} |
| 29 | + |
| 30 | +type backfiller struct { |
| 31 | + idmap *idtools.IDMappings |
| 32 | + lowerDiffDirs []string |
| 33 | +} |
| 34 | + |
| 35 | +// Backfill supplies the ownership/permissions/attributes of a directory from a |
| 36 | +// lower layer so that we don't have to create it in an upper layer using |
| 37 | +// default values that will be mistaken for a reason that the directory was |
| 38 | +// pulled up to that layer. |
| 39 | +func (b *backfiller) Backfill(pathname string) (*tar.Header, error) { |
| 40 | + for _, lowerDiffDir := range b.lowerDiffDirs { |
| 41 | + candidate := filepath.Join(lowerDiffDir, pathname) |
| 42 | + // if the asked-for path is in this lower, return a tar header for it |
| 43 | + if st, err := os.Lstat(candidate); err == nil { |
| 44 | + var linkTarget string |
| 45 | + if st.Mode()&fs.ModeType == fs.ModeSymlink { |
| 46 | + target, err := os.Readlink(candidate) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + linkTarget = target |
| 51 | + } |
| 52 | + hdr, err := tar.FileInfoHeader(st, linkTarget) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + // this is where we'd delete "opaque" from the header, if FileInfoHeader read xattrs |
| 57 | + hdr.Name = strings.Trim(filepath.ToSlash(pathname), "/") |
| 58 | + if st.Mode()&fs.ModeType == fs.ModeDir { |
| 59 | + hdr.Name += "/" |
| 60 | + } |
| 61 | + if b.idmap != nil && !b.idmap.Empty() { |
| 62 | + if uid, gid, err := b.idmap.ToContainer(idtools.IDPair{UID: hdr.Uid, GID: hdr.Gid}); err == nil { |
| 63 | + hdr.Uid, hdr.Gid = uid, gid |
| 64 | + } |
| 65 | + } |
| 66 | + return hdr, nil |
| 67 | + } |
| 68 | + // if the directory or any of its parents is marked opaque, we're done looking at lowers |
| 69 | + p := strings.Trim(pathname, "/") |
| 70 | + subpathname := "" |
| 71 | + for { |
| 72 | + dir, subdir := filepath.Split(p) |
| 73 | + dir = strings.Trim(dir, "/") |
| 74 | + if dir == p { |
| 75 | + break |
| 76 | + } |
| 77 | + // kernel overlay style |
| 78 | + xval, err := system.Lgetxattr(filepath.Join(lowerDiffDir, dir), archive.GetOverlayXattrName("opaque")) |
| 79 | + if err == nil && len(xval) == 1 && xval[0] == 'y' { |
| 80 | + return nil, nil |
| 81 | + } |
| 82 | + // aufs or fuse-overlayfs using aufs-like whiteouts |
| 83 | + if _, err := os.Stat(filepath.Join(lowerDiffDir, dir, archive.WhiteoutOpaqueDir)); err == nil { |
| 84 | + return nil, nil |
| 85 | + } |
| 86 | + // kernel overlay "redirect" - starting with the next lower layer, we'll need to look elsewhere |
| 87 | + subpathname = strings.Trim(path.Join(subdir, subpathname), "/") |
| 88 | + xval, err = system.Lgetxattr(filepath.Join(lowerDiffDir, dir), archive.GetOverlayXattrName("redirect")) |
| 89 | + if err == nil && len(xval) > 0 { |
| 90 | + subdir := string(xval) |
| 91 | + if path.IsAbs(subdir) { |
| 92 | + // path is relative to the root of the mount point |
| 93 | + pathname = path.Join(subdir, subpathname) |
| 94 | + } else { |
| 95 | + // path is relative to the current directory |
| 96 | + parent, _ := filepath.Split(dir) |
| 97 | + parent = strings.Trim(parent, "/") |
| 98 | + pathname = path.Join(parent, subdir, subpathname) |
| 99 | + } |
| 100 | + break |
| 101 | + } |
| 102 | + p = dir |
| 103 | + } |
| 104 | + } |
| 105 | + return nil, nil |
| 106 | +} |
0 commit comments