83 lines
1.5 KiB
Go
83 lines
1.5 KiB
Go
package archive
|
|
|
|
import (
|
|
"os"
|
|
"fmt"
|
|
"github.com/blakesmith/ar"
|
|
"bytes"
|
|
"io"
|
|
"strings"
|
|
"compress/gzip"
|
|
"archive/tar"
|
|
"log"
|
|
)
|
|
|
|
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()
|
|
var controlBuf bytes.Buffer
|
|
|
|
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" {
|
|
io.Copy(&controlBuf, arReader)
|
|
return InspectPackageControl(controlBuf)
|
|
}
|
|
}
|
|
return "", nil
|
|
}
|
|
|
|
func InspectPackageControl(filename bytes.Buffer) (string, error) {
|
|
gzf, err := gzip.NewReader(bytes.NewReader(filename.Bytes()))
|
|
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
|
|
}
|