deb-simple/deb/archive/archive.go

81 lines
1.4 KiB
Go

package archive
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"github.com/blakesmith/ar"
"io"
"log"
"os"
"strings"
)
func InspectPackage(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
return "", fmt.Errorf("error opening package file %s: %s", filename, err)
}
arReader := ar.NewReader(f)
defer f.Close()
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)
}
}
return "", nil
}
func InspectPackageControl(reader io.Reader) (string, error) {
gzf, err := gzip.NewReader(reader)
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
}