├── LICENSE ├── Makefile ├── README.md ├── dpkg.go ├── dpkgread └── main.go ├── parser.go └── parser_test.go /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2025 Tadas Vilkeliskis 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | projectSourcePath := github.com/tadasv/go-dpkg 2 | dockerMountPath := /go/src/$(projectSourcePath) 3 | 4 | docker-shell: 5 | docker exec -ti go-dpkg-dev bash 6 | 7 | docker-clean: 8 | docker rm go-dpkg-dev 9 | 10 | docker-dev: 11 | docker run --name go-dpkg-dev -ti -d -w $(dockerMountPath) -v $(PWD):$(dockerMountPath) golang:1.6.2 bash 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-dpkg 2 | 3 | This is library for working with Debian package databases. 4 | 5 | Currently it only supports parsing `dpkg` status file, which is usually located at `/var/lib/dpkg/status`. 6 | -------------------------------------------------------------------------------- /dpkg.go: -------------------------------------------------------------------------------- 1 | package dpkg 2 | 3 | import ( 4 | "os" 5 | ) 6 | 7 | type Package struct { 8 | Package string 9 | Status string 10 | Priority string 11 | Architecture string 12 | MultiArch string 13 | Maintainer string 14 | Version string 15 | Section string 16 | InstalledSize int64 17 | Depends string 18 | PreDepends string 19 | Description string 20 | Source string 21 | Homepage string 22 | } 23 | 24 | func ReadPackagesFromFile(fileName string) ([]Package, error) { 25 | fd, err := os.Open(fileName) 26 | if err != nil { 27 | return nil, err 28 | } 29 | defer fd.Close() 30 | 31 | parser := NewParser(fd) 32 | return parser.Parse(), nil 33 | } 34 | -------------------------------------------------------------------------------- /dpkgread/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/tadasv/go-dpkg" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | fileName := "/var/lib/dpkg/status" 11 | 12 | if len(os.Args) == 2 { 13 | fileName = os.Args[1] 14 | } 15 | 16 | packages, err := dpkg.ReadPackagesFromFile(fileName) 17 | if err != nil { 18 | fmt.Printf("Error: %v\n", err) 19 | return 20 | } 21 | 22 | for _, pkg := range packages { 23 | fmt.Printf("%-32s%-32s %s\n", pkg.Package, pkg.Version, pkg.Status) 24 | } 25 | 26 | fmt.Printf("Read %d packages\n", len(packages)) 27 | } 28 | -------------------------------------------------------------------------------- /parser.go: -------------------------------------------------------------------------------- 1 | package dpkg 2 | 3 | import ( 4 | "bufio" 5 | "io" 6 | "strconv" 7 | "strings" 8 | ) 9 | 10 | type Parser struct { 11 | r *bufio.Reader 12 | } 13 | 14 | func NewParser(r io.Reader) *Parser { 15 | return &Parser{ 16 | r: bufio.NewReader(r), 17 | } 18 | } 19 | 20 | func (p *Parser) parseLine(line string) (string, string) { 21 | // returns (key, value) or ("", value) if multi-line value 22 | line = strings.TrimRight(line, "\n") 23 | 24 | if len(line) == 0 { 25 | return "", "" 26 | } 27 | 28 | if line[0] == ' ' { 29 | return "", line 30 | } 31 | 32 | separatorIndex := strings.Index(line, ":") 33 | key := line[0:separatorIndex] 34 | value := line[separatorIndex+1 : len(line)] 35 | 36 | return key, value 37 | } 38 | 39 | func (p *Parser) mapToPackage(m map[string]string) (Package, error) { 40 | pkg := Package{} 41 | 42 | for key, value := range m { 43 | value = strings.TrimRight(value, " \n") 44 | value = strings.TrimLeft(value, " ") 45 | 46 | switch key { 47 | case "Package": 48 | pkg.Package = value 49 | case "Version": 50 | pkg.Version = value 51 | case "Section": 52 | pkg.Section = value 53 | case "Installed-Size": 54 | i, err := strconv.Atoi(value) 55 | if err == nil { 56 | pkg.InstalledSize = int64(i) 57 | } 58 | case "Maintainer": 59 | pkg.Maintainer = value 60 | case "Status": 61 | pkg.Status = value 62 | case "Source": 63 | pkg.Source = value 64 | case "Architecture": 65 | pkg.Architecture = value 66 | case "Multi-Arch": 67 | pkg.MultiArch = value 68 | case "Depends": 69 | pkg.Depends = value 70 | case "Pre-Depends": 71 | pkg.PreDepends = value 72 | case "Description": 73 | pkg.Description = value 74 | case "Homepage": 75 | pkg.Homepage = value 76 | case "Priority": 77 | pkg.Priority = value 78 | } 79 | } 80 | 81 | return pkg, nil 82 | } 83 | 84 | func (p *Parser) Parse() []Package { 85 | prevKey := "" 86 | packages := []Package{} 87 | m := make(map[string]string) 88 | 89 | for { 90 | line, readError := p.r.ReadString('\n') 91 | key, value := p.parseLine(line) 92 | 93 | if key == "" && value != "" { 94 | m[prevKey] = m[prevKey] + value 95 | } else if key == "" && value == "" { 96 | if len(m) > 0 { 97 | pkg, err := p.mapToPackage(m) 98 | if err == nil { 99 | packages = append(packages, pkg) 100 | } 101 | m = make(map[string]string) 102 | } 103 | } else if key != "" && value != "" { 104 | prevKey = key 105 | m[key] = value 106 | } 107 | 108 | if readError != nil { 109 | if len(m) > 0 { 110 | pkg, err := p.mapToPackage(m) 111 | if err == nil { 112 | packages = append(packages, pkg) 113 | } 114 | } 115 | break 116 | } 117 | } 118 | 119 | return packages 120 | } 121 | -------------------------------------------------------------------------------- /parser_test.go: -------------------------------------------------------------------------------- 1 | package dpkg 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | ) 7 | 8 | func assertEqual(t *testing.T, expected string, current string) { 9 | if expected != current { 10 | t.Errorf("Invalid value. Expected '%v', got '%v'", expected, current) 11 | } 12 | } 13 | 14 | func TestMapToPackage(t *testing.T) { 15 | parser := NewParser(nil) 16 | m := map[string]string{ 17 | "Package": "package", 18 | "Version": "version", 19 | "Section": "section", 20 | "Installed-Size": "123", 21 | "Maintainer": "tadas", 22 | "Status": "status", 23 | "Source": "source", 24 | "Architecture": "amd64", 25 | "Multi-Arch": "same", 26 | "Depends": "depends", 27 | "Pre-Depends": "predepends", 28 | "Description": "desc", 29 | "Homepage": "home", 30 | "Priority": "priority", 31 | } 32 | 33 | pkg, err := parser.mapToPackage(m) 34 | if err != nil { 35 | t.Errorf("Expected no error, got %v", err) 36 | } 37 | 38 | assertEqual(t, "package", pkg.Package) 39 | assertEqual(t, "version", pkg.Version) 40 | assertEqual(t, "section", pkg.Section) 41 | assertEqual(t, "tadas", pkg.Maintainer) 42 | assertEqual(t, "status", pkg.Status) 43 | assertEqual(t, "source", pkg.Source) 44 | assertEqual(t, "amd64", pkg.Architecture) 45 | assertEqual(t, "same", pkg.MultiArch) 46 | assertEqual(t, "depends", pkg.Depends) 47 | assertEqual(t, "predepends", pkg.PreDepends) 48 | assertEqual(t, "desc", pkg.Description) 49 | assertEqual(t, "home", pkg.Homepage) 50 | assertEqual(t, "priority", pkg.Priority) 51 | 52 | if pkg.InstalledSize != 123 { 53 | t.Errorf("Invalid size: %v", pkg.InstalledSize) 54 | } 55 | 56 | } 57 | 58 | func TestParseLineHandlesEmptyString(t *testing.T) { 59 | parser := NewParser(nil) 60 | key, value := parser.parseLine("") 61 | 62 | assertEqual(t, "", key) 63 | assertEqual(t, "", value) 64 | } 65 | 66 | func TestParseLineHandlesNewLine(t *testing.T) { 67 | parser := NewParser(nil) 68 | key, value := parser.parseLine("\n") 69 | 70 | assertEqual(t, "", key) 71 | assertEqual(t, "", value) 72 | } 73 | 74 | func TestParseLineHandlesMultilineValue(t *testing.T) { 75 | parser := NewParser(nil) 76 | key, value := parser.parseLine(" some: value\n") 77 | 78 | assertEqual(t, "", key) 79 | assertEqual(t, " some: value", value) 80 | } 81 | 82 | func TestParseLineHandlesKeyValue(t *testing.T) { 83 | parser := NewParser(nil) 84 | key, value := parser.parseLine("Key: value\n") 85 | 86 | assertEqual(t, "Key", key) 87 | assertEqual(t, " value", value) 88 | } 89 | 90 | func TestParseAllBlankLines(t *testing.T) { 91 | data := "\n\n\n\n\n" 92 | reader := bytes.NewBufferString(data) 93 | parser := NewParser(reader) 94 | packages := parser.Parse() 95 | if len(packages) != 0 { 96 | t.Errorf("Expected 0 packages, got: %v", len(packages)) 97 | } 98 | } 99 | 100 | func TestParseValidData(t *testing.T) { 101 | data := `Package: libquadmath0 102 | Status: install ok installed 103 | Priority: optional 104 | Section: libs 105 | Installed-Size: 275 106 | Maintainer: Debian GCC Maintainers 107 | Architecture: amd64 108 | Multi-Arch: same 109 | Source: gcc-4.9 110 | Version: 4.9.2-10 111 | Depends: gcc-4.9-base (= 4.9.2-10), libc6 (>= 2.14) 112 | Pre-Depends: multiarch-support 113 | Description: GCC Quad-Precision Math Library 114 | A library, which provides quad-precision mathematical functions on targets 115 | supporting the __float128 datatype. The library is used to provide on such 116 | targets the REAL(16) type in the GNU Fortran compiler. 117 | Homepage: http://gcc.gnu.org/ 118 | 119 | Package: libedit2 120 | Status: install ok installed 121 | Priority: standard 122 | Section: libs 123 | Installed-Size: 277 124 | Maintainer: LLVM Packaging Team 125 | Architecture: amd64 126 | Multi-Arch: same 127 | Source: libedit 128 | Version: 3.1-20140620-2 129 | Depends: libbsd0 (>= 0.0), libc6 (>= 2.17), libtinfo5 130 | Pre-Depends: multiarch-support 131 | Description: BSD editline and history libraries 132 | Command line editor library provides generic line editing, 133 | history, and tokenization functions. 134 | . 135 | It slightly resembles GNU readline. 136 | Homepage: http://www.thrysoee.dk/editline/` 137 | 138 | reader := bytes.NewBufferString(data) 139 | parser := NewParser(reader) 140 | packages := parser.Parse() 141 | 142 | if len(packages) != 2 { 143 | t.Errorf("Expected 2 packages, got: %v", len(packages)) 144 | } 145 | 146 | pkg := packages[0] 147 | assertEqual(t, "libquadmath0", pkg.Package) 148 | assertEqual(t, "4.9.2-10", pkg.Version) 149 | assertEqual(t, "libs", pkg.Section) 150 | assertEqual(t, "Debian GCC Maintainers ", pkg.Maintainer) 151 | assertEqual(t, "install ok installed", pkg.Status) 152 | assertEqual(t, "gcc-4.9", pkg.Source) 153 | assertEqual(t, "amd64", pkg.Architecture) 154 | assertEqual(t, "same", pkg.MultiArch) 155 | assertEqual(t, "gcc-4.9-base (= 4.9.2-10), libc6 (>= 2.14)", pkg.Depends) 156 | assertEqual(t, "multiarch-support", pkg.PreDepends) 157 | assertEqual(t, "http://gcc.gnu.org/", pkg.Homepage) 158 | assertEqual(t, "optional", pkg.Priority) 159 | if pkg.InstalledSize != 275 { 160 | t.Errorf("Incorrect size: %v", pkg.InstalledSize) 161 | } 162 | 163 | pkg = packages[1] 164 | assertEqual(t, "libedit2", pkg.Package) 165 | assertEqual(t, "3.1-20140620-2", pkg.Version) 166 | assertEqual(t, "libs", pkg.Section) 167 | assertEqual(t, "LLVM Packaging Team ", pkg.Maintainer) 168 | assertEqual(t, "install ok installed", pkg.Status) 169 | assertEqual(t, "libedit", pkg.Source) 170 | assertEqual(t, "amd64", pkg.Architecture) 171 | assertEqual(t, "same", pkg.MultiArch) 172 | assertEqual(t, "libbsd0 (>= 0.0), libc6 (>= 2.17), libtinfo5", pkg.Depends) 173 | assertEqual(t, "multiarch-support", pkg.PreDepends) 174 | assertEqual(t, "http://www.thrysoee.dk/editline/", pkg.Homepage) 175 | assertEqual(t, "standard", pkg.Priority) 176 | if pkg.InstalledSize != 277 { 177 | t.Errorf("Incorrect size: %v", pkg.InstalledSize) 178 | } 179 | } 180 | --------------------------------------------------------------------------------