From 63f273750c37dd32d9aa18d4a9e10f768c08fe27 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Mon, 9 Apr 2018 22:34:09 +0000 Subject: [PATCH] Support symlink layer in image import. Signed-off-by: Lantao Liu --- pkg/containerd/importer/importer.go | 35 ++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/pkg/containerd/importer/importer.go b/pkg/containerd/importer/importer.go index 2025f03c0..085f8e3f7 100644 --- a/pkg/containerd/importer/importer.go +++ b/pkg/containerd/importer/importer.go @@ -60,6 +60,19 @@ func isLayerTar(name string) bool { return slashes == 2 && strings.HasSuffix(name, "/layer.tar") } +// followSymlinkLayer returns actual layer name of the symlink layer. +func followSymlinkLayer(name string) (string, error) { + parts := strings.Split(name, "/") + if len(parts) != 3 || parts[0] != ".." { + return "", errors.New("invalid symlink layer") + } + name = strings.TrimPrefix(name, "../") + if !isLayerTar(name) { + return "", errors.New("invalid layer tar") + } + return name, nil +} + // isDotJSON returns true if name is like "deadbeefdeadbeef.json" func isDotJSON(name string) bool { slashes := len(strings.Split(name, "/")) @@ -97,9 +110,10 @@ func Import(ctx context.Context, client *containerd.Client, reader io.Reader) (_ tr := tar.NewReader(reader) var ( - mfsts []manifestDotJSON - layers = make(map[string]ocispec.Descriptor) // key: filename (deadbeeddeadbeef/layer.tar) - configs = make(map[string]imageConfig) // key: filename (deadbeeddeadbeef.json) + mfsts []manifestDotJSON + symlinkLayers = make(map[string]string) // key: filename (deadbeeddeadbeef/layer.tar), value: linkname (targetlayerid/layer.tar) + layers = make(map[string]ocispec.Descriptor) // key: filename (deadbeeddeadbeef/layer.tar) + configs = make(map[string]imageConfig) // key: filename (deadbeeddeadbeef.json) ) for { hdr, err := tr.Next() @@ -109,6 +123,14 @@ func Import(ctx context.Context, client *containerd.Client, reader io.Reader) (_ if err != nil { return nil, errors.Wrap(err, "get next file") } + if hdr.Typeflag == tar.TypeSymlink && isLayerTar(hdr.Name) { + linkname, err := followSymlinkLayer(hdr.Linkname) + if err != nil { + return nil, errors.Wrapf(err, "follow symlink layer from %q to %q", hdr.Name, hdr.Linkname) + } + symlinkLayers[hdr.Name] = linkname + continue + } if hdr.Typeflag != tar.TypeReg && hdr.Typeflag != tar.TypeRegA { continue } @@ -136,6 +158,13 @@ func Import(ctx context.Context, client *containerd.Client, reader io.Reader) (_ continue } } + for name, linkname := range symlinkLayers { + desc, ok := layers[linkname] + if !ok { + return nil, errors.Errorf("no target for symlink layer from %q to %q", name, linkname) + } + layers[name] = desc + } var refs []string defer func() { if retErr == nil {