rpm

package module
v0.0.0-...-8cb9fd9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2020 License: BSD-3-Clause Imports: 20 Imported by: 1

README

go-rpm GoDoc Build Status Go Report Card

A native implementation of the RPM file specification in Go.

$ go get github.com/cavaliercoder/go-rpm

The go-rpm package aims to enable cross-platform tooling for yum/dnf/rpm written in Go (E.g. y10k).

Initial goals include like-for-like implementation of existing rpm ecosystem features such as:

  • Reading of modern and legacy rpm package file formats
  • Reading, creating and updating modern and legacy yum repository metadata
  • Reading of the rpm database
package main

import (
	"fmt"
	"github.com/cavaliercoder/go-rpm"
)

func main() {
	p, err := rpm.OpenPackageFile("golang-1.6.3-2.el7.rpm")
	if err != nil {
		panic(err)
	}

	fmt.Printf("Loaded package: %v - %s\n", p, p.Summary())

	// Output: golang-0:1.6.3-2.el7.x86_64 - The Go Programming Language
}

Tools

This package also includes two tools rpmdump and rpminfo.

The code for both tools demonstrates some use-cases of this package. They are both also useful for interrogating RPM packages on any platform.

$ rpminfo golang-1.6.3-2.el7.x86_64.rpm
Name        : golang
Version     : 1.6.3
Release     : 2.el7
Architecture: x86_64
Group       : Unspecified
Size        : 11809071
License     : BSD and Public Domain
Signature   : RSA/SHA256, Sun Nov 20 18:01:16 2016, Key ID 24c6a8a7f4a80eb5
Source RPM  : golang-1.6.3-2.el7.src.rpm
Build Date  : Tue Nov 15 12:20:30 2016
Build Host  : c1bm.rdu2.centos.org
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://golang.org/
Summary     : The Go Programming Language
Description :
The Go Programming Language.

Documentation

Overview

Package rpm provides a native implementation of the RPM file specification.

package main

import (
	"fmt"
	"github.com/cavaliercoder/go-rpm"
)

func main() {
	p, err := rpm.OpenPackageFile("my-package.rpm")
	if err != nil {
		panic(err)
	}

	fmt.Printf("Loaded package: %v", p)
}

Index

Examples

Constants

View Source
const (
	DepFlagAny            = 0
	DepFlagLesser         = (1 << 1)
	DepFlagGreater        = (1 << 2)
	DepFlagEqual          = (1 << 3)
	DepFlagLesserOrEqual  = (DepFlagEqual | DepFlagLesser)
	DepFlagGreaterOrEqual = (DepFlagEqual | DepFlagGreater)
	DepFlagPrereq         = (1 << 6)
	DepFlagScriptPre      = (1 << 9)
	DepFlagScriptPost     = (1 << 10)
	DepFlagScriptPreUn    = (1 << 11)
	DepFlagScriptPostUn   = (1 << 12)
	DepFlagRpmlib         = (1 << 24)
)

Dependency flags indicate how versions comparisons should be computed when comparing versions of dependent packages.

View Source
const (
	FileFlagNone      = 0
	FileFlagConfig    = (1 << 0)  // %%config
	FileFlagDoc       = (1 << 1)  // %%doc
	FileFlagIcon      = (1 << 2)  // %%donotuse
	FileFlagMissingOk = (1 << 3)  // %%config(missingok)
	FileFlagNoReplace = (1 << 4)  // %%config(noreplace)
	FileFlagGhost     = (1 << 6)  // %%ghost
	FileFlagLicense   = (1 << 7)  // %%license
	FileFlagReadme    = (1 << 8)  // %%readme
	FileFlagPubkey    = (1 << 11) // %%pubkey
	FileFlagArtifact  = (1 << 12) // %%artifact
)

File flags make up some attributes of files depending on how they were specified in the rpmspec

View Source
const (
	IndexDataTypeNull int = iota
	IndexDataTypeChar
	IndexDataTypeInt8
	IndexDataTypeInt16
	IndexDataTypeInt32
	IndexDataTypeInt64
	IndexDataTypeString
	IndexDataTypeBinary
	IndexDataTypeStringArray
	IndexDataTypeI8NString
)

Header index value data types.

View Source
const RPMDate = "Mon Jan _2 15:04:05 2006"

RPMDate is the Time format used by rpm tools.

Variables

