archive.writeCompressed: bubble up error when closing archive file (MR 25)

This also prevents a double Close() if the compressor is ever set to 'fd'
This commit is contained in:
Clayton Craft
2023-02-26 11:41:37 -08:00
parent 5e2f975bd3
commit f24d0139c9

View File

@@ -279,13 +279,21 @@ func (archive *Archive) addFile(source string, dest string) error {
return nil
}
func (archive *Archive) writeCompressed(path string, mode os.FileMode) error {
func (archive *Archive) writeCompressed(path string, mode os.FileMode) (err error) {
var compressor io.WriteCloser
defer func() {
e := compressor.Close()
if e != nil && err == nil {
err = e
}
}()
fd, err := os.Create(path)
if err != nil {
return err
}
var compressor io.WriteCloser
// Note: fd.Close omitted since it'll be closed in "compressor"
switch archive.compress_format {
case FormatGzip:
@@ -326,10 +334,6 @@ func (archive *Archive) writeCompressed(path string, mode os.FileMode) error {
return err
}
if err := compressor.Close(); err != nil {
return err
}
// call fsync just to be sure
if err := fd.Sync(); err != nil {
return err