Documentation
¶
Overview ¶
Package source reads, verifies, unpacks, builds and indexes Debian source packages.
A source package is a .dsc control file plus the files it lists: the pristine orig tarball(s) and a Debian diff (format "1.0"), a debian tarball whose debian/patches/series quilt patches are applied on unpack ("3.0 (quilt)"), or a single tarball ("3.0 (native)").
The package covers the surrounding documents too: the .dsc itself (ParseDSC, DSC.Marshal, DSC.Sign, ParseSignedDSC), a Sources index (ReadSources, WriteSources), the .changes upload description (ParseChanges, Changes.Marshal) and the .buildinfo build record (ParseBuildInfo, BuildInfo.Marshal). Every one is deb822 and goes through deb.ParseControlFile; clear-signed wrappers are verified with a gpg.Keyring and created with a gpg.Signer.
Unpack verifies the files against the checksums of the .dsc, extracts the tarballs the way dpkg-source -x does and applies the patches with the same strictness (fuzz 0), writing only inside the target directory (safepath). Build is the opposite for formats "3.0 (native)" and "3.0 (quilt)": it packs a prepared tree and writes the .dsc.
Format "3.0 (git)" is read and written as metadata but cannot be unpacked: that needs a git client, which this package never runs.
Index ¶
- Constants
- Variables
- func ApplyPatch(root string, patch io.Reader, opts PatchOptions) ([]string, error)
- func SourcesFileName(compression string) (string, error)
- func Unpack(d *DSC, srcDir, dest string, opts *UnpackOptions) error
- func VerifyFiles(files []FileRef, dir string) error
- func WriteSources(w io.Writer, entries []SourceEntry, compression string) error
- type BinaryDescription
- type BuildInfo
- type BuildOptions
- type BuildResult
- type Changes
- type DSC
- type EnvVar
- type Field
- type FileRef
- type Format
- type InstalledPackage
- type PackageListEntry
- type PatchOptions
- type SourceEntry
- type SourcesOptions
- type UnpackOptions
Examples ¶
Constants ¶
const ( // DefaultMaxEntries bounds the archive entries of one source package. DefaultMaxEntries = 1 << 20 // DefaultMaxBytes bounds the file bytes extracted from one source package. DefaultMaxBytes int64 = 8 << 30 )
Default limits of Unpack.
const ChangesFormat = "1.8"
ChangesFormat is the Format of the .changes files this package writes.
const MaxDocumentBytes = 16 << 20
MaxDocumentBytes bounds a .dsc, .changes or .buildinfo file.
const MaxPatchBytes = 256 << 20
MaxPatchBytes bounds one patch file.
Variables ¶
var ( // ErrInvalid is returned for a document that is not valid deb822 of the // expected kind: a field is missing or malformed. ErrInvalid = errors.New("source: invalid document") // ErrUnsupportedFormat is returned for a source format this package cannot // unpack or build ("3.0 (git)", "3.0 (custom)", unknown formats). ErrUnsupportedFormat = errors.New("source: unsupported source format") // ErrChecksum is returned when a file does not match the size or the // checksum the .dsc lists for it. ErrChecksum = errors.New("source: checksum mismatch") // ErrMissingFile is returned when a file the .dsc lists is not available. ErrMissingFile = errors.New("source: file listed by the .dsc is missing") // ErrUnexpectedFile is returned for a file in the .dsc that does not // belong to the source format. ErrUnexpectedFile = errors.New("source: unexpected file for the source format") // ErrTooLarge is returned when a document or an unpacked tree exceeds a // limit. ErrTooLarge = errors.New("source: size limit exceeded") // ErrPatch is returned when a patch is malformed or does not apply // cleanly (fuzz 0). ErrPatch = errors.New("source: patch does not apply") // ErrUnsafe is returned for an archive entry or a patch that names a path // outside the target directory, or a special file. ErrUnsafe = errors.New("source: unsafe path or file type") // ErrNotSigned is returned by the Verify functions for a document that // carries no clear-signed wrapper. ErrNotSigned = errors.New("source: document is not signed") // ErrLocalChanges is returned by Build when the orig files of the tree // differ from the orig tarball plus the quilt series. ErrLocalChanges = errors.New("source: tree differs from the orig tarball plus the patches") // ErrDestExists is returned when the unpack or build target exists and the // options do not allow replacing it. ErrDestExists = errors.New("source: target already exists") )
Errors reported by the package; test with errors.Is.
Functions ¶
func ApplyPatch ¶
ApplyPatch applies the unified diff in patch to the tree at root with the strictness dpkg-source asks of patch(1): fuzz 0, so every context line must match, though a hunk may sit at a different line than it claims. A file that ends up empty is removed, and so is a directory that the removal empties. Files are only read and written inside root, and never through a symlink. It returns the root-relative names of the files it changed. A patch that does not apply fails with ErrPatch, and the tree is then partly patched.
func SourcesFileName ¶
SourcesFileName returns the name of the index file for a compression: "Sources", "Sources.gz", "Sources.xz" or "Sources.zst".
func Unpack ¶
func Unpack(d *DSC, srcDir, dest string, opts *UnpackOptions) error
Unpack extracts the source package that d describes, whose files are in srcDir, to the directory dest, and gives the same tree as dpkg-source -x: the files are first verified against the checksums of the .dsc; then the orig tarball (its single top-level directory becomes dest), the component tarballs, and the debian tarball or the .diff.gz are extracted, and the patches of debian/patches/series of a "3.0 (quilt)" package are applied with fuzz 0, leaving the .pc directory quilt expects.
Entries and patches only ever write inside dest, never through a symlink, and special files are refused with ErrUnsafe. Formats "3.0 (git)" and "3.0 (custom)" fail with ErrUnsupportedFormat.
Example ¶
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/M0Rf30/pkgformat/deb/source"
)
func main() {
dir := filepath.Join("testdata", "packages", "sl")
m, _ := filepath.Glob(filepath.Join(dir, "*.dsc"))
data, err := os.ReadFile(m[0])
if err != nil {
log.Fatal(err)
}
d, err := source.ParseDSC(data)
if err != nil {
log.Fatal(err)
}
// Checksums are verified, the orig and debian tarballs extracted and the
// quilt patches applied, as dpkg-source -x does.
tmp, _ := os.MkdirTemp("", "unpack")
defer os.RemoveAll(tmp)
dest := filepath.Join(tmp, "sl")
if err := source.Unpack(d, dir, dest, nil); err != nil {
log.Fatal(err)
}
_, err = os.Stat(filepath.Join(dest, "debian", "control"))
fmt.Println(err)
}
Output: <nil>
func VerifyFiles ¶
VerifyFiles checks every file of a .dsc, .changes or .buildinfo file list, found in dir by name, against the size and every digest the list carries for it. A missing file fails with ErrMissingFile, a difference with ErrChecksum.
func WriteSources ¶
func WriteSources(w io.Writer, entries []SourceEntry, compression string) error
WriteSources writes entries as a Sources index to w, in the order given, each followed by an empty line, compressed according to compression ("", "none", "gzip", "xz" or "zstd").
Types ¶
type BinaryDescription ¶
BinaryDescription is one line of the Description field of a .changes file: a binary package and its synopsis.
type BuildInfo ¶
type BuildInfo struct {
// Format is the buildinfo format, "1.0".
Format string
Source string
Binary []string
Architecture []string
Version string
// BinaryOnlyChanges is the changelog text of a binary-only upload.
BinaryOnlyChanges string
// Files lists the files the build produced, with their digests (the
// Checksums-Md5, Checksums-Sha1 and Checksums-Sha256 fields).
Files []FileRef
BuildOrigin string
BuildArchitecture string
BuildDate time.Time
BuildKernel string
BuildPath string
// BuildTaintedBy lists the reasons the build environment is tainted.
BuildTaintedBy []string
// InstalledBuildDepends lists the packages installed for the build.
InstalledBuildDepends []InstalledPackage
// Environment holds the whitelisted variables of the build environment.
Environment []EnvVar
// Extra holds the fields not modelled above, in document order.
Extra []Field
}
BuildInfo is a Debian .buildinfo file, the record of a build.
func ParseBuildInfo ¶
ParseBuildInfo parses a .buildinfo file, clear-signed or not. A signature is not verified: use ParseSignedBuildInfo for that.
Example ¶
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/M0Rf30/pkgformat/deb/source"
)
func main() {
data, err := os.ReadFile(filepath.Join("testdata", "changes", "binary", "hello_2.10-5_amd64.buildinfo"))
if err != nil {
log.Fatal(err)
}
b, err := source.ParseBuildInfo(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(b.Source, b.BuildArchitecture, b.InstalledBuildDepends[0], b.Environment[0].Name)
}
Output: hello amd64 autoconf (= 2.71-3) DEB_BUILD_OPTIONS
func ParseSignedBuildInfo ¶
ParseSignedBuildInfo verifies the clear-signed .buildinfo file against kr and parses it. An unsigned file fails with ErrNotSigned.
func ReadBuildInfo ¶
ReadBuildInfo reads and parses a .buildinfo file from r, reading at most MaxDocumentBytes.
func (*BuildInfo) Control ¶
func (b *BuildInfo) Control() *deb.ControlFile
Control renders the .buildinfo file as a deb822 paragraph in the order dpkg-genbuildinfo writes it.
type BuildOptions ¶
type BuildOptions struct {
// OrigDir holds the orig tarballs of a "3.0 (quilt)" package. The default
// is the parent directory of the tree, where dpkg-source looks.
OrigDir string
// OutDir receives the .dsc, the debian tarball and a copy of the orig
// tarballs. The default is the parent directory of the tree.
OutDir string
// Compression is the compression of the tarball that is created: "xz"
// (the default), "gzip" or "zstd": the compressions dpkg-source reads.
Compression string
// ModTime is the modification time of every archive entry that is not
// older. The default is the date of the latest debian/changelog entry, so
// that a build is reproducible.
ModTime time.Time
// Signer, when set, clear-signs the .dsc.
Signer gpg.Signer
// IgnorePatterns are tar exclude patterns added to the defaults that
// dpkg-source uses (".git", "*.o", ...).
IgnorePatterns []string
}
BuildOptions tunes Build. The zero value is valid.
type BuildResult ¶
type BuildResult struct {
// DSC is the description written to DSCPath, without a signature.
DSC *DSC
// DSCPath is the path of the .dsc file.
DSCPath string
// Files lists every file of the source package in OutDir, the .dsc
// included.
Files []string
}
BuildResult describes what Build wrote.
func Build ¶
func Build(tree string, opts *BuildOptions) (*BuildResult, error)
Build packs the prepared source tree into a source package, the way dpkg-source -b does for the formats "3.0 (native)" and "3.0 (quilt)": the format comes from debian/source/format, the description from debian/changelog and debian/control.
A native package becomes one tarball of the whole tree. A quilt package needs its orig tarballs in BuildOptions.OrigDir; they are copied to the output next to a debian tarball of debian/, and the tree must equal the orig tarballs plus the patches debian/patches/series lists as applied by .pc/applied-patches, or Build fails with ErrLocalChanges (it never writes an automatic patch). The result is accepted by dpkg-source -x.
type Changes ¶
type Changes struct {
// Format is the changes format, "1.8" for current dpkg.
Format string
Date time.Time
Source string
// Binary lists the binary packages of the upload.
Binary []string
// BuiltForProfiles lists the build profiles the upload was built with.
BuiltForProfiles []string
// Architecture lists the architectures: "source", "all", "amd64", ...
Architecture []string
Version string
Distribution string
Urgency string
Maintainer string
ChangedBy string
Description []BinaryDescription
// Closes lists the bug numbers the upload closes.
Closes []int
// Changes is the text of the changelog entries, one logical line per line
// and an empty line for a paragraph break.
Changes string
// Files lists the files with every digest the document carries, and their
// Section and Priority.
Files []FileRef
// Extra holds the fields not modelled above, in document order.
Extra []Field
}
Changes is a Debian .changes file, the description of an upload.
func ParseChanges ¶
ParseChanges parses a .changes file, clear-signed or not. A signature is not verified: use ParseSignedChanges for that.
Example ¶
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/M0Rf30/pkgformat/deb/source"
)
func main() {
data, err := os.ReadFile(filepath.Join("testdata", "changes", "full", "hello_2.10-5_amd64.changes"))
if err != nil {
log.Fatal(err)
}
c, err := source.ParseChanges(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(c.Source, c.Version, c.Architecture, c.Distribution, c.Closes)
}
Output: hello 2.10-5 [source amd64] unstable [1103293]
func ParseSignedChanges ¶
ParseSignedChanges verifies the clear-signed .changes file against kr and parses it. An unsigned file fails with ErrNotSigned.
func ReadChanges ¶
ReadChanges reads and parses a .changes file from r, reading at most MaxDocumentBytes.
func (*Changes) Control ¶
func (c *Changes) Control() *deb.ControlFile
Control renders the .changes file as a deb822 paragraph in the order dpkg-genchanges writes it.
type DSC ¶
type DSC struct {
Format Format
// Source is the source package name.
Source string
// Binary lists the binary packages the source builds.
Binary []string
// Architecture lists the architecture words ("any", "all", "amd64").
Architecture []string
// Version is the full version, with epoch and Debian revision.
Version string
Origin string
Maintainer string
Uploaders string
Homepage string
StandardsVersion string
// Vcs holds the Vcs-Browser, Vcs-Git, ... fields by canonical name.
Vcs map[string]string
// Testsuite is the comma separated Testsuite field.
Testsuite string
TestsuiteTriggers string
// Dgit is the Dgit field a dgit upload adds.
Dgit string
// The build relationship fields as written; Relations parses them.
BuildDepends string
BuildDependsArch string
BuildDependsIndep string
BuildConflicts string
BuildConflictsArch string
BuildConflictsIndep string
PackageList []PackageListEntry
// Files lists the files of the source package, with every digest the
// document carries (Files, Checksums-Sha1, Checksums-Sha256,
// Checksums-Sha512).
Files []FileRef
// Extra holds the fields not modelled above, in document order.
Extra []Field
}
DSC is a Debian source control file.
func ParseDSC ¶
ParseDSC parses a .dsc, clear-signed or not. A signature is not verified: use ParseSignedDSC for that.
Example ¶
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/M0Rf30/pkgformat/deb/source"
)
func main() {
data, err := os.ReadFile(filepath.Join("testdata", "packages", "hello", "hello_2.10-5.dsc"))
if err != nil {
log.Fatal(err)
}
d, err := source.ParseDSC(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(d.Source, d.Version, d.Format)
for _, f := range d.Files {
fmt.Println(f.Name, f.Size)
}
deps, _ := d.Relations("Build-Depends")
fmt.Println(deps)
}
Output: hello 2.10-5 3.0 (quilt) hello_2.10.orig.tar.gz 725946 hello_2.10.orig.tar.gz.asc 819 hello_2.10-5.debian.tar.xz 13112 [debhelper-compat (= 13) texinfo]
func ParseSignedDSC ¶
ParseSignedDSC verifies the clear-signed .dsc against kr and parses it. The error wraps a gpg sentinel for a bad or unknown signature and is ErrNotSigned for an unsigned file.
func ReadDSC ¶
ReadDSC reads and parses a .dsc from r, reading at most MaxDocumentBytes.
func (*DSC) Control ¶
func (d *DSC) Control() *deb.ControlFile
Control renders the .dsc as a deb822 paragraph in the order dpkg-source writes the fields: the Files field comes last, preceded by the Checksums fields. A checksum field is written for every hash that a file carries.
func (*DSC) Relations ¶
func (d *DSC) Relations(field string) ([]deb.Alternatives, error)
Relations parses one of the build relationship fields ("Build-Depends", "Build-Depends-Arch", "Build-Depends-Indep", "Build-Conflicts", "Build-Conflicts-Arch", "Build-Conflicts-Indep") with deb.ParseRelations.
type Field ¶
Field is a deb822 field the typed structs do not model: it is kept so a document survives a parse and a write.
type FileRef ¶
type FileRef struct {
Name string
Size int64
MD5 string
SHA1 string
SHA256 string
SHA512 string
// Section and Priority are the extra columns of the Files field of a
// .changes file; they are empty in a .dsc.
Section string
Priority string
}
FileRef is one file listed by a document, with the digests it carries. A digest that the document does not list is empty.
type Format ¶
type Format string
Format is the Format field of a .dsc: the layout of the source package.
const ( // Format10 is the original layout: an orig.tar.gz and a .diff.gz, or a // single .tar.gz for a native package. Format10 Format = "1.0" // FormatQuilt is "3.0 (quilt)": orig tarballs, a debian tarball and the // patches of debian/patches/series. FormatQuilt Format = "3.0 (quilt)" // FormatNative is "3.0 (native)": one tarball. FormatNative Format = "3.0 (native)" // FormatGit is "3.0 (git)": a git repository bundle. It is parsed and // written but cannot be unpacked here. FormatGit Format = "3.0 (git)" // FormatCustom is "3.0 (custom)": files chosen by the packager. FormatCustom Format = "3.0 (custom)" )
The source formats.
type InstalledPackage ¶
type InstalledPackage struct {
Name string
// Architecture is set when the entry names one ("libc6:amd64").
Architecture string
Version string
}
InstalledPackage is one entry of Installed-Build-Depends: a package at the exact version that was installed for the build.
func (InstalledPackage) String ¶
func (p InstalledPackage) String() string
String renders the entry as "name (= version)".
type PackageListEntry ¶
type PackageListEntry struct {
Name string
Type string // "deb", "udeb", ...
Section string
Priority string
// Options are the key=value words after the priority: "arch=any",
// "essential=yes", "profile=!noudeb".
Options []string
}
PackageListEntry is one line of the Package-List field: a binary package the source builds.
func (PackageListEntry) String ¶
func (e PackageListEntry) String() string
String renders the entry as a Package-List line, without the leading space.
type PatchOptions ¶
type PatchOptions struct {
// Strip is the number of leading path components removed from the file
// names of the patch (patch -pN); dpkg-source always uses 1.
Strip int
// BackupDir, when set, receives a copy of every file before the patch
// changes it, below the same relative path, and an empty file for every
// file the patch creates: the layout of a quilt .pc/<patch> directory. It
// must be an absolute path.
BackupDir string
}
PatchOptions tunes ApplyPatch.
type SourceEntry ¶
type SourceEntry struct {
DSC
// Directory is the directory that holds the files, relative to the archive
// root ("pool/main/h/hello").
Directory string
Priority string
Section string
}
SourceEntry is one stanza of a Sources index: the description of a source package, the directory of the archive that holds its files, and the Priority and Section the archive assigns.
func IndexSources ¶
func IndexSources(root string, opts *SourcesOptions) ([]SourceEntry, error)
IndexSources builds the Sources stanzas of the .dsc files below root, like apt-ftparchive sources: the .dsc and every file it lists are hashed from disk with MD5, SHA-1, SHA-256 and SHA-512 and listed, the .dsc first. The entries are sorted by source name and version.
Example ¶
package main
import (
"bytes"
"fmt"
"log"
"path/filepath"
"github.com/M0Rf30/pkgformat/deb/source"
)
func main() {
entries, err := source.IndexSources(filepath.Join("testdata", "packages", "sl"), &source.SourcesOptions{Directory: "pool/main/s/sl"})
if err != nil {
log.Fatal(err)
}
var buf bytes.Buffer
if err := source.WriteSources(&buf, entries, "gzip"); err != nil {
log.Fatal(err)
}
back, err := source.ReadSources(&buf, "Sources.gz")
if err != nil {
log.Fatal(err)
}
fmt.Println(back[0].Source, back[0].Directory)
}
Output: sl pool/main/s/sl
func ReadSources ¶
func ReadSources(r io.Reader, name string) ([]SourceEntry, error)
ReadSources reads a Sources index from r. name selects the decompression by its extension (".gz", ".bz2", ".xz", ".lz4", ".zst", or none); reading more than deb.MaxIndexBytes of decompressed data fails with deb.ErrTooLarge.
func (*SourceEntry) Control ¶
func (e *SourceEntry) Control() *deb.ControlFile
Control renders the stanza of the index.
func (*SourceEntry) Marshal ¶
func (e *SourceEntry) Marshal() []byte
Marshal renders the stanza, ending with a newline.
type SourcesOptions ¶
type SourcesOptions struct {
// Directory is the Directory field of the stanzas for packages found
// directly in root; a package in a subdirectory appends it.
Directory string
// Priority and Section are given to every stanza. apt-ftparchive writes
// none without an override file; dpkg-scansources writes "optional" and
// "unknown".
Priority, Section string
}
SourcesOptions tunes IndexSources.
type UnpackOptions ¶
type UnpackOptions struct {
// Replace allows the target directory to exist and not be empty; it is
// then removed first, as dpkg-source -x does by default. Without it a
// target that exists must be an empty directory.
Replace bool
// SkipPatches leaves the quilt patches of a "3.0 (quilt)" package
// unapplied and writes no .pc directory (dpkg-source --skip-patches).
SkipPatches bool
// SkipDebianization extracts only the orig tarballs and skips the debian
// tarball or the .diff.gz (dpkg-source --skip-debianization).
SkipDebianization bool
// MaxEntries bounds the archive entries extracted; default
// [DefaultMaxEntries].
MaxEntries int
// MaxBytes bounds the file bytes extracted; default [DefaultMaxBytes].
MaxBytes int64
}
UnpackOptions tunes Unpack. The zero value is valid.