View Source
var (
	// ErrMD5ValidationFailed indicates that a RPM package failed checksum
	// validation.
	ErrMD5ValidationFailed = fmt.Errorf("MD5 checksum validation failed")

	// ErrGPGValidationFailed indicates that a RPM package failed GPG signature
	// validation.
	ErrGPGValidationFailed = fmt.Errorf("GPG signature validation failed")

	// ErrGPGUnknownSignature indicates that the RPM package signature tag is of
	// an unknown type.
	ErrGPGUnknownSignature = fmt.Errorf("unknown signature type")
)

Predefined checksum errors.

View Source
var (
	// ErrBadHeaderLength indicates that the read header section is not the
	// expected length.
	ErrBadHeaderLength = errors.New("RPM header section is incorrect length")

	// ErrNotHeader indicates that the read header section does start with the
	// expected descriptor.
	ErrNotHeader = errors.New("invalid RPM header descriptor")

	// ErrBadIndexCount indicates that number of indexes given in the read
	// header would exceed the actual size of the header.
	ErrBadIndexCount = errors.New("index count exceeds header size")

	// ErrIndexOutOfRange indicates that the read header index would exceed the
	// range of the header.
	ErrIndexOutOfRange = errors.New("index is out of range")

	// ErrBadIndexType indicates that the read index contains a value of an
	// unsupported data type.
	ErrBadIndexType = errors.New("unknown index data type")

	// ErrBadIndexValueCount indicates that the read index value would exceed
	// the range of the header store section.
	ErrBadIndexValueCount = errors.New("index value count is out of range")
)
View Source
var (
	// ErrNotRPMFile indicates that the read file does not start with the
	// expected descriptor.
	ErrNotRPMFile = fmt.Errorf("RPM file descriptor is invalid")

	// ErrUnsupportedVersion indicates that the read lead section version is not
	// currently supported.
	ErrUnsupportedVersion = fmt.Errorf("unsupported RPM package version")
)

Functions

func GPGCheck

func GPGCheck(r io.Reader, keyring openpgp.KeyRing) (string, error)

GPGCheck validates the integrity of a RPM package file read from the given io.Reader. Public keys in the given keyring are used to validate the package signature.

If validation succeeds, nil is returned. If validation fails, ErrGPGValidationFailed is returned.

This function is an expensive operation which reads the entire package file.

Example

ExampleGPGCheck reads a public GPG key and uses it to validate the signature of a local rpm package.

// read public key from gpgkey file
keyring, err := rpm.KeyRingFromFile("testdata/RPM-GPG-KEY-CentOS-7")
if err != nil {
	panic(err)
}

// open a rpm package for reading
f, err := os.Open("testdata/centos-release-7-2.1511.el7.centos.2.10.x86_64.rpm")
if err != nil {
	panic(err)
}

defer f.Close()

// validate gpg signature
if signer, err := rpm.GPGCheck(f, keyring); err == nil {
	fmt.Printf("Package signed by '%s'\n", signer)
} else if err == rpm.ErrGPGValidationFailed {
	fmt.Printf("Package failed GPG signature validation\n")
} else {
	panic(err)
}
Output:

Package signed by 'CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>'

func KeyRing

func KeyRing(r io.Reader) (openpgp.KeyRing, error)

KeyRing reads a openpgp.KeyRing from the given io.Reader which may then be used to validate GPG keys in RPM packages.

func KeyRingFromFile

func KeyRingFromFile(path string) (openpgp.KeyRing, error)

KeyRingFromFile reads a openpgp.KeyRing from the given file path which may then be used to validate GPG keys in RPM packages.

func KeyRingFromFiles

func KeyRingFromFiles(files []string) (openpgp.KeyRing, error)

KeyRingFromFiles reads a openpgp.KeyRing from the given file paths which may then be used to validate GPG keys in RPM packages.

This function might typically be used to read all keys in /etc/pki/rpm-gpg.

func MD5Check

func MD5Check(r io.Reader) error

MD5Check validates the integrity of a RPM package file read from the given io.Reader. An MD5 checksum is computed for the package payload and compared with the checksum value specified in the package header.

If validation succeeds, nil is returned. If validation fails, ErrMD5ValidationFailed is returned.

This function is an expensive operation which reads the entire package file.

Example

ExampleMD5Check validates a local rpm package named using the MD5 checksum value specified in the package header.

// open a rpm package for reading
f, err := os.Open("testdata/centos-release-7-2.1511.el7.centos.2.10.x86_64.rpm")
if err != nil {
	panic(err)
}

defer f.Close()

// validate md5 checksum
if err := rpm.MD5Check(f); err == nil {
	fmt.Printf("Package passed checksum validation\n")
} else if err == rpm.ErrMD5ValidationFailed {
	fmt.Printf("Package failed checksum validation\n")
} else {
	panic(err)
}
Output:

Package passed checksum validation

