github.com/anchore/syft@v1.38.2/syft/internal/fileresolver/metadata.go (about)

     1  package fileresolver
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/anchore/stereoscope/pkg/file"
     7  	"github.com/anchore/syft/internal"
     8  	"github.com/anchore/syft/syft/internal/windows"
     9  )
    10  
    11  func NewMetadataFromPath(path string, info os.FileInfo) file.Metadata {
    12  	var mimeType string
    13  	uid, gid := getXid(info)
    14  
    15  	ty := file.TypeFromMode(info.Mode())
    16  
    17  	if ty == file.TypeRegular {
    18  		usablePath := path
    19  		// denormalize the path back to windows so we can open the file
    20  		if windows.HostRunningOnWindows() {
    21  			usablePath = windows.FromPosix(usablePath)
    22  		}
    23  
    24  		f, err := os.Open(usablePath)
    25  		if err != nil {
    26  			// TODO: it may be that the file is inaccessible, however, this is not an error or a warning. In the future we need to track these as known-unknowns
    27  			f = nil
    28  		} else {
    29  			defer internal.CloseAndLogError(f, usablePath)
    30  		}
    31  
    32  		mimeType = file.MIMEType(f)
    33  	}
    34  
    35  	return file.Metadata{
    36  		FileInfo: info,
    37  		Path:     path,
    38  		Type:     ty,
    39  		// unsupported across platforms
    40  		UserID:   uid,
    41  		GroupID:  gid,
    42  		MIMEType: mimeType,
    43  	}
    44  }