From 26d7d91ea120e95d25fcaef8b7d3996b971b6269 Mon Sep 17 00:00:00 2001 From: Manuel Alejandro de Brito Fontes Date: Thu, 10 Nov 2022 12:00:18 -0300 Subject: [PATCH] Refactor inTotoMaterials to skip directories --- pkg/leeway/provenance.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/leeway/provenance.go b/pkg/leeway/provenance.go index 97f8f1d9..cf3b374f 100644 --- a/pkg/leeway/provenance.go +++ b/pkg/leeway/provenance.go @@ -295,9 +295,16 @@ type provenanceEnvironment struct { func (p *Package) inTotoMaterials() ([]in_toto.ProvenanceMaterial, error) { res := make([]in_toto.ProvenanceMaterial, 0, len(p.Sources)) for _, src := range p.Sources { - if stat, err := os.Lstat(src); err != nil { + stat, err := os.Lstat(src); + if err != nil { return nil, err - } else if !stat.Mode().IsRegular() { + } + + if stat.Mode().IsDir() { + continue + } + + if !stat.Mode().IsRegular() { continue } @@ -326,12 +333,14 @@ func sha256Hash(fn string) (res string, err error) { return "", xerrors.Errorf("cannot compute hash of %s: %w", fn, err) } + defer f.Close() + hash := sha256.New() _, err = io.Copy(hash, f) if err != nil { return "", xerrors.Errorf("cannot compute hash of %s: %w", fn, err) } - f.Close() + return fmt.Sprintf("%x", hash.Sum(nil)), nil }