Types

type Dependency

type Dependency interface {
	version.Interface

	// DepFlag constants
	Flags() int
}

Dependency is an interface which represents a relationship between two packages. It might indicate that one package requires, conflicts with, obsoletes or provides another package.

Dependency implements the PackageVersion interface and so may be used when comparing versions with other types of packages.

type FileInfo

type FileInfo struct {
	// contains filtered or unexported fields
}

A FileInfo describes a file in a RPM package and is returned by packagefile.Files.

FileInfo implements the os.FileInfo interface.

func (*FileInfo) Digest

func (f *FileInfo) Digest() string

Digest is the md5sum of a file in a RPM package

func (*FileInfo) Flags

func (f *FileInfo) Flags() int64

func (*FileInfo) Group

func (f *FileInfo) Group() string

Group is the name of the owner group of a file in a RPM package

func (*FileInfo) IsDir

func (f *FileInfo) IsDir() bool

IsDir returns true if a file is a directory in a RPM package

func (*FileInfo) Linkname

func (f *FileInfo) Linkname() string

Linkname is the link target of a link file in a RPM package

func (*FileInfo) ModTime

func (f *FileInfo) ModTime() time.Time

ModTime is the modification time of a file in a RPM package

func (*FileInfo) Mode

func (f *FileInfo) Mode() os.FileMode

Mode is the file mode in bits of a file in a RPM package

func (*FileInfo) Name

func (f *FileInfo) Name() string

Name is the full path of a file in a RPM package

func (*FileInfo) Owner

func (f *FileInfo) Owner() string

Owner is the name of the owner of a file in a RPM package

func (*FileInfo) Size

func (f *FileInfo) Size() int64

Size is the size in bytes of a file in a RPM package

func (*FileInfo) String

func (f *FileInfo) String() string

func (*FileInfo) Sys

func (f *FileInfo) Sys() interface{}

Sys is required to implement os.FileInfo and always returns nil

type GPGSignature

type GPGSignature []byte

GPGSignature is the raw byte representation of a package's signature.

func (GPGSignature) String

func (b GPGSignature) String() string
type Header struct {
	Version    int
	IndexCount int
	Length     int
	Indexes    IndexEntries
}

A Header stores metadata about a rpm package.

func ReadPackageHeader

func ReadPackageHeader(r io.Reader) (*Header, error)

ReadPackageHeader reads an RPM package file header structure from the given io.Reader.

This function should only be used if you intend to read a package header structure in isolation.

type IndexEntries

type IndexEntries []IndexEntry

IndexEntries is an array of IndexEntry structs.

func (IndexEntries) BytesByTag

func (c IndexEntries) BytesByTag(tag int) []byte

BytesByTag returns the raw value of an IndexEntry or nil if the tag is not found or has no value.

func (IndexEntries) IndexByTag

func (c IndexEntries) IndexByTag(tag int) *IndexEntry

IndexByTag returns a pointer to an IndexEntry with the given tag ID or nil if the tag is not found.

func (IndexEntries) IntByTag

func (c IndexEntries) IntByTag(tag int) int64

IntByTag returns the int64 value of an IndexEntry or 0 if the tag is not found or has no value. Values with a lower range (E.g. int8) are cast as an int64.

func (IndexEntries) IntsByTag

func (c IndexEntries) IntsByTag(tag int) []int64

IntsByTag returns the int64 values of an IndexEntry or nil if the tag is not found or has no value. Values with a lower range (E.g. int8) are cast as an int64.

func (IndexEntries) StringByTag

func (c IndexEntries) StringByTag(tag int) string

StringByTag returns the string value of an IndexEntry or an empty string if the tag is not found or has no value.

func (IndexEntries) StringsByTag

func (c IndexEntries) StringsByTag(tag int) []string

StringsByTag returns the slice of string values of an IndexEntry or nil if the tag is not found or has no value.

func (IndexEntries) TimeByTag

func (c IndexEntries) TimeByTag(tag int) time.Time

TimeByTag returns the value of an IndexEntry as a Go native timestamp or zero-time if the tag is not found or has no value.

func (IndexEntries) TimesByTag

func (c IndexEntries) TimesByTag(tag int) []time.Time

TimesByTag returns the value of an IndexEntry as a slice of Go native timestamps or nil if the tag is not found or has no value.

type IndexEntry

type IndexEntry struct {
	Tag       int
	Type      int
	Offset    int
	ItemCount int
	Value     interface{}
}

An IndexEntry is a rpm key/value tag stored in the package header.

type Lead

type Lead struct {
	VersionMajor    int
	VersionMinor    int
	Name            string
	Type            int
	Architecture    int
	OperatingSystem int
	SignatureType   int
}

