Reading control file from .deb package.

This commit is contained in:
Andrey Smirnov
2014-02-21 20:42:25 +04:00
parent f63b0dd315
commit 8951b4f42a
3 changed files with 91 additions and 0 deletions

59
debian/deb.go vendored Normal file
View File

@@ -0,0 +1,59 @@
package debian
import (
"archive/tar"
"compress/gzip"
"fmt"
"github.com/mkrautz/goar"
"io"
"os"
)
// GetControlFileFromDeb read control file from deb package
func GetControlFileFromDeb(packageFile string) (Stanza, error) {
file, err := os.Open(packageFile)
if err != nil {
return nil, err
}
defer file.Close()
library := ar.NewReader(file)
for {
header, err := library.Next()
if err == io.EOF {
return nil, fmt.Errorf("unable to find control.tar.gz part")
}
if err != nil {
return nil, fmt.Errorf("unable to read .deb archive: %s", err)
}
if header.Name == "control.tar.gz" {
ungzip, err := gzip.NewReader(library)
if err != nil {
return nil, fmt.Errorf("unable to ungzip: %s", err)
}
defer ungzip.Close()
untar := tar.NewReader(ungzip)
for {
tarHeader, err := untar.Next()
if err == io.EOF {
return nil, fmt.Errorf("unable to find control file")
}
if err != nil {
return nil, fmt.Errorf("unable to read .tar archive: %s", err)
}
if tarHeader.Name == "./control" {
reader := NewControlFileReader(untar)
stanza, err := reader.ReadStanza()
if err != nil {
return nil, err
}
return stanza, nil
}
}
}
}
}

32
debian/deb_test.go vendored Normal file
View File

@@ -0,0 +1,32 @@
package debian
import (
. "launchpad.net/gocheck"
"path/filepath"
"runtime"
)
type DebSuite struct {
debFile string
}
var _ = Suite(&DebSuite{})
func (s *DebSuite) SetUpSuite(c *C) {
_, __file__, _, _ := runtime.Caller(0)
s.debFile = filepath.Join(filepath.Dir(__file__), "../system/files/libboost-program-options-dev_1.49.0.1_i386.deb")
}
func (s *DebSuite) TestGetControlFileFromDeb(c *C) {
_, err := GetControlFileFromDeb("/no/such/file")
c.Check(err, ErrorMatches, ".*no such file or directory")
_, __file__, _, _ := runtime.Caller(0)
_, err = GetControlFileFromDeb(__file__)
c.Check(err, ErrorMatches, "unable to read .deb archive: ar: missing global header")
st, err := GetControlFileFromDeb(s.debFile)
c.Check(err, IsNil)
c.Check(st["Version"], Equals, "1.49.0.1")
c.Check(st["Package"], Equals, "libboost-program-options-dev")
}