Initial Commit
This commit is contained in:
114
internal/index/apt.go
Normal file
114
internal/index/apt.go
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* distrohop - A utility for correlating and identifying equivalent software
|
||||
* packages across different Linux distributions
|
||||
*
|
||||
* Copyright (C) 2025 Elara Ivy <elara@elara.ws>
|
||||
*
|
||||
* This file is part of distrohop.
|
||||
*
|
||||
* distrohop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* distrohop is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with distrohop. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package index
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/mholt/archives"
|
||||
"go.elara.ws/distrohop/internal/tags"
|
||||
)
|
||||
|
||||
type APT struct{}
|
||||
|
||||
func (APT) Name() string {
|
||||
return "apt"
|
||||
}
|
||||
|
||||
func (APT) IndexURL(baseURL, version, repo, arch string) ([]string, error) {
|
||||
indexURL, err := url.JoinPath(baseURL, "dists", version, repo, "Contents-"+arch+".gz")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Before Debian Wheezy, the path to Contents indices didn't include $COMP/repo, so we need to try
|
||||
// both the new and old URL formats. Ubuntu also still uses the pre-Debian-Wheezy convention.
|
||||
deprecatedURL, err := url.JoinPath(baseURL, "dists", version, "Contents-"+arch+".gz")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []string{indexURL, deprecatedURL}, nil
|
||||
}
|
||||
|
||||
func (APT) ReadPkgData(r io.Reader, out chan Record) {
|
||||
ctx := context.Background()
|
||||
format, r, err := archives.Identify(ctx, "", r)
|
||||
if err != nil {
|
||||
out <- Record{Error: err}
|
||||
return
|
||||
}
|
||||
|
||||
decomp, ok := format.(archives.Decompressor)
|
||||
if !ok {
|
||||
out <- Record{Error: errors.New("downloaded index is not a valid compressed file")}
|
||||
return
|
||||
}
|
||||
|
||||
dr, err := decomp.OpenReader(r)
|
||||
if err != nil {
|
||||
out <- Record{Error: err}
|
||||
return
|
||||
}
|
||||
defer dr.Close()
|
||||
|
||||
br := bufio.NewReader(dr)
|
||||
for {
|
||||
line, err := br.ReadString('\n')
|
||||
if errors.Is(err, io.EOF) {
|
||||
close(out)
|
||||
break
|
||||
} else if err != nil {
|
||||
out <- Record{Error: err}
|
||||
return
|
||||
}
|
||||
|
||||
lastSpaceIdx := strings.LastIndexByte(line, ' ')
|
||||
if lastSpaceIdx == -1 {
|
||||
continue
|
||||
}
|
||||
|
||||
fpath := "/" + strings.TrimSpace(line[:lastSpaceIdx])
|
||||
names := strings.Split(strings.TrimSpace(line[lastSpaceIdx+1:]), ",")
|
||||
for _, name := range names {
|
||||
slashIdx := strings.LastIndexByte(name, '/')
|
||||
if slashIdx != -1 {
|
||||
name = name[slashIdx+1:]
|
||||
}
|
||||
|
||||
if strings.Contains(fpath, "changelog.Debian") ||
|
||||
strings.Contains(fpath, "README.Debian") ||
|
||||
strings.Contains(fpath, "NEWS.Debian.gz") {
|
||||
continue
|
||||
}
|
||||
|
||||
out <- Record{
|
||||
Name: name,
|
||||
Tags: tags.Generate(fpath),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
154
internal/index/dnf.go
Normal file
154
internal/index/dnf.go
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* distrohop - A utility for correlating and identifying equivalent software
|
||||
* packages across different Linux distributions
|
||||
*
|
||||
* Copyright (C) 2025 Elara Ivy <elara@elara.ws>
|
||||
*
|
||||
* This file is part of distrohop.
|
||||
*
|
||||
* distrohop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* distrohop is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with distrohop. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package index
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/mholt/archives"
|
||||
"go.elara.ws/distrohop/internal/tags"
|
||||
)
|
||||
|
||||
type repomd struct {
|
||||
Locations []location `xml:"data>location"`
|
||||
}
|
||||
|
||||
type location struct {
|
||||
Href string `xml:"href,attr"`
|
||||
}
|
||||
|
||||
func (r repomd) getGzipFile() string {
|
||||
for _, loc := range r.Locations {
|
||||
if strings.HasSuffix(loc.Href, "filelists.xml.gz") {
|
||||
return loc.Href
|
||||
}
|
||||
}
|
||||
return "<unknown>"
|
||||
}
|
||||
|
||||
type DNF struct{}
|
||||
|
||||
func (DNF) Name() string {
|
||||
return "dnf"
|
||||
}
|
||||
|
||||
func (DNF) IndexURL(baseURL, version, repo, arch string) ([]string, error) {
|
||||
u, err := url.ParseRequestURI(baseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
repomdPath := fmt.Sprintf("/pub/fedora/linux/releases/%s/%s/%s/os/repodata/repomd.xml", version, repo, arch)
|
||||
u.Path = repomdPath
|
||||
|
||||
res, err := http.Get(u.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var data repomd
|
||||
err = xml.NewDecoder(res.Body).Decode(&data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gzipFile := data.getGzipFile()
|
||||
if gzipFile == "" {
|
||||
return nil, errors.New("no gzip file found in repomd.xml")
|
||||
}
|
||||
|
||||
u.Path = fmt.Sprintf("/pub/fedora/linux/releases/%s/%s/%s/os/%s", version, repo, arch, gzipFile)
|
||||
return []string{u.String()}, nil
|
||||
}
|
||||
|
||||
func (DNF) ReadPkgData(r io.Reader, out chan Record) {
|
||||
ctx := context.Background()
|
||||
format, r, err := archives.Identify(ctx, "", r)
|
||||
if err != nil {
|
||||
out <- Record{Error: err}
|
||||
return
|
||||
}
|
||||
|
||||
decomp, ok := format.(archives.Decompressor)
|
||||
if !ok {
|
||||
out <- Record{Error: errors.New("downloaded index is not a valid compressed file")}
|
||||
return
|
||||
}
|
||||
|
||||
dr, err := decomp.OpenReader(r)
|
||||
if err != nil {
|
||||
out <- Record{Error: err}
|
||||
return
|
||||
}
|
||||
defer dr.Close()
|
||||
|
||||
br := bufio.NewReader(dr)
|
||||
var currentPkg string
|
||||
|
||||
for {
|
||||
line, err := br.ReadString('\n')
|
||||
if errors.Is(err, io.EOF) {
|
||||
close(out)
|
||||
break
|
||||
} else if err != nil {
|
||||
out <- Record{Error: err}
|
||||
return
|
||||
}
|
||||
line = strings.TrimSpace(line)
|
||||
|
||||
switch {
|
||||
case strings.HasPrefix(line, "<file"):
|
||||
// Skip directories and symlinks
|
||||
if strings.HasPrefix(line[5:], `type="dir"`) || line[5] == 'l' {
|
||||
continue
|
||||
}
|
||||
|
||||
start := strings.IndexByte(line, '>') + 1
|
||||
end := strings.LastIndexByte(line, '<')
|
||||
fpath := line[start:end]
|
||||
|
||||
if strings.Contains(fpath, ".build-id") {
|
||||
continue
|
||||
}
|
||||
|
||||
out <- Record{
|
||||
Name: currentPkg,
|
||||
Tags: tags.Generate(fpath),
|
||||
}
|
||||
case strings.HasPrefix(line, "<package"):
|
||||
start := strings.LastIndex(line, `name="`) + 6
|
||||
end := start + strings.IndexByte(line[start:], '"')
|
||||
currentPkg = line[start:end]
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
60
internal/index/index.go
Normal file
60
internal/index/index.go
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* distrohop - A utility for correlating and identifying equivalent software
|
||||
* packages across different Linux distributions
|
||||
*
|
||||
* Copyright (C) 2025 Elara Ivy <elara@elara.ws>
|
||||
*
|
||||
* This file is part of distrohop.
|
||||
*
|
||||
* distrohop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* distrohop is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with distrohop. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package index
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Record represents a data record for a single package
|
||||
type Record struct {
|
||||
Name string
|
||||
Tags []string
|
||||
Error error
|
||||
}
|
||||
|
||||
type Importer interface {
|
||||
// Name returns the name of the importer
|
||||
Name() string
|
||||
// IndexURL generates a list of possible index URLs to try
|
||||
IndexURL(baseURL, version, repo, arch string) ([]string, error)
|
||||
// ReadPkgData reads data from an index file and sends it on out
|
||||
ReadPkgData(r io.Reader, out chan Record)
|
||||
}
|
||||
|
||||
var importers = []Importer{
|
||||
APT{},
|
||||
DNF{},
|
||||
Pacman{},
|
||||
}
|
||||
|
||||
// GetImporter gets an importer by its name
|
||||
func GetImporter(name string) (Importer, error) {
|
||||
for _, importer := range importers {
|
||||
if importer.Name() == name {
|
||||
return importer, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("no such importer: %q", name)
|
||||
}
|
||||
145
internal/index/pacman.go
Normal file
145
internal/index/pacman.go
Normal file
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* distrohop - A utility for correlating and identifying equivalent software
|
||||
* packages across different Linux distributions
|
||||
*
|
||||
* Copyright (C) 2025 Elara Ivy <elara@elara.ws>
|
||||
*
|
||||
* This file is part of distrohop.
|
||||
*
|
||||
* distrohop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* distrohop is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with distrohop. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package index
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/mholt/archives"
|
||||
"go.elara.ws/distrohop/internal/tags"
|
||||
)
|
||||
|
||||
type Pacman struct{}
|
||||
|
||||
func (Pacman) Name() string {
|
||||
return "pacman"
|
||||
}
|
||||
|
||||
func (Pacman) IndexURL(baseURL, version, repo, arch string) ([]string, error) {
|
||||
baseURL = os.Expand(baseURL, func(s string) string {
|
||||
switch s {
|
||||
case "repo":
|
||||
return repo
|
||||
case "arch":
|
||||
return arch
|
||||
}
|
||||
return "$" + s
|
||||
})
|
||||
|
||||
u, err := url.ParseRequestURI(baseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
filePath, err := url.JoinPath(u.Path, repo+".files")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = filePath
|
||||
return []string{u.String()}, nil
|
||||
}
|
||||
|
||||
func (Pacman) ReadPkgData(r io.Reader, out chan Record) {
|
||||
ctx := context.Background()
|
||||
format, r, err := archives.Identify(ctx, "", r)
|
||||
if err != nil {
|
||||
out <- Record{Error: err}
|
||||
return
|
||||
}
|
||||
|
||||
decomp, ok := format.(archives.Decompressor)
|
||||
if !ok {
|
||||
out <- Record{Error: errors.New("downloaded index is not a valid compressed file")}
|
||||
return
|
||||
}
|
||||
|
||||
dr, err := decomp.OpenReader(r)
|
||||
if err != nil {
|
||||
out <- Record{Error: err}
|
||||
return
|
||||
}
|
||||
defer dr.Close()
|
||||
|
||||
tr := tar.NewReader(dr)
|
||||
var currentPkg string
|
||||
|
||||
for {
|
||||
hdr, err := tr.Next()
|
||||
if errors.Is(err, io.EOF) {
|
||||
close(out)
|
||||
break
|
||||
} else if err != nil {
|
||||
out <- Record{Error: err}
|
||||
return
|
||||
}
|
||||
|
||||
switch path.Base(hdr.Name) {
|
||||
case "desc":
|
||||
data, err := io.ReadAll(tr)
|
||||
if err != nil {
|
||||
out <- Record{Error: err}
|
||||
return
|
||||
}
|
||||
|
||||
labelIdx := bytes.Index(data, []byte("%NAME%\n"))
|
||||
if labelIdx == -1 {
|
||||
continue
|
||||
}
|
||||
|
||||
start := labelIdx + 7
|
||||
end := start + bytes.IndexByte(data[start:], '\n')
|
||||
currentPkg = string(data[start:end])
|
||||
case "files":
|
||||
br := bufio.NewReader(tr)
|
||||
for {
|
||||
fpath, err := br.ReadString('\n')
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
} else if err != nil {
|
||||
out <- Record{Error: err}
|
||||
return
|
||||
}
|
||||
|
||||
fpath = strings.TrimSpace(fpath)
|
||||
if fpath == "%FILES%" || strings.HasSuffix(fpath, "/") {
|
||||
continue
|
||||
}
|
||||
|
||||
fpath = "/" + fpath
|
||||
|
||||
out <- Record{
|
||||
Name: currentPkg,
|
||||
Tags: tags.Generate(fpath),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user