From 0c20554f69a3da246ab3cc5a11b69361ac489758 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 21 Mar 2019 15:26:59 -0700 Subject: [PATCH 1/2] image build: fix archive detection As pointed out in #1459, docker cli fails to detect that the input is a tarball, in case it is generated by `git archive --format=tgz`. This happens because `git archive` adds some metadata to the initial tar header, and so it is more than 1 block (of 512 bytes) long, while we only provide 1 block to archive/tar.Next() and it fails. To fix, give it 2 blocks :) Signed-off-by: Kir Kolyshkin --- cli/command/image/build/context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/command/image/build/context.go b/cli/command/image/build/context.go index b7170df88e..5d0e96737d 100644 --- a/cli/command/image/build/context.go +++ b/cli/command/image/build/context.go @@ -89,7 +89,7 @@ func ValidateContextDirectory(srcPath string, excludes []string) error { func DetectArchiveReader(input io.ReadCloser) (rc io.ReadCloser, isArchive bool, err error) { buf := bufio.NewReader(input) - magic, err := buf.Peek(archiveHeaderSize) + magic, err := buf.Peek(archiveHeaderSize * 2) if err != nil && err != io.EOF { return nil, false, errors.Errorf("failed to peek context header from STDIN: %v", err) } From 06e250d37b779a199055cb0b0872757ae5dbe487 Mon Sep 17 00:00:00 2001 From: Lifubang Date: Fri, 19 Oct 2018 23:37:58 +0800 Subject: [PATCH 2/2] add test case for DetectArchiveReader Signed-off-by: Lifubang --- cli/command/image/build/context_test.go | 33 +++++++++++++++++++++ cli/command/image/testdata/Dockerfile.test | 3 ++ cli/command/image/testdata/gittar.test | Bin 0 -> 10240 bytes cli/command/image/testdata/tar.test | Bin 0 -> 10240 bytes 4 files changed, 36 insertions(+) create mode 100644 cli/command/image/testdata/Dockerfile.test create mode 100644 cli/command/image/testdata/gittar.test create mode 100644 cli/command/image/testdata/tar.test diff --git a/cli/command/image/build/context_test.go b/cli/command/image/build/context_test.go index d74add88b2..e0922542e7 100644 --- a/cli/command/image/build/context_test.go +++ b/cli/command/image/build/context_test.go @@ -297,3 +297,36 @@ func TestIsArchive(t *testing.T) { assert.Check(t, is.Equal(testcase.expected, IsArchive(testcase.header)), testcase.doc) } } + +func TestDetectArchiveReader(t *testing.T) { + var testcases = []struct { + file string + desc string + expected bool + }{ + { + file: "../testdata/tar.test", + desc: "tar file without pax headers", + expected: true, + }, + { + file: "../testdata/gittar.test", + desc: "tar file with pax headers", + expected: true, + }, + { + file: "../testdata/Dockerfile.test", + desc: "not a tar file", + expected: false, + }, + } + for _, testcase := range testcases { + content, err := os.Open(testcase.file) + assert.NilError(t, err) + defer content.Close() + + _, isArchive, err := DetectArchiveReader(content) + assert.NilError(t, err) + assert.Check(t, is.Equal(testcase.expected, isArchive), testcase.file) + } +} diff --git a/cli/command/image/testdata/Dockerfile.test b/cli/command/image/testdata/Dockerfile.test new file mode 100644 index 0000000000..5b54d9b40e --- /dev/null +++ b/cli/command/image/testdata/Dockerfile.test @@ -0,0 +1,3 @@ +FROM busybox +ADD ./README.md / +CMD ["cat", "/README.md"] diff --git a/cli/command/image/testdata/gittar.test b/cli/command/image/testdata/gittar.test new file mode 100644 index 0000000000000000000000000000000000000000..86ccb3187b4759770e6f472406e723fc822a0fb3 GIT binary patch literal 10240 zcmeIzO;5rw90u^6`4n|$16{ioF!4YfMh}Y#M~NYASCBZY8N=YWx2R#sIFJ!YFg(5N z<-d0AZ+o^6wVup!r&T_AwJNhkaQsGnlTrqot#9?MFUCQ{xnx{+nb_2$Sg(K9mBO2<>9`o_o}r;#?S z@1KaJaim#awa$7$m|P=)#F;l+Oy{|6)UDCQn`lU%@p?r5jw$U%_55=xL_ize<-m(C zJpcEjr9O`zc;?9-1tg!fvuS+@TCSZtPHP2Qnql*5SVW><&{Vtb)i8{_iCv zZxYg9d1#1>_p*9hu1a;WGQ0A_p=!rn{T_`;t+LMfTjx{joif&rMqY`z*o_Vj-qcNA ziKr^Z=3x5hv8#Wy;W{QBW>1;SH}&T{Zj*5c(jUwwp}*}eHP?JFY-p;h~9Vf zYjPd0$1OVqem-``x^voX=>N>WbNl?8w9UV9DfvIg{M5bA(@g4oS=@j BJ+J@( literal 0 HcmV?d00001