From 5eac3fd7b128209061cfe9fa6f7cc66077837ae8 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 11 May 2017 09:36:04 -0300 Subject: [PATCH] dont add folders to archive --- pipeline/archive/tar/tar.go | 2 +- pipeline/archive/tar/tar_test.go | 3 +++ pipeline/archive/zip/zip.go | 4 ++++ pipeline/archive/zip/zip_test.go | 3 +++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pipeline/archive/tar/tar.go b/pipeline/archive/tar/tar.go index a9f907562..62edbbee3 100644 --- a/pipeline/archive/tar/tar.go +++ b/pipeline/archive/tar/tar.go @@ -46,7 +46,7 @@ func (a Archive) Add(name, path string) (err error) { _ = file.Close() }() stat, err := file.Stat() - if err != nil { + if err != nil || stat.IsDir() { return } header := new(tar.Header) diff --git a/pipeline/archive/tar/tar_test.go b/pipeline/archive/tar/tar_test.go index 5e24edaf6..6fa150280 100644 --- a/pipeline/archive/tar/tar_test.go +++ b/pipeline/archive/tar/tar_test.go @@ -23,9 +23,12 @@ func TestTarGzFile(t *testing.T) { empty2, err := os.Create(folder + "/empty2.txt") assert.NoError(err) + assert.NoError(os.Mkdir(folder+"/folder-inside", 0755)) + archive := New(file) assert.NoError(archive.Add("empty.txt", empty.Name())) assert.Error(archive.Add("dont.txt", empty.Name()+"_nope")) + assert.NoError(archive.Add("empty.txt", folder+"/folder-inside")) assert.NoError(archive.Close()) assert.Error(archive.Add("empty2.txt", empty2.Name())) } diff --git a/pipeline/archive/zip/zip.go b/pipeline/archive/zip/zip.go index 232f7dac6..6da32bb5d 100644 --- a/pipeline/archive/zip/zip.go +++ b/pipeline/archive/zip/zip.go @@ -31,6 +31,10 @@ func (a Archive) Add(name, path string) (err error) { if err != nil { return } + stat, err := file.Stat() + if err != nil || stat.IsDir() { + return + } defer func() { _ = file.Close() }() f, err := a.z.Create(name) if err != nil { diff --git a/pipeline/archive/zip/zip_test.go b/pipeline/archive/zip/zip_test.go index 2084be2dc..584d93656 100644 --- a/pipeline/archive/zip/zip_test.go +++ b/pipeline/archive/zip/zip_test.go @@ -20,8 +20,11 @@ func TestZipFile(t *testing.T) { empty, err := os.Create(folder + "/empty.txt") assert.NoError(err) + assert.NoError(os.Mkdir(folder+"/folder-inside", 0755)) + archive := New(file) assert.NoError(archive.Add("empty.txt", empty.Name())) + assert.NoError(archive.Add("empty.txt", folder+"/folder-inside")) assert.Error(archive.Add("dont.txt", empty.Name()+"_nope")) assert.NoError(archive.Close()) }