dpkg

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: MIT Imports: 21 Imported by: 0

README

dpkg

dpkg is a Go package for managing Debian packages. It provides functionalities to read and parse .deb files, list installed packages, and filter packages based on their names.

Installation

To install the dpkg package, use the following command:

go get github.com/go-apt/dpkg

Main Functions

Reading the Contents of a .deb File

To read the contents of a .deb file and retrieve its metadata, use the Info function:

// Create a new instance of the Dpkg struct
d := dpkg.NewDpkg()

// Read the contents of the .deb file
pkg, err := d.Info("/path/to/debFile")
if err != nil {
    fmt.Fprintf(os.Stderr, "Error: %v\n", err)
    os.Exit(1)
}

// Print package name from the .deb file
fmt.Printf("Package from .deb file: %s\n", pkg.Package)
Validating a .deb File

To validate if a file is a valid .deb package, use the IsDebFile function:

debFile := "/path/to/debFile"

// Validate if the file is a .deb package
if !d.IsDebFile(debFile) {
    fmt.Fprintf(os.Stderr, "Error: %s is not a valid .deb file\n", debFile)
    os.Exit(1)
}

fmt.Printf("%s is a valid .deb file\n", debFile)
Listing Installed Packages

To list the installed packages and retrieve their metadata, use the List function:

// Create a new instance of the Dpkg struct
d := dpkg.NewDpkg()

// Read the contents of the /var/lib/dpkg/status file
packages, err := d.List()
if err != nil {
    fmt.Fprintf(os.Stderr, "Error: %v\n", err)
    os.Exit(1)
}

// Print package names from installed packages
for _, p := range packages {
    fmt.Printf("Package from dpkg status file: %s\n", p.Package)
}
Filtering Packages by Name

To filter packages by name, use the ListGrep function:

// Create a new instance of the Dpkg struct
d := dpkg.NewDpkg()

// Filter packages by name
filteredPackages, err := d.ListGrep("apt")
if err != nil {
    fmt.Fprintf(os.Stderr, "Error: %v\n", err)
    os.Exit(1)
}

// Print package names from filtered packages
for _, p := range filteredPackages {
    fmt.Printf("Packages from Grep search: %s\n", p.Package)
}

Documentation

Index

Constants

View Source
const (
	DPKG_DATABASE = "/var/lib/dpkg/status"
)

Variables

View Source
var (
	ErrDebHeader           = errors.New("go-apt/dpkg: invalid debian package (ar magic header not matched)")
	ErrNoControlFile       = errors.New("go-apt/dpkg: failed to find control.tar file")
	ErrNoDpkgStatusFile    = errors.New("go-apt/dpkg: failed to read " + DPKG_DATABASE + " file")
	ErrNoFilenameAvailable = errors.New("go-apt/dpkg: no Filename available for this package")
)

Functions

This section is empty.

Types

type DebPackage

type DebPackage struct {
	Package       string   `apt:"Package"`
	Version       string   `apt:"Version"`
	Architecture  string   `apt:"Architecture"`
	Maintainer    string   `apt:"Maintainer"`
	Essential     string   `apt:"Essential"`
	Protected     string   `apt:"Protected"`
	Status        string   `apt:"Status"`
	Priority      string   `apt:"Priority"`
	Section       string   `apt:"Section"`
	InstalledSize string   `apt:"Installed-Size"`
	Origin        string   `apt:"Origin"`
	MultiArch     string   `apt:"Multi-Arch"`
	Source        string   `apt:"Source"`
	Replaces      []string `apt:"Replaces"`
	Provides      []string `apt:"Provides"`
	Depends       []string `apt:"Depends"`
	PreDepends    []string `apt:"Pre-Depends"`
	Recommends    []string `apt:"Recommends"`
	Suggests      []string `apt:"Suggests"`
	Breaks        []string `apt:"Breaks"`
	Conflicts     []string `apt:"Conflicts"`
	Enhances      []string `apt:"Enhances"`
	Conffiles     string   `apt:"Conffiles"`
	Filename      string   `apt:"Filename"`
	Size          int64    `apt:"Size"`
	Description   string   `apt:"Description"`
	Homepage      string   `apt:"Homepage"`
	MD5Hash       string   `apt:"MD5sum"`
	SHA1Hash      string   `apt:"SHA1"`
	SHA256Hash    string   `apt:"SHA256"`
}

