deb-simple/deb/archive/archive.go

74 lines
1.2 KiB
Go
Raw Normal View History

2017-09-12 02:35:41 +00:00
package archive
import (
"archive/tar"
"bytes"
"compress/gzip"
2017-09-12 02:35:41 +00:00
"fmt"
"github.com/blakesmith/ar"
"io"
"log"
"strings"
2017-09-12 02:35:41 +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" {
return InspectPackageControl(arReader)
2017-09-12 02:35:41 +00:00
}
}
2017-09-12 02:35:41 +00:00
return "", nil
}
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
}