Skip to content

Commit 9284a22

Browse files
committed
rpm: change RepositoryHint to use URL formatting
This makes things easier to reason about and removes the ad-hoc parsing that used to happen. Signed-off-by: Hank Donnay <[email protected]>
1 parent 7e08e21 commit 9284a22

File tree

4 files changed

+224
-209
lines changed

4 files changed

+224
-209
lines changed

rpm/native_db.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"io"
8+
"net/url"
89
"runtime/trace"
910
"strings"
1011

@@ -63,7 +64,7 @@ func packagesFromDB(ctx context.Context, pkgdb string, db nativeDB) ([]*claircor
6364
p.Module = info.Module
6465
}
6566
p.Version = constructEVR(&b, &info)
66-
p.RepositoryHint = constructHint(&b, &info)
67+
p.RepositoryHint = constructHint(&info)
6768

6869
if s, ok := src[info.SourceNEVR]; ok {
6970
p.Source = s
@@ -174,14 +175,12 @@ func constructEVR(b *strings.Builder, info *Info) string {
174175
return b.String()
175176
}
176177

177-
func constructHint(b *strings.Builder, info *Info) string {
178-
b.Reset()
178+
func constructHint(info *Info) string {
179+
v := url.Values{}
179180
if info.Digest != "" {
180-
b.WriteString("hash:")
181181
switch info.DigestAlgo {
182182
case 8:
183-
b.WriteString("sha256:")
184-
b.WriteString(info.Digest)
183+
v.Add("hash", fmt.Sprintf("sha256:%s", info.Digest))
185184
}
186185
}
187186
if len(info.Signature) != 0 {
@@ -193,12 +192,9 @@ func constructHint(b *strings.Builder, info *Info) string {
193192
if p.SigType != 0 {
194193
continue
195194
}
196-
if b.Len() != 0 {
197-
b.WriteByte('|')
198-
}
199-
fmt.Fprintf(b, "key:%016x", p.IssuerKeyId)
195+
v.Add("key", fmt.Sprintf("%016x", p.IssuerKeyId))
200196
}
201197
}
202198
}
203-
return b.String()
199+
return v.Encode()
204200
}

rpm/packagescanner.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package rpm
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"io/fs"
9+
"net/url"
810
"os"
911
"path"
1012
"runtime/trace"
@@ -22,7 +24,7 @@ import (
2224
const (
2325
pkgName = "rpm"
2426
pkgKind = "package"
25-
pkgVersion = "9"
27+
pkgVersion = "10"
2628
)
2729

2830
var (
@@ -173,6 +175,35 @@ func (ps *Scanner) Scan(ctx context.Context, layer *claircore.Layer) ([]*clairco
173175
}
174176
pkgs = append(pkgs, ps...)
175177
}
178+
rm, err := repoMap(ctx, sys)
179+
switch {
180+
case errors.Is(err, nil) && len(rm) != 0: // OK
181+
for _, pkg := range pkgs {
182+
nerva := fmt.Sprintf("%s-%s.%s", pkg.Name, pkg.Version, pkg.Arch)
183+
repoid, ok := rm[nerva]
184+
if !ok {
185+
// Packages not installed via dnf, which may happen during
186+
// bootstrapping, aren't present in the dnf history database.
187+
// This means the process shouldn't bail if the package is
188+
// missing.
189+
continue
190+
}
191+
v, err := url.ParseQuery(pkg.RepositoryHint)
192+
if err != nil { // Shouldn't happen:
193+
zlog.Warn(ctx).
194+
AnErr("url.ParseQuery", err).
195+
Msg("malformed RepositoryHint")
196+
continue
197+
}
198+
v.Add("repoid", repoid)
199+
pkg.RepositoryHint = v.Encode()
200+
}
201+
case errors.Is(err, nil) && len(rm) == 0: // nothing found
202+
default: // some error
203+
zlog.Warn(ctx).
204+
AnErr("repoMap", err).
205+
Msg("unable to open dnf history database")
206+
}
176207

177208
return pkgs, nil
178209
}

0 commit comments

Comments
 (0)