DebPackage represents the metadata of a Debian package https://salsa.debian.org/dpkg-team/dpkg/-/blob/main/lib/dpkg/parse.c?ref_type=heads#L53

func (*DebPackage) CalcSize added in v0.0.3

func (dp *DebPackage) CalcSize()

CalcSize calculates the size of the package file

func (*DebPackage) CalculateAllHashes added in v0.0.3

func (dp *DebPackage) CalculateAllHashes() error

CalculateAllHashes calculates the MD5, SHA1, and SHA256 hashes of the package content

func (DebPackage) GetAptTag added in v0.0.3

func (dp DebPackage) GetAptTag(field interface{}) string

GetAptTag retrieves the 'apt' tag value for a given field name in the DebPackage struct

func (*DebPackage) HasFilename added in v0.0.3

func (dp *DebPackage) HasFilename() bool

HasFilename checks if the DebPackage has a filename

func (*DebPackage) MD5sum added in v0.0.2

func (dp *DebPackage) MD5sum() string

MD5sum returns the MD5 hash of the package content

func (*DebPackage) SHA1sum added in v0.0.3

func (dp *DebPackage) SHA1sum() string

SHA1sum returns the SHA1 hash of the package content

func (*DebPackage) SHA256sum added in v0.0.3

func (dp *DebPackage) SHA256sum() string

SHA256sum returns the SHA256 hash of the package content

func (*DebPackage) ShortDescription

func (dp *DebPackage) ShortDescription() string

ShortDescription returns the short description of the package

type Dpkg

type Dpkg struct {
	StatusFileLocation string
}

Dpkg represents a Debian package manager

func NewDpkg

func NewDpkg() *Dpkg

NewDpkg creates a new instance of Dpkg

func (*Dpkg) CompareVersions added in v0.0.3

func (dp *Dpkg) CompareVersions(v1, v2 string) int

CompareVersions compares Debian package versions based on https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-version

func (*Dpkg) Info

func (d *Dpkg) Info(debFile string) (*DebPackage, error)

Info retrieves the metadata of a Debian package

func (*Dpkg) IsDebFile

func (d *Dpkg) IsDebFile(debFile string) bool

IsDebFile checks if the file is a valid .deb package https://manpages.debian.org/buster/dpkg-dev/deb.5.en.html#FORMAT

func (*Dpkg) List

func (d *Dpkg) List() ([]DebPackage, error)

List lists packages from the default dpkg database

func (*Dpkg) ListGrep

func (d *Dpkg) ListGrep(pkgName string) ([]DebPackage, error)

ListGrep lists packages from the default dpkg database that match the given package name

func (*Dpkg) ScanPackages added in v0.0.3

func (d *Dpkg) ScanPackages(dir string) ([]byte, error)

ScanPackages scans the directory for packages matching the criteria

type PackagesScanner added in v0.0.3

type PackagesScanner struct {
	RootDir      string
	Arch         string
	Type         string
	Hashes       []string
	Multiversion bool
}

PackagesScanner represents a scanner for Debian packages

func NewPackagesScanner added in v0.0.3

func NewPackagesScanner(dir string) *PackagesScanner

NewPackagesScanner creates a new instance of PackagesScanner

func (*PackagesScanner) ScanPackages added in v0.0.3

func (ps *PackagesScanner) ScanPackages() ([]byte, error)

ScanPackages scans the directory for packages matching the criteria

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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