rename location.VirtualPath to AccessPath

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2022-03-22 18:24:50 -04:00
parent f999604a29
commit a3dc0fa97d
No known key found for this signature in database
GPG Key ID: 5CB45AE22BAB7EA7
24 changed files with 84 additions and 84 deletions

View File

@ -191,7 +191,7 @@ func toSyftCoordinates(f *spdx.File2_2) file.Coordinates {
func toSyftLocation(f *spdx.File2_2) *file.Location {
return &file.Location{
Coordinates: toSyftCoordinates(f),
VirtualPath: f.FileName,
AccessPath: f.FileName,
}
}

View File

@ -80,7 +80,7 @@ func (c Classifier) Classify(resolver source.FileResolver, location file.Locatio
if err != nil {
return nil, err
}
defer internal.CloseAndLogError(contentReader, location.VirtualPath)
defer internal.CloseAndLogError(contentReader, location.AccessPath)
// TODO: there is room for improvement here, as this may use an excessive amount of memory. Alternate approach is to leverage a RuneReader.
contents, err := ioutil.ReadAll(contentReader)
@ -126,7 +126,7 @@ func (c Classifier) Classify(resolver source.FileResolver, location file.Locatio
}
func filepathMatches(patterns []*regexp.Regexp, location file.Location) (bool, map[string]string) {
for _, path := range []string{location.RealPath, location.VirtualPath} {
for _, path := range []string{location.RealPath, location.AccessPath} {
if path == "" {
continue
}

View File

@ -43,7 +43,7 @@ func TestFilepathMatches(t *testing.T) {
{
name: "virtual-filepath-match",
location: file.Location{
VirtualPath: "/usr/bin/python2.7",
AccessPath: "/usr/bin/python2.7",
},
patterns: []string{
`python([0-9]+\.[0-9]+)$`,
@ -53,7 +53,7 @@ func TestFilepathMatches(t *testing.T) {
{
name: "full-filepath-match",
location: file.Location{
VirtualPath: "/usr/bin/python2.7",
AccessPath: "/usr/bin/python2.7",
},
patterns: []string{
`.*/bin/python([0-9]+\.[0-9]+)$`,

View File

@ -72,7 +72,7 @@ func (i *Cataloger) catalogLocation(resolver source.FileResolver, location file.
if err != nil {
return "", err
}
defer internal.CloseAndLogError(contentReader, location.VirtualPath)
defer internal.CloseAndLogError(contentReader, location.AccessPath)
buf := &bytes.Buffer{}
encoder := base64.NewEncoder(base64.StdEncoding, buf)

View File

@ -72,7 +72,7 @@ func (i *Cataloger) catalogLocation(resolver source.FileResolver, location file.
if err != nil {
return nil, err
}
defer internal.CloseAndLogError(contentReader, location.VirtualPath)
defer internal.CloseAndLogError(contentReader, location.AccessPath)
// create a set of hasher objects tied together with a single writer to feed content into
hashers := make([]hash.Hash, len(i.hashes))

View File

@ -125,7 +125,7 @@ func extractValue(resolver source.FileResolver, location file.Location, start, l
if err != nil {
return "", fmt.Errorf("unable to fetch reader for location=%q : %w", location, err)
}
defer internal.CloseAndLogError(readCloser, location.VirtualPath)
defer internal.CloseAndLogError(readCloser, location.AccessPath)
n, err := io.CopyN(ioutil.Discard, readCloser, start)
if err != nil {

View File

@ -20,7 +20,7 @@ func catalogLocationByLine(resolver source.FileResolver, location file.Location,
if err != nil {
return nil, fmt.Errorf("unable to fetch reader for location=%q : %w", location, err)
}
defer internal.CloseAndLogError(readCloser, location.VirtualPath)
defer internal.CloseAndLogError(readCloser, location.AccessPath)
var scanner = bufio.NewReader(readCloser)
var position int64
@ -69,7 +69,7 @@ func searchForSecretsWithinLine(resolver source.FileResolver, location file.Loca
if secret != nil {
secrets = append(secrets, *secret)
}
internal.CloseAndLogError(reader, location.VirtualPath)
internal.CloseAndLogError(reader, location.AccessPath)
}
}

View File

@ -14,8 +14,8 @@ type Location struct {
Coordinates `cyclonedx:""` // Empty string here means there is no intermediate property name, e.g. syft:locations:0:path without "coordinates"
// note: it is IMPORTANT to ignore anything but the coordinates for a Location when considering the ID (hash value)
// since the coordinates are the minimally correct ID for a location (symlinks should not come into play)
VirtualPath string `hash:"ignore"` // The path to the file which may or may not have hardlinks / symlinks
ref file.Reference `hash:"ignore"` // The file reference relative to the stereoscope.FileCatalog that has more information about this location.
AccessPath string `hash:"ignore"` // The path to the file which may or may not have hardlinks / symlinks
ref file.Reference `hash:"ignore"` // The file reference relative to the stereoscope.FileCatalog that has more information about this location.
}
// NewLocation creates a new Location representing a path without denoting a filesystem or FileCatalog reference.
@ -28,12 +28,12 @@ func NewLocation(realPath string) Location {
}
// NewVirtualLocation creates a new location for a path accessed by a virtual path (a path with a symlink or hardlink somewhere in the path)
func NewVirtualLocation(realPath, virtualPath string) Location {
func NewVirtualLocation(realPath, accessPath string) Location {
return Location{
Coordinates: Coordinates{
RealPath: realPath,
},
VirtualPath: virtualPath,
AccessPath: accessPath,
}
}
@ -45,7 +45,7 @@ func NewLocationFromCoordinates(coordinates Coordinates) Location {
}
// NewLocationFromImage creates a new Location representing the given path (extracted from the ref) relative to the given image.
func NewLocationFromImage(virtualPath string, ref file.Reference, img *image.Image) Location {
func NewLocationFromImage(accessPath string, ref file.Reference, img *image.Image) Location {
entry, err := img.FileCatalog.Get(ref)
if err != nil {
log.Warnf("unable to find file catalog entry for ref=%+v", ref)
@ -53,8 +53,8 @@ func NewLocationFromImage(virtualPath string, ref file.Reference, img *image.Ima
Coordinates: Coordinates{
RealPath: string(ref.RealPath),
},
VirtualPath: virtualPath,
ref: ref,
AccessPath: accessPath,
ref: ref,
}
}
@ -63,8 +63,8 @@ func NewLocationFromImage(virtualPath string, ref file.Reference, img *image.Ima
RealPath: string(ref.RealPath),
FileSystemID: entry.Layer.Metadata.Digest,
},
VirtualPath: virtualPath,
ref: ref,
AccessPath: accessPath,
ref: ref,
}
}
@ -87,8 +87,8 @@ func NewVirtualLocationFromDirectory(responsePath, virtualResponsePath string, r
Coordinates: Coordinates{
RealPath: responsePath,
},
VirtualPath: virtualResponsePath,
ref: ref,
AccessPath: virtualResponsePath,
ref: ref,
}
}
@ -100,8 +100,8 @@ func (l Location) String() string {
str += fmt.Sprintf("RealPath=%q", l.RealPath)
if l.VirtualPath != "" {
str += fmt.Sprintf(" VirtualPath=%q", l.VirtualPath)
if l.AccessPath != "" {
str += fmt.Sprintf(" AccessPath=%q", l.AccessPath)
}
if l.FileSystemID != "" {

View File

@ -11,7 +11,7 @@ func TestLocation_ID(t *testing.T) {
tests := []struct {
name string
coordinates Coordinates
virtualPath string
accessPath string
ref file.Reference
}{
{
@ -27,7 +27,7 @@ func TestLocation_ID(t *testing.T) {
RealPath: "path!",
FileSystemID: "filesystem!",
},
virtualPath: "virtualPath!",
accessPath: "accessPath!",
ref: file.Reference{
RealPath: "other-real-path!",
},
@ -37,7 +37,7 @@ func TestLocation_ID(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
l := Location{
Coordinates: test.coordinates,
VirtualPath: test.virtualPath,
AccessPath: test.accessPath,
ref: test.ref,
}
assert.Equal(t, l.ID(), test.coordinates.ID())

View File

@ -69,7 +69,7 @@ func IdentifyRelease(resolver source.FileResolver) *Release {
}
content, err := ioutil.ReadAll(contentReader)
internal.CloseAndLogError(contentReader, location.VirtualPath)
internal.CloseAndLogError(contentReader, location.AccessPath)
if err != nil {
log.Warnf("unable to read %q: %+v", location.RealPath, err)
break

View File

@ -94,9 +94,9 @@ func (c *Catalog) Add(p Package) {
c.idsByPath[l.RealPath] = append(c.idsByPath[l.RealPath], id)
observedPaths.Add(l.RealPath)
}
if l.VirtualPath != "" && l.RealPath != l.VirtualPath && !observedPaths.Contains(l.VirtualPath) {
c.idsByPath[l.VirtualPath] = append(c.idsByPath[l.VirtualPath], id)
observedPaths.Add(l.VirtualPath)
if l.AccessPath != "" && l.RealPath != l.AccessPath && !observedPaths.Contains(l.AccessPath) {
c.idsByPath[l.AccessPath] = append(c.idsByPath[l.AccessPath], id)
observedPaths.Add(l.AccessPath)
}
}
}

View File

@ -53,7 +53,7 @@ func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []arti
}
pkgs, err := parseDpkgStatus(dbContents)
internal.CloseAndLogError(dbContents, dbLocation.VirtualPath)
internal.CloseAndLogError(dbContents, dbLocation.AccessPath)
if err != nil {
log.Warnf("dpkg cataloger: unable to catalog package=%+v: %w", dbLocation.RealPath, err)
continue
@ -85,7 +85,7 @@ func addLicenses(resolver source.FileResolver, dbLocation file.Location, p *pkg.
copyrightReader, copyrightLocation := fetchCopyrightContents(resolver, dbLocation, p)
if copyrightReader != nil && copyrightLocation != nil {
defer internal.CloseAndLogError(copyrightReader, copyrightLocation.VirtualPath)
defer internal.CloseAndLogError(copyrightReader, copyrightLocation.AccessPath)
// attach the licenses
p.Licenses = parseLicensesFromCopyright(copyrightReader)
@ -130,7 +130,7 @@ func getAdditionalFileListing(resolver source.FileResolver, dbLocation file.Loca
md5Reader, md5Location := fetchMd5Contents(resolver, dbLocation, p)
if md5Reader != nil && md5Location != nil {
defer internal.CloseAndLogError(md5Reader, md5Location.VirtualPath)
defer internal.CloseAndLogError(md5Reader, md5Location.AccessPath)
// attach the file list
files = append(files, parseDpkgMD5Info(md5Reader)...)
@ -141,7 +141,7 @@ func getAdditionalFileListing(resolver source.FileResolver, dbLocation file.Loca
conffilesReader, conffilesLocation := fetchConffileContents(resolver, dbLocation, p)
if conffilesReader != nil && conffilesLocation != nil {
defer internal.CloseAndLogError(conffilesReader, conffilesLocation.VirtualPath)
defer internal.CloseAndLogError(conffilesReader, conffilesLocation.AccessPath)
// attach the file list
files = append(files, parseDpkgConffileInfo(conffilesReader)...)

View File

@ -51,7 +51,7 @@ func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []arti
}
discoveredPackages, discoveredRelationships, err := parser(location.RealPath, contentReader)
internal.CloseAndLogError(contentReader, location.VirtualPath)
internal.CloseAndLogError(contentReader, location.AccessPath)
if err != nil {
// TODO: should we fail? or only log?
log.Warnf("cataloger '%s' failed to parse entries at location=%+v: %+v", c.upstreamCataloger, location, err)

View File

@ -142,7 +142,7 @@ func (c *PackageCataloger) fetchTopLevelPackages(resolver source.FileResolver, m
if err != nil {
return nil, nil, err
}
defer internal.CloseAndLogError(topLevelContents, topLevelLocation.VirtualPath)
defer internal.CloseAndLogError(topLevelContents, topLevelLocation.AccessPath)
scanner := bufio.NewScanner(topLevelContents)
for scanner.Scan() {
@ -171,7 +171,7 @@ func (c *PackageCataloger) fetchDirectURLData(resolver source.FileResolver, meta
if err != nil {
return nil, nil, err
}
defer internal.CloseAndLogError(directURLContents, directURLLocation.VirtualPath)
defer internal.CloseAndLogError(directURLContents, directURLLocation.AccessPath)
buffer, err := ioutil.ReadAll(directURLContents)
if err != nil {
@ -198,7 +198,7 @@ func (c *PackageCataloger) assembleEggOrWheelMetadata(resolver source.FileResolv
if err != nil {
return nil, nil, err
}
defer internal.CloseAndLogError(metadataContents, metadataLocation.VirtualPath)
defer internal.CloseAndLogError(metadataContents, metadataLocation.AccessPath)
metadata, err := parseWheelOrEggMetadata(metadataLocation.RealPath, metadataContents)
if err != nil {

View File

@ -42,7 +42,7 @@ func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []arti
}
discoveredPkgs, err := parseRpmDB(resolver, location, dbContentReader)
internal.CloseAndLogError(dbContentReader, location.VirtualPath)
internal.CloseAndLogError(dbContentReader, location.AccessPath)
if err != nil {
return nil, nil, fmt.Errorf("unable to catalog rpmdb package=%+v: %w", location.RealPath, err)
}

View File

@ -18,7 +18,7 @@ func TestFingerprint(t *testing.T) {
RealPath: "39.0742° N, 21.8243° E",
FileSystemID: "Earth",
},
VirtualPath: "/Ancient-Greece",
AccessPath: "/Ancient-Greece",
},
},
Licenses: []string{

View File

@ -193,10 +193,10 @@ func (r *allLayersResolver) FileContentsByLocation(location file.Location) (io.R
switch entry.Metadata.TypeFlag {
case tar.TypeSymlink, tar.TypeLink:
// the location we are searching may be a symlink, we should always work with the resolved file
newLocation := r.RelativeFileByPath(location, location.VirtualPath)
newLocation := r.RelativeFileByPath(location, location.AccessPath)
if newLocation == nil {
// this is a dead link
return nil, fmt.Errorf("no contents for location=%q", location.VirtualPath)
return nil, fmt.Errorf("no contents for location=%q", location.AccessPath)
}
location = *newLocation
}

View File

@ -381,59 +381,59 @@ func Test_imageAllLayersResolver_resolvesLinks(t *testing.T) {
Coordinates: file.Coordinates{
RealPath: "/etc/group",
},
VirtualPath: "/etc/group",
AccessPath: "/etc/group",
},
{
Coordinates: file.Coordinates{
RealPath: "/etc/passwd",
},
VirtualPath: "/etc/passwd",
AccessPath: "/etc/passwd",
},
{
Coordinates: file.Coordinates{
RealPath: "/etc/shadow",
},
VirtualPath: "/etc/shadow",
AccessPath: "/etc/shadow",
},
{
Coordinates: file.Coordinates{
RealPath: "/file-1.txt",
},
VirtualPath: "/file-1.txt",
AccessPath: "/file-1.txt",
},
// copy 1
{
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/file-2.txt",
AccessPath: "/file-2.txt",
},
{
Coordinates: file.Coordinates{
RealPath: "/file-3.txt",
},
VirtualPath: "/file-3.txt",
AccessPath: "/file-3.txt",
},
// copy 2
{
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/file-2.txt",
AccessPath: "/file-2.txt",
},
// copy 1
{
Coordinates: file.Coordinates{
RealPath: "/parent/file-4.txt",
},
VirtualPath: "/parent/file-4.txt",
AccessPath: "/parent/file-4.txt",
},
// copy 2
{
Coordinates: file.Coordinates{
RealPath: "/parent/file-4.txt",
},
VirtualPath: "/parent/file-4.txt",
AccessPath: "/parent/file-4.txt",
},
},
},
@ -450,27 +450,27 @@ func Test_imageAllLayersResolver_resolvesLinks(t *testing.T) {
Coordinates: file.Coordinates{
RealPath: "/file-1.txt",
},
VirtualPath: "/link-1",
AccessPath: "/link-1",
},
// copy 1
{
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/link-2",
AccessPath: "/link-2",
},
// copy 2
{
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/link-2",
AccessPath: "/link-2",
},
{
Coordinates: file.Coordinates{
RealPath: "/file-3.txt",
},
VirtualPath: "/link-within",
AccessPath: "/link-within",
},
},
},
@ -488,13 +488,13 @@ func Test_imageAllLayersResolver_resolvesLinks(t *testing.T) {
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/link-2",
AccessPath: "/link-2",
},
{
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/link-2",
AccessPath: "/link-2",
},
},
},
@ -512,13 +512,13 @@ func Test_imageAllLayersResolver_resolvesLinks(t *testing.T) {
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/link-indirect",
AccessPath: "/link-indirect",
},
{
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/link-indirect",
AccessPath: "/link-indirect",
},
},
},
@ -536,7 +536,7 @@ func Test_imageAllLayersResolver_resolvesLinks(t *testing.T) {
assert.Len(t, actualLocations, len(test.expected))
for i, actual := range actualLocations {
assert.Equal(t, test.expected[i].RealPath, actual.RealPath)
assert.Equal(t, test.expected[i].VirtualPath, actual.VirtualPath)
assert.Equal(t, test.expected[i].AccessPath, actual.AccessPath)
}
})
}

View File

@ -10,8 +10,8 @@ import (
func Test_allRegularFiles(t *testing.T) {
type access struct {
realPath string
virtualPath string
realPath string
accessPath string
}
tests := []struct {
name string
@ -56,8 +56,8 @@ func Test_allRegularFiles(t *testing.T) {
virtualLocations := strset.New()
for _, l := range locations {
realLocations.Add(l.RealPath)
if l.VirtualPath != "" {
virtualLocations.Add(l.VirtualPath)
if l.AccessPath != "" {
virtualLocations.Add(l.AccessPath)
}
}
assert.ElementsMatch(t, tt.wantRealPaths.List(), realLocations.List(), "mismatched real paths")

View File

@ -261,7 +261,7 @@ func (r directoryResolver) addSymlinkToIndex(p string, info os.FileInfo) (string
}
location := file.NewLocationFromDirectory(p, *ref)
location.VirtualPath = p
location.AccessPath = p
metadata := file.MetadataFromPath(p, usedInfo, r.isInIndex(location))
metadata.LinkDestination = linkTarget
r.addFileMetadataToIndex(ref, metadata)

View File

@ -634,14 +634,14 @@ func Test_IndexingNestedSymLinks(t *testing.T) {
require.NoError(t, err)
require.Len(t, locations, 1)
assert.Equal(t, "readme", locations[0].RealPath)
assert.Equal(t, "link_to_new_readme", locations[0].VirtualPath)
assert.Equal(t, "link_to_new_readme", locations[0].AccessPath)
// check that we can access the same file via 2 symlinks
locations, err = resolver.FilesByPath("./link_to_link_to_new_readme")
require.NoError(t, err)
require.Len(t, locations, 1)
assert.Equal(t, "readme", locations[0].RealPath)
assert.Equal(t, "link_to_link_to_new_readme", locations[0].VirtualPath)
assert.Equal(t, "link_to_link_to_new_readme", locations[0].AccessPath)
// check that we can access the same file via 2 symlinks
locations, err = resolver.FilesByGlob("**/link_*")
@ -661,7 +661,7 @@ func Test_IndexingNestedSymLinks(t *testing.T) {
actualRealPaths := strset.New()
actualVirtualPaths := strset.New()
for _, a := range locations {
actualVirtualPaths.Add(a.VirtualPath)
actualVirtualPaths.Add(a.AccessPath)
actualRealPaths.Add(a.RealPath)
}

View File

@ -83,7 +83,7 @@ func (r *excludingResolver) AllLocations() <-chan file.Location {
}
func locationMatches(location *file.Location, exclusionFn excludeFn) bool {
return exclusionFn(location.RealPath) || exclusionFn(location.VirtualPath)
return exclusionFn(location.RealPath) || exclusionFn(location.AccessPath)
}
func filterLocations(locations []file.Location, err error, exclusionFn excludeFn) ([]file.Location, error) {

View File

@ -124,7 +124,7 @@ func makeLocation(path string) file.Location {
RealPath: path,
FileSystemID: "",
},
VirtualPath: "",
AccessPath: "",
}
}

View File

@ -364,43 +364,43 @@ func Test_imageSquashResolver_resolvesLinks(t *testing.T) {
Coordinates: file.Coordinates{
RealPath: "/etc/group",
},
VirtualPath: "/etc/group",
AccessPath: "/etc/group",
},
{
Coordinates: file.Coordinates{
RealPath: "/etc/passwd",
},
VirtualPath: "/etc/passwd",
AccessPath: "/etc/passwd",
},
{
Coordinates: file.Coordinates{
RealPath: "/etc/shadow",
},
VirtualPath: "/etc/shadow",
AccessPath: "/etc/shadow",
},
{
Coordinates: file.Coordinates{
RealPath: "/file-1.txt",
},
VirtualPath: "/file-1.txt",
AccessPath: "/file-1.txt",
},
{
Coordinates: file.Coordinates{
RealPath: "/file-3.txt",
},
VirtualPath: "/file-3.txt",
AccessPath: "/file-3.txt",
},
{
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/file-2.txt",
AccessPath: "/file-2.txt",
},
{
Coordinates: file.Coordinates{
RealPath: "/parent/file-4.txt",
},
VirtualPath: "/parent/file-4.txt",
AccessPath: "/parent/file-4.txt",
},
},
},
@ -417,19 +417,19 @@ func Test_imageSquashResolver_resolvesLinks(t *testing.T) {
Coordinates: file.Coordinates{
RealPath: "/file-3.txt",
},
VirtualPath: "/link-within",
AccessPath: "/link-within",
},
{
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/link-2",
AccessPath: "/link-2",
},
{
Coordinates: file.Coordinates{
RealPath: "/file-1.txt",
},
VirtualPath: "/link-1",
AccessPath: "/link-1",
},
},
},
@ -447,7 +447,7 @@ func Test_imageSquashResolver_resolvesLinks(t *testing.T) {
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/link-2",
AccessPath: "/link-2",
},
},
},
@ -465,7 +465,7 @@ func Test_imageSquashResolver_resolvesLinks(t *testing.T) {
Coordinates: file.Coordinates{
RealPath: "/file-2.txt",
},
VirtualPath: "/link-indirect",
AccessPath: "/link-indirect",
},
},
},
@ -486,12 +486,12 @@ func Test_imageSquashResolver_resolvesLinks(t *testing.T) {
expectedMap := make(map[string]string)
for _, e := range test.expected {
expectedMap[e.VirtualPath] = e.RealPath
expectedMap[e.AccessPath] = e.RealPath
}
actualMap := make(map[string]string)
for _, a := range test.expected {
actualMap[a.VirtualPath] = a.RealPath
actualMap[a.AccessPath] = a.RealPath
}
assert.Equal(t, expectedMap, actualMap)