2017-09-12 02:35:41 +00:00
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
2018-10-14 08:35:50 +00:00
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2017-09-12 02:35:41 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/blakesmith/ar"
|
|
|
|
"io"
|
|
|
|
"log"
|
2018-10-14 08:35:50 +00:00
|
|
|
"strings"
|
2017-09-12 02:35:41 +00:00
|
|
|
)
|
|
|
|
|
2020-06-14 09:38:47 +00:00
|
|
|
func InspectPackage(r io.Reader) (string, error) {
|
|
|
|
arReader := ar.NewReader(r)
|
2017-09-12 02:35:41 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
header, err := arReader.Next()
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error in inspectPackage loop: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Trim(header.Name, "/") == "control.tar.gz" {
|
2018-10-14 08:35:50 +00:00
|
|
|
return InspectPackageControl(arReader)
|
2017-09-12 02:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-14 09:38:47 +00:00
|
|
|
|
2017-09-12 02:35:41 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-10-14 08:35:50 +00:00
|
|
|
func InspectPackageControl(reader io.Reader) (string, error) {
|
|
|
|
gzf, err := gzip.NewReader(reader)
|
2017-09-12 02:35:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error creating gzip reader: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tarReader := tar.NewReader(gzf)
|
|
|
|
var controlBuf bytes.Buffer
|
|
|
|
for {
|
|
|
|
header, err := tarReader.Next()
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to inspect package: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := header.Name
|
|
|
|
|
|
|
|
switch header.Typeflag {
|
|
|
|
case tar.TypeDir:
|
|
|
|
continue
|
|
|
|
case tar.TypeReg:
|
|
|
|
if name == "./control" {
|
|
|
|
io.Copy(&controlBuf, tarReader)
|
|
|
|
return controlBuf.String(), nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Printf(
|
|
|
|
"Unable to figure out type : %c in file %s\n",
|
|
|
|
header.Typeflag, name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|