github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/file/coordinates.go (about)

     1  package file
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/anchore/syft/internal/log"
     7  	"github.com/anchore/syft/syft/artifact"
     8  )
     9  
    10  // Coordinates contains the minimal information needed to describe how to find a file within any possible source object (e.g. image and directory sources)
    11  type Coordinates struct {
    12  	RealPath     string `json:"path" cyclonedx:"path"`                 // The path where all path ancestors have no hardlinks / symlinks
    13  	FileSystemID string `json:"layerID,omitempty" cyclonedx:"layerID"` // An ID representing the filesystem. For container images, this is a layer digest. For directories or a root filesystem, this is blank.
    14  }
    15  
    16  func NewCoordinates(realPath, fsID string) Coordinates {
    17  	return Coordinates{
    18  		RealPath:     realPath,
    19  		FileSystemID: fsID,
    20  	}
    21  }
    22  
    23  func (c Coordinates) ID() artifact.ID {
    24  	f, err := artifact.IDByHash(c)
    25  	if err != nil {
    26  		// TODO: what to do in this case?
    27  		log.Warnf("unable to get fingerprint of location coordinate=%+v: %+v", c, err)
    28  		return ""
    29  	}
    30  
    31  	return f
    32  }
    33  
    34  func (c Coordinates) String() string {
    35  	str := fmt.Sprintf("RealPath=%q", c.RealPath)
    36  
    37  	if c.FileSystemID != "" {
    38  		str += fmt.Sprintf(" Layer=%q", c.FileSystemID)
    39  	}
    40  	return fmt.Sprintf("Location<%s>", str)
    41  }