Skip to content

Commit

Permalink
move coordinates into separate file
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
  • Loading branch information
wagoodman committed Nov 18, 2021
1 parent 4872aa6 commit dfcb57a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
34 changes: 34 additions & 0 deletions syft/source/coordinates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package source

import (
"fmt"

"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
)

// Coordinates contains the minimal information needed to describe how to find a file within any possible source object (e.g. image and directory sources)
type Coordinates struct {
RealPath string `json:"path"` // The path where all path ancestors have no hardlinks / symlinks
FileSystemID string `json:"layerID,omitempty"` // An ID representing the filesystem. For container images, this is a layer digest. For directories or a root filesystem, this is blank.
}

func (c Coordinates) ID() artifact.ID {
f, err := artifact.IDFromHash(c)
if err != nil {
// TODO: what to do in this case?
log.Warnf("unable to get fingerprint of location coordinate=%+v: %+v", c, err)
return ""
}

return f
}

func (c Coordinates) String() string {
str := fmt.Sprintf("RealPath=%q", c.RealPath)

if c.FileSystemID != "" {
str += fmt.Sprintf(" Layer=%q", c.FileSystemID)
}
return fmt.Sprintf("Location<%s>", str)
}
32 changes: 4 additions & 28 deletions syft/source/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/image"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
)

var _ hashstructure.Hashable = (*Location)(nil)

// 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).
type Location struct {
Expand All @@ -19,12 +20,6 @@ type Location struct {
ref file.Reference // The file reference relative to the stereoscope.FileCatalog that has more information about this location.
}

// Coordinates contains the minimal information needed to describe how to find a file within any possible source object (e.g. image and directory sources)
type Coordinates struct {
RealPath string `json:"path"` // The path where all path ancestors have no hardlinks / symlinks
FileSystemID string `json:"layerID,omitempty"` // An ID representing the filesystem. For container images this is a layer digest, directories or root filesystem this is blank.
}

// NewLocation creates a new Location representing a path without denoting a filesystem or FileCatalog reference.
func NewLocation(realPath string) Location {
return Location{
Expand Down Expand Up @@ -102,26 +97,7 @@ func (l Location) String() string {
}

func (l Location) Hash() (uint64, error) {
// Location hash should be a pass-through for the coordinates and not include ref or VirtualPath.
// since location is part of the package definition it is important that only coordinates are used during object
// hashing. (Location hash should be a pass-through for the coordinates and not include ref or VirtualPath.)
return hashstructure.Hash(l.ID(), hashstructure.FormatV2, nil)
}

func (c Coordinates) ID() artifact.ID {
f, err := artifact.IDFromHash(c)
if err != nil {
// TODO: what to do in this case?
log.Warnf("unable to get fingerprint of location coordinate=%+v: %+v", c, err)
return ""
}

return f
}

func (c Coordinates) String() string {
str := fmt.Sprintf("RealPath=%q", c.RealPath)

if c.FileSystemID != "" {
str += fmt.Sprintf(" Layer=%q", c.FileSystemID)
}
return fmt.Sprintf("Location<%s>", str)
}

0 comments on commit dfcb57a

Please sign in to comment.