Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename file.Location.VirtualPath to AccessPath #2288

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion syft/file/cataloger/filecontent/cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (i *Cataloger) catalogLocation(resolver file.Resolver, location file.Locati
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)
Expand Down
2 changes: 1 addition & 1 deletion syft/file/cataloger/filedigest/cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (i *Cataloger) catalogLocation(resolver file.Resolver, location file.Locati
if err != nil {
return nil, err
}
defer internal.CloseAndLogError(contentReader, location.VirtualPath)
defer internal.CloseAndLogError(contentReader, location.AccessPath)

digests, err := intFile.NewDigestsFromFile(contentReader, i.hashes)
if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions syft/file/cataloger/internal/all_regular_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (

func Test_allRegularFiles(t *testing.T) {
tests := []struct {
name string
setup func() file.Resolver
wantRealPaths *strset.Set
wantVirtualPaths *strset.Set
name string
setup func() file.Resolver
wantRealPaths *strset.Set
wantAccessPaths *strset.Set
}{
{
name: "image",
Expand All @@ -35,8 +35,8 @@ func Test_allRegularFiles(t *testing.T) {

return r
},
wantRealPaths: strset.New("/file-1.txt"),
wantVirtualPaths: strset.New("/file-1.txt", "/symlink-1", "/hardlink-1"),
wantRealPaths: strset.New("/file-1.txt"),
wantAccessPaths: strset.New("/file-1.txt", "/symlink-1", "/hardlink-1"),
},
{
name: "directory",
Expand All @@ -47,8 +47,8 @@ func Test_allRegularFiles(t *testing.T) {
require.NoError(t, err)
return r
},
wantRealPaths: strset.New("file1.txt", "nested/file2.txt"),
wantVirtualPaths: strset.New("file1.txt", "nested/file2.txt", "nested/linked-file1.txt"),
wantRealPaths: strset.New("file1.txt", "nested/file2.txt"),
wantAccessPaths: strset.New("file1.txt", "nested/file2.txt", "nested/linked-file1.txt"),
},
}
for _, tt := range tests {
Expand All @@ -59,19 +59,19 @@ 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)
}
}

// this is difficult to reproduce in a cross-platform way
realLocations.Remove("/hardlink-1")
virtualLocations.Remove("/hardlink-1")
tt.wantRealPaths.Remove("/hardlink-1")
tt.wantVirtualPaths.Remove("/hardlink-1")
tt.wantAccessPaths.Remove("/hardlink-1")

assert.ElementsMatch(t, tt.wantRealPaths.List(), realLocations.List(), "real paths differ: "+cmp.Diff(tt.wantRealPaths.List(), realLocations.List()))
assert.ElementsMatch(t, tt.wantVirtualPaths.List(), virtualLocations.List(), "virtual paths differ: "+cmp.Diff(tt.wantVirtualPaths.List(), virtualLocations.List()))
assert.ElementsMatch(t, tt.wantAccessPaths.List(), virtualLocations.List(), "virtual paths differ: "+cmp.Diff(tt.wantAccessPaths.List(), virtualLocations.List()))
})
}
}
2 changes: 1 addition & 1 deletion syft/file/cataloger/secrets/cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func extractValue(resolver file.Resolver, location file.Location, start, length
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(io.Discard, readCloser, start)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func catalogLocationByLine(resolver file.Resolver, location file.Location, patte
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
Expand Down Expand Up @@ -65,7 +65,7 @@ func searchForSecretsWithinLine(resolver file.Resolver, location file.Location,
if secret != nil {
secrets = append(secrets, *secret)
}
internal.CloseAndLogError(reader, location.VirtualPath)
internal.CloseAndLogError(reader, location.AccessPath)
}
}

Expand Down
46 changes: 23 additions & 23 deletions syft/file/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// Location represents a path relative to a particular filesystem resolved to a specific file.Reference. This struct is used as a key
// in content fetching to uniquely identify a file relative to a request (the VirtualPath).
// in content fetching to uniquely identify a file relative to a request (the AccessPath).
type Location struct {
LocationData `cyclonedx:""`
LocationMetadata `cyclonedx:""`
Expand All @@ -20,8 +20,8 @@ type LocationData 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" json:"accessPath"` // 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" json:"accessPath"` // 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.
}

func (l LocationData) Reference() file.Reference {
Expand Down Expand Up @@ -68,7 +68,7 @@ func NewLocation(realPath string) Location {
Coordinates: Coordinates{
RealPath: realPath,
},
VirtualPath: realPath,
AccessPath: realPath,
},
LocationMetadata: LocationMetadata{
Annotations: map[string]string{},
Expand All @@ -77,13 +77,13 @@ 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{
LocationData: LocationData{
Coordinates: Coordinates{
RealPath: realPath,
},
VirtualPath: virtualPath,
AccessPath: accessPath,
},
LocationMetadata: LocationMetadata{
Annotations: map[string]string{},
Expand All @@ -95,36 +95,36 @@ func NewLocationFromCoordinates(coordinates Coordinates) Location {
return Location{
LocationData: LocationData{
Coordinates: coordinates,
VirtualPath: coordinates.RealPath,
AccessPath: coordinates.RealPath,
},
LocationMetadata: LocationMetadata{
Annotations: map[string]string{},
}}
}

// NewVirtualLocationFromCoordinates creates a new location for the given Coordinates via a virtual path.
func NewVirtualLocationFromCoordinates(coordinates Coordinates, virtualPath string) Location {
func NewVirtualLocationFromCoordinates(coordinates Coordinates, accessPath string) Location {
return Location{
LocationData: LocationData{
Coordinates: coordinates,
VirtualPath: virtualPath,
AccessPath: accessPath,
},
LocationMetadata: LocationMetadata{
Annotations: map[string]string{},
}}
}

// NewLocationFromImage creates a new Location representing the given path (extracted from the Reference) 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 {
layer := img.FileCatalog.Layer(ref)
return Location{
LocationData: LocationData{
Coordinates: Coordinates{
RealPath: string(ref.RealPath),
FileSystemID: layer.Metadata.Digest,
},
VirtualPath: virtualPath,
ref: ref,
AccessPath: accessPath,
ref: ref,
},
LocationMetadata: LocationMetadata{
Annotations: map[string]string{},
Expand All @@ -139,8 +139,8 @@ func NewLocationFromDirectory(responsePath string, ref file.Reference) Location
Coordinates: Coordinates{
RealPath: responsePath,
},
VirtualPath: responsePath,
ref: ref,
AccessPath: responsePath,
ref: ref,
},
LocationMetadata: LocationMetadata{
Annotations: map[string]string{},
Expand All @@ -149,24 +149,24 @@ func NewLocationFromDirectory(responsePath string, ref file.Reference) Location
}

// NewVirtualLocationFromDirectory creates a new Location representing the given path (extracted from the Reference) relative to the given directory with a separate virtual access path.
func NewVirtualLocationFromDirectory(responsePath, virtualResponsePath string, ref file.Reference) Location {
func NewVirtualLocationFromDirectory(responsePath, responseAccessPath string, ref file.Reference) Location {
return Location{
LocationData: LocationData{
Coordinates: Coordinates{
RealPath: responsePath,
},
VirtualPath: virtualResponsePath,
ref: ref,
AccessPath: responseAccessPath,
ref: ref,
},
LocationMetadata: LocationMetadata{
Annotations: map[string]string{},
},
}
}

func (l Location) AccessPath() string {
if l.VirtualPath != "" {
return l.VirtualPath
func (l Location) Path() string {
if l.AccessPath != "" {
return l.AccessPath
}
return l.RealPath
}
Expand All @@ -179,8 +179,8 @@ func (l Location) String() string {

str += fmt.Sprintf("RealPath=%q", l.RealPath)

if l.VirtualPath != "" && l.VirtualPath != l.RealPath {
str += fmt.Sprintf(" VirtualPath=%q", l.VirtualPath)
if l.AccessPath != "" && l.AccessPath != l.RealPath {
str += fmt.Sprintf(" AccessPath=%q", l.AccessPath)
}

if l.FileSystemID != "" {
Expand All @@ -191,6 +191,6 @@ func (l Location) String() string {

func (l Location) Equals(other Location) bool {
return l.RealPath == other.RealPath &&
l.VirtualPath == other.VirtualPath &&
l.AccessPath == other.AccessPath &&
l.FileSystemID == other.FileSystemID
}
14 changes: 7 additions & 7 deletions syft/file/location_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestLocationSet(t *testing.T) {
RealPath: "/etc/hosts",
FileSystemID: "a",
},
VirtualPath: "/var/etc/hosts",
AccessPath: "/var/etc/hosts",
},
}

Expand All @@ -27,7 +27,7 @@ func TestLocationSet(t *testing.T) {
RealPath: "/etc/hosts",
FileSystemID: "a",
},
VirtualPath: "/home/wagoodman/hosts",
AccessPath: "/home/wagoodman/hosts",
},
}

Expand All @@ -37,7 +37,7 @@ func TestLocationSet(t *testing.T) {
RealPath: "/bin",
FileSystemID: "a",
},
VirtualPath: "/usr/bin",
AccessPath: "/usr/bin",
},
}

Expand All @@ -47,7 +47,7 @@ func TestLocationSet(t *testing.T) {
RealPath: "/bin",
FileSystemID: "b",
},
VirtualPath: "/usr/bin",
AccessPath: "/usr/bin",
},
}

Expand Down Expand Up @@ -100,7 +100,7 @@ func TestLocationSet_Hash(t *testing.T) {
RealPath: "/etc/hosts",
FileSystemID: "a",
},
VirtualPath: "/var/etc/hosts",
AccessPath: "/var/etc/hosts",
},
}

Expand Down Expand Up @@ -128,7 +128,7 @@ func TestLocationSet_Hash(t *testing.T) {
RealPath: "/bin",
FileSystemID: "a",
},
VirtualPath: "/usr/bin",
AccessPath: "/usr/bin",
},
}

Expand All @@ -138,7 +138,7 @@ func TestLocationSet_Hash(t *testing.T) {
RealPath: "/bin",
FileSystemID: "b",
},
VirtualPath: "/usr/bin",
AccessPath: "/usr/bin",
},
}

Expand Down
2 changes: 1 addition & 1 deletion syft/file/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestLocation_ID(t *testing.T) {
l := Location{
LocationData: LocationData{
Coordinates: test.coordinates,
VirtualPath: test.virtualPath,
AccessPath: test.virtualPath,
ref: test.ref,
},
}
Expand Down
4 changes: 2 additions & 2 deletions syft/file/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ func (l Locations) Len() int {

func (l Locations) Less(i, j int) bool {
if l[i].RealPath == l[j].RealPath {
if l[i].VirtualPath == l[j].VirtualPath {
if l[i].AccessPath == l[j].AccessPath {
return l[i].FileSystemID < l[j].FileSystemID
}
return l[i].VirtualPath < l[j].VirtualPath
return l[i].AccessPath < l[j].AccessPath
}
return l[i].RealPath < l[j].RealPath
}
Expand Down
2 changes: 1 addition & 1 deletion syft/format/common/spdxhelpers/to_syft_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ func Test_directPackageFiles(t *testing.T) {
RealPath: "some-file",
FileSystemID: "",
},
VirtualPath: "some-file",
AccessPath: "some-file",
},
LocationMetadata: file.LocationMetadata{
Annotations: map[string]string{},
Expand Down
4 changes: 2 additions & 2 deletions syft/format/github/internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func toPath(s source.Description, p pkg.Package) string {
if len(locations) > 0 {
location := locations[0]
packagePath := location.RealPath
if location.VirtualPath != "" {
packagePath = location.VirtualPath
if location.AccessPath != "" {
packagePath = location.AccessPath
}
packagePath = strings.TrimPrefix(packagePath, "/")
switch metadata := s.Metadata.(type) {
Expand Down
2 changes: 1 addition & 1 deletion syft/format/spdxtagvalue/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ FileCopyrightText: NOASSERTION
f = file.Location{
LocationData: file.LocationData{
Coordinates: c,
VirtualPath: "",
AccessPath: "",
},
LocationMetadata: file.LocationMetadata{},
}
Expand Down
2 changes: 1 addition & 1 deletion syft/format/syftjson/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func Test_EncodeDecodeCycle(t *testing.T) {
}

for _, d := range deep.Equal(p, actualPackages[idx]) {
if strings.Contains(d, ".VirtualPath: ") {
if strings.Contains(d, ".AccessPath: ") {
// location.Virtual path is not exposed in the json output
continue
}
Expand Down
4 changes: 2 additions & 2 deletions syft/internal/fileresolver/container_image_all_layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ func (r *ContainerImageAllLayers) FileContentsByLocation(location file.Location)
switch entry.Metadata.Type {
case stereoscopeFile.TypeSymLink, stereoscopeFile.TypeHardLink:
// 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
case stereoscopeFile.TypeDirectory:
Expand Down
2 changes: 1 addition & 1 deletion syft/internal/fileresolver/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (r Directory) FilesByPath(userPaths ...string) ([]file.Location, error) {
continue
}

// we should be resolving symlinks and preserving this information as a VirtualPath to the real file
// we should be resolving symlinks and preserving this information as a AccessPath to the real file
ref, err := r.searchContext.SearchByPath(userStrPath, filetree.FollowBasenameLinks)
if err != nil {
log.Tracef("unable to evaluate symlink for path=%q : %+v", userPath, err)
Expand Down
Loading
Loading