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-01-13 23:49:02 +00:00
|
|
|
featurefmt.RegisterLister("apk", &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-01-13 23:49:02 +00:00
|
|
|
func (l lister) ListFeatures(files tarutil.FilesMap) ([]database.FeatureVersion, error) {
|
|
|
|
file, exists := files["lib/apk/db/installed"]
|
2016-11-10 20:05:47 +00:00
|
|
|
if !exists {
|
|
|
|
return []database.FeatureVersion{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
pkgSet := make(map[string]database.FeatureVersion)
|
|
|
|
ipkg := database.FeatureVersion{}
|
|
|
|
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:":
|
|
|
|
ipkg.Feature.Name = line[2:]
|
|
|
|
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.
|
|
|
|
ipkg = database.FeatureVersion{}
|
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.
|
2016-12-28 01:45:11 +00:00
|
|
|
if ipkg.Feature.Name != "" && ipkg.Version != "" {
|
|
|
|
pkgSet[ipkg.Feature.Name+"#"+ipkg.Version] = ipkg
|
2016-11-10 20:05:47 +00:00
|
|
|
ipkg = database.FeatureVersion{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the map into a slice.
|
|
|
|
pkgs := make([]database.FeatureVersion, 0, len(pkgSet))
|
|
|
|
for _, pkg := range pkgSet {
|
|
|
|
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"}
|
|
|
|
}
|