Skip to content

Commit

Permalink
Add support for symbol link inside docker tar file (#987)
Browse files Browse the repository at this point in the history
* Add support for symbol link inside docker tar file

* docker tar file containing multiple images will have symbol link for
layer files to save space, this will read from the target file instead
of the symbol link file

* Add unit test for symbol link inside tar file

* Fix format
  • Loading branch information
yongxiu committed Apr 21, 2021
1 parent 2bd4a53 commit daa5add
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/v1/tarball/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"sync"

"github.com/google/go-containerregistry/internal/gzip"
Expand Down Expand Up @@ -216,6 +218,10 @@ func extractFileFromTar(opener Opener, filePath string) (io.ReadCloser, error) {
return nil, err
}
if hdr.Name == filePath {
if hdr.Typeflag == tar.TypeSymlink || hdr.Typeflag == tar.TypeLink {
currentDir := filepath.Dir(filePath)
return extractFileFromTar(opener, path.Join(currentDir, hdr.Linkname))
}
close = false
return tarFile{
Reader: tf,
Expand Down
32 changes: 32 additions & 0 deletions pkg/v1/tarball/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package tarball

import (
"io/ioutil"
"testing"

"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/validate"
)

Expand Down Expand Up @@ -95,3 +97,33 @@ func TestBundleMultiple(t *testing.T) {
})
}
}

func TestLayerLink(t *testing.T) {
tag, err := name.NewTag("bazel/v1/tarball:test_image_3", name.WeakValidation)
if err != nil {
t.Fatalf("Error creating tag: %v", err)
}
img, err := ImageFromPath("testdata/test_link.tar", &tag)
if err != nil {
t.Fatalf("Error loading image: %v", img)
}
hash := v1.Hash{
Algorithm: "sha256",
Hex: "8897395fd26dc44ad0e2a834335b33198cb41ac4d98dfddf58eced3853fa7b17",
}
layer, err := img.LayerByDiffID(hash)
if err != nil {
t.Fatalf("Error getting layer by diff ID: %v, %v", hash, err)
}
rc, err := layer.Uncompressed()
if err != nil {
t.Fatal(err)
}
bs, err := ioutil.ReadAll(rc)
if err != nil {
t.Fatal(err)
}
if len(bs) == 0 {
t.Errorf("layer.Uncompressed() returned a link file")
}
}
Binary file added pkg/v1/tarball/testdata/test_link.tar
Binary file not shown.

0 comments on commit daa5add

Please sign in to comment.