2024-08-04 08:16:29 +02:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package arch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/packages"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
"code.gitea.io/gitea/modules/validation"
|
|
|
|
|
|
|
|
"github.com/mholt/archiver/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Arch Linux Packages
|
|
|
|
// https://man.archlinux.org/man/PKGBUILD.5
|
|
|
|
|
|
|
|
const (
|
|
|
|
PropertyDescription = "arch.description"
|
|
|
|
PropertyArch = "arch.architecture"
|
|
|
|
PropertyDistribution = "arch.distribution"
|
|
|
|
|
|
|
|
SettingKeyPrivate = "arch.key.private"
|
|
|
|
SettingKeyPublic = "arch.key.public"
|
|
|
|
|
|
|
|
RepositoryPackage = "_arch"
|
|
|
|
RepositoryVersion = "_repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
reName = regexp.MustCompile(`^[a-zA-Z0-9@._+-]+$`)
|
|
|
|
reVer = regexp.MustCompile(`^[a-zA-Z0-9:_.+]+-+[0-9]+$`)
|
2024-09-27 10:21:22 +02:00
|
|
|
reOptDep = regexp.MustCompile(`^[a-zA-Z0-9@._+-]+([<>]?=?([0-9]+:)?[a-zA-Z0-9@._+-]+)?(:.*)?$`)
|
|
|
|
rePkgVer = regexp.MustCompile(`^[a-zA-Z0-9@._+-]+([<>]?=?([0-9]+:)?[a-zA-Z0-9@._+-]+)?$`)
|
2024-08-11 12:35:11 +02:00
|
|
|
|
|
|
|
magicZSTD = []byte{0x28, 0xB5, 0x2F, 0xFD}
|
|
|
|
magicXZ = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A}
|
2024-08-26 04:45:24 +02:00
|
|
|
magicGZ = []byte{0x1F, 0x8B}
|
2024-08-04 08:16:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Package struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version"` // Includes version, release and epoch
|
2024-08-11 12:35:11 +02:00
|
|
|
CompressType string `json:"compress_type"`
|
2024-08-04 08:16:29 +02:00
|
|
|
VersionMetadata VersionMetadata
|
|
|
|
FileMetadata FileMetadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arch package metadata related to specific version.
|
|
|
|
// Version metadata the same across different architectures and distributions.
|
|
|
|
type VersionMetadata struct {
|
|
|
|
Base string `json:"base"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
ProjectURL string `json:"project_url"`
|
|
|
|
Groups []string `json:"groups,omitempty"`
|
|
|
|
Provides []string `json:"provides,omitempty"`
|
|
|
|
License []string `json:"license,omitempty"`
|
|
|
|
Depends []string `json:"depends,omitempty"`
|
|
|
|
OptDepends []string `json:"opt_depends,omitempty"`
|
|
|
|
MakeDepends []string `json:"make_depends,omitempty"`
|
|
|
|
CheckDepends []string `json:"check_depends,omitempty"`
|
|
|
|
Conflicts []string `json:"conflicts,omitempty"`
|
|
|
|
Replaces []string `json:"replaces,omitempty"`
|
|
|
|
Backup []string `json:"backup,omitempty"`
|
2024-09-27 10:21:22 +02:00
|
|
|
XData []string `json:"xdata,omitempty"`
|
2024-08-04 08:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FileMetadata Metadata related to specific package file.
|
|
|
|
// This metadata might vary for different architecture and distribution.
|
|
|
|
type FileMetadata struct {
|
|
|
|
CompressedSize int64 `json:"compressed_size"`
|
|
|
|
InstalledSize int64 `json:"installed_size"`
|
|
|
|
MD5 string `json:"md5"`
|
|
|
|
SHA256 string `json:"sha256"`
|
|
|
|
BuildDate int64 `json:"build_date"`
|
|
|
|
Packager string `json:"packager"`
|
|
|
|
Arch string `json:"arch"`
|
|
|
|
PgpSigned string `json:"pgp"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParsePackage Function that receives arch package archive data and returns it's metadata.
|
|
|
|
func ParsePackage(r *packages.HashedBuffer) (*Package, error) {
|
|
|
|
md5, _, sha256, _ := r.Sums()
|
|
|
|
_, err := r.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-11 12:35:11 +02:00
|
|
|
header := make([]byte, 5)
|
|
|
|
_, err = r.Read(header)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = r.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tarball archiver.Reader
|
|
|
|
var tarballType string
|
|
|
|
if bytes.Equal(header[:len(magicZSTD)], magicZSTD) {
|
|
|
|
tarballType = "zst"
|
|
|
|
tarball = archiver.NewTarZstd()
|
|
|
|
} else if bytes.Equal(header[:len(magicXZ)], magicXZ) {
|
|
|
|
tarballType = "xz"
|
|
|
|
tarball = archiver.NewTarXz()
|
2024-08-26 04:45:24 +02:00
|
|
|
} else if bytes.Equal(header[:len(magicGZ)], magicGZ) {
|
|
|
|
tarballType = "gz"
|
|
|
|
tarball = archiver.NewTarGz()
|
2024-08-11 12:35:11 +02:00
|
|
|
} else {
|
|
|
|
return nil, errors.New("not supported compression")
|
|
|
|
}
|
|
|
|
err = tarball.Open(r, 0)
|
2024-08-04 08:16:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-11 12:35:11 +02:00
|
|
|
defer tarball.Close()
|
2024-08-04 08:16:29 +02:00
|
|
|
|
|
|
|
var pkg *Package
|
2024-09-27 10:21:22 +02:00
|
|
|
var mTree bool
|
2024-08-04 08:16:29 +02:00
|
|
|
|
|
|
|
for {
|
2024-08-11 12:35:11 +02:00
|
|
|
f, err := tarball.Read()
|
2024-08-04 08:16:29 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch f.Name() {
|
|
|
|
case ".PKGINFO":
|
2024-08-11 12:35:11 +02:00
|
|
|
pkg, err = ParsePackageInfo(tarballType, f)
|
2024-08-04 08:16:29 +02:00
|
|
|
if err != nil {
|
2024-09-27 10:21:22 +02:00
|
|
|
_ = f.Close()
|
2024-08-04 08:16:29 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case ".MTREE":
|
2024-09-27 10:21:22 +02:00
|
|
|
mTree = true
|
2024-08-04 08:16:29 +02:00
|
|
|
}
|
2024-09-27 10:21:22 +02:00
|
|
|
_ = f.Close()
|
2024-08-04 08:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if pkg == nil {
|
|
|
|
return nil, util.NewInvalidArgumentErrorf(".PKGINFO file not found")
|
|
|
|
}
|
|
|
|
|
2024-09-27 10:21:22 +02:00
|
|
|
if !mTree {
|
2024-08-04 08:16:29 +02:00
|
|
|
return nil, util.NewInvalidArgumentErrorf(".MTREE file not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg.FileMetadata.CompressedSize = r.Size()
|
|
|
|
pkg.FileMetadata.MD5 = hex.EncodeToString(md5)
|
|
|
|
pkg.FileMetadata.SHA256 = hex.EncodeToString(sha256)
|
|
|
|
|
|
|
|
return pkg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParsePackageInfo Function that accepts reader for .PKGINFO file from package archive,
|
|
|
|
// validates all field according to PKGBUILD spec and returns package.
|
2024-08-11 12:35:11 +02:00
|
|
|
func ParsePackageInfo(compressType string, r io.Reader) (*Package, error) {
|
|
|
|
p := &Package{
|
|
|
|
CompressType: compressType,
|
|
|
|
}
|
2024-08-04 08:16:29 +02:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key, value, find := strings.Cut(line, "=")
|
|
|
|
if !find {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key = strings.TrimSpace(key)
|
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
switch key {
|
|
|
|
case "pkgname":
|
|
|
|
p.Name = value
|
|
|
|
case "pkgbase":
|
|
|
|
p.VersionMetadata.Base = value
|
|
|
|
case "pkgver":
|
|
|
|
p.Version = value
|
|
|
|
case "pkgdesc":
|
|
|
|
p.VersionMetadata.Description = value
|
|
|
|
case "url":
|
|
|
|
p.VersionMetadata.ProjectURL = value
|
|
|
|
case "packager":
|
|
|
|
p.FileMetadata.Packager = value
|
|
|
|
case "arch":
|
|
|
|
p.FileMetadata.Arch = value
|
|
|
|
case "provides":
|
|
|
|
p.VersionMetadata.Provides = append(p.VersionMetadata.Provides, value)
|
|
|
|
case "license":
|
|
|
|
p.VersionMetadata.License = append(p.VersionMetadata.License, value)
|
|
|
|
case "depend":
|
|
|
|
p.VersionMetadata.Depends = append(p.VersionMetadata.Depends, value)
|
|
|
|
case "optdepend":
|
|
|
|
p.VersionMetadata.OptDepends = append(p.VersionMetadata.OptDepends, value)
|
|
|
|
case "makedepend":
|
|
|
|
p.VersionMetadata.MakeDepends = append(p.VersionMetadata.MakeDepends, value)
|
|
|
|
case "checkdepend":
|
|
|
|
p.VersionMetadata.CheckDepends = append(p.VersionMetadata.CheckDepends, value)
|
|
|
|
case "backup":
|
|
|
|
p.VersionMetadata.Backup = append(p.VersionMetadata.Backup, value)
|
|
|
|
case "group":
|
|
|
|
p.VersionMetadata.Groups = append(p.VersionMetadata.Groups, value)
|
|
|
|
case "conflict":
|
|
|
|
p.VersionMetadata.Conflicts = append(p.VersionMetadata.Conflicts, value)
|
|
|
|
case "replaces":
|
|
|
|
p.VersionMetadata.Replaces = append(p.VersionMetadata.Replaces, value)
|
|
|
|
case "xdata":
|
2024-09-27 10:21:22 +02:00
|
|
|
p.VersionMetadata.XData = append(p.VersionMetadata.XData, value)
|
2024-08-04 08:16:29 +02:00
|
|
|
case "builddate":
|
|
|
|
bd, err := strconv.ParseInt(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.FileMetadata.BuildDate = bd
|
|
|
|
case "size":
|
|
|
|
is, err := strconv.ParseInt(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.FileMetadata.InstalledSize = is
|
|
|
|
default:
|
|
|
|
return nil, util.NewInvalidArgumentErrorf("property is not supported %s", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, errors.Join(scanner.Err(), ValidatePackageSpec(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidatePackageSpec Arch package validation according to PKGBUILD specification.
|
|
|
|
func ValidatePackageSpec(p *Package) error {
|
|
|
|
if !reName.MatchString(p.Name) {
|
|
|
|
return util.NewInvalidArgumentErrorf("invalid package name")
|
|
|
|
}
|
|
|
|
if !reName.MatchString(p.VersionMetadata.Base) {
|
|
|
|
return util.NewInvalidArgumentErrorf("invalid package base")
|
|
|
|
}
|
|
|
|
if !reVer.MatchString(p.Version) {
|
|
|
|
return util.NewInvalidArgumentErrorf("invalid package version")
|
|
|
|
}
|
|
|
|
if p.FileMetadata.Arch == "" {
|
|
|
|
return util.NewInvalidArgumentErrorf("architecture should be specified")
|
|
|
|
}
|
|
|
|
if p.VersionMetadata.ProjectURL != "" {
|
|
|
|
if !validation.IsValidURL(p.VersionMetadata.ProjectURL) {
|
|
|
|
return util.NewInvalidArgumentErrorf("invalid project URL")
|
|
|
|
}
|
|
|
|
}
|
2024-09-27 10:21:22 +02:00
|
|
|
for _, checkDepend := range p.VersionMetadata.CheckDepends {
|
|
|
|
if !rePkgVer.MatchString(checkDepend) {
|
|
|
|
return util.NewInvalidArgumentErrorf("invalid check dependency: %s", checkDepend)
|
2024-08-04 08:16:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-27 10:21:22 +02:00
|
|
|
for _, depend := range p.VersionMetadata.Depends {
|
|
|
|
if !rePkgVer.MatchString(depend) {
|
|
|
|
return util.NewInvalidArgumentErrorf("invalid dependency: %s", depend)
|
2024-08-04 08:16:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-27 10:21:22 +02:00
|
|
|
for _, makeDepend := range p.VersionMetadata.MakeDepends {
|
|
|
|
if !rePkgVer.MatchString(makeDepend) {
|
|
|
|
return util.NewInvalidArgumentErrorf("invalid make dependency: %s", makeDepend)
|
2024-08-04 08:16:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-27 10:21:22 +02:00
|
|
|
for _, provide := range p.VersionMetadata.Provides {
|
|
|
|
if !rePkgVer.MatchString(provide) {
|
|
|
|
return util.NewInvalidArgumentErrorf("invalid provides: %s", provide)
|
2024-08-04 08:16:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-27 10:21:22 +02:00
|
|
|
for _, conflict := range p.VersionMetadata.Conflicts {
|
|
|
|
if !rePkgVer.MatchString(conflict) {
|
|
|
|
return util.NewInvalidArgumentErrorf("invalid conflicts: %s", conflict)
|
2024-08-04 08:16:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-27 10:21:22 +02:00
|
|
|
for _, replace := range p.VersionMetadata.Replaces {
|
|
|
|
if !rePkgVer.MatchString(replace) {
|
|
|
|
return util.NewInvalidArgumentErrorf("invalid replaces: %s", replace)
|
2024-08-04 08:16:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-27 10:21:22 +02:00
|
|
|
for _, optDepend := range p.VersionMetadata.OptDepends {
|
|
|
|
if !reOptDep.MatchString(optDepend) {
|
|
|
|
return util.NewInvalidArgumentErrorf("invalid optional dependency: %s", optDepend)
|
2024-08-04 08:16:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-27 10:21:22 +02:00
|
|
|
for _, b := range p.VersionMetadata.Backup {
|
|
|
|
if strings.HasPrefix(b, "/") {
|
2024-08-04 08:16:29 +02:00
|
|
|
return util.NewInvalidArgumentErrorf("backup file contains leading forward slash")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Desc Create pacman package description file.
|
|
|
|
func (p *Package) Desc() string {
|
|
|
|
entries := []string{
|
2024-08-11 12:35:11 +02:00
|
|
|
"FILENAME", fmt.Sprintf("%s-%s-%s.pkg.tar.%s", p.Name, p.Version, p.FileMetadata.Arch, p.CompressType),
|
2024-08-04 08:16:29 +02:00
|
|
|
"NAME", p.Name,
|
|
|
|
"BASE", p.VersionMetadata.Base,
|
|
|
|
"VERSION", p.Version,
|
|
|
|
"DESC", p.VersionMetadata.Description,
|
|
|
|
"GROUPS", strings.Join(p.VersionMetadata.Groups, "\n"),
|
|
|
|
"CSIZE", fmt.Sprintf("%d", p.FileMetadata.CompressedSize),
|
|
|
|
"ISIZE", fmt.Sprintf("%d", p.FileMetadata.InstalledSize),
|
|
|
|
"MD5SUM", p.FileMetadata.MD5,
|
|
|
|
"SHA256SUM", p.FileMetadata.SHA256,
|
|
|
|
"PGPSIG", p.FileMetadata.PgpSigned,
|
|
|
|
"URL", p.VersionMetadata.ProjectURL,
|
|
|
|
"LICENSE", strings.Join(p.VersionMetadata.License, "\n"),
|
|
|
|
"ARCH", p.FileMetadata.Arch,
|
|
|
|
"BUILDDATE", fmt.Sprintf("%d", p.FileMetadata.BuildDate),
|
|
|
|
"PACKAGER", p.FileMetadata.Packager,
|
|
|
|
"REPLACES", strings.Join(p.VersionMetadata.Replaces, "\n"),
|
|
|
|
"CONFLICTS", strings.Join(p.VersionMetadata.Conflicts, "\n"),
|
|
|
|
"PROVIDES", strings.Join(p.VersionMetadata.Provides, "\n"),
|
|
|
|
"DEPENDS", strings.Join(p.VersionMetadata.Depends, "\n"),
|
|
|
|
"OPTDEPENDS", strings.Join(p.VersionMetadata.OptDepends, "\n"),
|
|
|
|
"MAKEDEPENDS", strings.Join(p.VersionMetadata.MakeDepends, "\n"),
|
|
|
|
"CHECKDEPENDS", strings.Join(p.VersionMetadata.CheckDepends, "\n"),
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for i := 0; i < len(entries); i += 2 {
|
|
|
|
if entries[i+1] != "" {
|
|
|
|
_, _ = fmt.Fprintf(&buf, "%%%s%%\n%s\n\n", entries[i], entries[i+1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|