A Lead is the deprecated lead section of an RPM file which is used in legacy RPM versions to store package metadata.

func ReadPackageLead

func ReadPackageLead(r io.Reader) (*Lead, error)

ReadPackageLead reads the deprecated lead section of an RPM file which is used in legacy RPM versions to store package metadata.

This function should only be used if you intend to read a package lead in isolation.

type PackageFile

type PackageFile struct {
	Lead    Lead
	Headers []Header
	// contains filtered or unexported fields
}

A PackageFile is an RPM package definition loaded directly from the package file itself.

func OpenPackageFile

func OpenPackageFile(path string) (*PackageFile, error)

OpenPackageFile reads a rpm package from the file system or a URL and returns a pointer to it.

func OpenPackageFiles

func OpenPackageFiles(path string) ([]*PackageFile, error)

OpenPackageFiles reads all rpm packages with the .rpm suffix from the given directory on the file systems and returns a slice of pointers to the loaded packages.

func ReadPackageFile

func ReadPackageFile(r io.Reader) (*PackageFile, error)

ReadPackageFile reads a rpm package file from a stream and returns a pointer to it.

func (*PackageFile) Architecture

func (c *PackageFile) Architecture() string

func (*PackageFile) ArchiveSize

func (c *PackageFile) ArchiveSize() uint64

ArchiveSize specifies the size of the archived payload of the package in bytes.

func (*PackageFile) BuildHost

func (c *PackageFile) BuildHost() string

func (*PackageFile) BuildTime

func (c *PackageFile) BuildTime() time.Time

func (*PackageFile) ChangeLog

func (c *PackageFile) ChangeLog() []string

func (*PackageFile) Checksum

func (c *PackageFile) Checksum() (string, error)

Checksum computes and returns the SHA256 checksum (encoded in hexadecimal) of the package file.

Checksum is a convenience function for tools that make use of package file SHA256 checksums. These might include many of the databases files created by the createrepo tool.

Checksum reopens the package using the file path that was given via OpenPackageFile. If the package was opened with any other method, Checksum will return "File not found".

func (*PackageFile) ChecksumType

func (c *PackageFile) ChecksumType() string

ChecksumType returns "sha256"

func (*PackageFile) Conflicts

func (c *PackageFile) Conflicts() []Dependency

func (*PackageFile) Description

func (c *PackageFile) Description() string

func (*PackageFile) Distribution

func (c *PackageFile) Distribution() string

func (*PackageFile) Enhances

func (c *PackageFile) Enhances() []Dependency

func (*PackageFile) Epoch

func (c *PackageFile) Epoch() int

func (*PackageFile) FileSize

func (c *PackageFile) FileSize() uint64

FileSize returns the size of the package file in bytes if it was opened with OpenPackageFile.

func (*PackageFile) FileTime

func (c *PackageFile) FileTime() time.Time

FileTime returns the time at which the RPM package file was last modified if it was opened with OpenPackageFile.

func (*PackageFile) Files

func (c *PackageFile) Files() []FileInfo

Files returns file information for each file that is installed by this RPM package.

Example

Lists all the files in a RPM package.

// open a package file
pkg, err := rpm.OpenPackageFile("./testdata/epel-release-7-5.noarch.rpm")
if err != nil {
	panic(err)
}

// list each file
files := pkg.Files()
fmt.Printf("total %v\n", len(files))
for _, fi := range files {
	fmt.Printf("%v %v %v %5v %v %v\n",
		fi.Mode().Perm(),
		fi.Owner(),
		fi.Group(),
		fi.Size(),
		fi.ModTime().UTC().Format("Jan 02 15:04"),
		fi.Name())
}
Output:

total 7
-rw-r--r-- root root  1662 Nov 25 16:23 /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
-rw-r--r-- root root  1056 Nov 25 16:23 /etc/yum.repos.d/epel-testing.repo
-rw-r--r-- root root   957 Nov 25 16:23 /etc/yum.repos.d/epel.repo
-rw-r--r-- root root    41 Nov 25 16:23 /usr/lib/rpm/macros.d/macros.epel
-rw-r--r-- root root  2813 Nov 25 16:23 /usr/lib/systemd/system-preset/90-epel.preset
-rwxr-xr-x root root  4096 Nov 25 16:26 /usr/share/doc/epel-release-7
-rw-r--r-- root root 18385 Nov 25 16:23 /usr/share/doc/epel-release-7/GPL

func (*PackageFile) GIFImage

func (c *PackageFile) GIFImage() []byte

func (*PackageFile) GPGSignature

