Skip to content

Commit

Permalink
add more semantic error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ybirader committed Aug 26, 2023
1 parent 4d9a9b6 commit 07497ba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/alecthomas/assert/v2 v2.3.0
github.com/klauspost/compress v1.16.7
github.com/pkg/errors v0.9.1
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
43 changes: 26 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"archive/zip"
"bytes"
"encoding/binary"
"errors"
"hash/crc32"
"io"
"io/fs"
Expand All @@ -16,6 +15,7 @@ import (
"unicode/utf8"

"github.com/klauspost/compress/flate"
"github.com/pkg/errors"
)

type Archiver struct {
Expand Down Expand Up @@ -52,7 +52,7 @@ func NewArchiver(archive *os.File) (*Archiver, error) {

fileProcessPool, err := NewFileProcessPool(a.numberOfWorkers, fileProcessExecutor)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "ERROR: could not create file processor pool")
}
a.fileProcessPool = fileProcessPool

Expand All @@ -62,7 +62,7 @@ func NewArchiver(archive *os.File) (*Archiver, error) {

fileWriterPool, err := NewFileProcessPool(1, fileWriterExecutor)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "ERROR: could not create file writer pool")
}
a.fileWriterPool = fileWriterPool

Expand All @@ -72,7 +72,7 @@ func NewArchiver(archive *os.File) (*Archiver, error) {
func (a *Archiver) changeRoot(root string) error {
absRoot, err := filepath.Abs(root)
if err != nil {
return err
return errors.Errorf("ERROR: could not determine absolute path of %s", root)
}

a.chroot = absRoot
Expand All @@ -82,12 +82,12 @@ func (a *Archiver) changeRoot(root string) error {
func (a *Archiver) ArchiveDir(root string) error {
err := a.changeRoot(root)
if err != nil {
return err
return errors.Wrapf(err, "ERROR: could not set chroot of archive to %s", root)
}

err = a.walkDir()
if err != nil {
return err
return errors.Wrap(err, "ERROR: could not walk directory")
}

return nil
Expand Down Expand Up @@ -163,7 +163,7 @@ func (a *Archiver) walkDir() error {

relativeToRoot, err := filepath.Rel(a.chroot, path)
if err != nil {
return err
return errors.Errorf("ERROR: could not determine relative path of %s", path)
}

f := File{Path: relativeToRoot, Info: info}
Expand All @@ -172,7 +172,7 @@ func (a *Archiver) walkDir() error {
})

if err != nil {
return err
return errors.Errorf("ERROR: could not walk directory %s", a.chroot)
}

a.fileProcessPool.Close()
Expand All @@ -188,7 +188,7 @@ func (a *Archiver) ArchiveFiles(files ...string) error {
for _, path := range files {
info, err := os.Lstat(path)
if err != nil {
return err
return errors.Errorf("ERROR: could not get stat of %s", path)
}

f := File{Path: path, Info: info}
Expand All @@ -204,7 +204,7 @@ func (a *Archiver) ArchiveFiles(files ...string) error {
func (a *Archiver) Close() error {
err := a.w.Close()
if err != nil {
return err
return errors.New("ERROR: could not close archiver")
}

return nil
Expand All @@ -213,13 +213,13 @@ func (a *Archiver) Close() error {
func (a *Archiver) archive(f *File) error {
fileWriter, err := a.w.CreateRaw(f.Header)
if err != nil {
return err
return errors.Errorf("ERROR: could not write raw header for %s", f.Path)
}

_, err = io.Copy(fileWriter, &f.CompressedData)

if err != nil {
return err
return errors.Errorf("ERROR: could not write content for %s", f.Path)
}

return nil
Expand All @@ -231,7 +231,7 @@ func (a *Archiver) compress(file *File) error {
buf := bytes.Buffer{}
err := a.compressToBuffer(&buf, file)
if err != nil {
return err
return errors.Wrapf(err, "ERROR: could not compress to buffer %s", file.Path)
}
file.CompressedData = buf
return nil
Expand All @@ -240,16 +240,25 @@ func (a *Archiver) compress(file *File) error {
func (a *Archiver) compressToBuffer(buf *bytes.Buffer, file *File) error {
f, err := os.Open(file.Path)
if err != nil {
return err
return errors.Errorf("ERROR: could not open file %s", file.Path)
}
compressor, err := flate.NewWriter(buf, DefaultCompression)
if err != nil {
return err
}
defer compressor.Close()

defer func() {
cErr := compressor.Close()
if cErr != nil {
err = cErr
}

}()

_, err = io.Copy(compressor, f)

if err != nil {
return err
return errors.Errorf("ERROR: could not compress file %s", file.Path)
}

return nil
Expand All @@ -261,7 +270,7 @@ const extendedTimestampTag = 0x5455
func (a *Archiver) createHeader(file *File) error {
header, err := zip.FileInfoHeader(file.Info)
if err != nil {
return err
return errors.Errorf("ERROR: could not create file header for %s", file.Path)
}

if a.dirArchive() {
Expand Down

0 comments on commit 07497ba

Please sign in to comment.