2017-01-13 23:49:02 +00:00
|
|
|
// Copyright 2017 clair authors
|
2016-11-10 20:05:47 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
// Package apk implements a featurefmt.Lister for APK packages.
|
2016-11-10 20:05:47 +00:00
|
|
|
package apk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-12-28 01:45:11 +00:00
|
|
|
|
2016-11-10 20:05:47 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2017-01-13 23:49:02 +00:00
|
|
|
"github.com/coreos/clair/ext/featurefmt"
|
2016-12-28 01:45:11 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt"
|
2017-01-03 21:00:20 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
2017-01-13 23:49:02 +00:00
|
|
|
"github.com/coreos/clair/pkg/tarutil"
|
2016-11-10 20:05:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-06-22 18:01:41 +00:00
|
|
|
featurefmt.RegisterLister("apk", dpkg.ParserName, &lister{})
|
2016-11-10 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
type lister struct{}
|
2016-11-10 20:05:47 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
func (l lister) ListFeatures(files tarutil.FilesMap) ([]database.Feature, error) {
|
2017-01-13 23:49:02 +00:00
|
|
|
file, exists := files["lib/apk/db/installed"]
|
2016-11-10 20:05:47 +00:00
|
|
|
if !exists {
|
2017-07-26 23:22:29 +00:00
|
|
|
return []database.Feature{}, nil
|
2016-11-10 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over each line in the "installed" file attempting to parse each
|
|
|
|
// package into a feature that will be stored in a set to guarantee
|
|
|
|
// uniqueness.
|
2017-07-26 23:22:29 +00:00
|
|
|
pkgSet := make(map[string]database.Feature)
|
|
|
|
ipkg := database.Feature{}
|
2016-11-10 20:05:47 +00:00
|
|
|
scanner := bufio.NewScanner(bytes.NewBuffer(file))
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if len(line) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the package name or version.
|
|
|
|
switch {
|
|
|
|
case line[:2] == "P:":
|
2017-07-26 23:22:29 +00:00
|
|
|
ipkg.Name = line[2:]
|
2016-11-10 20:05:47 +00:00
|
|
|
case line[:2] == "V:":
|
2016-12-28 01:45:11 +00:00
|
|
|
version := string(line[2:])
|
2017-01-03 21:00:20 +00:00
|
|
|
err := versionfmt.Valid(dpkg.ParserName, version)
|
2016-11-10 20:05:47 +00:00
|
|
|
if err != nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithError(err).WithField("version", version).Warning("could not parse package version. skipping")
|
2016-12-28 01:45:11 +00:00
|
|
|
} else {
|
|
|
|
ipkg.Version = version
|
2016-11-10 20:05:47 +00:00
|
|
|
}
|
2017-04-11 19:48:11 +00:00
|
|
|
case line == "":
|
|
|
|
// Restart if the parser reaches another package definition before
|
|
|
|
// creating a valid package.
|
2017-07-26 23:22:29 +00:00
|
|
|
ipkg = database.Feature{}
|
2016-11-10 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a whole feature, store it in the set and try to parse a new
|
|
|
|
// one.
|
2017-07-26 23:22:29 +00:00
|
|
|
if ipkg.Name != "" && ipkg.Version != "" {
|
|
|
|
pkgSet[ipkg.Name+"#"+ipkg.Version] = ipkg
|
|
|
|
ipkg = database.Feature{}
|
2016-11-10 20:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
// Convert the map into a slice and attach the version format
|
|
|
|
pkgs := make([]database.Feature, 0, len(pkgSet))
|
2016-11-10 20:05:47 +00:00
|
|
|
for _, pkg := range pkgSet {
|
2017-07-26 23:22:29 +00:00
|
|
|
pkg.VersionFormat = dpkg.ParserName
|
2016-11-10 20:05:47 +00:00
|
|
|
pkgs = append(pkgs, pkg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkgs, nil
|
|
|
|
}
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
func (l lister) RequiredFilenames() []string {
|
2016-11-10 20:05:47 +00:00
|
|
|
return []string{"lib/apk/db/installed"}
|
|
|
|
}
|