Skip to content

Commit

Permalink
fix: improve tar error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Aug 23, 2022
1 parent 0325b04 commit ff0eeac
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pkg/archive/tar/tar.go
Expand Up @@ -3,6 +3,7 @@ package tar

import (
"archive/tar"
"fmt"
"io"
"os"

Expand Down Expand Up @@ -30,18 +31,18 @@ func (a Archive) Close() error {
func (a Archive) Add(f config.File) error {
info, err := os.Lstat(f.Source) // #nosec
if err != nil {
return err
return fmt.Errorf("%s: %w", f.Source, err)
}
var link string
if info.Mode()&os.ModeSymlink != 0 {
link, err = os.Readlink(f.Source) // #nosec
if err != nil {
return err
return fmt.Errorf("%s: %w", f.Source, err)
}
}
header, err := tar.FileInfoHeader(info, link)
if err != nil {
return err
return fmt.Errorf("%s: %w", f.Source, err)
}
header.Name = f.Destination
if !f.Info.MTime.IsZero() {
Expand All @@ -59,16 +60,18 @@ func (a Archive) Add(f config.File) error {
header.Gname = f.Info.Group
}
if err = a.tw.WriteHeader(header); err != nil {
return err
return fmt.Errorf("%s: %w", f.Source, err)
}
if info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
return nil
}
file, err := os.Open(f.Source) // #nosec
if err != nil {
return err
return fmt.Errorf("%s: %w", f.Source, err)
}
defer file.Close()
_, err = io.Copy(a.tw, file)
return err
if _, err := io.Copy(a.tw, file); err != nil {
return fmt.Errorf("%s: %w", f.Source, err)
}
return nil
}

0 comments on commit ff0eeac

Please sign in to comment.