Support applying with parent directories
Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/containerd/containerd/archive"
|
||||
"github.com/containerd/containerd/errdefs"
|
||||
"github.com/containerd/containerd/mount"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -31,60 +32,97 @@ import (
|
||||
func apply(ctx context.Context, mounts []mount.Mount, r io.Reader) error {
|
||||
switch {
|
||||
case len(mounts) == 1 && mounts[0].Type == "overlay":
|
||||
path, err := getOverlayPath(mounts[0].Options)
|
||||
path, parents, err := getOverlayPath(mounts[0].Options)
|
||||
if err != nil {
|
||||
if errdefs.IsInvalidArgument(err) {
|
||||
break
|
||||
}
|
||||
return err
|
||||
}
|
||||
_, err = archive.Apply(ctx, path, r,
|
||||
archive.WithConvertWhiteout(archive.OverlayConvertWhiteout))
|
||||
opts := []archive.ApplyOpt{
|
||||
archive.WithConvertWhiteout(archive.OverlayConvertWhiteout),
|
||||
}
|
||||
if len(parents) > 0 {
|
||||
opts = append(opts, archive.WithParents(parents))
|
||||
}
|
||||
_, err = archive.Apply(ctx, path, r, opts...)
|
||||
return err
|
||||
case len(mounts) == 1 && mounts[0].Type == "aufs":
|
||||
path, err := getAufsPath(mounts[0].Options)
|
||||
path, parents, err := getAufsPath(mounts[0].Options)
|
||||
if err != nil {
|
||||
if errdefs.IsInvalidArgument(err) {
|
||||
break
|
||||
}
|
||||
return err
|
||||
}
|
||||
_, err = archive.Apply(ctx, path, r,
|
||||
archive.WithConvertWhiteout(archive.AufsConvertWhiteout))
|
||||
opts := []archive.ApplyOpt{
|
||||
archive.WithConvertWhiteout(archive.AufsConvertWhiteout),
|
||||
}
|
||||
if len(parents) > 0 {
|
||||
opts = append(opts, archive.WithParents(parents))
|
||||
}
|
||||
_, err = archive.Apply(ctx, path, r, opts...)
|
||||
return err
|
||||
default:
|
||||
return mount.WithTempMount(ctx, mounts, func(root string) error {
|
||||
_, err := archive.Apply(ctx, root, r)
|
||||
return err
|
||||
})
|
||||
}
|
||||
return mount.WithTempMount(ctx, mounts, func(root string) error {
|
||||
_, err := archive.Apply(ctx, root, r)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func getOverlayPath(options []string) (string, error) {
|
||||
func getOverlayPath(options []string) (upper string, lower []string, err error) {
|
||||
const upperdirPrefix = "upperdir="
|
||||
const lowerdirPrefix = "lowerdir="
|
||||
|
||||
for _, o := range options {
|
||||
if strings.HasPrefix(o, upperdirPrefix) {
|
||||
return strings.TrimPrefix(o, upperdirPrefix), nil
|
||||
upper = strings.TrimPrefix(o, upperdirPrefix)
|
||||
} else if strings.HasPrefix(o, lowerdirPrefix) {
|
||||
lower = strings.Split(strings.TrimPrefix(o, lowerdirPrefix), ":")
|
||||
}
|
||||
}
|
||||
return "", errors.New("upperdir not found")
|
||||
if upper == "" {
|
||||
return "", nil, errors.Wrap(errdefs.ErrInvalidArgument, "upperdir not found")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func getAufsPath(options []string) (string, error) {
|
||||
// getAufsPath handles options as given by the containerd aufs package only,
|
||||
// formatted as "br:<upper>=rw[:<lower>=ro+wh]*"
|
||||
func getAufsPath(options []string) (upper string, lower []string, err error) {
|
||||
const (
|
||||
sep = ":"
|
||||
brPrefix1 = "br:"
|
||||
brPrefix2 = "br="
|
||||
rwSuffix = "=rw"
|
||||
sep = ":"
|
||||
brPrefix = "br:"
|
||||
rwSuffix = "=rw"
|
||||
roSuffix = "=ro+wh"
|
||||
)
|
||||
for _, o := range options {
|
||||
if strings.HasPrefix(o, brPrefix1) {
|
||||
o = strings.TrimPrefix(o, brPrefix1)
|
||||
} else if strings.HasPrefix(o, brPrefix2) {
|
||||
o = strings.TrimPrefix(o, brPrefix2)
|
||||
if strings.HasPrefix(o, brPrefix) {
|
||||
o = strings.TrimPrefix(o, brPrefix)
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, b := range strings.Split(o, sep) {
|
||||
if strings.HasSuffix(b, rwSuffix) {
|
||||
return strings.TrimSuffix(b, rwSuffix), nil
|
||||
if upper != "" {
|
||||
return "", nil, errors.Wrap(errdefs.ErrInvalidArgument, "multiple rw branch found")
|
||||
}
|
||||
upper = strings.TrimSuffix(b, rwSuffix)
|
||||
} else if strings.HasSuffix(b, roSuffix) {
|
||||
if upper == "" {
|
||||
return "", nil, errors.Wrap(errdefs.ErrInvalidArgument, "rw branch be first")
|
||||
}
|
||||
lower = append(lower, strings.TrimSuffix(b, roSuffix))
|
||||
} else {
|
||||
return "", nil, errors.Wrap(errdefs.ErrInvalidArgument, "unhandled aufs suffix")
|
||||
}
|
||||
|
||||
}
|
||||
break
|
||||
}
|
||||
return "", errors.New("rw branch not found")
|
||||
if upper == "" {
|
||||
return "", nil, errors.Wrap(errdefs.ErrInvalidArgument, "rw branch not found")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@@ -23,34 +23,32 @@ import (
|
||||
)
|
||||
|
||||
func TestGetOverlayPath(t *testing.T) {
|
||||
good := []string{"upperdir=/test/upper", "lowerdir=/test/lower", "workdir=/test/work"}
|
||||
path, err := getOverlayPath(good)
|
||||
good := []string{"upperdir=/test/upper", "lowerdir=/test/lower1:/test/lower2", "workdir=/test/work"}
|
||||
path, parents, err := getOverlayPath(good)
|
||||
if err != nil {
|
||||
t.Fatalf("Get overlay path failed: %v", err)
|
||||
}
|
||||
if path != "/test/upper" {
|
||||
t.Fatalf("Unexpected upperdir: %q", path)
|
||||
}
|
||||
if len(parents) != 2 || parents[0] != "/test/lower1" || parents[1] != "/test/lower2" {
|
||||
t.Fatalf("Unexpected parents: %v", parents)
|
||||
}
|
||||
|
||||
bad := []string{"lowerdir=/test/lower"}
|
||||
_, err = getOverlayPath(bad)
|
||||
_, _, err = getOverlayPath(bad)
|
||||
if err == nil {
|
||||
t.Fatalf("An error is expected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAufsPath(t *testing.T) {
|
||||
rwDir := "/test/rw"
|
||||
for _, test := range []struct {
|
||||
options []string
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
options: []string{"random:option", "br:" + rwDir + "=rw:/test/ro=ro+wh"},
|
||||
expectErr: false,
|
||||
},
|
||||
{
|
||||
options: []string{"random:option", "br=" + rwDir + "=rw:/test/ro=ro+wh"},
|
||||
options: []string{"random:option", "br:/test/rw=rw:/test/ro=ro+wh"},
|
||||
expectErr: false,
|
||||
},
|
||||
{
|
||||
@@ -62,7 +60,7 @@ func TestGetAufsPath(t *testing.T) {
|
||||
expectErr: true,
|
||||
},
|
||||
} {
|
||||
path, err := getAufsPath(test.options)
|
||||
path, parents, err := getAufsPath(test.options)
|
||||
if test.expectErr {
|
||||
if err == nil {
|
||||
t.Fatalf("An error is expected")
|
||||
@@ -72,8 +70,12 @@ func TestGetAufsPath(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Get aufs path failed: %v", err)
|
||||
}
|
||||
if path != rwDir {
|
||||
if path != "/test/rw" {
|
||||
t.Fatalf("Unexpected rw dir: %q", path)
|
||||
}
|
||||
if len(parents) != 1 || parents[0] != "/test/ro" {
|
||||
t.Fatalf("Unexpected parents: %v", parents)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -139,7 +139,7 @@ func (s windowsDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts
|
||||
return emptyDesc, err
|
||||
}
|
||||
|
||||
if _, err := archive.Apply(ctx, layer, rc, archive.WithParentLayers(parentLayerPaths), archive.AsWindowsContainerLayer()); err != nil {
|
||||
if _, err := archive.Apply(ctx, layer, rc, archive.WithParents(parentLayerPaths), archive.AsWindowsContainerLayer()); err != nil {
|
||||
return emptyDesc, err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user