From 7bb9fc415ac50abf5f754005264bfe152eeca7d6 Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Mon, 23 Nov 2015 17:20:44 -0800 Subject: [PATCH] Fix for zero-sized layers Moved a defer up to a better spot. Fixed TestUntarPathWithInvalidDest to actually fail for the right reason Closes #18170 Signed-off-by: Doug Davis --- image/tarexport/load.go | 4 ++-- integration-cli/docker_cli_save_load_test.go | 9 +++++++++ integration-cli/fixtures/load/emptyLayer.tar | Bin 0 -> 30720 bytes pkg/archive/archive.go | 8 +++++++- pkg/archive/archive_test.go | 11 ++++++++--- 5 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 integration-cli/fixtures/load/emptyLayer.tar diff --git a/image/tarexport/load.go b/image/tarexport/load.go index 5d5507e0f3..4247e11f3c 100644 --- a/image/tarexport/load.go +++ b/image/tarexport/load.go @@ -112,12 +112,12 @@ func (l *tarexporter) loadLayer(filename string, rootFS image.RootFS) (layer.Lay logrus.Debugf("Error reading embedded tar: %v", err) return nil, err } + defer rawTar.Close() + inflatedLayerData, err := archive.DecompressStream(rawTar) if err != nil { return nil, err } - - defer rawTar.Close() defer inflatedLayerData.Close() return l.ls.Register(inflatedLayerData, rootFS.ChainID()) diff --git a/integration-cli/docker_cli_save_load_test.go b/integration-cli/docker_cli_save_load_test.go index b81a03319e..0c2f8f32bb 100644 --- a/integration-cli/docker_cli_save_load_test.go +++ b/integration-cli/docker_cli_save_load_test.go @@ -291,3 +291,12 @@ func (s *DockerSuite) TestSaveDirectoryPermissions(c *check.C) { c.Assert(found, checker.Equals, true, check.Commentf("failed to find the layer with the right content listing")) } + +// Test loading a weird image where one of the layers is of zero size. +// The layer.tar file is actually zero bytes, no padding or anything else. +// See issue: 18170 +func (s *DockerSuite) TestLoadZeroSizeLayer(c *check.C) { + testRequires(c, DaemonIsLinux) + + dockerCmd(c, "load", "-i", "fixtures/load/emptyLayer.tar") +} diff --git a/integration-cli/fixtures/load/emptyLayer.tar b/integration-cli/fixtures/load/emptyLayer.tar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oM1xKm>>Y5g-CYfCvx)B0vO)z%wTBe{YUtx&QzG literal 0 HcmV?d00001 diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index 1c8e1153b3..453777865f 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -128,7 +128,13 @@ func DecompressStream(archive io.Reader) (io.ReadCloser, error) { p := pools.BufioReader32KPool buf := p.Get(archive) bs, err := buf.Peek(10) - if err != nil { + if err != nil && err != io.EOF { + // Note: we'll ignore any io.EOF error because there are some odd + // cases where the layer.tar file will be empty (zero bytes) and + // that results in an io.EOF from the Peek() call. So, in those + // cases we'll just treat it as a non-compressed stream and + // that means just create an empty layer. + // See Issue 18170 return nil, err } diff --git a/pkg/archive/archive_test.go b/pkg/archive/archive_test.go index 6c54c02d18..0b5dbb17bb 100644 --- a/pkg/archive/archive_test.go +++ b/pkg/archive/archive_test.go @@ -216,11 +216,16 @@ func TestUntarPathWithInvalidDest(t *testing.T) { invalidDestFolder := path.Join(tempFolder, "invalidDest") // Create a src file srcFile := path.Join(tempFolder, "src") - _, err = os.Create(srcFile) + tarFile := path.Join(tempFolder, "src.tar") + os.Create(srcFile) + os.Create(invalidDestFolder) // being a file (not dir) should cause an error + cmd := exec.Command("/bin/sh", "-c", "tar cf "+tarFile+" "+srcFile) + _, err = cmd.CombinedOutput() if err != nil { - t.Fatalf("Fail to create the source file") + t.Fatal(err) } - err = UntarPath(srcFile, invalidDestFolder) + + err = UntarPath(tarFile, invalidDestFolder) if err == nil { t.Fatalf("UntarPath with invalid destination path should throw an error.") }