-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdpkg.go
More file actions
70 lines (58 loc) · 1.55 KB
/
dpkg.go
File metadata and controls
70 lines (58 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package dpkg
import (
"os"
"strings"
)
// Dpkg represents a Debian package manager
type Dpkg struct {
StatusFileLocation string
}
// NewDpkg creates a new instance of Dpkg
func NewDpkg() *Dpkg {
return &Dpkg{
StatusFileLocation: DPKG_DATABASE,
}
}
// Info retrieves the metadata of a Debian package
func (d *Dpkg) Info(debFile string) (*DebPackage, error) {
pkg, err := d.readArchive(debFile)
if err != nil {
return nil, err
}
return pkg, nil
}
// List lists packages from the default dpkg database
func (d *Dpkg) List() ([]DebPackage, error) {
return parseStatusFile(d.StatusFileLocation)
}
// ListGrep lists packages from the default dpkg database that match the given package name
func (d *Dpkg) ListGrep(pkgName string) ([]DebPackage, error) {
packages, err := parseStatusFile(d.StatusFileLocation)
if err != nil {
return nil, err
}
var filteredPackages []DebPackage
for _, pkg := range packages {
if strings.Contains(pkg.Fields["Package"], pkgName) {
filteredPackages = append(filteredPackages, pkg)
}
}
return filteredPackages, nil
}
// IsDebFile checks if the file is a valid .deb package
// https://manpages.debian.org/buster/dpkg-dev/deb.5.en.html#FORMAT
func (d *Dpkg) IsDebFile(debFile string) bool {
file, err := os.Open(debFile)
if err != nil {
return false
}
defer file.Close()
magicValue := "!<arch>\ndebian-binary"
magic := make([]byte, len(magicValue))
_, err = file.Read(magic)
if err != nil {
return false
}
// Check the "magic value" for the ar format
return strings.HasPrefix(string(magic), magicValue)
}