2023-05-01 01:22:25 +01:00
|
|
|
package debanator
|
|
|
|
|
|
|
|
import (
|
2023-05-01 21:28:28 +01:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha1"
|
|
|
|
"crypto/sha256"
|
2023-05-01 01:22:25 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-05-01 21:28:28 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-05-01 01:22:25 +01:00
|
|
|
|
|
|
|
"pault.ag/go/debian/control"
|
2023-05-01 21:28:28 +01:00
|
|
|
"pault.ag/go/debian/deb"
|
2023-05-01 01:22:25 +01:00
|
|
|
"pault.ag/go/debian/dependency"
|
|
|
|
"pault.ag/go/debian/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A group of debs of a package for different arches/version
|
|
|
|
type LogicalPackage struct {
|
|
|
|
Name string
|
|
|
|
// arch:version:package
|
|
|
|
Arches map[dependency.Arch]map[version.Version]control.BinaryIndex
|
|
|
|
}
|
|
|
|
|
2023-05-01 21:28:28 +01:00
|
|
|
func BinaryIndexFromDeb(r ReaderAtCloser, filePath string) (*control.BinaryIndex, error) {
|
|
|
|
debFile, err := deb.Load(r, "fakepath")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read deb: %w", err)
|
2023-05-01 01:22:25 +01:00
|
|
|
}
|
2023-05-01 21:28:28 +01:00
|
|
|
md5sum := md5.New()
|
|
|
|
sha1sum := sha1.New()
|
|
|
|
sha256sum := sha256.New()
|
|
|
|
hashWriter := io.MultiWriter(md5sum, sha1sum, sha256sum)
|
|
|
|
size, err := io.Copy(hashWriter, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("hash file: %w", err)
|
2023-05-01 01:22:25 +01:00
|
|
|
}
|
2023-05-01 21:28:28 +01:00
|
|
|
bi := control.BinaryIndex{
|
|
|
|
Paragraph: control.Paragraph{
|
|
|
|
Values: make(map[string]string),
|
|
|
|
},
|
|
|
|
Package: debFile.Control.Package,
|
|
|
|
Source: debFile.Control.Source,
|
|
|
|
Version: debFile.Control.Version,
|
|
|
|
InstalledSize: fmt.Sprintf("%d", debFile.Control.InstalledSize),
|
|
|
|
Size: strconv.Itoa(int(size)),
|
|
|
|
Maintainer: debFile.Control.Maintainer,
|
|
|
|
Architecture: debFile.Control.Architecture,
|
|
|
|
MultiArch: debFile.Control.MultiArch,
|
|
|
|
Description: debFile.Control.Description,
|
|
|
|
Homepage: debFile.Control.Homepage,
|
|
|
|
Section: debFile.Control.Section,
|
|
|
|
// FIXME: gross, make this more centrally managed somehow
|
|
|
|
Filename: strings.TrimPrefix(filePath, "/"),
|
|
|
|
Priority: debFile.Control.Priority,
|
|
|
|
MD5sum: fmt.Sprintf("%x", md5sum.Sum(nil)),
|
|
|
|
SHA1: fmt.Sprintf("%x", sha1sum.Sum(nil)),
|
|
|
|
SHA256: fmt.Sprintf("%x", sha256sum.Sum(nil)),
|
2023-05-01 01:22:25 +01:00
|
|
|
}
|
2023-05-01 21:28:28 +01:00
|
|
|
if debFile.Control.Depends.String() != "" {
|
|
|
|
bi.Paragraph.Set("Depends", debFile.Control.Depends.String())
|
2023-05-01 01:22:25 +01:00
|
|
|
}
|
2023-05-01 21:28:28 +01:00
|
|
|
if debFile.Control.Recommends.String() != "" {
|
|
|
|
bi.Paragraph.Set("Recommends", debFile.Control.Recommends.String())
|
2023-05-01 01:22:25 +01:00
|
|
|
}
|
2023-05-01 21:28:28 +01:00
|
|
|
if debFile.Control.Suggests.String() != "" {
|
|
|
|
bi.Paragraph.Set("Suggests", debFile.Control.Suggests.String())
|
2023-05-01 01:22:25 +01:00
|
|
|
}
|
2023-05-01 21:28:28 +01:00
|
|
|
if debFile.Control.Breaks.String() != "" {
|
|
|
|
bi.Paragraph.Set("Breaks", debFile.Control.Breaks.String())
|
2023-05-01 01:22:25 +01:00
|
|
|
}
|
2023-05-01 21:28:28 +01:00
|
|
|
if debFile.Control.Replaces.String() != "" {
|
|
|
|
bi.Paragraph.Set("Replaces", debFile.Control.Replaces.String())
|
|
|
|
}
|
|
|
|
if debFile.Control.BuiltUsing.String() != "" {
|
|
|
|
bi.Paragraph.Set("BuiltUsing", debFile.Control.BuiltUsing.String())
|
|
|
|
}
|
|
|
|
return &bi, nil
|
2023-05-01 01:22:25 +01:00
|
|
|
}
|