mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-05-06 22:18:28 +00:00
aeb85a1b3c
While all the normal Debian package building tools create a control.tar.gz archive member that contains files with a leading "./" path element, such as "./control", dpkg and other archive tools like reprepro are happy with omitting the "./" path element and having files like "control". Allow for either for compatibility with weird packages that people may want to import into aptly.
100 lines
2.0 KiB
Go
100 lines
2.0 KiB
Go
package deb
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bufio"
|
|
"compress/gzip"
|
|
"fmt"
|
|
"github.com/mkrautz/goar"
|
|
"github.com/smira/aptly/utils"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// GetControlFileFromDeb reads 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" || tarHeader.Name == "control" {
|
|
reader := NewControlFileReader(untar)
|
|
stanza, err := reader.ReadStanza()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return stanza, nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetControlFileFromDsc reads control file from dsc package
|
|
func GetControlFileFromDsc(dscFile string, verifier utils.Verifier) (Stanza, error) {
|
|
file, err := os.Open(dscFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
line, err := bufio.NewReader(file).ReadString('\n')
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
file.Seek(0, 0)
|
|
|
|
var text *os.File
|
|
|
|
if strings.Index(line, "BEGIN PGP SIGN") != -1 {
|
|
text, err = verifier.ExtractClearsigned(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer text.Close()
|
|
} else {
|
|
text = file
|
|
}
|
|
|
|
reader := NewControlFileReader(text)
|
|
stanza, err := reader.ReadStanza()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return stanza, nil
|
|
|
|
}
|