Skip to content

Commit

Permalink
Merge pull request #31 from saracen/fix-buffer-size
Browse files Browse the repository at this point in the history
Fix buffer size limit
  • Loading branch information
saracen committed Sep 11, 2021
2 parents a58a00e + efc7f6d commit 9ba5721
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
8 changes: 7 additions & 1 deletion archiver_options.go
Expand Up @@ -4,7 +4,10 @@ import (
"errors"
)

var ErrMinConcurrency = errors.New("concurrency must be at least 1")
var (
ErrMinConcurrency = errors.New("concurrency must be at least 1")
ErrMinBufferSize = errors.New("buffer size option cannot be less than -1")
)

// ArchiverOption is an option used when creating an archiver.
type ArchiverOption func(*archiverOptions) error
Expand Down Expand Up @@ -47,6 +50,9 @@ func WithArchiverConcurrency(n int) ArchiverOption {
// written to temporary files before being written back to the zip file.
func WithArchiverBufferSize(n int) ArchiverOption {
return func(o *archiverOptions) error {
if n < -1 {
return ErrMinBufferSize
}
o.bufferSize = n
return nil
}
Expand Down
47 changes: 47 additions & 0 deletions archiver_test.go
Expand Up @@ -301,6 +301,53 @@ func TestArchiveWithConcurrency(t *testing.T) {
}
}

func TestArchiveWithBufferSize(t *testing.T) {
testFiles := map[string]testFile{
"foo.go": {mode: 0666},
"bar.go": {mode: 0666},
}

tests := []struct {
buffersize int
pass bool
}{
{-100, false},
{-2, false},
{-1, true},
{0, true},
{32 * 1024, true},
{64 * 1024, true},
}

files, dir := testCreateFiles(t, testFiles)
defer os.RemoveAll(dir)

for _, test := range tests {
func() {
f, err := ioutil.TempFile("", "fastzip-test")
require.NoError(t, err)
defer os.Remove(f.Name())
defer f.Close()

a, err := NewArchiver(f, dir, WithArchiverBufferSize(test.buffersize))
if !test.pass {
require.Error(t, err)
return
}

require.NoError(t, err)
require.NoError(t, a.Archive(context.Background(), files))
require.NoError(t, a.Close())

bytes, entries := a.Written()
require.EqualValues(t, 0, bytes)
require.EqualValues(t, 3, entries)

testExtract(t, f.Name(), testFiles)
}()
}
}

func TestArchiveChroot(t *testing.T) {
dir, err := ioutil.TempDir("", "fastzip-test")
require.NoError(t, err)
Expand Down

0 comments on commit 9ba5721

Please sign in to comment.