Skip to content

Commit

Permalink
feat: set InstalledFiles for DEB and RPM packages (#5488)
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <knqyf263@gmail.com>
Co-authored-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
lebauce and knqyf263 committed Nov 16, 2023
1 parent ae4bcf6 commit 44d0b28
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
14 changes: 14 additions & 0 deletions pkg/fanal/analyzer/pkg/dpkg/dpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func (a dpkgAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAnalysis
return path != availableFile
}

packageFiles := make(map[string][]string)

// parse other files
err = fsutils.WalkDir(input.FS, ".", required, func(path string, d fs.DirEntry, r io.Reader) error {
// parse list files
Expand All @@ -74,6 +76,7 @@ func (a dpkgAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAnalysis
if err != nil {
return err
}
packageFiles[strings.TrimSuffix(filepath.Base(path), ".list")] = systemFiles
systemInstalledFiles = append(systemInstalledFiles, systemFiles...)
return nil
}
Expand All @@ -89,6 +92,17 @@ func (a dpkgAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAnalysis
return nil, xerrors.Errorf("dpkg walk error: %w", err)
}

// map the packages to their respective files
for i, pkgInfo := range packageInfos {
for j, pkg := range pkgInfo.Packages {
installedFiles, found := packageFiles[pkg.Name]
if !found {
installedFiles = packageFiles[pkg.Name+":"+pkg.Arch]
}
packageInfos[i].Packages[j].InstalledFiles = installedFiles
}
}

return &analyzer.AnalysisResult{
PackageInfos: packageInfos,
SystemInstalledFiles: systemInstalledFiles,
Expand Down
8 changes: 5 additions & 3 deletions pkg/fanal/analyzer/pkg/rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ func (a rpmPkgAnalyzer) listPkgs(db RPMDB) (types.Packages, []string, error) {
if err != nil {
return nil, nil, xerrors.Errorf("unable to get installed files: %w", err)
}
files = lo.Map(files, func(file string, _ int) string {
return filepath.ToSlash(file)
})

for i, file := range files {
files[i] = filepath.ToSlash(file)
}
}

// RPM DB uses MD5 digest
Expand Down Expand Up @@ -171,6 +172,7 @@ func (a rpmPkgAnalyzer) listPkgs(db RPMDB) (types.Packages, []string, error) {
DependsOn: pkg.Requires, // Will be replaced with package IDs
Maintainer: pkg.Vendor,
Digest: d,
InstalledFiles: files,
}
pkgs = append(pkgs, p)
installedFiles = append(installedFiles, files...)
Expand Down
5 changes: 5 additions & 0 deletions pkg/fanal/analyzer/pkg/rpm/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ func Test_rpmPkgAnalyzer_listPkgs(t *testing.T) {
SrcVersion: "2.17",
SrcRelease: "317.el7",
Maintainer: "Red Hat",
InstalledFiles: []string{
"/etc/ld.so.conf",
"/etc/rpc",
"/lib64/libm-2.27.so",
},
},
},
wantFiles: []string{
Expand Down

0 comments on commit 44d0b28

Please sign in to comment.