Ensure adding a broken tar doesn't silently fail

Signed-off-by: Aidan Hobson Sayers <aidanhs@cantab.net>
This commit is contained in:
Aidan Hobson Sayers 2015-11-20 16:49:33 +00:00
parent 688bb55544
commit 12fc11efd0
2 changed files with 64 additions and 5 deletions

View file

@ -77,6 +77,11 @@ var (
defaultArchiver = &Archiver{Untar: Untar, UIDMaps: nil, GIDMaps: nil}
)
const (
// HeaderSize is the size in bytes of a tar header
HeaderSize = 512
)
const (
// Uncompressed represents the uncompressed.
Uncompressed Compression = iota
@ -88,7 +93,8 @@ const (
Xz
)
// IsArchive checks if it is a archive by the header.
// IsArchive checks for the magic bytes of a tar or any supported compression
// algorithm.
func IsArchive(header []byte) bool {
compression := DetectCompression(header)
if compression != Uncompressed {
@ -99,6 +105,23 @@ func IsArchive(header []byte) bool {
return err == nil
}
// IsArchivePath checks if the (possibly compressed) file at the given path
// starts with a tar file header.
func IsArchivePath(path string) bool {
file, err := os.Open(path)
if err != nil {
return false
}
defer file.Close()
rdr, err := DecompressStream(file)
if err != nil {
return false
}
r := tar.NewReader(rdr)
_, err = r.Next()
return err == nil
}
// DetectCompression detects the compression algorithm of the source.
func DetectCompression(source []byte) Compression {
for compression, m := range map[Compression][]byte{
@ -800,10 +823,7 @@ func (archiver *Archiver) UntarPath(src, dst string) error {
GIDMaps: archiver.GIDMaps,
}
}
if err := archiver.Untar(archive, dst, options); err != nil {
return err
}
return nil
return archiver.Untar(archive, dst, options)
}
// UntarPath is a convenience function which looks for an archive