func (c *PackageFile) GPGSignature() GPGSignature

func (*PackageFile) GetBytes

func (c *PackageFile) GetBytes(header, tag int) []byte

GetBytes returns the value of the given tag in the given header. Nil is returned if the header or tag do not exist, or it the tag exists but is a different type.

func (*PackageFile) GetInt

func (c *PackageFile) GetInt(header, tag int) int64

GetInt returns the int64 value of the given tag in the given header. Zero is returned if the header or tag do not exist, or if the tag exists but is a different type.

func (*PackageFile) GetInts

func (c *PackageFile) GetInts(header, tag int) []int64

GetInts returns the int64 values of the given tag in the given header. Nil is returned if the header or tag do not exist, or if the tag exists but is a different type.

func (*PackageFile) GetString

func (c *PackageFile) GetString(header, tag int) string

GetString returns the string value of the given tag in the given header. Nil is returned if the header or tag do not exist, or if the tag exists but is a different type.

func (*PackageFile) GetStrings

func (c *PackageFile) GetStrings(header, tag int) []string

GetStrings returns the string values of the given tag in the given header. Nil is returned if the header or tag do not exist, or if the tag exists but is a different type.

func (*PackageFile) Groups

func (c *PackageFile) Groups() []string

func (*PackageFile) Icon

func (c *PackageFile) Icon() []byte

func (*PackageFile) InstallTime

func (c *PackageFile) InstallTime() time.Time

func (*PackageFile) License

func (c *PackageFile) License() string

func (*PackageFile) Name

func (c *PackageFile) Name() string

func (*PackageFile) Obsoletes

func (c *PackageFile) Obsoletes() []Dependency

func (*PackageFile) OldFilenames

func (c *PackageFile) OldFilenames() []string

func (*PackageFile) OperatingSystem

func (c *PackageFile) OperatingSystem() string

func (*PackageFile) Packager

func (c *PackageFile) Packager() string

func (*PackageFile) Patch

func (c *PackageFile) Patch() []string

func (*PackageFile) Path

func (c *PackageFile) Path() string

Path returns the path which was given to open a package file if it was opened with OpenPackageFile.

func (*PackageFile) PayloadCompression

func (c *PackageFile) PayloadCompression() string

PayloadCompression returns the name of the compression used for the package payload. Typically xz.

func (*PackageFile) PayloadFormat

func (c *PackageFile) PayloadFormat() string

PayloadFormat returns the name of the format used for the package payload. Typically cpio.

func (*PackageFile) Platform

func (c *PackageFile) Platform() string

func (*PackageFile) PostInstallScript

func (c *PackageFile) PostInstallScript() string

func (*PackageFile) PostUninstallScript

func (c *PackageFile) PostUninstallScript() string

func (*PackageFile) PreInstallScript

func (c *PackageFile) PreInstallScript() string

func (*PackageFile) PreUninstallScript

func (c *PackageFile) PreUninstallScript() string

func (*PackageFile) Provides

func (c *PackageFile) Provides() []Dependency

func (*PackageFile) RPMVersion

func (c *PackageFile) RPMVersion() string

func (*PackageFile) Recommends

func (c *PackageFile) Recommends() []Dependency

func (*PackageFile) Release

func (c *PackageFile) Release() string

func (*PackageFile) Requires

func (c *PackageFile) Requires() []Dependency

func (*PackageFile) Size

func (c *PackageFile) Size() uint64

Size specifies the disk space consumed by installation of the package.

func (*PackageFile) Source

func (c *PackageFile) Source() []string

func (*PackageFile) SourceRPM

func (c *PackageFile) SourceRPM() string

func (*PackageFile) String

func (c *PackageFile) String() string

String returns the package identifier in the form '[name]-version-[release].[architecture]'.

func (*PackageFile) Suggests

func (c *PackageFile) Suggests() []Dependency

func (*PackageFile) Summary

func (c *PackageFile) Summary() string

func (*PackageFile) Supplements

func (c *PackageFile) Supplements() []Dependency

func (*PackageFile) URL

func (c *PackageFile) URL() string

func (*PackageFile) Vendor

func (c *PackageFile) Vendor() string

func (*PackageFile) Version

func (c *PackageFile) Version() string

func (*PackageFile) XPMImage

func (c *PackageFile) XPMImage() []byte

type Time

type Time time.Time

Time provides formatting for time.Time as is typically used by tools in the RPM ecosystem.

func (Time) String

func (c Time) String() string

Directories

Path Synopsis
cmd
Package version provides functions for comparing RPM package versions.
Package version provides functions for comparing RPM package versions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL