Skip to content

Commit 3a1a932

Browse files
committed
check for overflow when writing archive file
1 parent 9c31929 commit 3a1a932

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

archiver.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,20 @@ func (a *Archiver) archive(file *pool.File) error {
268268
}
269269

270270
_, err = io.Copy(fileWriter, &file.CompressedData)
271-
272271
if err != nil {
273272
return errors.Errorf("ERROR: could not write content for %s", file.Path)
274273
}
275274

275+
if file.Status == pool.FileFull {
276+
file.Overflow.Seek(0, io.SeekStart)
277+
_, err = io.Copy(fileWriter, file.Overflow)
278+
if err != nil {
279+
return errors.Errorf("ERROR: could not write overflow content for %s", file.Path)
280+
}
281+
282+
file.Overflow.Close()
283+
}
284+
276285
return nil
277286
}
278287

pool/file.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package pool
33
import (
44
"archive/zip"
55
"bytes"
6-
"io"
76
"io/fs"
87
"os"
98
"path/filepath"
@@ -36,7 +35,7 @@ func NewFile(path string, info fs.FileInfo, relativeTo string) (File, error) {
3635
return File{}, errors.Errorf("ERROR: could not get file info header for %s: %v", path, err)
3736
}
3837

39-
f := File{Path: path, Info: info, Header: hdr, CompressedData: *bytes.NewBuffer(make([]byte, 0, defaultBufferSize))}
38+
f := File{Path: path, Info: info, Header: hdr, CompressedData: *bytes.NewBuffer(make([]byte, 0, 50))}
4039
if relativeTo != "" {
4140
f.setNameRelativeTo(relativeTo)
4241
}
@@ -45,23 +44,31 @@ func NewFile(path string, info fs.FileInfo, relativeTo string) (File, error) {
4544
}
4645

4746
func (f *File) Write(p []byte) (n int, err error) {
48-
if f.CompressedData.Available() >= len(p) {
49-
f.written += int64(len(p))
50-
return f.CompressedData.Write(p)
47+
if f.CompressedData.Available() != 0 {
48+
maxWritable := min(f.CompressedData.Available(), len(p))
49+
f.written += int64(maxWritable)
50+
f.CompressedData.Write(p[:maxWritable])
51+
p = p[maxWritable:]
5152
}
5253

53-
if f.Overflow == nil {
54-
f.Overflow, err = os.CreateTemp("", "pzip-overflow")
54+
if len(p) > 0 {
55+
f.Status = FileFull
56+
57+
if f.Overflow == nil {
58+
f.Overflow, err = os.CreateTemp("", "pzip-overflow")
59+
if err != nil {
60+
return len(p), err
61+
}
62+
}
63+
64+
_, err := f.Overflow.Write(p)
5565
if err != nil {
5666
return len(p), err
5767
}
68+
f.written += int64(len(p))
5869
}
5970

60-
_, err = io.Copy(f.Overflow, bytes.NewReader(p))
61-
f.written += int64(len(p))
62-
63-
f.Status = FileFull
64-
return len(p), err
71+
return len(p), nil
6572
}
6673

6774
func (f *File) Written() int64 {

0 commit comments

Comments
 (0)