Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
feat: generate hash concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
paralta committed Jul 25, 2024
1 parent ad16d3d commit 8b5ab4d
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions scanner/utils/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package utils

import (
"crypto/sha256"
"errors"
"fmt"
"io"
"io/fs"
Expand All @@ -27,6 +26,7 @@ import (
"strings"

"github.com/openclarity/vmclarity/scanner/common"
"github.com/sourcegraph/conc/iter"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -108,24 +108,34 @@ func dirFiles(dir string) ([]string, error) {

// generateHash creates hashes for all files along with filenames and generates a hash for the hashes and filenames.
func generateHash(files []string, open func(string) (io.ReadCloser, error)) (string, error) {
h := sha256.New()
files = append([]string(nil), files...)
sort.Strings(files)
for _, file := range files {
if strings.Contains(file, "\n") {
return "", errors.New("filenames with newlines are not supported")

mapper := iter.Mapper[string, string]{
MaxGoroutines: len(files) / 2,
}

results := mapper.Map(files, func(f *string) string {
if strings.Contains(*f, "\n") {
return ""
}
r, err := open(file)
r, err := open(*f)
if err != nil {
return "", fmt.Errorf("failed to open file %s: %w", file, err)
return ""
}
hf := sha256.New()
_, err = io.Copy(hf, r)
r.Close()
if err != nil {
return "", fmt.Errorf("failed to create hash for file %s: %w", file, err)
return ""
}
fmt.Fprintf(h, "%x %s\n", hf.Sum(nil), file)
return fmt.Sprintf("%x %s\n", hf.Sum(nil), *f)
})

h := sha256.New()
for _, result := range results {
fmt.Fprintf(h, "%s", result)
}

return fmt.Sprintf("%x", h.Sum(nil)), nil // nolint:perfsprint
}

0 comments on commit 8b5ab4d

Please sign in to comment.